/
parser.rb
596 lines (507 loc) · 21.3 KB
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
require 'regexp_parser/error'
require 'regexp_parser/expression'
class Regexp::Parser
include Regexp::Expression
class ParserError < Regexp::Parser::Error; end
class UnknownTokenTypeError < ParserError
def initialize(type, token)
super "Unknown token type #{type} #{token.inspect}"
end
end
class UnknownTokenError < ParserError
def initialize(type, token)
super "Unknown #{type} token #{token.token}"
end
end
def self.parse(input, syntax = nil, options: nil, &block)
new.parse(input, syntax, options: options, &block)
end
def parse(input, syntax = nil, options: nil, &block)
root = Root.construct(options: extract_options(input, options))
self.root = root
self.node = root
self.nesting = [root]
self.options_stack = [root.options]
self.switching_options = false
self.conditional_nesting = []
self.captured_group_counts = Hash.new(0)
Regexp::Lexer.scan(input, syntax, options: options, collect_tokens: false) do |token|
parse_token(token)
end
# Trigger recursive setting of #nesting_level, which reflects how deep
# a node is in the tree. Do this at the end to account for tree rewrites.
root.nesting_level = 0
assign_referenced_expressions
if block_given?
block.call(root)
else
root
end
end
private
attr_accessor :root, :node, :nesting,
:options_stack, :switching_options, :conditional_nesting,
:captured_group_counts
def extract_options(input, options)
if options && !input.is_a?(String)
raise ArgumentError, 'options cannot be supplied unless parsing a String'
end
options = input.options if input.is_a?(::Regexp)
return {} unless options
enabled_options = {}
enabled_options[:i] = true if options & ::Regexp::IGNORECASE != 0
enabled_options[:m] = true if options & ::Regexp::MULTILINE != 0
enabled_options[:x] = true if options & ::Regexp::EXTENDED != 0
enabled_options
end
def parse_token(token)
case token.type
when :anchor; anchor(token)
when :assertion, :group; group(token)
when :backref; backref(token)
when :conditional; conditional(token)
when :escape; escape(token)
when :free_space; free_space(token)
when :keep; keep(token)
when :literal; literal(token)
when :meta; meta(token)
when :posixclass, :nonposixclass; posixclass(token)
when :property, :nonproperty; property(token)
when :quantifier; quantifier(token)
when :set; set(token)
when :type; type(token)
else
raise UnknownTokenTypeError.new(token.type, token)
end
close_completed_character_set_range
end
def anchor(token)
case token.token
when :bol; node << Anchor::BeginningOfLine.new(token, active_opts)
when :bos; node << Anchor::BOS.new(token, active_opts)
when :eol; node << Anchor::EndOfLine.new(token, active_opts)
when :eos; node << Anchor::EOS.new(token, active_opts)
when :eos_ob_eol; node << Anchor::EOSobEOL.new(token, active_opts)
when :match_start; node << Anchor::MatchStart.new(token, active_opts)
when :nonword_boundary; node << Anchor::NonWordBoundary.new(token, active_opts)
when :word_boundary; node << Anchor::WordBoundary.new(token, active_opts)
else
raise UnknownTokenError.new('Anchor', token)
end
end
def group(token)
case token.token
when :options, :options_switch
options_group(token)
when :close
close_group
when :comment
node << Group::Comment.new(token, active_opts)
else
open_group(token)
end
end
MOD_FLAGS = %w[i m x].map(&:to_sym)
ENC_FLAGS = %w[a d u].map(&:to_sym)
def options_group(token)
positive, negative = token.text.split('-', 2)
negative ||= ''
self.switching_options = token.token.equal?(:options_switch)
opt_changes = {}
new_active_opts = active_opts.dup
MOD_FLAGS.each do |flag|
if positive.include?(flag.to_s)
opt_changes[flag] = new_active_opts[flag] = true
end
if negative.include?(flag.to_s)
opt_changes[flag] = false
new_active_opts.delete(flag)
end
end
if (enc_flag = positive.reverse[/[adu]/])
enc_flag = enc_flag.to_sym
(ENC_FLAGS - [enc_flag]).each do |other|
opt_changes[other] = false if new_active_opts[other]
new_active_opts.delete(other)
end
opt_changes[enc_flag] = new_active_opts[enc_flag] = true
end
options_stack << new_active_opts
options_group = Group::Options.new(token, active_opts)
options_group.option_changes = opt_changes
nest(options_group)
end
def open_group(token)
group_class =
case token.token
when :absence; Group::Absence
when :atomic; Group::Atomic
when :capture; Group::Capture
when :named; Group::Named
when :passive; Group::Passive
when :lookahead; Assertion::Lookahead
when :lookbehind; Assertion::Lookbehind
when :nlookahead; Assertion::NegativeLookahead
when :nlookbehind; Assertion::NegativeLookbehind
else
raise UnknownTokenError.new('Group type open', token)
end
group = group_class.new(token, active_opts)
if group.capturing?
group.number = total_captured_group_count + 1
group.number_at_level = captured_group_count_at_level + 1
count_captured_group
end
# Push the active options to the stack again. This way we can simply pop the
# stack for any group we close, no matter if it had its own options or not.
options_stack << active_opts
nest(group)
end
def total_captured_group_count
captured_group_counts.values.reduce(0, :+)
end
def captured_group_count_at_level
captured_group_counts[node]
end
def count_captured_group
captured_group_counts[node] += 1
end
def close_group
options_stack.pop unless switching_options
self.switching_options = false
decrease_nesting
end
def decrease_nesting
while nesting.last.is_a?(SequenceOperation)
nesting.pop
self.node = nesting.last
end
nesting.pop
yield(node) if block_given?
self.node = nesting.last
self.node = node.last if node.last.is_a?(SequenceOperation)
end
def backref(token)
case token.token
when :name_ref
node << Backreference::Name.new(token, active_opts)
when :name_recursion_ref
node << Backreference::NameRecursionLevel.new(token, active_opts)
when :name_call
node << Backreference::NameCall.new(token, active_opts)
when :number, :number_ref # TODO: split in v3.0.0
node << Backreference::Number.new(token, active_opts)
when :number_recursion_ref
node << Backreference::NumberRecursionLevel.new(token, active_opts).tap do |exp|
# TODO: should split off new token number_recursion_rel_ref and new
# class NumberRelativeRecursionLevel in v3.0.0 to get rid of this
if exp.text =~ /[<'][+-]/
assign_effective_number(exp)
else
exp.effective_number = exp.number
end
end
when :number_call
node << Backreference::NumberCall.new(token, active_opts)
when :number_rel_ref
node << Backreference::NumberRelative.new(token, active_opts).tap do |exp|
assign_effective_number(exp)
end
when :number_rel_call
node << Backreference::NumberCallRelative.new(token, active_opts).tap do |exp|
assign_effective_number(exp)
end
else
raise UnknownTokenError.new('Backreference', token)
end
end
def assign_effective_number(exp)
exp.effective_number =
exp.number + total_captured_group_count + (exp.number < 0 ? 1 : 0)
exp.effective_number > 0 ||
raise(ParserError, "Invalid reference: #{exp.reference}")
end
def conditional(token)
case token.token
when :open
nest_conditional(Conditional::Expression.new(token, active_opts))
when :condition
conditional_nesting.last.condition = Conditional::Condition.new(token, active_opts)
conditional_nesting.last.add_sequence(active_opts, { ts: token.te })
when :separator
conditional_nesting.last.add_sequence(active_opts, { ts: token.te })
self.node = conditional_nesting.last.branches.last
when :close
conditional_nesting.pop
decrease_nesting
self.node =
if conditional_nesting.empty?
nesting.last
else
conditional_nesting.last
end
else
raise UnknownTokenError.new('Conditional', token)
end
end
def nest_conditional(exp)
conditional_nesting.push(exp)
nest(exp)
end
def nest(exp)
nesting.push(exp)
node << exp
self.node = exp
end
def escape(token)
case token.token
when :backspace; node << EscapeSequence::Backspace.new(token, active_opts)
when :escape; node << EscapeSequence::AsciiEscape.new(token, active_opts)
when :bell; node << EscapeSequence::Bell.new(token, active_opts)
when :form_feed; node << EscapeSequence::FormFeed.new(token, active_opts)
when :newline; node << EscapeSequence::Newline.new(token, active_opts)
when :carriage; node << EscapeSequence::Return.new(token, active_opts)
when :tab; node << EscapeSequence::Tab.new(token, active_opts)
when :vertical_tab; node << EscapeSequence::VerticalTab.new(token, active_opts)
when :codepoint; node << EscapeSequence::Codepoint.new(token, active_opts)
when :codepoint_list; node << EscapeSequence::CodepointList.new(token, active_opts)
when :hex; node << EscapeSequence::Hex.new(token, active_opts)
when :octal; node << EscapeSequence::Octal.new(token, active_opts)
when :control
if token.text =~ /\A(?:\\C-\\M|\\c\\M)/
# TODO: emit :meta_control_sequence token in v3.0.0
node << EscapeSequence::MetaControl.new(token, active_opts)
else
node << EscapeSequence::Control.new(token, active_opts)
end
when :meta_sequence
if token.text =~ /\A\\M-\\[Cc]/
# TODO: emit :meta_control_sequence token in v3.0.0:
node << EscapeSequence::MetaControl.new(token, active_opts)
else
node << EscapeSequence::Meta.new(token, active_opts)
end
else
# treating everything else as a literal
# TODO: maybe split this up a bit more in v3.0.0?
# E.g. escaped quantifiers or set meta chars are not the same
# as stuff that would be a literal even without the backslash.
# Right now, they all end up here.
node << EscapeSequence::Literal.new(token, active_opts)
end
end
def free_space(token)
case token.token
when :comment
node << Comment.new(token, active_opts)
when :whitespace
node << WhiteSpace.new(token, active_opts)
else
raise UnknownTokenError.new('FreeSpace', token)
end
end
def keep(token)
node << Keep::Mark.new(token, active_opts)
end
def literal(token)
node << Literal.new(token, active_opts)
end
def meta(token)
case token.token
when :dot
node << CharacterType::Any.new(token, active_opts)
when :alternation
sequence_operation(Alternation, token)
else
raise UnknownTokenError.new('Meta', token)
end
end
def sequence_operation(klass, token)
unless node.instance_of?(klass)
operator = klass.new(token, active_opts)
sequence = operator.add_sequence(active_opts, { ts: token.ts })
sequence.expressions = node.expressions
node.expressions = []
nest(operator)
end
node.add_sequence(active_opts, { ts: token.te })
end
def posixclass(token)
node << PosixClass.new(token, active_opts)
end
UP = Regexp::Expression::Property
UPTokens = Regexp::Syntax::Token::Property
def property(token)
case token.token
when :alnum; node << UP::Alnum.new(token, active_opts)
when :alpha; node << UP::Alpha.new(token, active_opts)
when :ascii; node << UP::Ascii.new(token, active_opts)
when :blank; node << UP::Blank.new(token, active_opts)
when :cntrl; node << UP::Cntrl.new(token, active_opts)
when :digit; node << UP::Digit.new(token, active_opts)
when :graph; node << UP::Graph.new(token, active_opts)
when :lower; node << UP::Lower.new(token, active_opts)
when :print; node << UP::Print.new(token, active_opts)
when :punct; node << UP::Punct.new(token, active_opts)
when :space; node << UP::Space.new(token, active_opts)
when :upper; node << UP::Upper.new(token, active_opts)
when :word; node << UP::Word.new(token, active_opts)
when :xdigit; node << UP::Xdigit.new(token, active_opts)
when :xposixpunct; node << UP::XPosixPunct.new(token, active_opts)
# only in Oniguruma (old rubies)
when :newline; node << UP::Newline.new(token, active_opts)
when :any; node << UP::Any.new(token, active_opts)
when :assigned; node << UP::Assigned.new(token, active_opts)
when :letter; node << UP::Letter::Any.new(token, active_opts)
when :cased_letter; node << UP::Letter::Cased.new(token, active_opts)
when :uppercase_letter; node << UP::Letter::Uppercase.new(token, active_opts)
when :lowercase_letter; node << UP::Letter::Lowercase.new(token, active_opts)
when :titlecase_letter; node << UP::Letter::Titlecase.new(token, active_opts)
when :modifier_letter; node << UP::Letter::Modifier.new(token, active_opts)
when :other_letter; node << UP::Letter::Other.new(token, active_opts)
when :mark; node << UP::Mark::Any.new(token, active_opts)
when :combining_mark; node << UP::Mark::Combining.new(token, active_opts)
when :nonspacing_mark; node << UP::Mark::Nonspacing.new(token, active_opts)
when :spacing_mark; node << UP::Mark::Spacing.new(token, active_opts)
when :enclosing_mark; node << UP::Mark::Enclosing.new(token, active_opts)
when :number; node << UP::Number::Any.new(token, active_opts)
when :decimal_number; node << UP::Number::Decimal.new(token, active_opts)
when :letter_number; node << UP::Number::Letter.new(token, active_opts)
when :other_number; node << UP::Number::Other.new(token, active_opts)
when :punctuation; node << UP::Punctuation::Any.new(token, active_opts)
when :connector_punctuation; node << UP::Punctuation::Connector.new(token, active_opts)
when :dash_punctuation; node << UP::Punctuation::Dash.new(token, active_opts)
when :open_punctuation; node << UP::Punctuation::Open.new(token, active_opts)
when :close_punctuation; node << UP::Punctuation::Close.new(token, active_opts)
when :initial_punctuation; node << UP::Punctuation::Initial.new(token, active_opts)
when :final_punctuation; node << UP::Punctuation::Final.new(token, active_opts)
when :other_punctuation; node << UP::Punctuation::Other.new(token, active_opts)
when :separator; node << UP::Separator::Any.new(token, active_opts)
when :space_separator; node << UP::Separator::Space.new(token, active_opts)
when :line_separator; node << UP::Separator::Line.new(token, active_opts)
when :paragraph_separator; node << UP::Separator::Paragraph.new(token, active_opts)
when :symbol; node << UP::Symbol::Any.new(token, active_opts)
when :math_symbol; node << UP::Symbol::Math.new(token, active_opts)
when :currency_symbol; node << UP::Symbol::Currency.new(token, active_opts)
when :modifier_symbol; node << UP::Symbol::Modifier.new(token, active_opts)
when :other_symbol; node << UP::Symbol::Other.new(token, active_opts)
when :other; node << UP::Codepoint::Any.new(token, active_opts)
when :control; node << UP::Codepoint::Control.new(token, active_opts)
when :format; node << UP::Codepoint::Format.new(token, active_opts)
when :surrogate; node << UP::Codepoint::Surrogate.new(token, active_opts)
when :private_use; node << UP::Codepoint::PrivateUse.new(token, active_opts)
when :unassigned; node << UP::Codepoint::Unassigned.new(token, active_opts)
when *UPTokens::Age; node << UP::Age.new(token, active_opts)
when *UPTokens::Derived; node << UP::Derived.new(token, active_opts)
when *UPTokens::Emoji; node << UP::Emoji.new(token, active_opts)
when *UPTokens::Enumerated; node << UP::Enumerated.new(token, active_opts)
when *UPTokens::Script; node << UP::Script.new(token, active_opts)
when *UPTokens::UnicodeBlock; node << UP::Block.new(token, active_opts)
else
raise UnknownTokenError.new('UnicodeProperty', token)
end
end
def quantifier(token)
target_node = node.extract_quantifier_target(token.text)
# in case of chained quantifiers, wrap target in an implicit passive group
# description of the problem: https://github.com/ammar/regexp_parser/issues/3
# rationale for this solution: https://github.com/ammar/regexp_parser/pull/69
if target_node.quantified?
new_group = Group::Passive.construct(
token: :passive,
ts: target_node.ts,
level: target_node.level,
set_level: target_node.set_level,
conditional_level: target_node.conditional_level,
options: active_opts,
)
new_group.implicit = true
new_group << target_node
increase_group_level(target_node)
node.expressions[node.expressions.index(target_node)] = new_group
target_node = new_group
end
unless token.token =~ /\A(?:zero_or_one|zero_or_more|one_or_more|interval)
(?:_greedy|_reluctant|_possessive)?\z/x
raise UnknownTokenError.new('Quantifier', token)
end
target_node.quantify(token, active_opts)
end
def increase_group_level(exp)
exp.level += 1
exp.quantifier.level += 1 if exp.quantifier
exp.terminal? || exp.each { |subexp| increase_group_level(subexp) }
end
def set(token)
case token.token
when :open; open_set(token)
when :close; close_set
when :negate; negate_set
when :range; range(token)
when :intersection; intersection(token)
else
raise UnknownTokenError.new('CharacterSet', token)
end
end
def open_set(token)
# TODO: this and Quantifier are the only cases where Expression#token
# does not match the scanner/lexer output. Fix in v3.0.0.
token.token = :character
nest(CharacterSet.new(token, active_opts))
end
def negate_set
node.negate
end
def close_set
decrease_nesting(&:close)
end
def range(token)
exp = CharacterSet::Range.new(token, active_opts)
scope = node.last.instance_of?(CharacterSet::IntersectedSequence) ? node.last : node
exp << scope.expressions.pop
nest(exp)
end
def intersection(token)
sequence_operation(CharacterSet::Intersection, token)
end
def type(token)
case token.token
when :digit; node << CharacterType::Digit.new(token, active_opts)
when :hex; node << CharacterType::Hex.new(token, active_opts)
when :linebreak; node << CharacterType::Linebreak.new(token, active_opts)
when :nondigit; node << CharacterType::NonDigit.new(token, active_opts)
when :nonhex; node << CharacterType::NonHex.new(token, active_opts)
when :nonspace; node << CharacterType::NonSpace.new(token, active_opts)
when :nonword; node << CharacterType::NonWord.new(token, active_opts)
when :space; node << CharacterType::Space.new(token, active_opts)
when :word; node << CharacterType::Word.new(token, active_opts)
when :xgrapheme; node << CharacterType::ExtendedGrapheme.new(token, active_opts)
else
raise UnknownTokenError.new('CharacterType', token)
end
end
def close_completed_character_set_range
decrease_nesting if node.instance_of?(CharacterSet::Range) && node.complete?
end
def active_opts
options_stack.last
end
# Assigns referenced expressions to refering expressions, e.g. if there is
# an instance of Backreference::Number, its #referenced_expression is set to
# the instance of Group::Capture that it refers to via its number.
def assign_referenced_expressions
# find all referencable and refering expressions
targets = { 0 => root }
referrers = []
root.each_expression do |exp|
exp.is_a?(Group::Capture) && targets[exp.identifier] = exp
referrers << exp if exp.referential?
end
# assign reference expression to refering expressions
# (in a second iteration because there might be forward references)
referrers.each do |exp|
exp.referenced_expression = targets[exp.reference] ||
raise(ParserError, "Invalid reference #{exp.reference} at pos #{exp.ts}")
end
end
end # module Regexp::Parser