Skip to content

Commit d6b3087

Browse files
authored
Fix parsing of subscript and argument lists (apple#20)
This fixes a bug where the parser cannot disambiguate between method calls and parenthesized expressions. It also makes the parser match ANTLR; disallow semicolons and newlines between tokens for `(` and `[` in subscripts, and argument lists.
1 parent 998a296 commit d6b3087

File tree

6 files changed

+38443
-45688
lines changed

6 files changed

+38443
-45688
lines changed

grammar.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ module.exports = grammar({
7272
$._ml4_string_chars,
7373
$._ml5_string_chars,
7474
$._ml6_string_chars,
75-
$._open_square_bracket,
76-
$._open_entry_bracket,
75+
// '[' without preceding newline or semicolon
76+
$._open_subscript_bracket,
77+
// '(' without preceding newline or semicolon
78+
$._open_argument_paren,
7779
],
7880

7981
extras: $ => [
@@ -85,15 +87,7 @@ module.exports = grammar({
8587
word: $ => $.identifier,
8688

8789
conflicts: $ => [
88-
// in ANTLR we deal with these by not allowing a newline or semicolon before subscript
89-
[$.objectProperty, $.subscriptExpr],
90-
[$.objectMethod, $.subscriptExpr],
91-
[$.objectEntry, $.subscriptExpr],
92-
[$.objectPredicate, $.subscriptExpr],
93-
9490
// these should be fixable in some other way (perhaps with prec)
95-
[$.propertyCallExpr, $.methodCallExpr],
96-
[$.variableExpr, $.methodCallExpr],
9791
[$.variableExpr, $.typedIdentifier],
9892

9993
// not sure what this one is about
@@ -256,7 +250,7 @@ module.exports = grammar({
256250
objectMethod: $ => seq($.methodHeader, "=", $._expr),
257251

258252
objectEntry: $ => seq(
259-
alias($._open_entry_bracket, "["),
253+
"[",
260254
$._expr,
261255
"]",
262256
choice(
@@ -327,7 +321,7 @@ module.exports = grammar({
327321
seq($.qualifiedIdentifier, optional($.typeArgumentList)),
328322
seq("(", $.type, ")"),
329323
prec(PREC.NULLABLE_TYPE, seq($.type, "?")),
330-
seq($.type, "(", commaSep1($._expr), ")"),
324+
seq($.type, alias($._open_argument_paren, "("), commaSep1($._expr), ")"),
331325
prec.left(PREC.UNION_TYPE, seq($.type, "|", $.type)),
332326
prec(PREC.DEFAULT_TYPE, seq("*", $.type)),
333327
prec(PREC.FUN_TYPE, seq("(", commaSep($.type), ")", "->", $.type))
@@ -357,7 +351,7 @@ module.exports = grammar({
357351
),
358352

359353
argumentList: $ => seq(
360-
'(',
354+
alias($._open_argument_paren, '('),
361355
commaSep($._expr),
362356
')'
363357
),
@@ -707,7 +701,7 @@ module.exports = grammar({
707701

708702
propertyCallExpr: $ => seq(choice("super", $._expr), choice(".", "?."), $.identifier),
709703

710-
subscriptExpr: $ => seq(choice("super", $._expr), alias($._open_square_bracket, "["), $._expr, "]"),
704+
subscriptExpr: $ => seq(choice("super", $._expr), alias($._open_subscript_bracket, "["), $._expr, "]"),
711705

712706
unaryExpr: $ => choice(
713707
prec.left(PREC.NEG, seq($._expr, '!!')),

src/grammar.json

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,7 @@
770770
"type": "SEQ",
771771
"members": [
772772
{
773-
"type": "ALIAS",
774-
"content": {
775-
"type": "SYMBOL",
776-
"name": "_open_entry_bracket"
777-
},
778-
"named": false,
773+
"type": "STRING",
779774
"value": "["
780775
},
781776
{
@@ -1112,7 +1107,12 @@
11121107
"name": "type"
11131108
},
11141109
{
1115-
"type": "STRING",
1110+
"type": "ALIAS",
1111+
"content": {
1112+
"type": "SYMBOL",
1113+
"name": "_open_argument_paren"
1114+
},
1115+
"named": false,
11161116
"value": "("
11171117
},
11181118
{
@@ -1400,7 +1400,12 @@
14001400
"type": "SEQ",
14011401
"members": [
14021402
{
1403-
"type": "STRING",
1403+
"type": "ALIAS",
1404+
"content": {
1405+
"type": "SYMBOL",
1406+
"name": "_open_argument_paren"
1407+
},
1408+
"named": false,
14041409
"value": "("
14051410
},
14061411
{
@@ -3184,7 +3189,7 @@
31843189
"type": "ALIAS",
31853190
"content": {
31863191
"type": "SYMBOL",
3187-
"name": "_open_square_bracket"
3192+
"name": "_open_subscript_bracket"
31883193
},
31893194
"named": false,
31903195
"value": "["
@@ -4071,30 +4076,6 @@
40714076
}
40724077
],
40734078
"conflicts": [
4074-
[
4075-
"objectProperty",
4076-
"subscriptExpr"
4077-
],
4078-
[
4079-
"objectMethod",
4080-
"subscriptExpr"
4081-
],
4082-
[
4083-
"objectEntry",
4084-
"subscriptExpr"
4085-
],
4086-
[
4087-
"objectPredicate",
4088-
"subscriptExpr"
4089-
],
4090-
[
4091-
"propertyCallExpr",
4092-
"methodCallExpr"
4093-
],
4094-
[
4095-
"variableExpr",
4096-
"methodCallExpr"
4097-
],
40984079
[
40994080
"variableExpr",
41004081
"typedIdentifier"
@@ -4166,11 +4147,11 @@
41664147
},
41674148
{
41684149
"type": "SYMBOL",
4169-
"name": "_open_square_bracket"
4150+
"name": "_open_subscript_bracket"
41704151
},
41714152
{
41724153
"type": "SYMBOL",
4173-
"name": "_open_entry_bracket"
4154+
"name": "_open_argument_paren"
41744155
}
41754156
],
41764157
"inline": [],

0 commit comments

Comments
 (0)