diff --git a/examples/grammars/arithmetic.cc b/examples/grammars/arithmetic.cc new file mode 100644 index 00000000..27438cd3 --- /dev/null +++ b/examples/grammars/arithmetic.cc @@ -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]+") }, + }); + } + } +} diff --git a/examples/grammars/arithmetic.hpp b/examples/grammars/arithmetic.hpp deleted file mode 100644 index ec2c5c80..00000000 --- a/examples/grammars/arithmetic.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/examples/grammars/json.cc b/examples/grammars/json.cc new file mode 100644 index 00000000..6eabef85 --- /dev/null +++ b/examples/grammars/json.cc @@ -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") }, + }); + } + } +} diff --git a/examples/grammars/json.hpp b/examples/grammars/json.hpp deleted file mode 100644 index a73f057f..00000000 --- a/examples/grammars/json.hpp +++ /dev/null @@ -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 diff --git a/spec/compiler/compile_examples.cc b/spec/compiler/compile_examples.cc index 57399843..359410c0 100644 --- a/spec/compiler/compile_examples.cc +++ b/spec/compiler/compile_examples.cc @@ -1,6 +1,5 @@ #include "compiler_spec_helper.h" -#include "grammars/json.hpp" -#include "grammars/arithmetic.hpp" +#include "helpers/example_grammars.h" #include 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"); }); }); diff --git a/spec/compiler/helpers/example_grammars.h b/spec/compiler/helpers/example_grammars.h new file mode 100644 index 00000000..4ade985b --- /dev/null +++ b/spec/compiler/helpers/example_grammars.h @@ -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 diff --git a/tree_sitter.gyp b/tree_sitter.gyp index d39534a8..646a0fd2 100644 --- a/tree_sitter.gyp +++ b/tree_sitter.gyp @@ -49,8 +49,7 @@ 'spec/compiler', ], 'sources': [ - '