2014-03-24 07:19:31 -07:00
|
|
|
#include "tree_sitter/compiler.h"
|
|
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
namespace examples {
|
|
|
|
|
using namespace tree_sitter::rules;
|
2014-03-24 09:14:29 -07:00
|
|
|
|
2014-03-24 07:19:31 -07:00
|
|
|
static rule_ptr comma_sep(const rule_ptr &rule) {
|
|
|
|
|
return choice({
|
|
|
|
|
seq({
|
|
|
|
|
rule,
|
2014-03-25 08:16:26 -07:00
|
|
|
repeat(seq({ str(","), rule })),
|
2014-03-24 07:19:31 -07:00
|
|
|
}),
|
|
|
|
|
blank(),
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-03-24 09:14:29 -07:00
|
|
|
|
2014-03-24 07:19:31 -07:00
|
|
|
Grammar json() {
|
2014-03-24 13:05:04 -07:00
|
|
|
return Grammar({
|
2014-03-24 07:19:31 -07:00
|
|
|
{ "value", choice({
|
|
|
|
|
sym("object"),
|
|
|
|
|
sym("array"),
|
2014-03-24 09:14:29 -07:00
|
|
|
sym("string"),
|
2014-03-24 07:19:31 -07:00
|
|
|
sym("number"),
|
|
|
|
|
sym("true"),
|
|
|
|
|
sym("false"),
|
|
|
|
|
sym("null"), }) },
|
|
|
|
|
{ "object", seq({
|
2014-03-25 08:16:26 -07:00
|
|
|
str("{"),
|
2014-03-24 07:19:31 -07:00
|
|
|
comma_sep(err(seq({
|
|
|
|
|
sym("string"),
|
2014-03-25 08:16:26 -07:00
|
|
|
str(":"),
|
2014-03-24 07:19:31 -07:00
|
|
|
sym("value") }))),
|
2014-03-25 08:16:26 -07:00
|
|
|
str("}"), }) },
|
2014-03-24 07:19:31 -07:00
|
|
|
{ "array", seq({
|
2014-03-25 08:16:26 -07:00
|
|
|
str("["),
|
2014-03-24 07:19:31 -07:00
|
|
|
comma_sep(err(sym("value"))),
|
2014-03-25 08:16:26 -07:00
|
|
|
str("]"), }) },
|
2014-03-24 07:19:31 -07:00
|
|
|
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
|
|
|
|
{ "number", pattern("\\d+(.\\d+)?") },
|
|
|
|
|
{ "null", str("null") },
|
|
|
|
|
{ "true", str("true") },
|
|
|
|
|
{ "false", str("false") },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|