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",
"EQUAL",
"BANG",
"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
245
246
247
248
249
250
251
252
253
# 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 { [:BANG, text]  }

    when (text = @ss.scan(/\=/))
       action { [:EQUAL, 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(/\\[dDsSwW]/))
       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



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

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

#_reduce_11(val, _values, result) ⇒ Object

reduce 10 omitted



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

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

#_reduce_12(val, _values, result) ⇒ Object



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

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

#_reduce_13(val, _values, result) ⇒ Object



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

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

#_reduce_14(val, _values, result) ⇒ Object



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

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

#_reduce_15(val, _values, result) ⇒ Object



374
375
376
377
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 374

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

#_reduce_16(val, _values, result) ⇒ Object



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

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

#_reduce_17(val, _values, result) ⇒ Object



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

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

#_reduce_18(val, _values, result) ⇒ Object



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

def _reduce_18(val, _values, result)
	 result = Group.new(val[3], :index => @capture_index_stack.pop, :lookahead => :postive)
       
    result
end

#_reduce_19(val, _values, result) ⇒ Object



396
397
398
399
400
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 396

def _reduce_19(val, _values, result)
	 result = Group.new(val[3], :index => @capture_index_stack.pop, :lookahead => :negative)
       
    result
end

#_reduce_20(val, _values, result) ⇒ Object



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

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

#_reduce_21(val, _values, result) ⇒ Object



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

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

#_reduce_22(val, _values, result) ⇒ Object



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

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

#_reduce_28(val, _values, result) ⇒ Object

reduce 27 omitted



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

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

#_reduce_29(val, _values, result) ⇒ Object



436
437
438
439
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 436

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

#_reduce_3(val, _values, result) ⇒ Object

reduce 2 omitted



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

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

#_reduce_30(val, _values, result) ⇒ Object



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

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

#_reduce_31(val, _values, result) ⇒ Object



446
447
448
449
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 446

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

#_reduce_33(val, _values, result) ⇒ Object

reduce 32 omitted



453
454
455
456
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 453

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

#_reduce_34(val, _values, result) ⇒ Object



458
459
460
461
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 458

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

#_reduce_37(val, _values, result) ⇒ Object

reduce 36 omitted



467
468
469
470
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 467

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

#_reduce_4(val, _values, result) ⇒ Object



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

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

#_reduce_5(val, _values, result) ⇒ Object



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

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

#_reduce_52(val, _values, result) ⇒ Object

reduce 51 omitted



500
501
502
503
504
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 500

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

#_reduce_53(val, _values, result) ⇒ Object



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

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

#_reduce_54(val, _values, result) ⇒ Object



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

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

#_reduce_55(val, _values, result) ⇒ Object



518
519
520
521
522
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 518

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

#_reduce_56(val, _values, result) ⇒ Object



524
525
526
527
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 524

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

#_reduce_57(val, _values, result) ⇒ Object



529
530
531
532
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 529

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

#_reduce_58(val, _values, result) ⇒ Object



534
535
536
537
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 534

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

#_reduce_6(val, _values, result) ⇒ Object



335
336
337
338
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 335

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

#_reduce_7(val, _values, result) ⇒ Object



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

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

#_reduce_8(val, _values, result) ⇒ Object



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

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

#_reduce_none(val, _values, result) ⇒ Object



539
540
541
# File 'lib/rack/mount/vendor/regin/regin/parser.rb', line 539

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