Now, grammars can handle whitespace by making it another ubiquitous token, like comments. For now, this has the side effect of whitespace being included in the tree that precedes it. This was already an issue for other ubiquitous tokens though, so it needs to be fixed anyway.
32 lines
919 B
C++
32 lines
919 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_]*") },
|
|
{ "_whitespace", pattern("[\\s\n]+") },
|
|
}).ubiquitous_tokens({ "_whitespace" });
|
|
|
|
} // namespace tree_sitter_examples
|