regexp_parser 2.13.0 → 2.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3064d283e8ab829bdaffd9312b9e975ff3c47ace80c2add1dd9336c4cefb955b
4
- data.tar.gz: 78c30fac8b319e64ba6cadf096b03e88f65fd87171f706bb4ad6f1d2abbcd9c5
3
+ metadata.gz: 600784883db727d900cd3145d80664751ca22c21176964af6a5ab4430d73e74b
4
+ data.tar.gz: f64b68ef9a7e2051cf39d096d980474e6b9396130370871a30db020bb0d7f656
5
5
  SHA512:
6
- metadata.gz: d3d1a370ef4671e0dfb50e30612af9364e26f82b0facdc3e526dcf403c06e9fefa52e31f68f7d1949ca28d213603a46af2385ff8831822494b29cc9d945b2f90
7
- data.tar.gz: b81338ab447b5f03156770c6ba7f165dcc18f4d0ff0c9c3e00a64baedae62da927d508e1fb68c7893eb19738bf6db9edec204bc248b89a92af0f81a60d31df16
6
+ metadata.gz: e515512c783c924c42fcff2b39ba48d8c78054b34afb6e23e38d4dac7dc57ce910d0ca4b9cd5ed2c26e97569dd80735f305cca1b3433353f61a21d31f7d0ae9a
7
+ data.tar.gz: 2133bd1e9a05cd2b8419b79b16f76dad6c96d8d2e35c11857d18ba3ae6c42df37f87832c0f7bdbf3ceba7de666b067ebb0c0d2b8ad0adf4f51520e8c84461e50
@@ -16,40 +16,12 @@ module Regexp::Expression
16
16
  ::Regexp.new(to_s(format))
17
17
  end
18
18
 
19
- def quantify(*args)
20
- self.quantifier = Quantifier.new(*args)
21
- end
22
-
23
- def unquantified_clone
24
- clone.tap { |exp| exp.quantifier = nil }
25
- end
26
-
27
- # Deprecated. Prefer `#repetitions` which has a more uniform interface.
28
- def quantity
29
- return [nil, nil] unless quantified?
30
-
31
- [quantifier.min, quantifier.max]
32
- end
33
-
34
- def repetitions
35
- return 1..1 unless quantified?
36
-
37
- min = quantifier.min
38
- max = quantifier.max < 0 ? Float::INFINITY : quantifier.max
39
- min..max
40
- end
41
-
42
- def greedy?
43
- quantified? and quantifier.greedy?
44
- end
45
-
46
- def reluctant?
47
- quantified? and quantifier.reluctant?
48
- end
49
- alias :lazy? :reluctant?
50
-
51
- def possessive?
52
- quantified? and quantifier.possessive?
19
+ # Returns the root expression of the current expression. This will be a
20
+ # Regexp::Expression::Root, unless called on a manually constructed node.
21
+ def find_root
22
+ exp = self
23
+ exp = exp.parent until exp.parent.nil?
24
+ exp
53
25
  end
54
26
 
55
27
  def to_h
@@ -85,30 +57,5 @@ module Regexp::Expression
85
57
  end
86
58
  end
87
59
  end
88
-
89
- # Assigns referenced expressions to referring expressions, e.g. if there is
90
- # an instance of Backreference::Number, its #referenced_expression is set to
91
- # the instance of Group::Capture that it refers to via its number.
92
- def assign_referenced_expressions
93
- # find all referenceable and referring expressions
94
- targets = {}
95
- targets[0] = [self] if instance_of?(Root)
96
- referrers = []
97
- each_expression do |exp|
98
- if exp.referential?
99
- referrers << exp
100
- elsif exp.is_a?(Group::Capture)
101
- (targets[exp.identifier] ||= []) << exp
102
- end
103
- end
104
- # assign referenced expressions to referring expressions
105
- # (in a second iteration because there might be forward references)
106
- referrers.each do |exp|
107
- exp.referenced_expressions = targets[exp.reference] || raise(
108
- Regexp::Parser::ParserError,
109
- "Invalid reference #{exp.reference} at pos #{exp.ts}"
110
- )
111
- end
112
- end
113
60
  end
114
61
  end
@@ -33,6 +33,7 @@ module Regexp::Expression
33
33
 
34
34
  def condition=(exp)
35
35
  expressions.delete(condition)
36
+ exp.parent = self
36
37
  expressions.unshift(exp)
37
38
  end
38
39
 
@@ -25,12 +25,8 @@ module Regexp::Expression
25
25
  class Options < Group::Base
26
26
  attr_accessor :option_changes
27
27
 
28
- def initialize_copy(orig)
29
- self.option_changes = orig.option_changes.dup
30
- super
31
- end
32
-
33
28
  def quantify(*args)
29
+ # in Ruby, /a(?i)+b/ raises SyntaxError, but /a(?i:)b/ is allowed
34
30
  if token == :options_switch
35
31
  raise Regexp::Parser::Error, 'Can not quantify an option switch'
36
32
  else
@@ -45,18 +41,10 @@ module Regexp::Expression
45
41
  end
46
42
 
47
43
  class Named < Group::Capture
48
- attr_reader :name
49
- alias identifier name
50
-
51
- def initialize(token, options = {})
52
- @name = token.text[3..-2]
53
- super
54
- end
55
-
56
- def initialize_copy(orig)
57
- @name = orig.name.dup
58
- super
44
+ def name
45
+ text[3..-2]
59
46
  end
47
+ alias identifier name
60
48
  end
61
49
 
62
50
  class Comment < Group::Base
@@ -7,5 +7,8 @@ module Regexp::Expression
7
7
  "regexp_parser v3.0.0. Please use `.construct(options: options)`."
8
8
  construct(options: options)
9
9
  end
10
+
11
+ # For \g<0> style references
12
+ def identifier; 0 end
10
13
  end
11
14
  end
@@ -8,7 +8,7 @@ module Regexp::Expression
8
8
  end
9
9
 
10
10
  def shortcut
11
- Regexp::Scanner.short_prop_map.key(token.to_s)
11
+ Regexp::Scanner::SHORT_PROP_MAP.key(token.to_s)
12
12
  end
13
13
  end
14
14
 
@@ -33,5 +33,15 @@ module Regexp::Expression
33
33
  options[:u] == true
34
34
  end
35
35
  alias :u? :unicode_classes?
36
+
37
+ def no_encoding?
38
+ options[:n] == true
39
+ end
40
+ alias :n? :no_encoding?
41
+
42
+ def fixed_encoding?
43
+ options[:s] == true
44
+ end
45
+ alias :s? :fixed_encoding?
36
46
  end
37
47
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regexp::Expression
4
+ class Base
5
+ def quantify(*args)
6
+ self.quantifier = Quantifier.new(*args)
7
+ end
8
+
9
+ def unquantified_clone
10
+ dup.tap { |exp| exp.quantifier = nil }
11
+ end
12
+
13
+ # Deprecated. Prefer `#repetitions` which has a more uniform interface.
14
+ def quantity
15
+ return [nil, nil] unless quantified?
16
+
17
+ [quantifier.min, quantifier.max]
18
+ end
19
+
20
+ def repetitions
21
+ return 1..1 unless quantified?
22
+
23
+ min = quantifier.min
24
+ max = quantifier.max < 0 ? Float::INFINITY : quantifier.max
25
+ min..max
26
+ end
27
+
28
+ def greedy?
29
+ quantified? and quantifier.greedy?
30
+ end
31
+
32
+ def reluctant?
33
+ quantified? and quantifier.reluctant?
34
+ end
35
+ alias :lazy? :reluctant?
36
+
37
+ def possessive?
38
+ quantified? and quantifier.possessive?
39
+ end
40
+ end
41
+ end
@@ -1,30 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Regexp::Expression
4
- module ReferencedExpressions
5
- attr_accessor :referenced_expressions
6
-
4
+ class Base
7
5
  def referenced_expression
8
- referenced_expressions && referenced_expressions.first
6
+ referenced_expressions.first
9
7
  end
10
8
 
11
- def initialize_copy(orig)
12
- exp_id = [self.class, self.starts_at]
13
-
14
- # prevent infinite recursion for recursive subexp calls
15
- copied = self.class.instance_eval { @copied_ref_exps ||= {} }
16
- self.referenced_expressions =
17
- if copied[exp_id]
18
- orig.referenced_expressions
19
- else
20
- copied[exp_id] = true
21
- orig.referenced_expressions && orig.referenced_expressions.map(&:dup)
22
- end
23
- copied.clear
9
+ # Find referenced expressions. E.g. if this is a Backreference::Number,
10
+ # find the Group::Capture instance that it refers to via its number.
11
+ def referenced_expressions
12
+ result = []
13
+ return result unless referential? && parent
24
14
 
25
- super
15
+ find_root.each_expression(true) do |exp|
16
+ result << exp if exp.referable? && exp.identifier == reference
17
+ end
18
+ result
26
19
  end
27
20
  end
28
-
29
- Base.include ReferencedExpressions
30
21
  end
@@ -158,4 +158,9 @@ module Regexp::Expression
158
158
  Backreference::Base.instance_eval { def referential?; true end }
159
159
  Conditional::Condition.instance_eval { def referential?; true end }
160
160
  Conditional::Expression.instance_eval { def referential?; true end }
161
+
162
+ Shared.class_eval { def referable?; self.class.referable? end }
163
+ Shared::ClassMethods.class_eval { def referable?; false end }
164
+ Group::Capture.instance_eval { def referable?; true end }
165
+ Root.instance_eval { def referable?; true end }
161
166
  end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regexp::Expression::Shared
4
+ # #to_s reproduces the original source, as an unparser would.
5
+ #
6
+ # It takes an optional format argument.
7
+ #
8
+ # Example:
9
+ #
10
+ # lit = Regexp::Parser.parse(/a +/x)[0]
11
+ #
12
+ # lit.to_s # => 'a+' # default; with quantifier
13
+ # lit.to_s(:full) # => 'a+' # default; with quantifier
14
+ # lit.to_s(:base) # => 'a' # without quantifier
15
+ # lit.to_s(:original) # => 'a +' # with quantifier AND intermittent decorations
16
+ #
17
+ def to_s(format = :full)
18
+ result = ''.dup
19
+ stack = trailing_decorations(format).reverse
20
+ stack << self
21
+ quantifiers = []
22
+
23
+ until stack.empty?
24
+ part = stack.pop
25
+ if part.instance_of?(String)
26
+ append_quantifiers(result, quantifiers)
27
+ result << part
28
+ elsif part.is_a?(::Regexp::Expression::Quantifier)
29
+ quantifiers << part
30
+ elsif part.decorative?
31
+ append_quantifiers(result, quantifiers, part.ts)
32
+ result << part.text
33
+ else
34
+ unless part.equal?(self) && format == :base
35
+ stack << part.quantifier if part.quantified?
36
+ end
37
+ stack.concat(part.parts.reverse)
38
+ end
39
+ end
40
+
41
+ append_quantifiers(result, quantifiers)
42
+ result
43
+ end
44
+ alias :to_str :to_s
45
+
46
+ protected
47
+
48
+ # Quantifiers follow their target in traversal order, but decorations can
49
+ # precede them in source order. Defer each quantifier until that point.
50
+ def append_quantifiers(result, quantifiers, position = nil)
51
+ while (quantifier = quantifiers.first)
52
+ break if position && quantifier.ts && position < quantifier.ts
53
+
54
+ result << quantifiers.shift.text
55
+ end
56
+ end
57
+
58
+ # Direct calls need decorations stored beside the expression (or its implicit
59
+ # wrapper). :full and :base include only those belonging to inner quantifiers.
60
+ def trailing_decorations(format)
61
+ target = self
62
+ if format != :original
63
+ return [] unless implicit_passive_group?(self) && last
64
+
65
+ target = last
66
+ end
67
+ return [] unless target.quantified? && target.quantifier.ts
68
+
69
+ sibling = self
70
+ while implicit_passive_group?(sibling.parent)
71
+ sibling = sibling.parent
72
+ end
73
+ return [] unless sibling.parent
74
+
75
+ siblings = sibling.parent.expressions
76
+ index = siblings.index { |exp| exp.equal?(sibling) }
77
+ return [] unless index
78
+
79
+ decorations = []
80
+ while (following = siblings[index += 1]) && following.decorative? &&
81
+ following.ts && following.ts < target.quantifier.ts
82
+ decorations << following
83
+ end
84
+ decorations
85
+ end
86
+
87
+ def implicit_passive_group?(exp)
88
+ exp.is_a?(::Regexp::Expression::Group::Passive) && exp.implicit?
89
+ end
90
+ end
@@ -6,7 +6,6 @@
6
6
  # large trees. Check tasks/benchmark/traversal.rb when working in here.
7
7
  module Regexp::Expression
8
8
  class Subexpression < Regexp::Expression::Base
9
-
10
9
  # Traverses the expression, passing each recursive child to the
11
10
  # given block.
12
11
  # If the block takes two arguments, the indices of the children within
@@ -17,6 +17,7 @@ module Regexp::Expression
17
17
  ts: params[:ts],
18
18
  )
19
19
  sequence.options = active_opts
20
+ sequence.parent = exp
20
21
  exp.expressions << sequence
21
22
  sequence
22
23
  end
@@ -10,8 +10,7 @@ module Regexp::Expression
10
10
 
11
11
  attr_accessor :type, :token, :text, :ts, :te,
12
12
  :level, :set_level, :conditional_level, :nesting_level,
13
- :options, :parent, :quantifier,
14
- :custom_to_s_handling, :pre_quantifier_decorations
13
+ :options, :parent, :quantifier
15
14
  end
16
15
  end
17
16
 
@@ -30,14 +29,14 @@ module Regexp::Expression
30
29
  private :init_from_token_and_options
31
30
 
32
31
  def initialize_copy(orig)
32
+ super
33
33
  self.text = orig.text.dup if orig.text
34
34
  self.options = orig.options.dup if orig.options
35
35
  self.quantifier = orig.quantifier.clone if orig.quantifier
36
36
  self.parent = nil # updated by Subexpression#initialize_copy
37
- if orig.pre_quantifier_decorations
38
- self.pre_quantifier_decorations = orig.pre_quantifier_decorations.map(&:dup)
37
+ if orig.respond_to?(:option_changes) && orig.option_changes
38
+ self.option_changes = orig.option_changes.dup
39
39
  end
40
- super
41
40
  end
42
41
 
43
42
  def starts_at
@@ -56,55 +55,6 @@ module Regexp::Expression
56
55
  to_s(:original).length
57
56
  end
58
57
 
59
- # #to_s reproduces the original source, as an unparser would.
60
- #
61
- # It takes an optional format argument.
62
- #
63
- # Example:
64
- #
65
- # lit = Regexp::Parser.parse(/a +/x)[0]
66
- #
67
- # lit.to_s # => 'a+' # default; with quantifier
68
- # lit.to_s(:full) # => 'a+' # default; with quantifier
69
- # lit.to_s(:base) # => 'a' # without quantifier
70
- # lit.to_s(:original) # => 'a +' # with quantifier AND intermittent decorations
71
- #
72
- def to_s(format = :full)
73
- result = ''.dup
74
- stack = []
75
- exp, current_parts, index = self, parts, 0
76
-
77
- loop do
78
- while index < current_parts.length
79
- part = current_parts[index]
80
- index += 1
81
- if part.instance_of?(String)
82
- result << part
83
- elsif !part.custom_to_s_handling
84
- stack.push([exp, format, current_parts, index])
85
- exp, format, current_parts, index = part, :original, part.parts, 0
86
- end
87
- end
88
-
89
- result << exp.pre_quantifier_decoration(format).to_s
90
- result << exp.quantifier_affix(format).to_s
91
- break if stack.empty?
92
-
93
- exp, format, current_parts, index = stack.pop
94
- end
95
-
96
- result
97
- end
98
- alias :to_str :to_s
99
-
100
- def pre_quantifier_decoration(expression_format = :original)
101
- pre_quantifier_decorations.to_a.join if expression_format == :original
102
- end
103
-
104
- def quantifier_affix(expression_format = :full)
105
- quantifier.to_s if quantified? && expression_format != :base
106
- end
107
-
108
58
  def offset
109
59
  [starts_at, full_length]
110
60
  end
@@ -13,10 +13,10 @@ module Regexp::Expression
13
13
 
14
14
  # Override base method to clone the expressions as well.
15
15
  def initialize_copy(orig)
16
+ super
16
17
  self.expressions = orig.expressions.map do |exp|
17
18
  exp.clone.tap { |copy| copy.parent = self }
18
19
  end
19
- super
20
20
  end
21
21
 
22
22
  def <<(exp)
@@ -49,21 +49,19 @@ module Regexp::Expression
49
49
  )
50
50
  end
51
51
 
52
+ # Find the expression targeted by this quantifier, ignoring decorations
53
+ # in between the two (e.g. whitespace in x-mode), and descending into
54
+ # sequences such as alternatives and conditional branches.
52
55
  def extract_quantifier_target(quantifier_description)
53
- pre_quantifier_decorations = []
54
- target = expressions.reverse_each.find do |exp|
55
- if exp.decorative?
56
- exp.custom_to_s_handling = true
57
- pre_quantifier_decorations << exp.text
58
- next
59
- end
60
- exp
61
- end
56
+ target = expressions.reverse_each.find { |exp| !exp.decorative? }
62
57
  target or raise Regexp::Parser::ParserError,
63
58
  "No valid target found for '#{quantifier_description}' quantifier"
64
59
 
65
- target.pre_quantifier_decorations = pre_quantifier_decorations
66
- target
60
+ if target.is_a?(Sequence)
61
+ target.extract_quantifier_target(quantifier_description)
62
+ else
63
+ target
64
+ end
67
65
  end
68
66
  end
69
67
  end
@@ -36,7 +36,9 @@ require_relative 'expression/methods/negative'
36
36
  require_relative 'expression/methods/options'
37
37
  require_relative 'expression/methods/parts'
38
38
  require_relative 'expression/methods/printing'
39
+ require_relative 'expression/methods/quantification'
39
40
  require_relative 'expression/methods/referenced_expressions'
40
41
  require_relative 'expression/methods/strfregexp'
41
42
  require_relative 'expression/methods/tests'
43
+ require_relative 'expression/methods/to_s'
42
44
  require_relative 'expression/methods/traverse'
@@ -42,7 +42,6 @@ class Regexp::Parser
42
42
  parse_token(token)
43
43
  end
44
44
 
45
- root.assign_referenced_expressions
46
45
  root.recursively_update_nesting_levels
47
46
 
48
47
  if block_given?
@@ -68,9 +67,11 @@ class Regexp::Parser
68
67
  return {} unless options
69
68
 
70
69
  enabled_options = {}
71
- enabled_options[:i] = true if options & ::Regexp::IGNORECASE != 0
72
- enabled_options[:m] = true if options & ::Regexp::MULTILINE != 0
73
- enabled_options[:x] = true if options & ::Regexp::EXTENDED != 0
70
+ enabled_options[:i] = true if options & ::Regexp::IGNORECASE != 0
71
+ enabled_options[:m] = true if options & ::Regexp::MULTILINE != 0
72
+ enabled_options[:n] = true if options & ::Regexp::NOENCODING != 0
73
+ enabled_options[:s] = true if options & ::Regexp::FIXEDENCODING != 0
74
+ enabled_options[:x] = true if options & ::Regexp::EXTENDED != 0
74
75
  enabled_options
75
76
  end
76
77
 
@@ -379,7 +380,7 @@ class Regexp::Parser
379
380
  unless node.instance_of?(klass)
380
381
  operator = klass.new(token, active_opts)
381
382
  sequence = operator.add_sequence(active_opts, { ts: token.ts })
382
- sequence.expressions = node.expressions
383
+ node.each { |exp| sequence << exp }
383
384
  node.expressions = []
384
385
  nest(operator)
385
386
  end
@@ -482,6 +483,7 @@ class Regexp::Parser
482
483
  # description of the problem: https://github.com/ammar/regexp_parser/issues/3
483
484
  # rationale for this solution: https://github.com/ammar/regexp_parser/pull/69
484
485
  if target_node.quantified?
486
+ target_parent = target_node.parent
485
487
  new_group = Group::Passive.construct(
486
488
  token: :passive,
487
489
  ts: target_node.ts,
@@ -491,12 +493,13 @@ class Regexp::Parser
491
493
  options: active_opts,
492
494
  )
493
495
  new_group.implicit = true
496
+ new_group.parent = target_parent
494
497
  new_group << target_node
495
498
  increment_group_level(target_node)
496
499
  unless target_node.terminal?
497
500
  target_node.each_expression { |exp| increment_group_level(exp) }
498
501
  end
499
- node.expressions[node.expressions.index(target_node)] = new_group
502
+ target_parent.expressions.map! { |exp| exp.equal?(target_node) ? new_group : exp }
500
503
  target_node = new_group
501
504
  end
502
505
 
@@ -5,18 +5,15 @@ class Regexp::Scanner
5
5
  class ValidationError < ScannerError
6
6
  # Centralizes and unifies the handling of validation related errors.
7
7
  def self.for(type, problem, reason = nil)
8
- types.fetch(type).new(problem, reason)
9
- end
10
-
11
- def self.types
12
- @types ||= {
13
- backref: InvalidBackrefError,
14
- group: InvalidGroupError,
15
- group_option: InvalidGroupOption,
16
- posix_class: UnknownPosixClassError,
17
- property: UnknownUnicodePropertyError,
18
- sequence: InvalidSequenceError,
19
- }
8
+ case type
9
+ when :backref then InvalidBackrefError
10
+ when :group then InvalidGroupError
11
+ when :group_option then InvalidGroupOption
12
+ when :posix_class then UnknownPosixClassError
13
+ when :property then UnknownUnicodePropertyError
14
+ when :sequence then InvalidSequenceError
15
+ else raise(TypeError, "no such validation error: #{type}")
16
+ end.new(problem, reason)
20
17
  end
21
18
  end
22
19