@@ -13,6 +13,7 @@ const PREC = {
1313 call : 15 ,
1414 as : 11 ,
1515 assign : 0 ,
16+ semi : - 1 ,
1617}
1718
1819module . exports = grammar ( {
@@ -42,40 +43,49 @@ module.exports = grammar({
4243 word : $ => $ . ident ,
4344
4445 rules : {
45- trans_unit : $ => repeat ( $ . _global_stmt ) ,
46+ trans_unit : $ => listSepBy ( ';' , $ . _expr ) ,
4647
47- _global_stmt : $ => choice (
48+ _expr : $ => choice (
49+ $ . ident ,
50+ $ . semi_expr ,
51+ $ . assign_expr ,
52+ $ . call_expr ,
53+ $ . paren_expr ,
54+ $ . as_expr ,
55+ $ . break_expr ,
56+ $ . cont_expr ,
57+ $ . labeled ,
58+ $ . block ,
59+ $ . if_expr ,
60+ $ . while_expr ,
61+ $ . for_expr ,
4862 $ . asm_block ,
49- $ . _decl_stmt ,
50- $ . empty_stmt ,
63+ $ . _decl ,
5164 ) ,
5265
53- _decl_stmt : $ => choice (
66+ _decl : $ => choice (
5467 $ . fn_def ,
5568 $ . binding ,
5669 ) ,
5770
58- fn_def : $ => prec ( 1 , seq (
59- field ( 'name' , $ . ident ) ,
71+ fn_def : $ => prec . right ( seq (
72+ field ( 'name' , $ . _pattern ) ,
6073 ':' ,
6174 field ( 'sign' , $ . fn_sign ) ,
62- choice (
63- ';' ,
64- seq (
65- '=' ,
66- field ( 'body' , $ . _fn_body ) ,
67- )
68- ) ,
75+ optional ( seq (
76+ '=' ,
77+ field ( 'body' , $ . _expr ) ,
78+ ) ) ,
6979 ) ) ,
7080
71- fn_sign : $ => seq (
81+ fn_sign : $ => prec . right ( seq (
7282 'fn' ,
7383 optional ( field ( 'paras' , $ . paras ) ) ,
7484 optional ( seq (
7585 '->' ,
7686 field ( 'ret' , $ . _type ) ,
7787 ) ) ,
78- ) ,
88+ ) ) ,
7989
8090 paras : $ => seq (
8191 '(' ,
@@ -91,50 +101,27 @@ module.exports = grammar({
91101 ) )
92102 ) ,
93103
94- _fn_body : $ => choice (
95- prec ( 1 , $ . block ) ,
96- $ . asm_block ,
97- $ . expr_stmt ,
98- ) ,
99-
100- binding : $ => choice (
101- $ . _typed_binding ,
102- $ . _auto_binding ,
103- ) ,
104-
105- _typed_binding : $ => seq (
106- field ( 'pat' , $ . _pattern ) ,
107- ':' ,
108- field ( 'ty' , $ . _type ) ,
109- optional ( seq (
110- '=' ,
111- field ( 'val' , $ . _expr ) ,
112- ) ) ,
113- ';' ,
114- ) ,
115-
116- _auto_binding : $ => seq (
104+ binding : $ => prec . right ( seq (
117105 field ( 'pat' , $ . _pattern ) ,
118106 ':' ,
107+ field ( 'ty' , optional ( $ . _type ) ) ,
119108 '=' ,
120- field ( 'val' , $ . _expr ) ,
121- ';' ,
122- ) ,
109+ field ( 'val' , choice (
110+ $ . _expr ,
111+ $ . underscore ,
112+ ) ) ,
113+ ) ) ,
123114
124115 _type : $ => choice (
125116 $ . ident ,
126- $ . type_infer ,
117+ $ . underscore ,
127118 ) ,
128119
129- type_infer : $ => $ . underscore ,
130-
131120 _pattern : $ => choice (
132- $ . pat_wild ,
121+ $ . underscore ,
133122 $ . pat_ident ,
134123 ) ,
135124
136- pat_wild : $ => $ . underscore ,
137-
138125 pat_ident : $ => seq (
139126 optional ( 'mut' ) ,
140127 field ( 'id' , $ . ident ) ,
@@ -153,20 +140,13 @@ module.exports = grammar({
153140 '}' ,
154141 ) ,
155142
156- expr_stmt : $ => seq ( $ . _expr , ';' ) ,
157-
158- _expr : $ => prec ( 1 , choice (
159- $ . ident ,
160- $ . assign_expr ,
161- $ . call_expr ,
162- $ . paren_expr ,
163- $ . as_expr ,
164- $ . break_expr ,
165- $ . cont_expr ,
166- $ . _expr_ending_with_block ,
143+ semi_expr : $ => prec . left ( PREC . semi , seq (
144+ field ( 'lhs' , $ . _expr ) ,
145+ ';' ,
146+ field ( 'rhs' , optional ( $ . _expr ) ) ,
167147 ) ) ,
168148
169- assign_expr : $ => prec . left ( PREC . assign , seq (
149+ assign_expr : $ => prec . right ( PREC . assign , seq (
170150 field ( 'lhs' , $ . _expr ) ,
171151 '=' ,
172152 field ( 'rhs' , $ . _expr ) ,
@@ -190,29 +170,21 @@ module.exports = grammar({
190170 ')' ,
191171 ) ,
192172
193- as_expr : $ => prec . left ( PREC . as , seq (
173+ as_expr : $ => prec ( PREC . as , seq (
194174 field ( 'val' , $ . _expr ) ,
195175 'as' ,
196176 field ( 'ty' , $ . _type ) ,
197177 ) ) ,
198178
199- break_expr : $ => prec . left ( seq (
179+ break_expr : $ => prec . right ( seq (
200180 'break' ,
201181 field ( 'lab' , optional ( $ . label ) ) ,
202182 field ( 'val' , $ . _expr ) ,
203183 ) ) ,
204184
205- cont_expr : $ => prec . left ( seq (
185+ cont_expr : $ => seq (
206186 'cont' ,
207187 field ( 'lab' , optional ( $ . label ) ) ,
208- ) ) ,
209-
210- _expr_ending_with_block : $ => choice (
211- $ . labeled ,
212- $ . block ,
213- $ . if_expr ,
214- $ . while_expr ,
215- $ . for_expr ,
216188 ) ,
217189
218190 if_expr : $ => seq (
@@ -263,7 +235,6 @@ module.exports = grammar({
263235
264236 block : $ => seq (
265237 '{' ,
266- repeat ( $ . _stmt ) ,
267238 optional ( $ . _expr ) ,
268239 '}' ,
269240 ) ,
@@ -291,3 +262,28 @@ function sepBy1(sep, rule) {
291262function sepBy ( sep , rule ) {
292263 return optional ( sepBy1 ( sep , rule ) ) ;
293264}
265+
266+ /**
267+ * @param {RuleOrLiteral } sep
268+ * @param {RuleOrLiteral } rule
269+ */
270+ function listSepBy1 ( sep , rule ) {
271+ return seq (
272+ sepBy1 ( sep , rule ) ,
273+ optional ( sep ) ,
274+ ) ;
275+ }
276+
277+ /**
278+ * @param {RuleOrLiteral } sep
279+ * @param {RuleOrLiteral } rule
280+ */
281+ function listSepBy ( sep , rule ) {
282+ return seq (
283+ repeat ( seq (
284+ rule ,
285+ sep ,
286+ ) ) ,
287+ optional ( rule ) ,
288+ ) ;
289+ }
0 commit comments