Skip to content

Commit c8028a4

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 998a296 commit c8028a4

File tree

9 files changed

+45982
-53708
lines changed

9 files changed

+45982
-53708
lines changed

grammar.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module.exports = grammar({
7979
extras: $ => [
8080
$.lineComment,
8181
$.blockComment,
82+
$.shebangComment,
8283
/[ \t\f\r\n;]/
8384
],
8485

@@ -105,11 +106,17 @@ module.exports = grammar({
105106

106107
rules: {
107108
module: $ => seq(
109+
optional($.shebangComment),
108110
optional($.moduleHeader),
109111
repeat(choice($.importClause, $.importGlobClause)),
110112
repeat($._moduleMember)
111113
),
112114

115+
shebangComment: $ => seq(
116+
"#!",
117+
/.*/
118+
),
119+
113120
moduleHeader: $ => seq(
114121
optional($.docComment),
115122
repeat($.annotation),
@@ -257,10 +264,10 @@ module.exports = grammar({
257264

258265
objectEntry: $ => seq(
259266
alias($._open_entry_bracket, "["),
260-
$._expr,
267+
field("key", $._expr),
261268
"]",
262269
choice(
263-
seq("=", $._expr),
270+
seq("=", field("valueExpr", $._expr)),
264271
repeat1($.objectBody)
265272
)
266273
),
@@ -269,10 +276,10 @@ module.exports = grammar({
269276

270277
objectPredicate: $ => seq(
271278
"[[",
272-
$._expr,
279+
field("conditionExpr", $._expr),
273280
"]]",
274281
choice(
275-
seq("=", $._expr),
282+
seq("=", field("valueExpr", $._expr)),
276283
repeat1($.objectBody)
277284
)
278285
),
@@ -288,20 +295,20 @@ module.exports = grammar({
288295
"in",
289296
$._expr,
290297
")",
291-
choice(
292-
$.objectBody,
293-
$._objectMember // deprecated in 0.15
294-
)
298+
$.objectBody
295299
),
296300

297301
whenGenerator: $ => seq(
298302
"when",
299303
"(",
300-
$._expr,
304+
field("condition", $._expr),
301305
")",
302-
choice(
303-
$.objectBody,
304-
$._objectMember // deprecated in 0.15
306+
field("thenBody", $.objectBody),
307+
optional(
308+
seq(
309+
"else",
310+
field("elseBody", $.objectBody)
311+
)
305312
)
306313
),
307314

@@ -323,6 +330,7 @@ module.exports = grammar({
323330
type: $ => choice(
324331
"unknown",
325332
"nothing",
333+
"module",
326334
$.stringConstant,
327335
seq($.qualifiedIdentifier, optional($.typeArgumentList)),
328336
seq("(", $.type, ")"),
@@ -703,11 +711,20 @@ module.exports = grammar({
703711

704712
objectLiteral: $ => prec(PREC.OBJ_LITERAL, seq($._expr2, $.objectBody)),
705713

706-
methodCallExpr: $ => seq(optional(seq(choice("super", $._expr), choice(".", "?."))), $.identifier, $.argumentList),
714+
methodCallExpr: $ => seq(
715+
optional(
716+
seq(
717+
field("receiver", choice("super", $._expr)),
718+
choice(".", "?.")
719+
)
720+
),
721+
$.identifier,
722+
$.argumentList
723+
),
707724

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

710-
subscriptExpr: $ => seq(choice("super", $._expr), alias($._open_square_bracket, "["), $._expr, "]"),
727+
subscriptExpr: $ => seq(field("receiver", choice("super", $._expr)), alias($._open_square_bracket, "["), $._expr, "]"),
711728

712729
unaryExpr: $ => choice(
713730
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": [
@@ -779,8 +804,12 @@
779804
"value": "["
780805
},
781806
{
782-
"type": "SYMBOL",
783-
"name": "_expr"
807+
"type": "FIELD",
808+
"name": "key",
809+
"content": {
810+
"type": "SYMBOL",
811+
"name": "_expr"
812+
}
784813
},
785814
{
786815
"type": "STRING",
@@ -797,8 +826,12 @@
797826
"value": "="
798827
},
799828
{
800-
"type": "SYMBOL",
801-
"name": "_expr"
829+
"type": "FIELD",
830+
"name": "valueExpr",
831+
"content": {
832+
"type": "SYMBOL",
833+
"name": "_expr"
834+
}
802835
}
803836
]
804837
},
@@ -834,8 +867,12 @@
834867
"value": "[["
835868
},
836869
{
837-
"type": "SYMBOL",
838-
"name": "_expr"
870+
"type": "FIELD",
871+
"name": "conditionExpr",
872+
"content": {
873+
"type": "SYMBOL",
874+
"name": "_expr"
875+
}
839876
},
840877
{
841878
"type": "STRING",
@@ -852,8 +889,12 @@
852889
"value": "="
853890
},
854891
{
855-
"type": "SYMBOL",
856-
"name": "_expr"
892+
"type": "FIELD",
893+
"name": "valueExpr",
894+
"content": {
895+
"type": "SYMBOL",
896+
"name": "_expr"
897+
}
857898
}
858899
]
859900
},
@@ -917,17 +958,8 @@
917958
"value": ")"
918959
},
919960
{
920-
"type": "CHOICE",
921-
"members": [
922-
{
923-
"type": "SYMBOL",
924-
"name": "objectBody"
925-
},
926-
{
927-
"type": "SYMBOL",
928-
"name": "_objectMember"
929-
}
930-
]
961+
"type": "SYMBOL",
962+
"name": "objectBody"
931963
}
932964
]
933965
},
@@ -943,23 +975,47 @@
943975
"value": "("
944976
},
945977
{
946-
"type": "SYMBOL",
947-
"name": "_expr"
978+
"type": "FIELD",
979+
"name": "condition",
980+
"content": {
981+
"type": "SYMBOL",
982+
"name": "_expr"
983+
}
948984
},
949985
{
950986
"type": "STRING",
951987
"value": ")"
952988
},
989+
{
990+
"type": "FIELD",
991+
"name": "thenBody",
992+
"content": {
993+
"type": "SYMBOL",
994+
"name": "objectBody"
995+
}
996+
},
953997
{
954998
"type": "CHOICE",
955999
"members": [
9561000
{
957-
"type": "SYMBOL",
958-
"name": "objectBody"
1001+
"type": "SEQ",
1002+
"members": [
1003+
{
1004+
"type": "STRING",
1005+
"value": "else"
1006+
},
1007+
{
1008+
"type": "FIELD",
1009+
"name": "elseBody",
1010+
"content": {
1011+
"type": "SYMBOL",
1012+
"name": "objectBody"
1013+
}
1014+
}
1015+
]
9591016
},
9601017
{
961-
"type": "SYMBOL",
962-
"name": "_objectMember"
1018+
"type": "BLANK"
9631019
}
9641020
]
9651021
}
@@ -1045,6 +1101,10 @@
10451101
"type": "STRING",
10461102
"value": "nothing"
10471103
},
1104+
{
1105+
"type": "STRING",
1106+
"value": "module"
1107+
},
10481108
{
10491109
"type": "SYMBOL",
10501110
"name": "stringConstant"
@@ -3087,17 +3147,21 @@
30873147
"type": "SEQ",
30883148
"members": [
30893149
{
3090-
"type": "CHOICE",
3091-
"members": [
3092-
{
3093-
"type": "STRING",
3094-
"value": "super"
3095-
},
3096-
{
3097-
"type": "SYMBOL",
3098-
"name": "_expr"
3099-
}
3100-
]
3150+
"type": "FIELD",
3151+
"name": "receiver",
3152+
"content": {
3153+
"type": "CHOICE",
3154+
"members": [
3155+
{
3156+
"type": "STRING",
3157+
"value": "super"
3158+
},
3159+
{
3160+
"type": "SYMBOL",
3161+
"name": "_expr"
3162+
}
3163+
]
3164+
}
31013165
},
31023166
{
31033167
"type": "CHOICE",
@@ -3168,17 +3232,21 @@
31683232
"type": "SEQ",
31693233
"members": [
31703234
{
3171-
"type": "CHOICE",
3172-
"members": [
3173-
{
3174-
"type": "STRING",
3175-
"value": "super"
3176-
},
3177-
{
3178-
"type": "SYMBOL",
3179-
"name": "_expr"
3180-
}
3181-
]
3235+
"type": "FIELD",
3236+
"name": "receiver",
3237+
"content": {
3238+
"type": "CHOICE",
3239+
"members": [
3240+
{
3241+
"type": "STRING",
3242+
"value": "super"
3243+
},
3244+
{
3245+
"type": "SYMBOL",
3246+
"name": "_expr"
3247+
}
3248+
]
3249+
}
31823250
},
31833251
{
31843252
"type": "ALIAS",
@@ -4065,6 +4133,10 @@
40654133
"type": "SYMBOL",
40664134
"name": "blockComment"
40674135
},
4136+
{
4137+
"type": "SYMBOL",
4138+
"name": "shebangComment"
4139+
},
40684140
{
40694141
"type": "PATTERN",
40704142
"value": "[ \\t\\f\\r\\n;]"

0 commit comments

Comments
 (0)