Add try and while statements to js grammar

This commit is contained in:
Max Brunsfeld 2014-05-09 21:36:18 -07:00
parent e8760eee34
commit 2d0f90c7d5
2 changed files with 37 additions and 0 deletions

View file

@ -12,7 +12,9 @@ namespace tree_sitter_examples {
{ "statement", choice({
sym("statement_block"),
sym("if_statement"),
sym("try_statement"),
sym("switch_statement"),
sym("while_statement"),
sym("for_statement"),
sym("break_statement"),
sym("var_declaration"),
@ -36,6 +38,16 @@ namespace tree_sitter_examples {
optional(prec(1, seq({
keyword("else"),
sym("statement") }))) }) },
{ "while_statement", seq({
keyword("while"),
in_parens(err(sym("expression"))),
sym("statement") }) },
{ "try_statement", seq({
keyword("try"),
sym("statement"),
keyword("catch"),
in_parens(err(sym("identifier"))),
sym("statement") }) },
{ "switch_statement", seq({
keyword("switch"),
in_parens(err(sym("expression"))),

View file

@ -54,6 +54,31 @@ for (var i = 1; someCondition(i); i = next()) {
(assignment (identifier) (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier))))))
==========================================
parses while loops
==========================================
while (someCondition(i)) {
doSomething();
}
---
(program (while_statement
(function_call (identifier) (identifier))
(statement_block (expression_statement (function_call (identifier))))))
==========================================
parses try/catch statements
==========================================
try {
doSomething();
} catch (e) {
logError(e);
}
---
(program (try_statement
(statement_block (expression_statement (function_call (identifier))))
(identifier)
(statement_block (expression_statement (function_call (identifier) (identifier))))))
===========================================
parses switch statements
===========================================