Skip to content

Commit 9d32fe0

Browse files
committed
Improve grammar and fix parser bugs
Ports many of the fixes previously introduced in apple#8 1. Add support for else body in when generator 2. Remove legacy syntax in for and when generators 3. Add support for shebang comments 4. Add field names to various nodes throughout the grammar 5. Add missing "module" type
1 parent d6b3087 commit 9d32fe0

File tree

9 files changed

+35395
-43748
lines changed

9 files changed

+35395
-43748
lines changed

grammar.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = grammar({
8181
extras: $ => [
8282
$.lineComment,
8383
$.blockComment,
84+
$.shebangComment,
8485
/[ \t\f\r\n;]/
8586
],
8687

@@ -99,11 +100,17 @@ module.exports = grammar({
99100

100101
rules: {
101102
module: $ => seq(
103+
optional($.shebangComment),
102104
optional($.moduleHeader),
103105
repeat(choice($.importClause, $.importGlobClause)),
104106
repeat($._moduleMember)
105107
),
106108

109+
shebangComment: $ => seq(
110+
"#!",
111+
/.*/
112+
),
113+
107114
moduleHeader: $ => seq(
108115
optional($.docComment),
109116
repeat($.annotation),
@@ -251,10 +258,10 @@ module.exports = grammar({
251258

252259
objectEntry: $ => seq(
253260
"[",
254-
$._expr,
261+
field("key", $._expr),
255262
"]",
256263
choice(
257-
seq("=", $._expr),
264+
seq("=", field("valueExpr", $._expr)),
258265
repeat1($.objectBody)
259266
)
260267
),
@@ -263,10 +270,10 @@ module.exports = grammar({
263270

264271
objectPredicate: $ => seq(
265272
"[[",
266-
$._expr,
273+
field("conditionExpr", $._expr),
267274
"]]",
268275
choice(
269-
seq("=", $._expr),
276+
seq("=", field("valueExpr", $._expr)),
270277
repeat1($.objectBody)
271278
)
272279
),
@@ -282,20 +289,20 @@ module.exports = grammar({
282289
"in",
283290
$._expr,
284291
")",
285-
choice(
286-
$.objectBody,
287-
$._objectMember // deprecated in 0.15
288-
)
292+
$.objectBody
289293
),
290294

291295
whenGenerator: $ => seq(
292296
"when",
293297
"(",
294-
$._expr,
298+
field("conditionExpr", $._expr),
295299
")",
296-
choice(
297-
$.objectBody,
298-
$._objectMember // deprecated in 0.15
300+
field("thenBody", $.objectBody),
301+
optional(
302+
seq(
303+
"else",
304+
field("elseBody", $.objectBody)
305+
)
299306
)
300307
),
301308

@@ -317,6 +324,7 @@ module.exports = grammar({
317324
type: $ => choice(
318325
"unknown",
319326
"nothing",
327+
"module",
320328
$.stringConstant,
321329
seq($.qualifiedIdentifier, optional($.typeArgumentList)),
322330
seq("(", $.type, ")"),
@@ -697,11 +705,20 @@ module.exports = grammar({
697705

698706
objectLiteral: $ => prec(PREC.OBJ_LITERAL, seq($._expr2, $.objectBody)),
699707

700-
methodCallExpr: $ => seq(optional(seq(choice("super", $._expr), choice(".", "?."))), $.identifier, $.argumentList),
708+
methodCallExpr: $ => seq(
709+
optional(
710+
seq(
711+
field("receiver", choice("super", $._expr)),
712+
choice(".", "?.")
713+
)
714+
),
715+
$.identifier,
716+
$.argumentList
717+
),
701718

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

704-
subscriptExpr: $ => seq(choice("super", $._expr), alias($._open_subscript_bracket, "["), $._expr, "]"),
721+
subscriptExpr: $ => seq(field("receiver", choice("super", $._expr)), alias($._open_subscript_bracket, "["), $._expr, "]"),
705722

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
},
3333
"scripts": {
3434
"build": "tree-sitter generate && node-gyp configure && node-gyp build",
35+
"generate": "tree-sitter generate --no-bindings",
3536
"test": "tree-sitter test",
36-
"getVersion": "node -p \"require('./package.json').version\"",
37-
"gen-lib": "tree-sitter generate -b --libdir ."
37+
"getVersion": "node -p \"require('./package.json').version\""
3838
},
3939
"tree-sitter": [
4040
{

queries/highlights.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
(lineComment) @comment
5656
(blockComment) @comment
5757
(docComment) @comment
58+
(shebangComment) @comment
5859

5960
; Operators
6061

src/grammar.json

Lines changed: 119 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
"module": {
66
"type": "SEQ",
77
"members": [
8+
{
9+
"type": "CHOICE",
10+
"members": [
11+
{
12+
"type": "SYMBOL",
13+
"name": "shebangComment"
14+
},
15+
{
16+
"type": "BLANK"
17+
}
18+
]
19+
},
820
{
921
"type": "CHOICE",
1022
"members": [
@@ -42,6 +54,19 @@
4254
}
4355
]
4456
},
57+
"shebangComment": {
58+
"type": "SEQ",
59+
"members": [
60+
{
61+
"type": "STRING",
62+
"value": "#!"
63+
},
64+
{
65+
"type": "PATTERN",
66+
"value": ".*"
67+
}
68+
]
69+
},
4570
"moduleHeader": {
4671
"type": "SEQ",
4772
"members": [
@@ -774,8 +799,12 @@
774799
"value": "["
775800
},
776801
{
777-
"type": "SYMBOL",
778-
"name": "_expr"
802+
"type": "FIELD",
803+
"name": "key",
804+
"content": {
805+
"type": "SYMBOL",
806+
"name": "_expr"
807+
}
779808
},
780809
{
781810
"type": "STRING",
@@ -792,8 +821,12 @@
792821
"value": "="
793822
},
794823
{
795-
"type": "SYMBOL",
796-
"name": "_expr"
824+
"type": "FIELD",
825+
"name": "valueExpr",
826+
"content": {
827+
"type": "SYMBOL",
828+
"name": "_expr"
829+
}
797830
}
798831
]
799832
},
@@ -829,8 +862,12 @@
829862
"value": "[["
830863
},
831864
{
832-
"type": "SYMBOL",
833-
"name": "_expr"
865+
"type": "FIELD",
866+
"name": "conditionExpr",
867+
"content": {
868+
"type": "SYMBOL",
869+
"name": "_expr"
870+
}
834871
},
835872
{
836873
"type": "STRING",
@@ -847,8 +884,12 @@
847884
"value": "="
848885
},
849886
{
850-
"type": "SYMBOL",
851-
"name": "_expr"
887+
"type": "FIELD",
888+
"name": "valueExpr",
889+
"content": {
890+
"type": "SYMBOL",
891+
"name": "_expr"
892+
}
852893
}
853894
]
854895
},
@@ -912,17 +953,8 @@
912953
"value": ")"
913954
},
914955
{
915-
"type": "CHOICE",
916-
"members": [
917-
{
918-
"type": "SYMBOL",
919-
"name": "objectBody"
920-
},
921-
{
922-
"type": "SYMBOL",
923-
"name": "_objectMember"
924-
}
925-
]
956+
"type": "SYMBOL",
957+
"name": "objectBody"
926958
}
927959
]
928960
},
@@ -938,23 +970,47 @@
938970
"value": "("
939971
},
940972
{
941-
"type": "SYMBOL",
942-
"name": "_expr"
973+
"type": "FIELD",
974+
"name": "conditionExpr",
975+
"content": {
976+
"type": "SYMBOL",
977+
"name": "_expr"
978+
}
943979
},
944980
{
945981
"type": "STRING",
946982
"value": ")"
947983
},
984+
{
985+
"type": "FIELD",
986+
"name": "thenBody",
987+
"content": {
988+
"type": "SYMBOL",
989+
"name": "objectBody"
990+
}
991+
},
948992
{
949993
"type": "CHOICE",
950994
"members": [
951995
{
952-
"type": "SYMBOL",
953-
"name": "objectBody"
996+
"type": "SEQ",
997+
"members": [
998+
{
999+
"type": "STRING",
1000+
"value": "else"
1001+
},
1002+
{
1003+
"type": "FIELD",
1004+
"name": "elseBody",
1005+
"content": {
1006+
"type": "SYMBOL",
1007+
"name": "objectBody"
1008+
}
1009+
}
1010+
]
9541011
},
9551012
{
956-
"type": "SYMBOL",
957-
"name": "_objectMember"
1013+
"type": "BLANK"
9581014
}
9591015
]
9601016
}
@@ -1040,6 +1096,10 @@
10401096
"type": "STRING",
10411097
"value": "nothing"
10421098
},
1099+
{
1100+
"type": "STRING",
1101+
"value": "module"
1102+
},
10431103
{
10441104
"type": "SYMBOL",
10451105
"name": "stringConstant"
@@ -3092,17 +3152,21 @@
30923152
"type": "SEQ",
30933153
"members": [
30943154
{
3095-
"type": "CHOICE",
3096-
"members": [
3097-
{
3098-
"type": "STRING",
3099-
"value": "super"
3100-
},
3101-
{
3102-
"type": "SYMBOL",
3103-
"name": "_expr"
3104-
}
3105-
]
3155+
"type": "FIELD",
3156+
"name": "receiver",
3157+
"content": {
3158+
"type": "CHOICE",
3159+
"members": [
3160+
{
3161+
"type": "STRING",
3162+
"value": "super"
3163+
},
3164+
{
3165+
"type": "SYMBOL",
3166+
"name": "_expr"
3167+
}
3168+
]
3169+
}
31063170
},
31073171
{
31083172
"type": "CHOICE",
@@ -3173,17 +3237,21 @@
31733237
"type": "SEQ",
31743238
"members": [
31753239
{
3176-
"type": "CHOICE",
3177-
"members": [
3178-
{
3179-
"type": "STRING",
3180-
"value": "super"
3181-
},
3182-
{
3183-
"type": "SYMBOL",
3184-
"name": "_expr"
3185-
}
3186-
]
3240+
"type": "FIELD",
3241+
"name": "receiver",
3242+
"content": {
3243+
"type": "CHOICE",
3244+
"members": [
3245+
{
3246+
"type": "STRING",
3247+
"value": "super"
3248+
},
3249+
{
3250+
"type": "SYMBOL",
3251+
"name": "_expr"
3252+
}
3253+
]
3254+
}
31873255
},
31883256
{
31893257
"type": "ALIAS",
@@ -4070,6 +4138,10 @@
40704138
"type": "SYMBOL",
40714139
"name": "blockComment"
40724140
},
4141+
{
4142+
"type": "SYMBOL",
4143+
"name": "shebangComment"
4144+
},
40734145
{
40744146
"type": "PATTERN",
40754147
"value": "[ \\t\\f\\r\\n;]"

0 commit comments

Comments
 (0)