[ruby/prism] Turn off extended mode when parsing extended group

https://github.com/ruby/prism/commit/098b3f08bc
This commit is contained in:
Kevin Newton 2024-08-22 10:22:51 -04:00 committed by git
parent 56a34b5af5
commit d57486cb10
3 changed files with 97 additions and 73 deletions

View File

@ -427,11 +427,11 @@ pm_regexp_options_remove(pm_regexp_options_t *options, uint8_t key) {
/** /**
* True if the given key is set in the options. * True if the given key is set in the options.
*/ */
static bool static uint8_t
pm_regexp_options_added_p(pm_regexp_options_t *options, uint8_t key) { pm_regexp_options_state(pm_regexp_options_t *options, uint8_t key) {
if (key >= PRISM_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= PRISM_REGEXP_OPTION_STATE_SLOT_MAXIMUM) { if (key >= PRISM_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= PRISM_REGEXP_OPTION_STATE_SLOT_MAXIMUM) {
key = (uint8_t) (key - PRISM_REGEXP_OPTION_STATE_SLOT_MINIMUM); key = (uint8_t) (key - PRISM_REGEXP_OPTION_STATE_SLOT_MINIMUM);
return options->values[key] == PM_REGEXP_OPTION_STATE_ADDED; return options->values[key];
} }
return false; return false;
@ -583,7 +583,10 @@ pm_regexp_parse_group(pm_regexp_parser_t *parser, uint16_t depth) {
// subexpression, then we are going to be setting the options // subexpression, then we are going to be setting the options
// for the parent group. In this case we are safe to return now. // for the parent group. In this case we are safe to return now.
if (*parser->cursor == ')') { if (*parser->cursor == ')') {
if (pm_regexp_options_added_p(&options, 'x')) parser->extended_mode = true; if (pm_regexp_options_state(&options, 'x') == PM_REGEXP_OPTION_STATE_ADDED) {
parser->extended_mode = true;
}
parser->cursor++; parser->cursor++;
return true; return true;
} }
@ -610,7 +613,15 @@ pm_regexp_parse_group(pm_regexp_parser_t *parser, uint16_t depth) {
// subexpression, then we are going to be setting the options // subexpression, then we are going to be setting the options
// for the parent group. In this case we are safe to return now. // for the parent group. In this case we are safe to return now.
if (*parser->cursor == ')') { if (*parser->cursor == ')') {
if (pm_regexp_options_added_p(&options, 'x')) parser->extended_mode = true; switch (pm_regexp_options_state(&options, 'x')) {
case PM_REGEXP_OPTION_STATE_ADDED:
parser->extended_mode = true;
break;
case PM_REGEXP_OPTION_STATE_REMOVED:
parser->extended_mode = false;
break;
}
parser->cursor++; parser->cursor++;
return true; return true;
} }
@ -624,8 +635,13 @@ pm_regexp_parse_group(pm_regexp_parser_t *parser, uint16_t depth) {
} }
bool extended_mode = parser->extended_mode; bool extended_mode = parser->extended_mode;
if (pm_regexp_options_added_p(&options, 'x')) { switch (pm_regexp_options_state(&options, 'x')) {
parser->extended_mode = true; case PM_REGEXP_OPTION_STATE_ADDED:
parser->extended_mode = true;
break;
case PM_REGEXP_OPTION_STATE_REMOVED:
parser->extended_mode = false;
break;
} }
// Now, parse the expressions within this group. // Now, parse the expressions within this group.

View File

@ -44,3 +44,5 @@ tap { /(?<a>)/ =~ to_s }
/(?<nil>)/ =~ "" /(?<nil>)/ =~ ""
def foo(nil:) = /(?<nil>)/ =~ "" def foo(nil:) = /(?<nil>)/ =~ ""
/(?-x:#)/x

View File

@ -1,10 +1,10 @@
@ ProgramNode (location: (1,0)-(46,32)) @ ProgramNode (location: (1,0)-(48,10))
├── flags: ∅ ├── flags: ∅
├── locals: [:foo, :ab, :abc, :a] ├── locals: [:foo, :ab, :abc, :a]
└── statements: └── statements:
@ StatementsNode (location: (1,0)-(46,32)) @ StatementsNode (location: (1,0)-(48,10))
├── flags: ∅ ├── flags: ∅
└── body: (length: 25) └── body: (length: 26)
├── @ CallNode (location: (1,0)-(1,9)) ├── @ CallNode (location: (1,0)-(1,9))
│ ├── flags: newline, ignore_visibility │ ├── flags: newline, ignore_visibility
│ ├── receiver: ∅ │ ├── receiver: ∅
@ -474,66 +474,72 @@
│ │ └── unescaped: "" │ │ └── unescaped: ""
│ ├── closing_loc: ∅ │ ├── closing_loc: ∅
│ └── block: ∅ │ └── block: ∅
└── @ DefNode (location: (46,0)-(46,32)) ├── @ DefNode (location: (46,0)-(46,32))
├── flags: newline │ ├── flags: newline
├── name: :foo │ ├── name: :foo
├── name_loc: (46,4)-(46,7) = "foo" │ ├── name_loc: (46,4)-(46,7) = "foo"
├── receiver: ∅ │ ├── receiver: ∅
├── parameters: │ ├── parameters:
│ @ ParametersNode (location: (46,8)-(46,12)) │ │ @ ParametersNode (location: (46,8)-(46,12))
│ ├── flags: ∅ │ │ ├── flags: ∅
│ ├── requireds: (length: 0) │ │ ├── requireds: (length: 0)
│ ├── optionals: (length: 0) │ │ ├── optionals: (length: 0)
│ ├── rest: ∅ │ │ ├── rest: ∅
│ ├── posts: (length: 0) │ │ ├── posts: (length: 0)
│ ├── keywords: (length: 1) │ │ ├── keywords: (length: 1)
│ │ └── @ RequiredKeywordParameterNode (location: (46,8)-(46,12)) │ │ │ └── @ RequiredKeywordParameterNode (location: (46,8)-(46,12))
│ │ ├── flags: ∅ │ │ │ ├── flags: ∅
│ │ ├── name: :nil │ │ │ ├── name: :nil
│ │ └── name_loc: (46,8)-(46,12) = "nil:" │ │ │ └── name_loc: (46,8)-(46,12) = "nil:"
│ ├── keyword_rest: ∅ │ │ ├── keyword_rest: ∅
│ └── block: ∅ │ │ └── block: ∅
├── body: │ ├── body:
│ @ StatementsNode (location: (46,16)-(46,32)) │ │ @ StatementsNode (location: (46,16)-(46,32))
│ ├── flags: ∅ │ │ ├── flags: ∅
│ └── body: (length: 1) │ │ └── body: (length: 1)
│ └── @ MatchWriteNode (location: (46,16)-(46,32)) │ │ └── @ MatchWriteNode (location: (46,16)-(46,32))
│ ├── flags: ∅ │ │ ├── flags: ∅
│ ├── call: │ │ ├── call:
│ │ @ CallNode (location: (46,16)-(46,32)) │ │ │ @ CallNode (location: (46,16)-(46,32))
│ │ ├── flags: ∅ │ │ │ ├── flags: ∅
│ │ ├── receiver: │ │ │ ├── receiver:
│ │ │ @ RegularExpressionNode (location: (46,16)-(46,26)) │ │ │ │ @ RegularExpressionNode (location: (46,16)-(46,26))
│ │ │ ├── flags: static_literal, forced_us_ascii_encoding │ │ │ │ ├── flags: static_literal, forced_us_ascii_encoding
│ │ │ ├── opening_loc: (46,16)-(46,17) = "/" │ │ │ │ ├── opening_loc: (46,16)-(46,17) = "/"
│ │ │ ├── content_loc: (46,17)-(46,25) = "(?<nil>)" │ │ │ │ ├── content_loc: (46,17)-(46,25) = "(?<nil>)"
│ │ │ ├── closing_loc: (46,25)-(46,26) = "/" │ │ │ │ ├── closing_loc: (46,25)-(46,26) = "/"
│ │ │ └── unescaped: "(?<nil>)" │ │ │ │ └── unescaped: "(?<nil>)"
│ │ ├── call_operator_loc: ∅ │ │ │ ├── call_operator_loc: ∅
│ │ ├── name: :=~ │ │ │ ├── name: :=~
│ │ ├── message_loc: (46,27)-(46,29) = "=~" │ │ │ ├── message_loc: (46,27)-(46,29) = "=~"
│ │ ├── opening_loc: ∅ │ │ │ ├── opening_loc: ∅
│ │ ├── arguments: │ │ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (46,30)-(46,32)) │ │ │ │ @ ArgumentsNode (location: (46,30)-(46,32))
│ │ │ ├── flags: ∅ │ │ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1) │ │ │ │ └── arguments: (length: 1)
│ │ │ └── @ StringNode (location: (46,30)-(46,32)) │ │ │ │ └── @ StringNode (location: (46,30)-(46,32))
│ │ │ ├── flags: ∅ │ │ │ │ ├── flags: ∅
│ │ │ ├── opening_loc: (46,30)-(46,31) = "\"" │ │ │ │ ├── opening_loc: (46,30)-(46,31) = "\""
│ │ │ ├── content_loc: (46,31)-(46,31) = "" │ │ │ │ ├── content_loc: (46,31)-(46,31) = ""
│ │ │ ├── closing_loc: (46,31)-(46,32) = "\"" │ │ │ │ ├── closing_loc: (46,31)-(46,32) = "\""
│ │ │ └── unescaped: "" │ │ │ │ └── unescaped: ""
│ │ ├── closing_loc: ∅ │ │ │ ├── closing_loc: ∅
│ │ └── block: ∅ │ │ │ └── block: ∅
│ └── targets: (length: 1) │ │ └── targets: (length: 1)
│ └── @ LocalVariableTargetNode (location: (46,20)-(46,23)) │ │ └── @ LocalVariableTargetNode (location: (46,20)-(46,23))
│ ├── flags: ∅ │ │ ├── flags: ∅
│ ├── name: :nil │ │ ├── name: :nil
│ └── depth: 0 │ │ └── depth: 0
├── locals: [:nil] │ ├── locals: [:nil]
├── def_keyword_loc: (46,0)-(46,3) = "def" │ ├── def_keyword_loc: (46,0)-(46,3) = "def"
├── operator_loc: ∅ │ ├── operator_loc: ∅
├── lparen_loc: (46,7)-(46,8) = "(" │ ├── lparen_loc: (46,7)-(46,8) = "("
├── rparen_loc: (46,12)-(46,13) = ")" │ ├── rparen_loc: (46,12)-(46,13) = ")"
├── equal_loc: (46,14)-(46,15) = "=" │ ├── equal_loc: (46,14)-(46,15) = "="
└── end_keyword_loc: ∅ │ └── end_keyword_loc: ∅
└── @ RegularExpressionNode (location: (48,0)-(48,10))
├── flags: newline, static_literal, extended, forced_us_ascii_encoding
├── opening_loc: (48,0)-(48,1) = "/"
├── content_loc: (48,1)-(48,8) = "(?-x:#)"
├── closing_loc: (48,8)-(48,10) = "/x"
└── unescaped: "(?-x:#)"