Skip to content

Commit

Permalink
[ruby/yarp] Handle parsing local variable singleton method definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Sep 13, 2023
1 parent 6e64d43 commit c421f08
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
3 changes: 3 additions & 0 deletions test/yarp/fixtures/methods.txt
Expand Up @@ -163,3 +163,6 @@ end

def foo(_a, _a, b, c)
end

foo = 1
def foo.bar; end
86 changes: 55 additions & 31 deletions test/yarp/snapshots/methods.txt
@@ -1,8 +1,8 @@
@ ProgramNode (location: (0...1194))
├── locals: [:a, :c]
@ ProgramNode (location: (0...1220))
├── locals: [:a, :c, :foo]
└── statements:
@ StatementsNode (location: (0...1194))
└── body: (length: 60)
@ StatementsNode (location: (0...1220))
└── body: (length: 62)
├── @ DefNode (location: (0...23))
│ ├── name: :foo
│ ├── name_loc: (4...7) = "foo"
Expand Down Expand Up @@ -1584,32 +1584,56 @@
│ ├── rparen_loc: (1147...1148) = ")"
│ ├── equal_loc: ∅
│ └── end_keyword_loc: (1164...1167) = "end"
└── @ DefNode (location: (1169...1194))
├── name: :foo
├── name_loc: (1173...1176) = "foo"
├── receiver: ∅
├── parameters:
│ @ ParametersNode (location: (1177...1189))
│ ├── requireds: (length: 4)
│ │ ├── @ RequiredParameterNode (location: (1177...1179))
│ │ │ └── name: :_a
│ │ ├── @ RequiredParameterNode (location: (1181...1183))
│ │ │ └── name: :_a
│ │ ├── @ RequiredParameterNode (location: (1185...1186))
│ │ │ └── name: :b
│ │ └── @ RequiredParameterNode (location: (1188...1189))
│ │ └── name: :c
│ ├── optionals: (length: 0)
│ ├── rest: ∅
│ ├── posts: (length: 0)
│ ├── keywords: (length: 0)
│ ├── keyword_rest: ∅
│ └── block: ∅
├── @ DefNode (location: (1169...1194))
│ ├── name: :foo
│ ├── name_loc: (1173...1176) = "foo"
│ ├── receiver: ∅
│ ├── parameters:
│ │ @ ParametersNode (location: (1177...1189))
│ │ ├── requireds: (length: 4)
│ │ │ ├── @ RequiredParameterNode (location: (1177...1179))
│ │ │ │ └── name: :_a
│ │ │ ├── @ RequiredParameterNode (location: (1181...1183))
│ │ │ │ └── name: :_a
│ │ │ ├── @ RequiredParameterNode (location: (1185...1186))
│ │ │ │ └── name: :b
│ │ │ └── @ RequiredParameterNode (location: (1188...1189))
│ │ │ └── name: :c
│ │ ├── optionals: (length: 0)
│ │ ├── rest: ∅
│ │ ├── posts: (length: 0)
│ │ ├── keywords: (length: 0)
│ │ ├── keyword_rest: ∅
│ │ └── block: ∅
│ ├── body: ∅
│ ├── locals: [:_a, :b, :c]
│ ├── def_keyword_loc: (1169...1172) = "def"
│ ├── operator_loc: ∅
│ ├── lparen_loc: (1176...1177) = "("
│ ├── rparen_loc: (1189...1190) = ")"
│ ├── equal_loc: ∅
│ └── end_keyword_loc: (1191...1194) = "end"
├── @ LocalVariableWriteNode (location: (1196...1203))
│ ├── name: :foo
│ ├── depth: 0
│ ├── name_loc: (1196...1199) = "foo"
│ ├── value:
│ │ @ IntegerNode (location: (1202...1203))
│ │ └── flags: decimal
│ └── operator_loc: (1200...1201) = "="
└── @ DefNode (location: (1204...1220))
├── name: :bar
├── name_loc: (1212...1215) = "bar"
├── receiver:
│ @ LocalVariableReadNode (location: (1208...1211))
│ ├── name: :foo
│ └── depth: 0
├── parameters: ∅
├── body: ∅
├── locals: [:_a, :b, :c]
├── def_keyword_loc: (1169...1172) = "def"
├── operator_loc:
├── lparen_loc: (1176...1177) = "("
├── rparen_loc: (1189...1190) = ")"
├── locals: []
├── def_keyword_loc: (1204...1207) = "def"
├── operator_loc: (1211...1212) = "."
├── lparen_loc:
├── rparen_loc:
├── equal_loc: ∅
└── end_keyword_loc: (1191...1194) = "end"
└── end_keyword_loc: (1217...1220) = "end"
3 changes: 2 additions & 1 deletion yarp/yarp.c
Expand Up @@ -11806,12 +11806,12 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
name = parser->previous;
break;
case YP_TOKEN_IDENTIFIER: {
yp_parser_scope_push(parser, true);
parser_lex(parser);

if (match_any_type_p(parser, 2, YP_TOKEN_DOT, YP_TOKEN_COLON_COLON)) {
receiver = parse_variable_call(parser);

yp_parser_scope_push(parser, true);
lex_state_set(parser, YP_LEX_STATE_FNAME);
parser_lex(parser);

Expand All @@ -11822,6 +11822,7 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_DEF_NAME_AFTER_RECEIVER);
}
} else {
yp_parser_scope_push(parser, true);
name = parser->previous;
}

Expand Down

0 comments on commit c421f08

Please sign in to comment.