- I want to move away from having complete grammars for real languages (e.g. javascript, golang) in this repo. These languages take a long time to compile, and they now exist in their own repos (node-tree-sitter-javascript etc). - I want to start testing more compiler edge cases through integration tests, so I want to put more small, weird grammars in here. That makes me not want to call the directory `examples`.
36 lines
937 B
C++
36 lines
937 B
C++
#include "tree_sitter/compiler.h"
|
|
#include "helpers.h"
|
|
|
|
namespace tree_sitter_examples {
|
|
|
|
using tree_sitter::Grammar;
|
|
using namespace tree_sitter::rules;
|
|
|
|
extern const Grammar arithmetic = Grammar({
|
|
{ "expression", choice({
|
|
sym("sum"),
|
|
sym("difference"),
|
|
sym("product"),
|
|
sym("quotient"),
|
|
sym("exponent"),
|
|
sym("group"),
|
|
sym("number"),
|
|
sym("variable") }) },
|
|
|
|
{ "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_]*") },
|
|
|
|
{ "comment", pattern("#.*") },
|
|
}).ubiquitous_tokens({
|
|
sym("comment"),
|
|
pattern("\\s"),
|
|
});
|
|
|
|
} // namespace tree_sitter_examples
|