Class: Regin::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/rack/mount/vendor/regin/regin/parser.rb,
lib/rack/mount/vendor/regin/regin/tokenizer.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"BAR",
"LBRACK",
"RBRACK",
"NEGATE",
"CCLASS",
"DOT",
"CHAR",
"LPAREN",
"RPAREN",
"QMARK",
"COLON",
"NAME",
"L_ANCHOR",
"R_ANCHOR",
"STAR",
"PLUS",
"LCURLY",
"RCURLY",
"\"alnum\"",
"\"alpha\"",
"\"ascii\"",
"\"blank\"",
"\"cntrl\"",
"\"digit\"",
"\"graph\"",
"\"lower\"",
"\"print\"",
"\"punct\"",
"\"space\"",
"\"upper\"",
"\"word\"",
"\"xdigit\"",
"MINUS",
"MULTILINE",
"IGNORECASE",
"EXTENDED",
"$start",
"expression",
"alternation",
"subexpression",
"expression_ary",
"quantified_atom",
"atom",
"quantifier",
"group",
"bracket_expression",
"anchor",
"options",
"quantifier_char",
"posix_bracket_expression",
"posix_bracket_type",
"modifier" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



24
25
26
27
28
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 24

def initialize
  @capture_index = 0
  @capture_index_stack = []
  @options_stack = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



14
15
16
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 14

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



13
14
15
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 13

def lineno
  @lineno
end

#options_stackObject

Returns the value of attribute options_stack.



22
23
24
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 22

def options_stack
  @options_stack
end

#stateObject

Returns the value of attribute state.



15
16
17
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 15

def state
  @state
end

Class Method Details

.parse_regexp(regexp) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 11

def self.parse_regexp(regexp)
  options = Options.from_int(regexp.options)

  parser = new
  parser.options_stack << options.to_h

  expression = parser.scan_str(regexp.source)
  expression = expression.dup(options.to_h) if options.any?
  expression
end

Instance Method Details

#_next_tokenObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 54

def _next_token
  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
  when nil
    case
    when (text = @ss.scan(/\\[dDsSwW]/))
       action { [:CCLASS, text] }

    when (text = @ss.scan(/\^|\\A/))
       action { [:L_ANCHOR, text] }

    when (text = @ss.scan(/\$|\\Z/))
       action { [:R_ANCHOR, text] }

    when (text = @ss.scan(/<(\w+)>/))
       action { [:NAME, @ss[1]] }

    when (text = @ss.scan(/\(/))
       action {
  @capture_index_stack << @capture_index
  @capture_index += 1
  @state = :OPTIONS if @ss.peek(1) == '?';
  [:LPAREN, text]
}


    when (text = @ss.scan(/\)/))
       action { [:RPAREN,  text] }

    when (text = @ss.scan(/\[/))
       action { @state = :CCLASS; [:LBRACK,  text] }

    when (text = @ss.scan(/\{/))
       action { [:LCURLY,  text] }

    when (text = @ss.scan(/\}/))
       action { [:RCURLY,  text] }

    when (text = @ss.scan(/\|/))
       action { [:BAR, text]   }

    when (text = @ss.scan(/\./))
       action { [:DOT, text]   }

    when (text = @ss.scan(/\?/))
       action { [:QMARK, text] }

    when (text = @ss.scan(/\+/))
       action { [:PLUS,  text] }

    when (text = @ss.scan(/\*/))
       action { [:STAR,  text] }

    when (text = @ss.scan(/\#/))
       action {
  if @options_stack[-1][:extended]
    @state = :COMMENT;
    next_token
  else
    [:CHAR, text]
  end
}


    when (text = @ss.scan(/\s|\n/))
       action {
  if @options_stack[-1][:extended]
    next_token
  else
    [:CHAR, text]
  end
}


    when (text = @ss.scan(/\\(.)/))
       action { [:CHAR, @ss[1]] }

    when (text = @ss.scan(/./))
       action { [:CHAR, text] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  when :CCLASS
    case
    when (text = @ss.scan(/\[/))
       action { [:LBRACK,  text] }

    when (text = @ss.scan(/\]/))
       action { @state = nil; [:RBRACK, text] }

    when (text = @ss.scan(/\^/))
       action { [@ss.string[@ss.pos-2, 1] == '[' ? :NEGATE : :CHAR, text] }

    when (text = @ss.scan(/:/))
       action {
  if @ss.string[@ss.pos-2, 1] == '['
    @state = :POSIX_CCLASS
    [:COLON, text]
  else
    [:CHAR, text]
  end
}


    when (text = @ss.scan(/\\-/))
       action { [:CHAR, text] }

    when (text = @ss.scan(/\\(.)/))
       action { [:CHAR, @ss[1]] }

    when (text = @ss.scan(/./))
       action { [:CHAR, text] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  when :POSIX_CCLASS
    case
    when (text = @ss.scan(/\w+/))
       action { [text, text] }

    when (text = @ss.scan(/:/))
       action { [:COLON, text] }

    when (text = @ss.scan(/\]/))
       action { @state = :CCLASS; [:RBRACK, text] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  when :OPTIONS
    case
    when (text = @ss.scan(/\?/))
       action {
  @state = nil unless @ss.peek(1) =~ /-|m|i|x|:/
  [:QMARK, text]
}


    when (text = @ss.scan(/\-/))
       action { [:MINUS, text] }

    when (text = @ss.scan(/m/))
       action { [:MULTILINE, text] }

    when (text = @ss.scan(/i/))
       action { [:IGNORECASE, text] }

    when (text = @ss.scan(/x/))
       action { [:EXTENDED, text] }

    when (text = @ss.scan(/\:/))
       action {
  @capture_index_stack.pop
  @capture_index -= 1
  @state = nil;
  [:COLON, text]
}


    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  when :COMMENT
    case
    when (text = @ss.scan(/\n/))
       action { @state = nil; next_token }

    when (text = @ss.scan(/./))
       action { next_token }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  else
    raise  ScanError, "undefined state: '" + state.to_s + "'"
  end  # case state
  token
end

#_reduce_1(val, _values, result) ⇒ Object

reduce 0 omitted



299
300
301
302
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 299

def _reduce_1(val, _values, result)
 result = Expression.new(val[0]) 
    result
end

#_reduce_11(val, _values, result) ⇒ Object

reduce 10 omitted



340
341
342
343
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 340

def _reduce_11(val, _values, result)
 result = CharacterClass.new(val[1]) 
    result
end

#_reduce_12(val, _values, result) ⇒ Object



345
346
347
348
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 345

def _reduce_12(val, _values, result)
 result = CharacterClass.new(val[2], :negate => true) 
    result
end

#_reduce_13(val, _values, result) ⇒ Object



350
351
352
353
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 350

def _reduce_13(val, _values, result)
 result = CharacterClass.new(val[0]) 
    result
end

#_reduce_14(val, _values, result) ⇒ Object



355
356
357
358
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 355

def _reduce_14(val, _values, result)
 result = CharacterClass.new('.') 
    result
end

#_reduce_15(val, _values, result) ⇒ Object



360
361
362
363
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 360

def _reduce_15(val, _values, result)
 result = Anchor.new(val[0]) 
    result
end

#_reduce_16(val, _values, result) ⇒ Object



365
366
367
368
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 365

def _reduce_16(val, _values, result)
 result = Character.new(val[0]) 
    result
end

#_reduce_17(val, _values, result) ⇒ Object



370
371
372
373
374
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 370

def _reduce_17(val, _values, result)
          result = Group.new(val[1], :index => @capture_index_stack.pop)
        
    result
end

#_reduce_18(val, _values, result) ⇒ Object



376
377
378
379
380
381
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 376

def _reduce_18(val, _values, result)
          result = Group.new(val[4], val[2].merge(:capture => false))
          @options_stack.pop
        
    result
end

#_reduce_19(val, _values, result) ⇒ Object



383
384
385
386
387
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 383

def _reduce_19(val, _values, result)
          result = Group.new(val[3], :capture => false);
        
    result
end

#_reduce_20(val, _values, result) ⇒ Object



389
390
391
392
393
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 389

def _reduce_20(val, _values, result)
          result = Group.new(val[3], :name => val[2], :index => @capture_index_stack.pop);
        
    result
end

#_reduce_26(val, _values, result) ⇒ Object

reduce 25 omitted



405
406
407
408
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 405

def _reduce_26(val, _values, result)
 result = val.join 
    result
end

#_reduce_27(val, _values, result) ⇒ Object



410
411
412
413
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 410

def _reduce_27(val, _values, result)
 result = val.join 
    result
end

#_reduce_28(val, _values, result) ⇒ Object



415
416
417
418
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 415

def _reduce_28(val, _values, result)
 result = val.join 
    result
end

#_reduce_29(val, _values, result) ⇒ Object



420
421
422
423
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 420

def _reduce_29(val, _values, result)
 result = val.join 
    result
end

#_reduce_3(val, _values, result) ⇒ Object

reduce 2 omitted



306
307
308
309
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 306

def _reduce_3(val, _values, result)
 result = val[0] + [val[2]] 
    result
end

#_reduce_31(val, _values, result) ⇒ Object

reduce 30 omitted



427
428
429
430
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 427

def _reduce_31(val, _values, result)
 result = val.join 
    result
end

#_reduce_32(val, _values, result) ⇒ Object



432
433
434
435
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 432

def _reduce_32(val, _values, result)
 result = val.join 
    result
end

#_reduce_35(val, _values, result) ⇒ Object

reduce 34 omitted



441
442
443
444
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 441

def _reduce_35(val, _values, result)
 result = val.join 
    result
end

#_reduce_4(val, _values, result) ⇒ Object



311
312
313
314
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 311

def _reduce_4(val, _values, result)
 result = Alternation.new(val[0], val[2])  
    result
end

#_reduce_5(val, _values, result) ⇒ Object



316
317
318
319
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 316

def _reduce_5(val, _values, result)
 result = Expression.new(val[0]) 
    result
end

#_reduce_50(val, _values, result) ⇒ Object

reduce 49 omitted



474
475
476
477
478
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 474

def _reduce_50(val, _values, result)
            @options_stack << result = { val[1] => false, val[2] => false, val[3] => false }
          
    result
end

#_reduce_51(val, _values, result) ⇒ Object



480
481
482
483
484
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 480

def _reduce_51(val, _values, result)
            @options_stack << result = { val[0] => true, val[2] => false, val[3] => false }
          
    result
end

#_reduce_52(val, _values, result) ⇒ Object



486
487
488
489
490
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 486

def _reduce_52(val, _values, result)
            @options_stack << result = { val[0] => true, val[1] => true, val[3] => false }
          
    result
end

#_reduce_53(val, _values, result) ⇒ Object



492
493
494
495
496
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 492

def _reduce_53(val, _values, result)
            @options_stack << result = { val[0] => true, val[1] => true, val[2] => true }
          
    result
end

#_reduce_54(val, _values, result) ⇒ Object



498
499
500
501
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 498

def _reduce_54(val, _values, result)
 result = :multiline 
    result
end

#_reduce_55(val, _values, result) ⇒ Object



503
504
505
506
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 503

def _reduce_55(val, _values, result)
 result = :ignorecase 
    result
end

#_reduce_56(val, _values, result) ⇒ Object



508
509
510
511
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 508

def _reduce_56(val, _values, result)
 result = :extended 
    result
end

#_reduce_6(val, _values, result) ⇒ Object



321
322
323
324
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 321

def _reduce_6(val, _values, result)
 result = val[0] + [val[1]] 
    result
end

#_reduce_7(val, _values, result) ⇒ Object



326
327
328
329
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 326

def _reduce_7(val, _values, result)
 result = [val[0]] 
    result
end

#_reduce_8(val, _values, result) ⇒ Object



331
332
333
334
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 331

def _reduce_8(val, _values, result)
 result = val[0].dup(:quantifier => val[1]) 
    result
end

#_reduce_none(val, _values, result) ⇒ Object



513
514
515
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 513

def _reduce_none(val, _values, result)
  val[0]
end

#actionObject



23
24
25
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 23

def action
  yield
end

#load_file(filename) ⇒ Object



33
34
35
36
37
38
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 33

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#next_tokenObject



46
47
48
49
50
51
52
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 46

def next_token
  return if @ss.eos?
  
  # skips empty actions
  until token = _next_token or @ss.eos?; end
  token
end

#scan_file(filename) ⇒ Object



40
41
42
43
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 40

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object



17
18
19
20
21
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 17

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan



27
28
29
30
# File 'lib/rack/mount/vendor/regin/regin/tokenizer.rb', line 27

def scan_str(str)
  scan_setup(str)
  do_parse
end