Skip to content

Commit

Permalink
[ruby/prism] Fix hash pattern rest
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Dec 14, 2023
1 parent 74b6e70 commit b7e89d4
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 74 deletions.
1 change: 1 addition & 0 deletions prism/diagnostic.c
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions prism/diagnostic.h
Expand Up @@ -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,
Expand Down
21 changes: 15 additions & 6 deletions prism/prism.c
Expand Up @@ -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);
Expand All @@ -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);

if (rest != NULL) {
pm_parser_err_node(parser, assoc, PM_ERR_PATTERN_EXPRESSION_AFTER_REST);
}

pm_node_list_append(&assocs, assoc);
pm_node_list_append(&assocs, assoc);
}
}

pm_hash_pattern_node_t *node = pm_hash_pattern_node_node_list_create(parser, &assocs, rest);
Expand Down
38 changes: 19 additions & 19 deletions test/prism/snapshots/seattlerb/case_in_hash_pat_rest.txt
Expand Up @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions test/prism/snapshots/seattlerb/parse_pattern_058.txt
Expand Up @@ -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:
Expand Down
28 changes: 14 additions & 14 deletions test/prism/snapshots/seattlerb/parse_pattern_058_2.txt
Expand Up @@ -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:
Expand Down
34 changes: 17 additions & 17 deletions test/prism/snapshots/seattlerb/parse_pattern_076.txt
Expand Up @@ -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:
Expand Down

0 comments on commit b7e89d4

Please sign in to comment.