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.
31 lines
874 B
C++
31 lines
874 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 json = Grammar({
|
|
{ "value", choice({
|
|
sym("object"),
|
|
sym("array"),
|
|
sym("string"),
|
|
sym("number"),
|
|
sym("true"),
|
|
sym("false"),
|
|
sym("null"), }) },
|
|
{ "object", in_braces(comma_sep(err(seq({
|
|
sym("string"),
|
|
str(":"),
|
|
sym("value") })))) },
|
|
{ "array", in_brackets(comma_sep(err(sym("value")))) },
|
|
{ "string", pattern("\"([^\"]|\\\\\")*\"") },
|
|
{ "number", pattern("\\d+(\\.\\d+)?") },
|
|
{ "null", keyword("null") },
|
|
{ "true", keyword("true") },
|
|
{ "false", keyword("false") },
|
|
{ "_whitespace", pattern("[\\s\n]+") },
|
|
}).ubiquitous_tokens({ "_whitespace" });
|
|
|
|
} // namespace tree_sitter_examples
|