Skip to content

Commit 4909c4f

Browse files
committed
In member_expression and subscript_expression, just parse lhs as an expression
This is simpler and more intuitive, and it will prevent the left-hand-side of 'a ?. ()' from being parsed as a bare identifier followed by a '?.' token. We need the 'a' to be wrapped in an expression in order for that construct to parse as a call expression. This works, except that it breaks the parsing of 'new Foo.Bar()'. That code should parse as a 'new_expression' with arguments, but it now parses as a 'call_expression' *containing* a 'new_expression'. The reason for the breakage is that 'new_expression' has a non-zero precedence, and it still allows a plain 'identifier' after the 'new' (as opposed to an expression). So when the parser sees the '.' token in 'new Foo.Bar()', it needs to decide whether the 'Foo' is an 'expression' or part of a 'new_expression'. Because of the non-zero precedence on 'new_expression', it chooses the latter (which is wrong).
1 parent 9ba9b40 commit 4909c4f

File tree

4 files changed

+25619
-27389
lines changed

4 files changed

+25619
-27389
lines changed

grammar.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ module.exports = grammar({
625625
),
626626

627627
call_expression: $ => prec(PREC.CALL, seq(
628-
field('function', choice($._expression, $.super, $.function)),
628+
field('function', $._expression),
629629
optional('?.'),
630630
field('arguments', choice($.arguments, $.template_string))
631631
)),
@@ -639,6 +639,7 @@ module.exports = grammar({
639639
_constructable_expression: $ => choice(
640640
// Primary Expression
641641
$.this,
642+
$.super,
642643
$.identifier,
643644
alias($._reserved_identifier, $.identifier),
644645
$.number,
@@ -669,23 +670,13 @@ module.exports = grammar({
669670
),
670671

671672
member_expression: $ => prec(PREC.MEMBER, seq(
672-
field('object', choice(
673-
$._expression,
674-
$.identifier,
675-
$.super,
676-
alias($._reserved_identifier, $.identifier)
677-
)),
673+
field('object', $._expression),
678674
choice('.', '?.'),
679675
field('property', alias($.identifier, $.property_identifier))
680676
)),
681677

682678
subscript_expression: $ => prec.right(PREC.MEMBER, seq(
683-
field('object', choice(
684-
$._expression,
685-
$.identifier,
686-
$.super,
687-
alias($._reserved_identifier, $.identifier)
688-
)),
679+
field('object', $._expression),
689680
optional('?.'),
690681
'[',
691682
field('index', $._expressions),

src/grammar.json

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,21 +2688,8 @@
26882688
"type": "FIELD",
26892689
"name": "function",
26902690
"content": {
2691-
"type": "CHOICE",
2692-
"members": [
2693-
{
2694-
"type": "SYMBOL",
2695-
"name": "_expression"
2696-
},
2697-
{
2698-
"type": "SYMBOL",
2699-
"name": "super"
2700-
},
2701-
{
2702-
"type": "SYMBOL",
2703-
"name": "function"
2704-
}
2705-
]
2691+
"type": "SYMBOL",
2692+
"name": "_expression"
27062693
}
27072694
},
27082695
{
@@ -2781,6 +2768,10 @@
27812768
"type": "SYMBOL",
27822769
"name": "this"
27832770
},
2771+
{
2772+
"type": "SYMBOL",
2773+
"name": "super"
2774+
},
27842775
{
27852776
"type": "SYMBOL",
27862777
"name": "identifier"
@@ -2899,30 +2890,8 @@
28992890
"type": "FIELD",
29002891
"name": "object",
29012892
"content": {
2902-
"type": "CHOICE",
2903-
"members": [
2904-
{
2905-
"type": "SYMBOL",
2906-
"name": "_expression"
2907-
},
2908-
{
2909-
"type": "SYMBOL",
2910-
"name": "identifier"
2911-
},
2912-
{
2913-
"type": "SYMBOL",
2914-
"name": "super"
2915-
},
2916-
{
2917-
"type": "ALIAS",
2918-
"content": {
2919-
"type": "SYMBOL",
2920-
"name": "_reserved_identifier"
2921-
},
2922-
"named": true,
2923-
"value": "identifier"
2924-
}
2925-
]
2893+
"type": "SYMBOL",
2894+
"name": "_expression"
29262895
}
29272896
},
29282897
{
@@ -2964,30 +2933,8 @@
29642933
"type": "FIELD",
29652934
"name": "object",
29662935
"content": {
2967-
"type": "CHOICE",
2968-
"members": [
2969-
{
2970-
"type": "SYMBOL",
2971-
"name": "_expression"
2972-
},
2973-
{
2974-
"type": "SYMBOL",
2975-
"name": "identifier"
2976-
},
2977-
{
2978-
"type": "SYMBOL",
2979-
"name": "super"
2980-
},
2981-
{
2982-
"type": "ALIAS",
2983-
"content": {
2984-
"type": "SYMBOL",
2985-
"name": "_reserved_identifier"
2986-
},
2987-
"named": true,
2988-
"value": "identifier"
2989-
}
2990-
]
2936+
"type": "SYMBOL",
2937+
"name": "_expression"
29912938
}
29922939
},
29932940
{

src/node-types.json

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@
147147
"type": "subscript_expression",
148148
"named": true
149149
},
150+
{
151+
"type": "super",
152+
"named": true
153+
},
150154
{
151155
"type": "template_string",
152156
"named": true
@@ -662,10 +666,6 @@
662666
{
663667
"type": "_expression",
664668
"named": true
665-
},
666-
{
667-
"type": "super",
668-
"named": true
669669
}
670670
]
671671
}
@@ -1763,10 +1763,6 @@
17631763
{
17641764
"type": "_expression",
17651765
"named": true
1766-
},
1767-
{
1768-
"type": "super",
1769-
"named": true
17701766
}
17711767
]
17721768
},
@@ -1984,6 +1980,10 @@
19841980
"type": "subscript_expression",
19851981
"named": true
19861982
},
1983+
{
1984+
"type": "super",
1985+
"named": true
1986+
},
19871987
{
19881988
"type": "template_string",
19891989
"named": true
@@ -2344,10 +2344,6 @@
23442344
{
23452345
"type": "_expression",
23462346
"named": true
2347-
},
2348-
{
2349-
"type": "super",
2350-
"named": true
23512347
}
23522348
]
23532349
}

0 commit comments

Comments
 (0)