Skip to content

Commit e74abfa

Browse files
authored
Merge pull request #125 from danzat/danzat/fix_class_literal_precedence
Danzat/fix class literal precedence
2 parents 81148c0 + 0a87133 commit e74abfa

File tree

4 files changed

+1375
-78
lines changed

4 files changed

+1375
-78
lines changed

grammar.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const PREC = {
2424
ARRAY: 16, // [Index]
2525
OBJ_ACCESS: 16, // .
2626
PARENS: 16, // (Expression)
27+
CLASS_LITERAL: 17, // .
2728
};
2829

2930
module.exports = grammar({
@@ -69,6 +70,7 @@ module.exports = grammar({
6970
// Only conflicts in switch expressions
7071
[$.lambda_expression, $.primary_expression],
7172
[$.inferred_parameters, $.primary_expression],
73+
[$.class_literal, $.field_access],
7274
],
7375

7476
word: $ => $.identifier,
@@ -309,7 +311,7 @@ module.exports = grammar({
309311

310312
parenthesized_expression: $ => seq('(', $.expression, ')'),
311313

312-
class_literal: $ => seq($._unannotated_type, '.', 'class'),
314+
class_literal: $ => prec.dynamic(PREC.CLASS_LITERAL, seq($._unannotated_type, '.', 'class')),
313315

314316
object_creation_expression: $ => choice(
315317
$._unqualified_object_creation_expression,
@@ -324,15 +326,15 @@ module.exports = grammar({
324326
optional($.class_body)
325327
)),
326328

327-
field_access: $ => seq(
329+
field_access: $ => prec.dynamic(PREC.OBJ_ACCESS, seq(
328330
field('object', choice($.primary_expression, $.super)),
329331
optional(seq(
330332
'.',
331333
$.super
332334
)),
333335
'.',
334336
field('field', choice($.identifier, $._reserved_identifier, $.this))
335-
),
337+
)),
336338

337339
array_access: $ => seq(
338340
field('array', $.primary_expression),

src/grammar.json

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,21 +2537,25 @@
25372537
]
25382538
},
25392539
"class_literal": {
2540-
"type": "SEQ",
2541-
"members": [
2542-
{
2543-
"type": "SYMBOL",
2544-
"name": "_unannotated_type"
2545-
},
2546-
{
2547-
"type": "STRING",
2548-
"value": "."
2549-
},
2550-
{
2551-
"type": "STRING",
2552-
"value": "class"
2553-
}
2554-
]
2540+
"type": "PREC_DYNAMIC",
2541+
"value": 17,
2542+
"content": {
2543+
"type": "SEQ",
2544+
"members": [
2545+
{
2546+
"type": "SYMBOL",
2547+
"name": "_unannotated_type"
2548+
},
2549+
{
2550+
"type": "STRING",
2551+
"value": "."
2552+
},
2553+
{
2554+
"type": "STRING",
2555+
"value": "class"
2556+
}
2557+
]
2558+
}
25552559
},
25562560
"object_creation_expression": {
25572561
"type": "CHOICE",
@@ -2637,72 +2641,76 @@
26372641
}
26382642
},
26392643
"field_access": {
2640-
"type": "SEQ",
2641-
"members": [
2642-
{
2643-
"type": "FIELD",
2644-
"name": "object",
2645-
"content": {
2646-
"type": "CHOICE",
2647-
"members": [
2648-
{
2649-
"type": "SYMBOL",
2650-
"name": "primary_expression"
2651-
},
2652-
{
2653-
"type": "SYMBOL",
2654-
"name": "super"
2655-
}
2656-
]
2657-
}
2658-
},
2659-
{
2660-
"type": "CHOICE",
2661-
"members": [
2662-
{
2663-
"type": "SEQ",
2644+
"type": "PREC_DYNAMIC",
2645+
"value": 16,
2646+
"content": {
2647+
"type": "SEQ",
2648+
"members": [
2649+
{
2650+
"type": "FIELD",
2651+
"name": "object",
2652+
"content": {
2653+
"type": "CHOICE",
26642654
"members": [
26652655
{
2666-
"type": "STRING",
2667-
"value": "."
2656+
"type": "SYMBOL",
2657+
"name": "primary_expression"
26682658
},
26692659
{
26702660
"type": "SYMBOL",
26712661
"name": "super"
26722662
}
26732663
]
2674-
},
2675-
{
2676-
"type": "BLANK"
26772664
}
2678-
]
2679-
},
2680-
{
2681-
"type": "STRING",
2682-
"value": "."
2683-
},
2684-
{
2685-
"type": "FIELD",
2686-
"name": "field",
2687-
"content": {
2665+
},
2666+
{
26882667
"type": "CHOICE",
26892668
"members": [
26902669
{
2691-
"type": "SYMBOL",
2692-
"name": "identifier"
2693-
},
2694-
{
2695-
"type": "SYMBOL",
2696-
"name": "_reserved_identifier"
2670+
"type": "SEQ",
2671+
"members": [
2672+
{
2673+
"type": "STRING",
2674+
"value": "."
2675+
},
2676+
{
2677+
"type": "SYMBOL",
2678+
"name": "super"
2679+
}
2680+
]
26972681
},
26982682
{
2699-
"type": "SYMBOL",
2700-
"name": "this"
2683+
"type": "BLANK"
27012684
}
27022685
]
2686+
},
2687+
{
2688+
"type": "STRING",
2689+
"value": "."
2690+
},
2691+
{
2692+
"type": "FIELD",
2693+
"name": "field",
2694+
"content": {
2695+
"type": "CHOICE",
2696+
"members": [
2697+
{
2698+
"type": "SYMBOL",
2699+
"name": "identifier"
2700+
},
2701+
{
2702+
"type": "SYMBOL",
2703+
"name": "_reserved_identifier"
2704+
},
2705+
{
2706+
"type": "SYMBOL",
2707+
"name": "this"
2708+
}
2709+
]
2710+
}
27032711
}
2704-
}
2705-
]
2712+
]
2713+
}
27062714
},
27072715
"array_access": {
27082716
"type": "SEQ",
@@ -7099,6 +7107,10 @@
70997107
[
71007108
"inferred_parameters",
71017109
"primary_expression"
7110+
],
7111+
[
7112+
"class_literal",
7113+
"field_access"
71027114
]
71037115
],
71047116
"precedences": [],

0 commit comments

Comments
 (0)