Skip to content

Commit

Permalink
Add #decorative?, fix comment group qtf parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynetics committed Feb 19, 2023
1 parent 496db66 commit 877b214
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `Regexp::Expression::Shared#{capturing?,comment?}`
* previously only available on capturing and comment groups
- `Regexp::Expression::Shared#{decorative?}`
* true for non-functionals: comment groups and comments and whitespace in x-mode
- `Regexp::Expression::Shared#parent`
- support calling `Subexpression#{each_expression,flat_map}` with a one-argument block
* in this case, only the expressions are passed to the block, no indices
Expand All @@ -24,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* e.g. in `/(?x:(?#hello)) /`, the x-option wrongly applied to the whitespace
- fixed nested comment groups breaking conditionals
* e.g. in `/(a)(?(1)b|c(?#hello)d)e/`, the 2nd conditional branch included "e"
- fixed quantifiers after comment groups being mis-assigned to that group
* e.g. in `/a(?#foo){3}/` (matches 'aaa')
- fixed scanner accepting unmatched closing parentheses ')'
* these are a `SyntaxError` in Ruby, so could only be passed as a String
* they now raise a `Regexp::Scanner::ScannerError`
Expand Down
6 changes: 5 additions & 1 deletion lib/regexp_parser/expression/methods/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def capturing?
end

def comment?
false # overridden to be true e.g. in Expression::Group::Comment
false # overridden to be true in Expression::{Comment,Group::Comment}
end

def decorative?
false # overridden to be true in Expression::{FreeSpace,Group::Comment}
end

def optional?
Expand Down
2 changes: 1 addition & 1 deletion lib/regexp_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def property(token)
end

def quantifier(token)
target_node = node.expressions.reverse.find { |exp| !exp.is_a?(FreeSpace) }
target_node = node.expressions.reverse.find { |exp| !exp.decorative? }
target_node or raise ParserError, "No valid target found for '#{token.text}'"

# in case of chained quantifiers, wrap target in an implicit passive group
Expand Down
9 changes: 5 additions & 4 deletions spec/expression/methods/tests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,14 @@
[0] => [capturing?: true],
[1] => [capturing?: false]

# test #comment?
include_examples 'parse', /(a)(?#b)c#d/x,
# test #comment?, #decorative?
include_examples 'parse', /(a)(?#b)c # d/x,
[] => [comment?: false],
[0] => [comment?: false],
[1] => [comment?: true],
[1] => [comment?: true, decorative?: true],
[2] => [comment?: false],
[3] => [comment?: true]
[3] => [comment?: false, decorative?: true],
[4] => [comment?: true, decorative?: true]

# test #optional?
include_examples 'parse', /a?/, [0] => [optional?: true]
Expand Down
5 changes: 5 additions & 0 deletions spec/parser/groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
3 => [:group, :comment, Group::Comment, capturing?: false, comment?: true],
5 => [:group, :comment, Group::Comment, capturing?: false, comment?: true]

include_examples 'parse', /a(?# is for apple){3}/,
[0] => [Literal, text: 'a', quantified?: true],
[0, :q] => [Quantifier, text: '{3}'],
[1] => [Group::Comment, text: '(?# is for apple)', quantified?: false]

if ruby_version_at_least('2.4.1')
include_examples 'parse', 'a(?~b)c(?~d)e',
1 => [:group, :absence, Group::Absence],
Expand Down

0 comments on commit 877b214

Please sign in to comment.