Skip to content

Commit

Permalink
Don't scan (?<!...> as named group
Browse files Browse the repository at this point in the history
fixes #85
  • Loading branch information
jaynetics committed Nov 16, 2022
1 parent 752c040 commit 81340c3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- fixed scanning of two negative lookbehind edge cases
* `(?<!x)y>` used to raise a ScannerError
* `(?<!x>)y` used to be misinterpreted as a named group
* thanks to [Sergio Medina](https://github.com/serch) for the report

## [2.6.0] - 2022-09-26 - [Janosch Müller](mailto:janosch84@gmail.com)

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions lib/regexp_parser/scanner/scanner.rl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
group_options = '?' . ( [^!#'():<=>~]+ . ':'? ) ?;

group_ref = [gk];
group_name_id_ab = ([^0-9\->] | utf8_multibyte) . ([^>] | utf8_multibyte)*;
group_name_id_sq = ([^0-9\-'] | utf8_multibyte) . ([^'] | utf8_multibyte)*;
group_name_id_ab = ([^!0-9\->] | utf8_multibyte) . ([^>] | utf8_multibyte)*;
group_name_id_sq = ([^0-9\-'] | utf8_multibyte) . ([^'] | utf8_multibyte)*;
group_number = '-'? . [1-9] . [0-9]*;
group_level = [+\-] . [0-9]+;

Expand Down
10 changes: 9 additions & 1 deletion spec/scanner/groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
include_examples 'scan', '(abc)', 0 => [:group, :capture, '(', 0, 1]

# Named groups
# only names that start with a hyphen or digit (ascii or other) are invalid
# Names that start with a hyphen or digit (ascii or other) are invalid.
# ")" is only allowed as first char of the name.
# "!" is allowed anywhere, but ?<!...> is treated as a lookbehind by Ruby.
include_examples 'scan', '(?<name>abc)', 0 => [:group, :named_ab, '(?<name>', 0, 8]
include_examples 'scan', "(?'name'abc)", 0 => [:group, :named_sq, "(?'name'", 0, 8]
include_examples 'scan', '(?<name_1>abc)', 0 => [:group, :named_ab, '(?<name_1>', 0,10]
Expand All @@ -19,7 +21,11 @@
include_examples 'scan', "(?'üüuuüü'abc)", 0 => [:group, :named_sq, "(?'üüuuüü'", 0,10]
include_examples 'scan', "(?<😋1234😋>abc)", 0 => [:group, :named_ab, "(?<😋1234😋>", 0,10]
include_examples 'scan', "(?'😋1234😋'abc)", 0 => [:group, :named_sq, "(?'😋1234😋'", 0,10]
include_examples 'scan', "(?<)x>y)", 0 => [:group, :named_ab, '(?<)x>', 0, 6]
include_examples 'scan', "(?')x'y)", 0 => [:group, :named_sq, "(?')x'", 0, 6]
include_examples 'scan', "(?'!x'y)", 0 => [:group, :named_sq, "(?'!x'", 0, 6]

# Passive groups
include_examples 'scan', '(?:abc)', 0 => [:group, :passive, '(?:', 0, 3]
include_examples 'scan', '(?:)', 0 => [:group, :passive, '(?:', 0, 3]
include_examples 'scan', '(?::)', 0 => [:group, :passive, '(?:', 0, 3]
Expand All @@ -33,6 +39,8 @@
include_examples 'scan', '(?!abc)', 0 => [:assertion, :nlookahead, '(?!', 0, 3]
include_examples 'scan', '(?<=abc)', 0 => [:assertion, :lookbehind, '(?<=', 0, 4]
include_examples 'scan', '(?<!abc)', 0 => [:assertion, :nlookbehind, '(?<!', 0, 4]
include_examples 'scan', '(?<!x)y>', 0 => [:assertion, :nlookbehind, '(?<!', 0, 4]
include_examples 'scan', '(?<!x>)y', 0 => [:assertion, :nlookbehind, '(?<!', 0, 4]

# Options
include_examples 'scan', '(?-mix:abc)', 0 => [:group, :options, '(?-mix:', 0, 7]
Expand Down

0 comments on commit 81340c3

Please sign in to comment.