Put example grammars in tree_sitter::examples ns
This commit is contained in:
parent
3a7c4bb5b1
commit
06cd18b483
7 changed files with 104 additions and 101 deletions
35
examples/grammars/arithmetic.cc
Normal file
35
examples/grammars/arithmetic.cc
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace examples {
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
Grammar arithmetic() {
|
||||
return Grammar("expression", {
|
||||
{ "expression", choice({
|
||||
seq({
|
||||
sym("term"),
|
||||
sym("plus"),
|
||||
sym("term") }),
|
||||
sym("term") }) },
|
||||
{ "term", choice({
|
||||
seq({
|
||||
sym("factor"),
|
||||
sym("times"),
|
||||
sym("factor") }),
|
||||
sym("factor") }) },
|
||||
{ "factor", choice({
|
||||
sym("variable"),
|
||||
sym("number"),
|
||||
seq({
|
||||
str("("),
|
||||
err(sym("expression")),
|
||||
str(")") }) }) },
|
||||
{ "plus", str("+") },
|
||||
{ "times", str("*") },
|
||||
{ "number", pattern("\\d+") },
|
||||
{ "variable", pattern("[a-zA-Z]+") },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
#ifndef tree_sitter__grammars__arithmetic_h
|
||||
#define tree_sitter__grammars__arithmetic_h
|
||||
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace test_grammars {
|
||||
using tree_sitter::Grammar;
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
Grammar arithmetic() {
|
||||
return Grammar("expression", {
|
||||
{ "expression", choice({
|
||||
seq({
|
||||
sym("term"),
|
||||
sym("plus"),
|
||||
sym("term") }),
|
||||
sym("term") }) },
|
||||
{ "term", choice({
|
||||
seq({
|
||||
sym("factor"),
|
||||
sym("times"),
|
||||
sym("factor") }),
|
||||
sym("factor") }) },
|
||||
{ "factor", choice({
|
||||
sym("variable"),
|
||||
sym("number"),
|
||||
seq({
|
||||
str("("),
|
||||
err(sym("expression")),
|
||||
str(")") }) }) },
|
||||
{ "plus", str("+") },
|
||||
{ "times", str("*") },
|
||||
{ "number", pattern("\\d+") },
|
||||
{ "variable", pattern("[a-zA-Z]+") },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
52
examples/grammars/json.cc
Normal file
52
examples/grammars/json.cc
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#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({ _sym("comma"), rule })),
|
||||
}),
|
||||
blank(),
|
||||
});
|
||||
}
|
||||
|
||||
Grammar json() {
|
||||
return Grammar("value", {
|
||||
{ "value", choice({
|
||||
sym("object"),
|
||||
sym("array"),
|
||||
sym("string"),
|
||||
sym("number"),
|
||||
sym("true"),
|
||||
sym("false"),
|
||||
sym("null"), }) },
|
||||
{ "object", seq({
|
||||
_sym("left_brace"),
|
||||
comma_sep(err(seq({
|
||||
sym("string"),
|
||||
_sym("colon"),
|
||||
sym("value") }))),
|
||||
_sym("right_brace"), }) },
|
||||
{ "array", seq({
|
||||
_sym("left_bracket"),
|
||||
comma_sep(err(sym("value"))),
|
||||
_sym("right_bracket"), }) },
|
||||
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
||||
{ "number", pattern("\\d+(.\\d+)?") },
|
||||
{ "comma", str(",") },
|
||||
{ "colon", str(":") },
|
||||
{ "left_bracket", str("[") },
|
||||
{ "right_bracket", str("]") },
|
||||
{ "left_brace", str("{") },
|
||||
{ "right_brace", str("}") },
|
||||
{ "null", str("null") },
|
||||
{ "true", str("true") },
|
||||
{ "false", str("false") },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
#ifndef tree_sitter__grammars__json_h
|
||||
#define tree_sitter__grammars__json_h
|
||||
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace test_grammars {
|
||||
using tree_sitter::Grammar;
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
static rule_ptr comma_sep(const rule_ptr &rule) {
|
||||
return choice({
|
||||
seq({
|
||||
rule,
|
||||
repeat(seq({ _sym("comma"), rule })),
|
||||
}),
|
||||
blank(),
|
||||
});
|
||||
}
|
||||
|
||||
Grammar json() {
|
||||
return Grammar("value", {
|
||||
{ "value", choice({
|
||||
sym("object"),
|
||||
sym("array"),
|
||||
sym("string"),
|
||||
sym("number"),
|
||||
sym("true"),
|
||||
sym("false"),
|
||||
sym("null"), }) },
|
||||
{ "object", seq({
|
||||
_sym("left_brace"),
|
||||
comma_sep(err(seq({
|
||||
sym("string"),
|
||||
_sym("colon"),
|
||||
sym("value") }))),
|
||||
_sym("right_brace"), }) },
|
||||
{ "array", seq({
|
||||
_sym("left_bracket"),
|
||||
comma_sep(err(sym("value"))),
|
||||
_sym("right_bracket"), }) },
|
||||
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
||||
{ "number", pattern("\\d+(.\\d+)?") },
|
||||
{ "comma", str(",") },
|
||||
{ "colon", str(":") },
|
||||
{ "left_bracket", str("[") },
|
||||
{ "right_bracket", str("]") },
|
||||
{ "left_brace", str("{") },
|
||||
{ "right_brace", str("}") },
|
||||
{ "null", str("null") },
|
||||
{ "true", str("true") },
|
||||
{ "false", str("false") },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
#include "compiler_spec_helper.h"
|
||||
#include "grammars/json.hpp"
|
||||
#include "grammars/arithmetic.hpp"
|
||||
#include "helpers/example_grammars.h"
|
||||
#include <fstream>
|
||||
|
||||
static string src_dir() {
|
||||
|
|
@ -15,12 +14,12 @@ describe("compiling the example grammars", []() {
|
|||
string example_parser_dir = src_dir() + "/examples/parsers/";
|
||||
|
||||
it("compiles the arithmetic grammar", [&]() {
|
||||
Grammar grammar = test_grammars::arithmetic();
|
||||
Grammar grammar = examples::arithmetic();
|
||||
ofstream(example_parser_dir + "arithmetic.c") << compile(grammar, "arithmetic");
|
||||
});
|
||||
|
||||
it("compiles the json grammar", [&]() {
|
||||
Grammar grammar = test_grammars::json();
|
||||
Grammar grammar = examples::json();
|
||||
ofstream(example_parser_dir + "json.c") << compile(grammar, "json");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
13
spec/compiler/helpers/example_grammars.h
Normal file
13
spec/compiler/helpers/example_grammars.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef tree_sitter_example_grammars_h
|
||||
#define tree_sitter_example_grammars_h
|
||||
|
||||
#include "tree_sitter/compiler.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace examples {
|
||||
Grammar arithmetic();
|
||||
Grammar json();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -49,8 +49,7 @@
|
|||
'spec/compiler',
|
||||
],
|
||||
'sources': [
|
||||
'<!@(find spec/compiler -name "*.h" -or -name "*.cc")',
|
||||
'<!@(find examples/grammars -name "*.hpp")',
|
||||
'<!@(find spec/compiler examples/grammars -name "*.h" -or -name "*.cc")',
|
||||
],
|
||||
},
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue