From 6ffedd6828bb56f697f7a5fa1f0542944307b2b0 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 22 Jun 2026 15:56:29 -0400 Subject: [PATCH] Handle EOF in hash pattern key --- snapshots/pattern_one_line_bare_key.txt | 62 +++++++++++++++++++ src/prism.c | 2 +- test/prism/errors/pattern_string_key.txt | 1 - .../fixtures/pattern_one_line_bare_key.txt | 1 + 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 snapshots/pattern_one_line_bare_key.txt create mode 100644 test/prism/fixtures/pattern_one_line_bare_key.txt diff --git a/snapshots/pattern_one_line_bare_key.txt b/snapshots/pattern_one_line_bare_key.txt new file mode 100644 index 0000000000..6c58e90b1f --- /dev/null +++ b/snapshots/pattern_one_line_bare_key.txt @@ -0,0 +1,62 @@ +@ ProgramNode (location: (1,0)-(1,15)) +├── flags: ∅ +├── locals: [:b] +└── statements: + @ StatementsNode (location: (1,0)-(1,15)) + ├── flags: ∅ + └── body: (length: 1) + └── @ MatchRequiredNode (location: (1,0)-(1,15)) + ├── flags: newline + ├── value: + │ @ CallNode (location: (1,0)-(1,3)) + │ ├── flags: variable_call, ignore_visibility + │ ├── receiver: ∅ + │ ├── call_operator_loc: ∅ + │ ├── name: :foo + │ ├── message_loc: (1,0)-(1,3) = "foo" + │ ├── opening_loc: ∅ + │ ├── arguments: ∅ + │ ├── closing_loc: ∅ + │ ├── equal_loc: ∅ + │ └── block: ∅ + ├── pattern: + │ @ HashPatternNode (location: (1,7)-(1,15)) + │ ├── flags: ∅ + │ ├── constant: ∅ + │ ├── elements: (length: 2) + │ │ ├── @ AssocNode (location: (1,7)-(1,11)) + │ │ │ ├── flags: static_literal + │ │ │ ├── key: + │ │ │ │ @ SymbolNode (location: (1,7)-(1,9)) + │ │ │ │ ├── flags: static_literal, forced_us_ascii_encoding + │ │ │ │ ├── opening_loc: ∅ + │ │ │ │ ├── value_loc: (1,7)-(1,8) = "a" + │ │ │ │ ├── closing_loc: (1,8)-(1,9) = ":" + │ │ │ │ └── unescaped: "a" + │ │ │ ├── value: + │ │ │ │ @ IntegerNode (location: (1,10)-(1,11)) + │ │ │ │ ├── flags: static_literal, decimal + │ │ │ │ └── value: 1 + │ │ │ └── operator_loc: ∅ + │ │ └── @ AssocNode (location: (1,13)-(1,15)) + │ │ ├── flags: ∅ + │ │ ├── key: + │ │ │ @ SymbolNode (location: (1,13)-(1,15)) + │ │ │ ├── flags: static_literal, forced_us_ascii_encoding + │ │ │ ├── opening_loc: ∅ + │ │ │ ├── value_loc: (1,13)-(1,14) = "b" + │ │ │ ├── closing_loc: (1,14)-(1,15) = ":" + │ │ │ └── unescaped: "b" + │ │ ├── value: + │ │ │ @ ImplicitNode (location: (1,13)-(1,14)) + │ │ │ ├── flags: ∅ + │ │ │ └── value: + │ │ │ @ LocalVariableTargetNode (location: (1,13)-(1,14)) + │ │ │ ├── flags: ∅ + │ │ │ ├── name: :b + │ │ │ └── depth: 0 + │ │ └── operator_loc: ∅ + │ ├── rest: ∅ + │ ├── opening_loc: ∅ + │ └── closing_loc: ∅ + └── operator_loc: (1,4)-(1,6) = "=>" diff --git a/src/prism.c b/src/prism.c index 0512afae41..fe249799f0 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16907,7 +16907,7 @@ parse_pattern_hash(pm_parser_t *parser, pm_constant_id_list_t *captures, pm_node parse_pattern_hash_key(parser, &keys, key); pm_node_t *value = NULL; - if (match7(parser, PM_TOKEN_COMMA, PM_TOKEN_KEYWORD_THEN, PM_TOKEN_BRACE_RIGHT, PM_TOKEN_BRACKET_RIGHT, PM_TOKEN_PARENTHESIS_RIGHT, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON)) { + if (match8(parser, PM_TOKEN_COMMA, PM_TOKEN_KEYWORD_THEN, PM_TOKEN_BRACE_RIGHT, PM_TOKEN_BRACKET_RIGHT, PM_TOKEN_PARENTHESIS_RIGHT, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON, PM_TOKEN_EOF)) { if (PM_NODE_TYPE_P(key, PM_SYMBOL_NODE)) { value = parse_pattern_hash_implicit_value(parser, captures, (pm_symbol_node_t *) key); } else { diff --git a/test/prism/errors/pattern_string_key.txt b/test/prism/errors/pattern_string_key.txt index 41bc1fa57b..0a873838d9 100644 --- a/test/prism/errors/pattern_string_key.txt +++ b/test/prism/errors/pattern_string_key.txt @@ -2,7 +2,6 @@ case:a ^~~~ expected an `end` to close the `case` statement in b:"","#{}" ^~~~~ expected a label after the `,` in the hash pattern - ^ expected a pattern expression after the key ^ expected a delimiter after the patterns of an `in` clause ^ unexpected end-of-input, assuming it is closing the parent top level context diff --git a/test/prism/fixtures/pattern_one_line_bare_key.txt b/test/prism/fixtures/pattern_one_line_bare_key.txt new file mode 100644 index 0000000000..289f6232b4 --- /dev/null +++ b/test/prism/fixtures/pattern_one_line_bare_key.txt @@ -0,0 +1 @@ +foo => a: 1, b: \ No newline at end of file