2014-03-24 07:19:31 -07:00
|
|
|
#include "tree_sitter/compiler.h"
|
2014-04-27 21:45:05 -07:00
|
|
|
#include "helpers.h"
|
2014-03-24 07:19:31 -07:00
|
|
|
|
2014-03-28 13:51:32 -07:00
|
|
|
namespace tree_sitter_examples {
|
2014-03-24 09:14:29 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
using tree_sitter::Grammar;
|
|
|
|
|
using namespace tree_sitter::rules;
|
2014-04-14 23:11:10 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
extern const Grammar arithmetic({
|
|
|
|
|
{ "expression", choice({
|
|
|
|
|
sym("sum"),
|
|
|
|
|
sym("difference"),
|
|
|
|
|
sym("product"),
|
|
|
|
|
sym("quotient"),
|
|
|
|
|
sym("exponent"),
|
|
|
|
|
sym("group"),
|
|
|
|
|
sym("number"),
|
|
|
|
|
sym("variable") }) },
|
2014-03-29 18:37:51 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
{ "sum", infix_op("+", "expression", 1) },
|
|
|
|
|
{ "difference", infix_op("-", "expression", 1) },
|
|
|
|
|
{ "product", infix_op("*", "expression", 2) },
|
|
|
|
|
{ "quotient", infix_op("/", "expression", 2) },
|
|
|
|
|
{ "exponent", infix_op("^", "expression", 3) },
|
|
|
|
|
{ "group", in_parens(err(sym("expression"))) },
|
|
|
|
|
|
|
|
|
|
{ "number", pattern("\\d+") },
|
|
|
|
|
{ "variable", pattern("\\a[\\w_]*") },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter_examples
|