rack-mount 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/README.rdoc +1 -1
  2. data/lib/rack/mount.rb +0 -2
  3. data/lib/rack/mount/analysis/frequency.rb +10 -3
  4. data/lib/rack/mount/analysis/splitting.rb +79 -62
  5. data/lib/rack/mount/generatable_regexp.rb +48 -45
  6. data/lib/rack/mount/generation/route.rb +10 -5
  7. data/lib/rack/mount/generation/route_set.rb +23 -31
  8. data/lib/rack/mount/prefix.rb +14 -15
  9. data/lib/rack/mount/recognition/code_generation.rb +51 -61
  10. data/lib/rack/mount/recognition/route.rb +7 -22
  11. data/lib/rack/mount/recognition/route_set.rb +40 -16
  12. data/lib/rack/mount/regexp_with_named_groups.rb +33 -7
  13. data/lib/rack/mount/route_set.rb +7 -5
  14. data/lib/rack/mount/strexp.rb +11 -40
  15. data/lib/rack/mount/strexp/parser.rb +158 -0
  16. data/lib/rack/mount/strexp/tokenizer.rb +84 -0
  17. data/lib/rack/mount/utils.rb +26 -163
  18. data/lib/rack/mount/vendor/multimap/multimap.rb +1 -0
  19. data/lib/rack/mount/vendor/reginald/reginald.rb +55 -0
  20. data/lib/rack/mount/vendor/reginald/reginald/alternation.rb +50 -0
  21. data/lib/rack/mount/vendor/reginald/reginald/anchor.rb +20 -0
  22. data/lib/rack/mount/vendor/reginald/reginald/character.rb +53 -0
  23. data/lib/rack/mount/vendor/reginald/reginald/character_class.rb +61 -0
  24. data/lib/rack/mount/vendor/reginald/reginald/expression.rb +75 -0
  25. data/lib/rack/mount/vendor/reginald/reginald/group.rb +61 -0
  26. data/lib/rack/mount/vendor/reginald/reginald/parser.rb +306 -0
  27. data/lib/rack/mount/vendor/reginald/reginald/tokenizer.rb +141 -0
  28. metadata +14 -15
  29. data/lib/rack/mount/const.rb +0 -45
  30. data/lib/rack/mount/meta_method.rb +0 -104
@@ -164,6 +164,7 @@ class Multimap < Hash
164
164
  # Ignore alias_method since the definition above serves
165
165
  # as its documentation.
166
166
  #++
167
+ undef :each_association
167
168
  module_eval "alias_method :each_association, :each_pair"
168
169
 
169
170
  # call-seq:
@@ -0,0 +1,55 @@
1
+ module Reginald
2
+ autoload :Alternation, 'reginald/alternation'
3
+ autoload :Anchor, 'reginald/anchor'
4
+ autoload :Character, 'reginald/character'
5
+ autoload :CharacterClass, 'reginald/character_class'
6
+ autoload :Expression, 'reginald/expression'
7
+ autoload :Group, 'reginald/group'
8
+ autoload :Parser, 'reginald/parser'
9
+
10
+ begin
11
+ eval('/(?<foo>.*)/').named_captures
12
+
13
+ def self.regexp_supports_named_captures?
14
+ true
15
+ end
16
+ rescue SyntaxError, NoMethodError
17
+ def self.regexp_supports_named_captures?
18
+ false
19
+ end
20
+ end
21
+
22
+ def self.parse(regexp)
23
+ # TODO: The parser should be aware of extended expressions
24
+ # instead of having to sanitize them before.
25
+ if regexp.options & Regexp::EXTENDED != 0
26
+ source = regexp.source
27
+ source.gsub!(/#.+$/, '')
28
+ source.gsub!(/\s+/, '')
29
+ source.gsub!(/\\\//, '/')
30
+ regexp = Regexp.compile(source)
31
+ end
32
+
33
+ parser = Parser.new
34
+ expression = parser.scan_str(regexp.source)
35
+ expression.ignorecase = regexp.casefold?
36
+
37
+ capture_index = 0
38
+ tag_captures = Proc.new do |expr|
39
+ expr.each do |atom|
40
+ if atom.is_a?(Group)
41
+ if atom.capture
42
+ atom.index = capture_index
43
+ capture_index += 1
44
+ end
45
+ tag_captures.call(atom)
46
+ elsif atom.is_a?(Expression)
47
+ tag_captures.call(atom)
48
+ end
49
+ end
50
+ end
51
+ tag_captures.call(expression)
52
+
53
+ expression
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ module Reginald
2
+ class Alternation < Array
3
+ def self.reduce(alternation_or_expression, expression)
4
+ if alternation_or_expression.first.is_a?(Alternation)
5
+ alternation_or_expression = alternation_or_expression.first
6
+ alternation_or_expression << expression
7
+ new(*alternation_or_expression)
8
+ else
9
+ new(alternation_or_expression, expression)
10
+ end
11
+ end
12
+
13
+ def initialize(*args)
14
+ if args.length == 1 && args.first.is_a?(Array)
15
+ super(args.first)
16
+ else
17
+ super(args)
18
+ end
19
+ end
20
+
21
+ def literal?
22
+ false
23
+ end
24
+
25
+ def to_s
26
+ map { |e| e.to_s }.join('|')
27
+ end
28
+
29
+ def to_regexp
30
+ Regexp.compile("\\A#{to_s}\\Z")
31
+ end
32
+
33
+ def inspect
34
+ to_s.inspect
35
+ end
36
+
37
+ def match(char)
38
+ to_regexp.match(char)
39
+ end
40
+
41
+ def include?(char)
42
+ any? { |e| e.include?(char) }
43
+ end
44
+
45
+ def freeze
46
+ each { |e| e.freeze }
47
+ super
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ module Reginald
2
+ class Anchor < Struct.new(:value)
3
+ def to_s
4
+ "#{value}"
5
+ end
6
+
7
+ def inspect
8
+ to_s.inspect
9
+ end
10
+
11
+ def literal?
12
+ false
13
+ end
14
+
15
+ def freeze
16
+ value.freeze
17
+ super
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ module Reginald
2
+ class Character < String
3
+ attr_accessor :quantifier
4
+
5
+ def initialize(char)
6
+ raise ArgumentError if char.length != 1
7
+ super
8
+ end
9
+
10
+ def literal?
11
+ quantifier.nil?
12
+ end
13
+
14
+ def to_s
15
+ "#{super}#{quantifier}"
16
+ end
17
+
18
+ def to_regexp
19
+ Regexp.compile("\\A#{to_s}\\Z")
20
+ end
21
+
22
+ def inspect
23
+ to_s.inspect
24
+ end
25
+
26
+ def match(char)
27
+ to_regexp.match(char)
28
+ end
29
+
30
+ def include?(char)
31
+ to_str == char
32
+ end
33
+
34
+ def ==(other)
35
+ case other
36
+ when String
37
+ other == to_s
38
+ else
39
+ eql?(other)
40
+ end
41
+ end
42
+
43
+ def eql?(other)
44
+ other.is_a?(self.class) && super &&
45
+ self.quantifier == other.quantifier
46
+ end
47
+
48
+ def freeze
49
+ quantifier.freeze
50
+ super
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,61 @@
1
+ module Reginald
2
+ class CharacterClass < Struct.new(:value)
3
+ attr_accessor :negate, :quantifier
4
+
5
+ def negated?
6
+ negate ? true : false
7
+ end
8
+
9
+ def literal?
10
+ false
11
+ end
12
+
13
+ def to_s
14
+ if value == '.' || value == '\d'
15
+ "#{value}#{quantifier}"
16
+ else
17
+ "[#{negate && '^'}#{value}]#{quantifier}"
18
+ end
19
+ end
20
+
21
+ def to_regexp
22
+ Regexp.compile("\\A#{to_s}\\Z")
23
+ end
24
+
25
+ def inspect
26
+ to_s.inspect
27
+ end
28
+
29
+ def match(char)
30
+ to_regexp.match(char)
31
+ end
32
+
33
+ def include?(char)
34
+ re = quantifier ? to_s.sub(/#{Regexp.escape(quantifier)}$/, '') : to_s
35
+ Regexp.compile("\\A#{re}\\Z").match(char)
36
+ end
37
+
38
+ def ==(other)
39
+ case other
40
+ when String
41
+ other == to_s
42
+ else
43
+ eql?(other)
44
+ end
45
+ end
46
+
47
+ def eql?(other)
48
+ other.is_a?(self.class) &&
49
+ self.value == other.value &&
50
+ self.negate == other.negate &&
51
+ self.quantifier == other.quantifier
52
+ end
53
+
54
+ def freeze
55
+ value.freeze
56
+ negate.freeze
57
+ quantifier.freeze
58
+ super
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,75 @@
1
+ module Reginald
2
+ class Expression < Array
3
+ attr_accessor :ignorecase
4
+
5
+ def self.reduce(expression_or_atom, atom = nil)
6
+ if expression_or_atom.is_a?(Expression)
7
+ expression_or_atom << atom if atom
8
+ new(*expression_or_atom)
9
+ elsif atom.nil?
10
+ new(expression_or_atom)
11
+ else
12
+ new(expression_or_atom, atom)
13
+ end
14
+ end
15
+
16
+ def initialize(*args)
17
+ @ignorecase = false
18
+
19
+ if args.length == 1 && args.first.is_a?(Array)
20
+ super(args.first)
21
+ else
22
+ super(args)
23
+ end
24
+ end
25
+
26
+ def literal?
27
+ ignorecase == false && all? { |e| e.literal? }
28
+ end
29
+
30
+ def to_s
31
+ map { |e| e.to_s }.join
32
+ end
33
+
34
+ def to_regexp
35
+ Regexp.compile("\\A#{to_s}\\Z")
36
+ end
37
+
38
+ def inspect
39
+ "#<Expression #{to_s.inspect}>"
40
+ end
41
+
42
+ def match(char)
43
+ to_regexp.match(char)
44
+ end
45
+
46
+ def include?(char)
47
+ any? { |e| e.include?(char) }
48
+ end
49
+
50
+ def casefold?
51
+ ignorecase
52
+ end
53
+
54
+ def ==(other)
55
+ case other
56
+ when String
57
+ other == to_s
58
+ when Array
59
+ super
60
+ else
61
+ eql?(other)
62
+ end
63
+ end
64
+
65
+ def eql?(other)
66
+ other.is_a?(self.class) && super &&
67
+ self.ignorecase == other.ignorecase
68
+ end
69
+
70
+ def freeze
71
+ each { |e| e.freeze }
72
+ super
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,61 @@
1
+ module Reginald
2
+ class Group < Struct.new(:expression)
3
+ attr_accessor :quantifier, :capture, :index, :name
4
+
5
+ def initialize(*args)
6
+ @capture = true
7
+ super
8
+ end
9
+
10
+ def literal?
11
+ quantifier.nil? && expression.literal?
12
+ end
13
+
14
+ def to_s
15
+ "(#{capture ? '' : '?:'}#{expression.to_s})#{quantifier}"
16
+ end
17
+
18
+ def to_regexp
19
+ Regexp.compile("\\A#{to_s}\\Z")
20
+ end
21
+
22
+ def inspect
23
+ to_s.inspect
24
+ end
25
+
26
+ def match(char)
27
+ to_regexp.match(char)
28
+ end
29
+
30
+ def include?(char)
31
+ expression.include?(char)
32
+ end
33
+
34
+ def capture?
35
+ capture
36
+ end
37
+
38
+ def ==(other)
39
+ case other
40
+ when String
41
+ other == to_s
42
+ else
43
+ eql?(other)
44
+ end
45
+ end
46
+
47
+ def eql?(other)
48
+ other.is_a?(self.class) &&
49
+ self.expression == other.expression &&
50
+ self.quantifier == other.quantifier &&
51
+ self.capture == other.capture &&
52
+ self.index == other.index &&
53
+ self.name == other.name
54
+ end
55
+
56
+ def freeze
57
+ expression.freeze
58
+ super
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,306 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.6
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require 'reginald/tokenizer'
10
+ module Reginald
11
+ class Parser < Racc::Parser
12
+ ##### State transition tables begin ###
13
+
14
+ racc_action_table = [
15
+ 5, 31, 11, 6, 7, 9, 40, 10, 45, 14,
16
+ 5, 12, 11, 6, 7, 9, 41, 10, 37, 14,
17
+ 5, 12, 11, 6, 7, 9, 28, 10, 47, 14,
18
+ 5, 12, 11, 6, 7, 9, 48, 10, 46, nil,
19
+ 5, 12, 11, 6, 7, 9, 13, 10, 14, nil,
20
+ 5, 12, 11, 6, 7, 9, 27, 10, 38, 39,
21
+ 5, 12, 11, 6, 7, 9, 19, 10, nil, 19,
22
+ nil, 12, 17, 18, 20, 17, 18, 20, 32, nil,
23
+ nil, 34, 33, 35, 42, nil, nil, 34, 33, 35,
24
+ 22, nil, 24, 23, 25, 24, 23, 25 ]
25
+
26
+ racc_action_check = [
27
+ 0, 20, 0, 0, 0, 0, 31, 0, 40, 26,
28
+ 39, 0, 39, 39, 39, 39, 31, 39, 26, 44,
29
+ 2, 39, 2, 2, 2, 2, 13, 2, 44, 43,
30
+ 14, 2, 14, 14, 14, 14, 45, 14, 43, nil,
31
+ 29, 14, 29, 29, 29, 29, 1, 29, 1, nil,
32
+ 10, 29, 10, 10, 10, 10, 10, 10, 27, 27,
33
+ 38, 10, 38, 38, 38, 38, 3, 38, nil, 15,
34
+ nil, 38, 3, 3, 3, 15, 15, 15, 21, nil,
35
+ nil, 21, 21, 21, 36, nil, nil, 36, 36, 36,
36
+ 5, nil, 5, 5, 5, 22, 22, 22 ]
37
+
38
+ racc_action_pointer = [
39
+ -3, 46, 17, 57, nil, 85, nil, nil, nil, nil,
40
+ 47, nil, nil, 26, 27, 60, nil, nil, nil, nil,
41
+ -7, 74, 88, nil, nil, nil, 7, 46, nil, 37,
42
+ nil, -2, nil, nil, nil, nil, 80, nil, 57, 7,
43
+ 0, nil, nil, 27, 17, 18, nil, nil, nil ]
44
+
45
+ racc_action_default = [
46
+ -30, -30, -2, -6, -7, -30, -10, -11, -12, -13,
47
+ -30, -23, -24, -30, -30, -4, -5, -25, -26, -27,
48
+ -30, -30, -30, -17, -18, -19, -30, -30, 49, -1,
49
+ -3, -30, -8, -14, -15, -16, -30, -20, -30, -30,
50
+ -30, -29, -9, -30, -30, -30, -21, -22, -28 ]
51
+
52
+ racc_goto_table = [
53
+ 1, 15, 21, 16, 29, nil, nil, nil, nil, nil,
54
+ 26, nil, nil, nil, nil, 30, nil, nil, nil, 36,
55
+ nil, nil, nil, nil, nil, nil, nil, nil, 15, nil,
56
+ nil, nil, nil, nil, nil, nil, nil, nil, 43, 44 ]
57
+
58
+ racc_goto_check = [
59
+ 1, 3, 6, 4, 2, nil, nil, nil, nil, nil,
60
+ 1, nil, nil, nil, nil, 4, nil, nil, nil, 6,
61
+ nil, nil, nil, nil, nil, nil, nil, nil, 3, nil,
62
+ nil, nil, nil, nil, nil, nil, nil, nil, 1, 1 ]
63
+
64
+ racc_goto_pointer = [
65
+ nil, 0, -10, -1, 0, nil, -3, nil ]
66
+
67
+ racc_goto_default = [
68
+ nil, nil, 2, 3, nil, 4, nil, 8 ]
69
+
70
+ racc_reduce_table = [
71
+ 0, 0, :racc_error,
72
+ 3, 20, :_reduce_1,
73
+ 1, 20, :_reduce_2,
74
+ 3, 21, :_reduce_3,
75
+ 2, 21, :_reduce_4,
76
+ 2, 21, :_reduce_5,
77
+ 1, 21, :_reduce_none,
78
+ 1, 22, :_reduce_none,
79
+ 3, 22, :_reduce_8,
80
+ 4, 22, :_reduce_9,
81
+ 1, 22, :_reduce_10,
82
+ 1, 22, :_reduce_11,
83
+ 1, 22, :_reduce_12,
84
+ 1, 22, :_reduce_13,
85
+ 2, 25, :_reduce_14,
86
+ 2, 25, :_reduce_15,
87
+ 2, 25, :_reduce_16,
88
+ 1, 25, :_reduce_none,
89
+ 1, 25, :_reduce_none,
90
+ 1, 25, :_reduce_none,
91
+ 3, 24, :_reduce_20,
92
+ 5, 24, :_reduce_21,
93
+ 5, 24, :_reduce_22,
94
+ 1, 26, :_reduce_none,
95
+ 1, 26, :_reduce_none,
96
+ 1, 23, :_reduce_none,
97
+ 1, 23, :_reduce_none,
98
+ 1, 23, :_reduce_none,
99
+ 5, 23, :_reduce_28,
100
+ 3, 23, :_reduce_29 ]
101
+
102
+ racc_reduce_n = 30
103
+
104
+ racc_shift_n = 49
105
+
106
+ racc_token_table = {
107
+ false => 0,
108
+ :error => 1,
109
+ :BAR => 2,
110
+ :LBRACK => 3,
111
+ :RBRACK => 4,
112
+ :L_ANCHOR => 5,
113
+ :CHAR_CLASS => 6,
114
+ :DOT => 7,
115
+ :CHAR => 8,
116
+ :QMARK => 9,
117
+ :LPAREN => 10,
118
+ :RPAREN => 11,
119
+ :COLON => 12,
120
+ :NAME => 13,
121
+ :R_ANCHOR => 14,
122
+ :STAR => 15,
123
+ :PLUS => 16,
124
+ :LCURLY => 17,
125
+ :RCURLY => 18 }
126
+
127
+ racc_nt_base = 19
128
+
129
+ racc_use_result_var = true
130
+
131
+ Racc_arg = [
132
+ racc_action_table,
133
+ racc_action_check,
134
+ racc_action_default,
135
+ racc_action_pointer,
136
+ racc_goto_table,
137
+ racc_goto_check,
138
+ racc_goto_default,
139
+ racc_goto_pointer,
140
+ racc_nt_base,
141
+ racc_reduce_table,
142
+ racc_token_table,
143
+ racc_shift_n,
144
+ racc_reduce_n,
145
+ racc_use_result_var ]
146
+
147
+ Racc_token_to_s_table = [
148
+ "$end",
149
+ "error",
150
+ "BAR",
151
+ "LBRACK",
152
+ "RBRACK",
153
+ "L_ANCHOR",
154
+ "CHAR_CLASS",
155
+ "DOT",
156
+ "CHAR",
157
+ "QMARK",
158
+ "LPAREN",
159
+ "RPAREN",
160
+ "COLON",
161
+ "NAME",
162
+ "R_ANCHOR",
163
+ "STAR",
164
+ "PLUS",
165
+ "LCURLY",
166
+ "RCURLY",
167
+ "$start",
168
+ "expression",
169
+ "branch",
170
+ "atom",
171
+ "quantifier",
172
+ "group",
173
+ "bracket_expression",
174
+ "anchor" ]
175
+
176
+ Racc_debug_parser = false
177
+
178
+ ##### State transition tables end #####
179
+
180
+ # reduce 0 omitted
181
+
182
+ def _reduce_1(val, _values, result)
183
+ result = Expression.new([Alternation.reduce(val[0], val[2])])
184
+ result
185
+ end
186
+
187
+ def _reduce_2(val, _values, result)
188
+ result = Expression.reduce(val[0])
189
+ result
190
+ end
191
+
192
+ def _reduce_3(val, _values, result)
193
+ val[1].quantifier = val[2]
194
+ result = Expression.reduce(val[0], val[1])
195
+
196
+ result
197
+ end
198
+
199
+ def _reduce_4(val, _values, result)
200
+ result = Expression.reduce(val[0], val[1])
201
+ result
202
+ end
203
+
204
+ def _reduce_5(val, _values, result)
205
+ val[0].quantifier = val[1]
206
+ result = val[0]
207
+
208
+ result
209
+ end
210
+
211
+ # reduce 6 omitted
212
+
213
+ # reduce 7 omitted
214
+
215
+ def _reduce_8(val, _values, result)
216
+ result = CharacterClass.new(val[1])
217
+ result
218
+ end
219
+
220
+ def _reduce_9(val, _values, result)
221
+ result = CharacterClass.new(val[2]); result.negate = true
222
+ result
223
+ end
224
+
225
+ def _reduce_10(val, _values, result)
226
+ result = CharacterClass.new(val[0])
227
+ result
228
+ end
229
+
230
+ def _reduce_11(val, _values, result)
231
+ result = CharacterClass.new(val[0])
232
+ result
233
+ end
234
+
235
+ def _reduce_12(val, _values, result)
236
+ result = Anchor.new(val[0])
237
+ result
238
+ end
239
+
240
+ def _reduce_13(val, _values, result)
241
+ result = Character.new(val[0])
242
+ result
243
+ end
244
+
245
+ def _reduce_14(val, _values, result)
246
+ result = val.join
247
+ result
248
+ end
249
+
250
+ def _reduce_15(val, _values, result)
251
+ result = val.join
252
+ result
253
+ end
254
+
255
+ def _reduce_16(val, _values, result)
256
+ result = val.join
257
+ result
258
+ end
259
+
260
+ # reduce 17 omitted
261
+
262
+ # reduce 18 omitted
263
+
264
+ # reduce 19 omitted
265
+
266
+ def _reduce_20(val, _values, result)
267
+ result = Group.new(val[1])
268
+ result
269
+ end
270
+
271
+ def _reduce_21(val, _values, result)
272
+ result = Group.new(val[3]); result.capture = false
273
+ result
274
+ end
275
+
276
+ def _reduce_22(val, _values, result)
277
+ result = Group.new(val[3]); result.name = val[2]
278
+ result
279
+ end
280
+
281
+ # reduce 23 omitted
282
+
283
+ # reduce 24 omitted
284
+
285
+ # reduce 25 omitted
286
+
287
+ # reduce 26 omitted
288
+
289
+ # reduce 27 omitted
290
+
291
+ def _reduce_28(val, _values, result)
292
+ result = val.join
293
+ result
294
+ end
295
+
296
+ def _reduce_29(val, _values, result)
297
+ result = val.join
298
+ result
299
+ end
300
+
301
+ def _reduce_none(val, _values, result)
302
+ val[0]
303
+ end
304
+
305
+ end # class Parser
306
+ end # module Reginald