Now, you can't a particular occurrence of a symbol in a grammar. You can only hide a symbol globally (right now, by beginning its name with an underscore).
46 lines
1.4 KiB
C++
46 lines
1.4 KiB
C++
#include "tree_sitter/compiler.h"
|
|
|
|
namespace tree_sitter {
|
|
namespace examples {
|
|
using namespace tree_sitter::rules;
|
|
|
|
static rule_ptr comma_sep(const rule_ptr &rule) {
|
|
return choice({
|
|
seq({
|
|
rule,
|
|
repeat(seq({ str(","), rule })),
|
|
}),
|
|
blank(),
|
|
});
|
|
}
|
|
|
|
Grammar json() {
|
|
return Grammar({
|
|
{ "value", choice({
|
|
sym("object"),
|
|
sym("array"),
|
|
sym("string"),
|
|
sym("number"),
|
|
sym("true"),
|
|
sym("false"),
|
|
sym("null"), }) },
|
|
{ "object", seq({
|
|
str("{"),
|
|
comma_sep(err(seq({
|
|
sym("string"),
|
|
str(":"),
|
|
sym("value") }))),
|
|
str("}"), }) },
|
|
{ "array", seq({
|
|
str("["),
|
|
comma_sep(err(sym("value"))),
|
|
str("]"), }) },
|
|
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
|
{ "number", pattern("\\d+(.\\d+)?") },
|
|
{ "null", str("null") },
|
|
{ "true", str("true") },
|
|
{ "false", str("false") },
|
|
});
|
|
}
|
|
}
|
|
}
|