Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 94 additions & 54 deletions snapshots/endless_methods.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@ ProgramNode (location: (1,0)-(5,22))
@ ProgramNode (location: (1,0)-(7,15))
├── flags: ∅
├── locals: []
├── locals: [:x]
└── statements:
@ StatementsNode (location: (1,0)-(5,22))
@ StatementsNode (location: (1,0)-(7,15))
├── flags: ∅
└── body: (length: 3)
└── body: (length: 4)
├── @ DefNode (location: (1,0)-(1,11))
│ ├── flags: newline
│ ├── name: :foo
Expand Down Expand Up @@ -61,55 +61,95 @@
│ ├── rparen_loc: ∅
│ ├── equal_loc: (3,8)-(3,9) = "="
│ └── end_keyword_loc: ∅
└── @ DefNode (location: (5,0)-(5,22))
├── @ DefNode (location: (5,0)-(5,22))
│ ├── flags: newline
│ ├── name: :method
│ ├── name_loc: (5,4)-(5,10) = "method"
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ StatementsNode (location: (5,13)-(5,22))
│ │ ├── flags: ∅
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (5,13)-(5,22))
│ │ ├── flags: ∅
│ │ ├── receiver:
│ │ │ @ CallNode (location: (5,13)-(5,18))
│ │ │ ├── flags: ∅
│ │ │ ├── receiver:
│ │ │ │ @ IntegerNode (location: (5,13)-(5,14))
│ │ │ │ ├── flags: static_literal, decimal
│ │ │ │ └── value: 1
│ │ │ ├── call_operator_loc: ∅
│ │ │ ├── name: :+
│ │ │ ├── message_loc: (5,15)-(5,16) = "+"
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments:
│ │ │ │ @ ArgumentsNode (location: (5,17)-(5,18))
│ │ │ │ ├── flags: ∅
│ │ │ │ └── arguments: (length: 1)
│ │ │ │ └── @ IntegerNode (location: (5,17)-(5,18))
│ │ │ │ ├── flags: static_literal, decimal
│ │ │ │ └── value: 2
│ │ │ ├── closing_loc: ∅
│ │ │ └── block: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :+
│ │ ├── message_loc: (5,19)-(5,20) = "+"
│ │ ├── opening_loc: ∅
│ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (5,21)-(5,22))
│ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1)
│ │ │ └── @ IntegerNode (location: (5,21)-(5,22))
│ │ │ ├── flags: static_literal, decimal
│ │ │ └── value: 3
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ ├── locals: []
│ ├── def_keyword_loc: (5,0)-(5,3) = "def"
│ ├── operator_loc: ∅
│ ├── lparen_loc: ∅
│ ├── rparen_loc: ∅
│ ├── equal_loc: (5,11)-(5,12) = "="
│ └── end_keyword_loc: ∅
└── @ LocalVariableWriteNode (location: (7,0)-(7,15))
├── flags: newline
├── name: :method
├── name_loc: (5,4)-(5,10) = "method"
├── receiver: ∅
├── parameters: ∅
├── body:
│ @ StatementsNode (location: (5,13)-(5,22))
├── name: :x
├── depth: 0
├── name_loc: (7,0)-(7,1) = "x"
├── value:
│ @ DefNode (location: (7,4)-(7,15))
│ ├── flags: ∅
│ └── body: (length: 1)
│ └── @ CallNode (location: (5,13)-(5,22))
│ ├── flags: ∅
│ ├── receiver:
│ │ @ CallNode (location: (5,13)-(5,18))
│ │ ├── flags: ∅
│ │ ├── receiver:
│ │ │ @ IntegerNode (location: (5,13)-(5,14))
│ │ │ ├── flags: static_literal, decimal
│ │ │ └── value: 1
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :+
│ │ ├── message_loc: (5,15)-(5,16) = "+"
│ │ ├── opening_loc: ∅
│ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (5,17)-(5,18))
│ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1)
│ │ │ └── @ IntegerNode (location: (5,17)-(5,18))
│ │ │ ├── flags: static_literal, decimal
│ │ │ └── value: 2
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ ├── call_operator_loc: ∅
│ ├── name: :+
│ ├── message_loc: (5,19)-(5,20) = "+"
│ ├── opening_loc: ∅
│ ├── arguments:
│ │ @ ArgumentsNode (location: (5,21)-(5,22))
│ │ ├── flags: ∅
│ │ └── arguments: (length: 1)
│ │ └── @ IntegerNode (location: (5,21)-(5,22))
│ │ ├── flags: static_literal, decimal
│ │ └── value: 3
│ ├── closing_loc: ∅
│ └── block: ∅
├── locals: []
├── def_keyword_loc: (5,0)-(5,3) = "def"
├── operator_loc: ∅
├── lparen_loc: ∅
├── rparen_loc: ∅
├── equal_loc: (5,11)-(5,12) = "="
└── end_keyword_loc: ∅
│ ├── name: :f
│ ├── name_loc: (7,8)-(7,9) = "f"
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ StatementsNode (location: (7,12)-(7,15))
│ │ ├── flags: ∅
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (7,12)-(7,15))
│ │ ├── flags: ignore_visibility
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :p
│ │ ├── message_loc: (7,12)-(7,13) = "p"
│ │ ├── opening_loc: ∅
│ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (7,14)-(7,15))
│ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1)
│ │ │ └── @ IntegerNode (location: (7,14)-(7,15))
│ │ │ ├── flags: static_literal, decimal
│ │ │ └── value: 1
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ ├── locals: []
│ ├── def_keyword_loc: (7,4)-(7,7) = "def"
│ ├── operator_loc: ∅
│ ├── lparen_loc: ∅
│ ├── rparen_loc: ∅
│ ├── equal_loc: (7,10)-(7,11) = "="
│ └── end_keyword_loc: ∅
└── operator_loc: (7,2)-(7,3) = "="
10 changes: 9 additions & 1 deletion src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -19505,7 +19505,15 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
pm_do_loop_stack_push(parser, false);
statements = (pm_node_t *) pm_statements_node_create(parser);

pm_node_t *statement = parse_expression(parser, PM_BINDING_POWER_DEFINED + 1, binding_power < PM_BINDING_POWER_COMPOSITION, false, PM_ERR_DEF_ENDLESS, (uint16_t) (depth + 1));
// In endless method bodies, we need to handle command calls carefully.
Comment thread
st0012 marked this conversation as resolved.
// We want to allow command calls in assignment context but maintain
// the same binding power to avoid changing how operators are parsed.
// Note that we're intentionally NOT allowing code like `private def foo = puts "Hello"`
// because the original parser, parse.y, can't handle it and we want to maintain the same behavior
bool allow_command_call = (binding_power == PM_BINDING_POWER_ASSIGNMENT) ||
(binding_power < PM_BINDING_POWER_COMPOSITION);

pm_node_t *statement = parse_expression(parser, PM_BINDING_POWER_DEFINED + 1, allow_command_call, false, PM_ERR_DEF_ENDLESS, (uint16_t) (depth + 1));

if (accept1(parser, PM_TOKEN_KEYWORD_RESCUE_MODIFIER)) {
context_push(parser, PM_CONTEXT_RESCUE_MODIFIER);
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/private_endless_method.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
private def foo = puts "Hello"
^ unexpected string literal, expecting end-of-input

2 changes: 2 additions & 0 deletions test/prism/fixtures/endless_methods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ def foo = 1
def bar = A ""

def method = 1 + 2 + 3

x = def f = p 1
Loading