Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit b26e42a

Browse files
committed
grammar: give top-level expressions high prio, clean up module rules, and inline terms
1 parent 97e4f3d commit b26e42a

32 files changed

+13575
-14107
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ fmt:
99
test: gen
1010
$(TREE_SITTER) test
1111

12+
.PHONY: debug
13+
debug: gen
14+
$(TREE_SITTER) test -d
15+
1216
.PHONY: gen
1317
gen:
1418
$(TREE_SITTER) generate

grammar.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const OP2_RIGHT_ASSOC = ["=!", "++", "--"];
6969
//
7070
///////////////////////////////////////////////////////////////////////////////
7171
const PREC = {
72+
TOP_LEVEL_EXPRESSION: 100,
7273
UNARY_OP: 10,
7374
BINARY_OP: 9,
7475
MODULE_DECLARATION: 8,
@@ -111,21 +112,17 @@ module.exports = grammar({
111112

112113
extras: ($) => [/[\x00-\x20\x80-\xA0]/, $.comment],
113114

115+
inline: ($) => [$.term],
116+
117+
conflicts: ($) => [],
118+
114119
rules: {
115120
source_file: ($) => repeat($._structure_item),
116121

117122
_structure_item: ($) =>
118-
prec(
119-
PREC.MODULE_DECLARATION,
120-
choice(
121-
$.type_declaration,
122-
$.function_spec,
123-
$.function_declaration,
124-
$.module_attribute,
125-
$.module_name,
126-
$.module_export,
127-
$.expression
128-
)
123+
choice(
124+
prec(PREC.TOP_LEVEL_EXPRESSION, $.expression),
125+
prec(PREC.MODULE_DECLARATION, $._module)
129126
),
130127

131128
////////////////////////////////////////////////////////////////////////////
@@ -134,6 +131,16 @@ module.exports = grammar({
134131
//
135132
////////////////////////////////////////////////////////////////////////////
136133

134+
_module: ($) =>
135+
choice(
136+
$.type_declaration,
137+
$.function_spec,
138+
$.function_declaration,
139+
$.module_attribute,
140+
$.module_name,
141+
$.module_export
142+
),
143+
137144
module_attribute: ($) =>
138145
seq(DASH, $.atom, delim(PARENS_LEFT, $.expression, PARENS_RIGHT), DOT),
139146

src/grammar.json

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,54 @@
1010
}
1111
},
1212
"_structure_item": {
13-
"type": "PREC",
14-
"value": 8,
15-
"content": {
16-
"type": "CHOICE",
17-
"members": [
18-
{
19-
"type": "SYMBOL",
20-
"name": "type_declaration"
21-
},
22-
{
23-
"type": "SYMBOL",
24-
"name": "function_spec"
25-
},
26-
{
27-
"type": "SYMBOL",
28-
"name": "function_declaration"
29-
},
30-
{
31-
"type": "SYMBOL",
32-
"name": "module_attribute"
33-
},
34-
{
35-
"type": "SYMBOL",
36-
"name": "module_name"
37-
},
38-
{
39-
"type": "SYMBOL",
40-
"name": "module_export"
41-
},
42-
{
13+
"type": "CHOICE",
14+
"members": [
15+
{
16+
"type": "PREC",
17+
"value": 100,
18+
"content": {
4319
"type": "SYMBOL",
4420
"name": "expression"
4521
}
46-
]
47-
}
22+
},
23+
{
24+
"type": "PREC",
25+
"value": 8,
26+
"content": {
27+
"type": "SYMBOL",
28+
"name": "_module"
29+
}
30+
}
31+
]
32+
},
33+
"_module": {
34+
"type": "CHOICE",
35+
"members": [
36+
{
37+
"type": "SYMBOL",
38+
"name": "type_declaration"
39+
},
40+
{
41+
"type": "SYMBOL",
42+
"name": "function_spec"
43+
},
44+
{
45+
"type": "SYMBOL",
46+
"name": "function_declaration"
47+
},
48+
{
49+
"type": "SYMBOL",
50+
"name": "module_attribute"
51+
},
52+
{
53+
"type": "SYMBOL",
54+
"name": "module_name"
55+
},
56+
{
57+
"type": "SYMBOL",
58+
"name": "module_export"
59+
}
60+
]
4861
},
4962
"module_attribute": {
5063
"type": "SEQ",
@@ -3907,7 +3920,9 @@
39073920
],
39083921
"conflicts": [],
39093922
"externals": [],
3910-
"inline": [],
3923+
"inline": [
3924+
"term"
3925+
],
39113926
"supertypes": []
39123927
}
39133928

0 commit comments

Comments
 (0)