Skip to content

Commit

Permalink
Expose a number simple statement as nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Feb 10, 2023
1 parent 17ae9c2 commit 38afcb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
41 changes: 23 additions & 18 deletions corpus/test
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,31 @@ function foo(xs: vector of int): int
(type))))
(type))
(stmt_list
(id)
(expr
(id))
(id)
(expr
(id))
(expr
(constant
(boolean)))
(expr
(for
(id)
(expr
(id)))
(for
(id)
(expr
(id)))
(while
(expr
(constant
(integer))))
(case_list
(expr_list
(boolean))))
(switch
(expr
(expr
(constant
(integer))))
(stmt_list)
(stmt_list))
(expr
(constant
(integer))))))
(case_list
(expr_list
(expr
(constant
(integer))))
(stmt_list)
(stmt_list)))
(return
(expr
(constant
(integer)))))))
24 changes: 18 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ module.exports = grammar({
seq('{', optional($.stmt_list), '}'),
seq('print', $.expr_list, ';'),
seq('event', $.event_hdr, ';'),
prec_r(seq('if', '(', $.expr, ')', $._stmt, optional(seq('else', $._stmt)))),
seq('switch', $.expr, '{', optional($.case_list), '}'),
seq('for', '(', $.id, optional(seq(',', $.id)), 'in', $.expr, ')', $._stmt),
seq('for', '(', '[', list1($.id, ','), ']', optional(seq(',', $.id)), 'in', $.expr, ')', $._stmt),
seq('while', '(', $.expr, ')', $._stmt),
$.switch,
$.if,
$.for,
$.while,
$.return,
seq(choice('next', 'break', 'fallthrough'), ';'),
seq('return', optional($.expr), ';'),
seq(choice('add', 'delete'), $.expr, ';'),
// Precedence works around ambiguity with `var_decl` in `_decl` at `source_file` scope.
prec(-1, $.var_decl),
Expand All @@ -92,6 +91,19 @@ module.exports = grammar({
';',
),

if: $ => prec_r(seq('if', '(', $.expr, ')', $._stmt, optional(seq('else', $._stmt)))),

for: $ => choice(
seq('for', '(', $.id, optional(seq(',', $.id)), 'in', $.expr, ')', $._stmt),
seq('for', '(', '[', list1($.id, ','), ']', optional(seq(',', $.id)), 'in', $.expr, ')', $._stmt),
),

while: $ => seq('while', '(', $.expr, ')', $._stmt),

switch: $ => seq('switch', $.expr, '{', optional($.case_list), '}'),

return: $ => seq('return', optional($.expr), ';'),

stmt_list: $ => repeat1($._stmt),

case_list: $ => repeat1(
Expand Down

0 comments on commit 38afcb2

Please sign in to comment.