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