Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 48f40c1

Browse files
committed
Implement if/unless/elsif/else.
"if modifier", (return foo if bar) not yet implemented. Rules "inspired" from ruby grammar.
1 parent 8c59563 commit 48f40c1

File tree

5 files changed

+26206
-21506
lines changed

5 files changed

+26206
-21506
lines changed

grammar.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const const_start = /[A-Z]/,
2020
SHIFT: 9,
2121
ADD: 10,
2222
MULTIPLY: 11,
23+
CONDITIONAL: 99,
2324
};
2425

2526
module.exports = grammar({
@@ -84,6 +85,8 @@ module.exports = grammar({
8485
$.extend,
8586
$.abstract_def,
8687
$.def,
88+
prec(PREC.CONDITIONAL, $.if),
89+
prec(PREC.CONDITIONAL, $.unless),
8790
$.return,
8891
$._expression,
8992
),
@@ -178,6 +181,46 @@ module.exports = grammar({
178181
'end',
179182
),
180183

184+
if: $ => seq(
185+
'if',
186+
field('condition', $._statement),
187+
choice($._terminator, field('consequence', $.then)),
188+
field('alternative', optional(choice($.else, $.elsif))),
189+
'end',
190+
),
191+
192+
unless: $ => seq(
193+
'unless',
194+
field('condition', $._statement),
195+
choice($._terminator, field('consequence', $.then)),
196+
field('alternative', optional(choice($.else, $.elsif))),
197+
'end',
198+
),
199+
200+
elsif: $ => seq(
201+
'elsif',
202+
field('condition', $._statement),
203+
choice($._terminator, field('consequence', $.then)),
204+
field('alternative', optional(choice($.else, $.elsif))),
205+
),
206+
207+
else: $ => seq(
208+
'else',
209+
optional($._terminator),
210+
optional($._statements),
211+
),
212+
213+
then: $ => choice(
214+
seq(
215+
$._terminator,
216+
$._statements,
217+
),
218+
seq(
219+
optional($._terminator),
220+
'then',
221+
optional($._statements),
222+
),
223+
),
181224
return: $ => seq('return', optional($._expression)),
182225

183226
_expression: $ =>

src/grammar.json

Lines changed: 285 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)