regexp_parser 2.5.0 → 2.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -235,7 +235,15 @@ class Regexp::Parser
235
235
  when :number, :number_ref
236
236
  node << Backreference::Number.new(token, active_opts)
237
237
  when :number_recursion_ref
238
- node << Backreference::NumberRecursionLevel.new(token, active_opts)
238
+ node << Backreference::NumberRecursionLevel.new(token, active_opts).tap do |exp|
239
+ # TODO: should split off new token number_recursion_rel_ref and new
240
+ # class NumberRelativeRecursionLevel in v3.0.0 to get rid of this
241
+ if exp.text =~ /[<'][+-]/
242
+ assign_effective_number(exp)
243
+ else
244
+ exp.effective_number = exp.number
245
+ end
246
+ end
239
247
  when :number_call
240
248
  node << Backreference::NumberCall.new(token, active_opts)
241
249
  when :number_rel_ref
@@ -254,6 +262,8 @@ class Regexp::Parser
254
262
  def assign_effective_number(exp)
255
263
  exp.effective_number =
256
264
  exp.number + total_captured_group_count + (exp.number < 0 ? 1 : 0)
265
+ exp.effective_number > 0 ||
266
+ raise(ParserError, "Invalid reference: #{exp.reference}")
257
267
  end
258
268
 
259
269
  def conditional(token)
@@ -569,15 +579,17 @@ class Regexp::Parser
569
579
  # an instance of Backreference::Number, its #referenced_expression is set to
570
580
  # the instance of Group::Capture that it refers to via its number.
571
581
  def assign_referenced_expressions
572
- targets = {}
573
582
  # find all referencable expressions
583
+ targets = { 0 => root }
574
584
  root.each_expression do |exp|
575
585
  exp.is_a?(Group::Capture) && targets[exp.identifier] = exp
576
586
  end
577
587
  # assign them to any refering expressions
578
588
  root.each_expression do |exp|
579
- exp.respond_to?(:reference) &&
580
- exp.referenced_expression = targets[exp.reference]
589
+ next unless exp.respond_to?(:reference)
590
+
591
+ exp.referenced_expression = targets[exp.reference] ||
592
+ raise(ParserError, "Invalid reference: #{exp.reference}")
581
593
  end
582
594
  end
583
595
  end # module Regexp::Parser
@@ -90,8 +90,8 @@
90
90
  group_options = '?' . ( [^!#'():<=>~]+ . ':'? ) ?;
91
91
 
92
92
  group_ref = [gk];
93
- group_name_id_ab = ([^0-9\->] | utf8_multibyte) . ([^>] | utf8_multibyte)*;
94
- group_name_id_sq = ([^0-9\-'] | utf8_multibyte) . ([^'] | utf8_multibyte)*;
93
+ group_name_id_ab = ([^!0-9\->] | utf8_multibyte) . ([^>] | utf8_multibyte)*;
94
+ group_name_id_sq = ([^0-9\-'] | utf8_multibyte) . ([^'] | utf8_multibyte)*;
95
95
  group_number = '-'? . [1-9] . [0-9]*;
96
96
  group_level = [+\-] . [0-9]+;
97
97