[ruby/prism] Fix hash pattern rest

https://github.com/ruby/prism/commit/43c4232cfc
This commit is contained in:
Kevin Newton 2023-12-13 21:35:42 -05:00 committed by git
parent 74b6e70ef4
commit b7e89d4b17
7 changed files with 85 additions and 74 deletions

View File

@ -219,6 +219,7 @@ static const char* const diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = {
[PM_ERR_PATTERN_EXPRESSION_AFTER_PIN] = "expected a pattern expression after the `^` pin operator",
[PM_ERR_PATTERN_EXPRESSION_AFTER_PIPE] = "expected a pattern expression after the `|` operator",
[PM_ERR_PATTERN_EXPRESSION_AFTER_RANGE] = "expected a pattern expression after the range operator",
[PM_ERR_PATTERN_EXPRESSION_AFTER_REST] = "unexpected pattern expression after the `**` expression",
[PM_ERR_PATTERN_HASH_KEY] = "expected a key in the hash pattern",
[PM_ERR_PATTERN_HASH_KEY_LABEL] = "expected a label as the key in the hash pattern", // TODO // THIS // AND // ABOVE // IS WEIRD
[PM_ERR_PATTERN_IDENT_AFTER_HROCKET] = "expected an identifier after the `=>` operator",

View File

@ -211,6 +211,7 @@ typedef enum {
PM_ERR_PATTERN_EXPRESSION_AFTER_PIN,
PM_ERR_PATTERN_EXPRESSION_AFTER_PIPE,
PM_ERR_PATTERN_EXPRESSION_AFTER_RANGE,
PM_ERR_PATTERN_EXPRESSION_AFTER_REST,
PM_ERR_PATTERN_HASH_KEY,
PM_ERR_PATTERN_HASH_KEY_LABEL,
PM_ERR_PATTERN_IDENT_AFTER_HROCKET,

View File

@ -13121,10 +13121,15 @@ parse_pattern_hash(pm_parser_t *parser, pm_node_t *first_assoc) {
break;
}
pm_node_t *assoc;
if (match1(parser, PM_TOKEN_USTAR_STAR)) {
assoc = parse_pattern_keyword_rest(parser);
pm_node_t *assoc = parse_pattern_keyword_rest(parser);
if (rest == NULL) {
rest = assoc;
} else {
pm_parser_err_node(parser, assoc, PM_ERR_PATTERN_EXPRESSION_AFTER_REST);
pm_node_list_append(&assocs, assoc);
}
} else {
expect1(parser, PM_TOKEN_LABEL, PM_ERR_PATTERN_LABEL_AFTER_COMMA);
pm_node_t *key = (pm_node_t *) pm_symbol_node_label_create(parser, &parser->previous);
@ -13138,10 +13143,14 @@ parse_pattern_hash(pm_parser_t *parser, pm_node_t *first_assoc) {
}
pm_token_t operator = not_provided(parser);
assoc = (pm_node_t *) pm_assoc_node_create(parser, key, &operator, value);
}
pm_node_t *assoc = (pm_node_t *) pm_assoc_node_create(parser, key, &operator, value);
pm_node_list_append(&assocs, assoc);
if (rest != NULL) {
pm_parser_err_node(parser, assoc, PM_ERR_PATTERN_EXPRESSION_AFTER_REST);
}
pm_node_list_append(&assocs, assoc);
}
}
pm_hash_pattern_node_t *node = pm_hash_pattern_node_node_list_create(parser, &assocs, rest);

View File

@ -16,27 +16,27 @@
│ ├── pattern:
│ │ @ HashPatternNode (location: (2,3)-(2,15))
│ │ ├── constant: ∅
│ │ ├── elements: (length: 2)
│ │ │ ├── @ AssocNode (location: (2,3)-(2,7))
│ │ │ │ ├── key:
│ │ │ │ │ @ SymbolNode (location: (2,3)-(2,5))
│ │ │ │ │ ├── flags: ∅
│ │ │ │ │ ├── opening_loc: ∅
│ │ │ │ │ ├── value_loc: (2,3)-(2,4) = "b"
│ │ │ │ │ ├── closing_loc: (2,4)-(2,5) = ":"
│ │ │ │ │ └── unescaped: "b"
│ │ │ │ ├── value:
│ │ │ │ │ @ LocalVariableTargetNode (location: (2,6)-(2,7))
│ │ │ │ │ ├── name: :c
│ │ │ │ │ └── depth: 0
│ │ │ │ └── operator_loc: ∅
│ │ │ └── @ AssocSplatNode (location: (2,9)-(2,15))
│ │ ├── elements: (length: 1)
│ │ │ └── @ AssocNode (location: (2,3)-(2,7))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,3)-(2,5))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,3)-(2,4) = "b"
│ │ │ │ ├── closing_loc: (2,4)-(2,5) = ":"
│ │ │ │ └── unescaped: "b"
│ │ │ ├── value:
│ │ │ │ @ LocalVariableTargetNode (location: (2,11)-(2,15))
│ │ │ │ ├── name: :rest
│ │ │ │ @ LocalVariableTargetNode (location: (2,6)-(2,7))
│ │ │ │ ├── name: :c
│ │ │ │ └── depth: 0
│ │ │ └── operator_loc: (2,9)-(2,11) = "**"
│ │ ├── rest: ∅
│ │ │ └── operator_loc: ∅
│ │ ├── rest:
│ │ │ @ AssocSplatNode (location: (2,9)-(2,15))
│ │ │ ├── value:
│ │ │ │ @ LocalVariableTargetNode (location: (2,11)-(2,15))
│ │ │ │ ├── name: :rest
│ │ │ │ └── depth: 0
│ │ │ └── operator_loc: (2,9)-(2,11) = "**"
│ │ ├── opening_loc: ∅
│ │ └── closing_loc: ∅
│ ├── statements:

View File

@ -26,24 +26,24 @@
│ ├── pattern:
│ │ @ HashPatternNode (location: (2,3)-(2,15))
│ │ ├── constant: ∅
│ │ ├── elements: (length: 2)
│ │ │ ── @ AssocNode (location: (2,4)-(2,6))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ └── unescaped: "a"
│ │ │ ├── value: ∅
│ │ │ └── operator_loc: ∅
│ │ │ └── @ AssocSplatNode (location: (2,8)-(2,14))
│ │ │ ├── value:
│ │ │ │ @ LocalVariableTargetNode (location: (2,10)-(2,14))
│ │ │ │ ├── name: :rest
│ │ │ │ └── depth: 0
│ │ │ └── operator_loc: (2,8)-(2,10) = "**"
│ │ ├── rest: ∅
│ │ ├── elements: (length: 1)
│ │ │ ── @ AssocNode (location: (2,4)-(2,6))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ └── unescaped: "a"
│ │ │ ├── value: ∅
│ │ │ └── operator_loc: ∅
│ │ ├── rest:
│ │ │ @ AssocSplatNode (location: (2,8)-(2,14))
│ │ │ ├── value:
│ │ │ │ @ LocalVariableTargetNode (location: (2,10)-(2,14))
│ │ │ │ ├── name: :rest
│ │ │ │ └── depth: 0
│ │ │ └── operator_loc: (2,8)-(2,10) = "**"
│ │ ├── opening_loc: (2,3)-(2,4) = "{"
│ │ └── closing_loc: (2,14)-(2,15) = "}"
│ ├── statements:

View File

@ -26,21 +26,21 @@
│ ├── pattern:
│ │ @ HashPatternNode (location: (2,3)-(2,11))
│ │ ├── constant: ∅
│ │ ├── elements: (length: 2)
│ │ │ ├── @ AssocNode (location: (2,4)-(2,6))
│ │ │ │ ├── key:
│ │ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ │ ├── flags: ∅
│ │ │ │ │ ├── opening_loc: ∅
│ │ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ │ └── unescaped: "a"
│ │ │ │ ├── value: ∅
│ │ │ │ └── operator_loc: ∅
│ │ │ └── @ AssocSplatNode (location: (2,8)-(2,10))
│ │ ├── elements: (length: 1)
│ │ │ └── @ AssocNode (location: (2,4)-(2,6))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ └── unescaped: "a"
│ │ │ ├── value: ∅
│ │ │ └── operator_loc: (2,8)-(2,10) = "**"
│ │ ├── rest: ∅
│ │ │ └── operator_loc: ∅
│ │ ├── rest:
│ │ │ @ AssocSplatNode (location: (2,8)-(2,10))
│ │ │ ├── value: ∅
│ │ │ └── operator_loc: (2,8)-(2,10) = "**"
│ │ ├── opening_loc: (2,3)-(2,4) = "{"
│ │ └── closing_loc: (2,10)-(2,11) = "}"
│ ├── statements:

View File

@ -26,23 +26,23 @@
│ ├── pattern:
│ │ @ HashPatternNode (location: (2,3)-(2,16))
│ │ ├── constant: ∅
│ │ ├── elements: (length: 2)
│ │ │ ── @ AssocNode (location: (2,4)-(2,8))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ └── unescaped: "a"
│ │ │ ├── value:
│ │ │ │ @ IntegerNode (location: (2,7)-(2,8))
│ │ │ │ └── flags: decimal
│ │ │ └── operator_loc: ∅
│ │ │ └── @ NoKeywordsParameterNode (location: (2,10)-(2,15))
│ │ │ ├── operator_loc: (2,10)-(2,12) = "**"
│ │ │ └── keyword_loc: (2,12)-(2,15) = "nil"
│ │ ├── rest: ∅
│ │ ├── elements: (length: 1)
│ │ │ ── @ AssocNode (location: (2,4)-(2,8))
│ │ │ ├── key:
│ │ │ │ @ SymbolNode (location: (2,4)-(2,6))
│ │ │ │ ├── flags: ∅
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── value_loc: (2,4)-(2,5) = "a"
│ │ │ │ ├── closing_loc: (2,5)-(2,6) = ":"
│ │ │ │ └── unescaped: "a"
│ │ │ ├── value:
│ │ │ │ @ IntegerNode (location: (2,7)-(2,8))
│ │ │ │ └── flags: decimal
│ │ │ └── operator_loc: ∅
│ │ ├── rest:
│ │ │ @ NoKeywordsParameterNode (location: (2,10)-(2,15))
│ │ │ ├── operator_loc: (2,10)-(2,12) = "**"
│ │ │ └── keyword_loc: (2,12)-(2,15) = "nil"
│ │ ├── opening_loc: (2,3)-(2,4) = "{"
│ │ └── closing_loc: (2,15)-(2,16) = "}"
│ ├── statements: