Add some infix math operators to javascript grammar

This commit is contained in:
Max Brunsfeld 2014-04-23 22:25:48 -07:00
parent 7be8d469b8
commit 52c338ed60
3 changed files with 9008 additions and 7087 deletions

View file

@ -21,6 +21,13 @@ namespace tree_sitter_examples {
seq({ element, repeat(seq({ str(","), element })) }),
blank() });
}
static rule_ptr infix(int precedence, std::string op) {
return prec(precedence, seq({
sym("expression"),
str(op),
sym("expression") }));
}
extern const Grammar javascript({
{ "program", repeat(sym("statement")) },
@ -92,7 +99,14 @@ namespace tree_sitter_examples {
sym("assignment"),
sym("ternary"),
sym("literal"),
sym("math_op"),
sym("literal"),
sym("identifier") }) },
{ "math_op", choice({
infix(2, "*"),
infix(2, "/"),
infix(1, "+"),
infix(1, "-") }) },
{ "ternary", seq({
sym("expression"),
str("?"),

File diff suppressed because it is too large Load diff

View file

@ -7,3 +7,15 @@ print(isDone() ? stuff : otherStuff);
(function_call
(identifier)
(ternary (function_call (identifier)) (identifier) (identifier)))))
==========================================
parses mathematical operators
==========================================
a + b * c - d / e
---
(program (expression_statement
(math_op
(identifier)
(math_op
(math_op (identifier) (identifier))
(math_op (identifier) (identifier))))))