Make compile take a name argument
Start work on JSON fixture grammar
This commit is contained in:
parent
bb42543f1b
commit
5776846227
12 changed files with 603 additions and 24 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#include "spec_helper.h"
|
||||
#include "compile.h"
|
||||
#include "../fixtures/grammars/arithmetic.h"
|
||||
#include "../fixtures/grammars/test_grammars.h"
|
||||
#include <fstream>
|
||||
|
||||
START_TEST
|
||||
|
|
@ -10,7 +10,12 @@ describe("compiling grammars", []() {
|
|||
|
||||
it("works for the arithmetic grammar", [&]() {
|
||||
Grammar grammar = test_grammars::arithmetic();
|
||||
ofstream(test_parser_dir + "/arithmetic.c") << compile(grammar);
|
||||
ofstream(test_parser_dir + "/arithmetic.c") << compile(grammar, "arithmetic");
|
||||
});
|
||||
|
||||
it("works for the json grammar", [&]() {
|
||||
Grammar grammar = test_grammars::json();
|
||||
ofstream(test_parser_dir + "/json.c") << compile(grammar, "json");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
2
spec/fixtures/grammars/arithmetic.cpp
vendored
2
spec/fixtures/grammars/arithmetic.cpp
vendored
|
|
@ -1,4 +1,4 @@
|
|||
#include "arithmetic.h"
|
||||
#include "test_grammars.h"
|
||||
#include "rules.h"
|
||||
|
||||
using namespace tree_sitter;
|
||||
|
|
|
|||
10
spec/fixtures/grammars/arithmetic.h
vendored
10
spec/fixtures/grammars/arithmetic.h
vendored
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef TreeSitter_arithmetic_h
|
||||
#define TreeSitter_arithmetic_h
|
||||
|
||||
#include "grammar.h"
|
||||
|
||||
namespace test_grammars {
|
||||
tree_sitter::Grammar arithmetic();
|
||||
}
|
||||
|
||||
#endif
|
||||
43
spec/fixtures/grammars/json.cpp
vendored
Normal file
43
spec/fixtures/grammars/json.cpp
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include "test_grammars.h"
|
||||
#include "rules.h"
|
||||
|
||||
using namespace tree_sitter;
|
||||
using namespace rules;
|
||||
|
||||
static rule_ptr comma_sep(const rule_ptr &rule) {
|
||||
return seq({
|
||||
rule,
|
||||
choice({
|
||||
repeat(seq({ str(","), rule })),
|
||||
blank()
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
namespace test_grammars {
|
||||
Grammar json() {
|
||||
return Grammar({
|
||||
{ "value", choice({
|
||||
sym("object"),
|
||||
sym("array"),
|
||||
sym("string"),
|
||||
sym("number") }) },
|
||||
{ "object", seq({
|
||||
str("{"),
|
||||
comma_sep(seq({
|
||||
sym("string"),
|
||||
str(":"),
|
||||
sym("value") })),
|
||||
str("}"), }) },
|
||||
{ "array", seq({
|
||||
str("["),
|
||||
comma_sep(sym("value")),
|
||||
str("]"), }) },
|
||||
{ "string", seq({
|
||||
str("\""),
|
||||
pattern("\\w+"),
|
||||
str("\"") }) },
|
||||
{ "number", pattern("\\d+") }
|
||||
});
|
||||
}
|
||||
}
|
||||
11
spec/fixtures/grammars/test_grammars.h
vendored
Normal file
11
spec/fixtures/grammars/test_grammars.h
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef TreeSitter_test_grammars_h
|
||||
#define TreeSitter_test_grammars_h
|
||||
|
||||
#include "grammar.h"
|
||||
|
||||
namespace test_grammars {
|
||||
tree_sitter::Grammar arithmetic();
|
||||
tree_sitter::Grammar json();
|
||||
}
|
||||
|
||||
#endif
|
||||
487
spec/fixtures/parsers/json.c
vendored
Normal file
487
spec/fixtures/parsers/json.c
vendored
Normal file
|
|
@ -0,0 +1,487 @@
|
|||
#include "parser.h"
|
||||
#include <ctype.h>
|
||||
|
||||
enum ts_symbol {
|
||||
ts_symbol_value,
|
||||
ts_symbol_object,
|
||||
ts_symbol_array,
|
||||
ts_symbol___END__,
|
||||
ts_symbol_number,
|
||||
ts_symbol_6,
|
||||
ts_symbol_7,
|
||||
ts_symbol_5,
|
||||
ts_symbol_2,
|
||||
ts_symbol_1,
|
||||
ts_symbol_4,
|
||||
ts_symbol_3,
|
||||
ts_symbol_string,
|
||||
};
|
||||
|
||||
static const char *ts_symbol_names[] = {
|
||||
"value",
|
||||
"object",
|
||||
"array",
|
||||
"__END__",
|
||||
"number",
|
||||
"6",
|
||||
"7",
|
||||
"5",
|
||||
"2",
|
||||
"1",
|
||||
"4",
|
||||
"3",
|
||||
"string",
|
||||
};
|
||||
|
||||
static void ts_lex(TSParser *parser) {
|
||||
START_LEXER();
|
||||
switch (LEX_STATE()) {
|
||||
case 0:
|
||||
if (LOOKAHEAD_CHAR() == '\0')
|
||||
ADVANCE(1);
|
||||
LEX_ERROR(1, EXPECT({"''"}));
|
||||
case 1:
|
||||
ACCEPT_TOKEN(ts_symbol___END__);
|
||||
case 2:
|
||||
if (LOOKAHEAD_CHAR() == ',')
|
||||
ADVANCE(3);
|
||||
ACCEPT_TOKEN(ts_symbol_2);
|
||||
case 3:
|
||||
LEX_ERROR(0, EXPECT({}));
|
||||
case 4:
|
||||
if (LOOKAHEAD_CHAR() == ']')
|
||||
ADVANCE(5);
|
||||
LEX_ERROR(1, EXPECT({"']'"}));
|
||||
case 5:
|
||||
ACCEPT_TOKEN(ts_symbol_3);
|
||||
case 6:
|
||||
if (LOOKAHEAD_CHAR() == ',')
|
||||
ADVANCE(7);
|
||||
ACCEPT_TOKEN(ts_symbol_6);
|
||||
case 7:
|
||||
LEX_ERROR(0, EXPECT({}));
|
||||
case 8:
|
||||
if (LOOKAHEAD_CHAR() == '\"')
|
||||
ADVANCE(12);
|
||||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(11);
|
||||
if (LOOKAHEAD_CHAR() == '{')
|
||||
ADVANCE(10);
|
||||
if (LOOKAHEAD_CHAR() == '[')
|
||||
ADVANCE(9);
|
||||
LEX_ERROR(4, EXPECT({"'['", "'{'", "<digit>", "'\"'"}));
|
||||
case 9:
|
||||
ACCEPT_TOKEN(ts_symbol_1);
|
||||
case 10:
|
||||
ACCEPT_TOKEN(ts_symbol_4);
|
||||
case 11:
|
||||
if (isdigit(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(11);
|
||||
ACCEPT_TOKEN(ts_symbol_number);
|
||||
case 12:
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(13);
|
||||
LEX_ERROR(1, EXPECT({"<word>"}));
|
||||
case 13:
|
||||
if (isalnum(LOOKAHEAD_CHAR()))
|
||||
ADVANCE(13);
|
||||
ACCEPT_TOKEN(ts_symbol_string);
|
||||
case 14:
|
||||
if (LOOKAHEAD_CHAR() == '}')
|
||||
ADVANCE(15);
|
||||
LEX_ERROR(1, EXPECT({"'}'"}));
|
||||
case 15:
|
||||
ACCEPT_TOKEN(ts_symbol_7);
|
||||
case 16:
|
||||
if (LOOKAHEAD_CHAR() == ':')
|
||||
ADVANCE(17);
|
||||
LEX_ERROR(1, EXPECT({"':'"}));
|
||||
case 17:
|
||||
ACCEPT_TOKEN(ts_symbol_5);
|
||||
case 18:
|
||||
if (LOOKAHEAD_CHAR() == '\"')
|
||||
ADVANCE(12);
|
||||
LEX_ERROR(1, EXPECT({"'\"'"}));
|
||||
default:
|
||||
LEX_PANIC();
|
||||
}
|
||||
FINISH_LEXER();
|
||||
}
|
||||
|
||||
static TSParseResult ts_parse(const char *input) {
|
||||
START_PARSER();
|
||||
switch (PARSE_STATE()) {
|
||||
case 0:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(34);
|
||||
case ts_symbol_string:
|
||||
SHIFT(34);
|
||||
case ts_symbol_array:
|
||||
SHIFT(34);
|
||||
case ts_symbol_object:
|
||||
SHIFT(34);
|
||||
case ts_symbol_4:
|
||||
SHIFT(28);
|
||||
case ts_symbol_1:
|
||||
SHIFT(2);
|
||||
case ts_symbol_value:
|
||||
SHIFT(1);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"value", "1", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 1:
|
||||
SET_LEX_STATE(0);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol___END__:
|
||||
ACCEPT_INPUT();
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"__END__"}));
|
||||
}
|
||||
case 2:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(14);
|
||||
case ts_symbol_string:
|
||||
SHIFT(14);
|
||||
case ts_symbol_array:
|
||||
SHIFT(14);
|
||||
case ts_symbol_object:
|
||||
SHIFT(14);
|
||||
case ts_symbol_4:
|
||||
SHIFT(7);
|
||||
case ts_symbol_value:
|
||||
SHIFT(25);
|
||||
case ts_symbol_1:
|
||||
SHIFT(3);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 3:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(14);
|
||||
case ts_symbol_string:
|
||||
SHIFT(14);
|
||||
case ts_symbol_array:
|
||||
SHIFT(14);
|
||||
case ts_symbol_object:
|
||||
SHIFT(14);
|
||||
case ts_symbol_4:
|
||||
SHIFT(7);
|
||||
case ts_symbol_value:
|
||||
SHIFT(4);
|
||||
case ts_symbol_1:
|
||||
SHIFT(3);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 4:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
SHIFT(5);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 5:
|
||||
SET_LEX_STATE(4);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_3:
|
||||
SHIFT(6);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"3"}));
|
||||
}
|
||||
case 6:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
REDUCE(ts_symbol_array, 4);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 7:
|
||||
SET_LEX_STATE(18);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_string:
|
||||
SHIFT(8);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"string"}));
|
||||
}
|
||||
case 8:
|
||||
SET_LEX_STATE(16);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_5:
|
||||
SHIFT(9);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"5"}));
|
||||
}
|
||||
case 9:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(21);
|
||||
case ts_symbol_string:
|
||||
SHIFT(21);
|
||||
case ts_symbol_array:
|
||||
SHIFT(21);
|
||||
case ts_symbol_object:
|
||||
SHIFT(21);
|
||||
case ts_symbol_4:
|
||||
SHIFT(15);
|
||||
case ts_symbol_value:
|
||||
SHIFT(22);
|
||||
case ts_symbol_1:
|
||||
SHIFT(10);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 10:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(14);
|
||||
case ts_symbol_string:
|
||||
SHIFT(14);
|
||||
case ts_symbol_array:
|
||||
SHIFT(14);
|
||||
case ts_symbol_object:
|
||||
SHIFT(14);
|
||||
case ts_symbol_4:
|
||||
SHIFT(7);
|
||||
case ts_symbol_value:
|
||||
SHIFT(11);
|
||||
case ts_symbol_1:
|
||||
SHIFT(3);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 11:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
SHIFT(12);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 12:
|
||||
SET_LEX_STATE(4);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_3:
|
||||
SHIFT(13);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"3"}));
|
||||
}
|
||||
case 13:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
REDUCE(ts_symbol_array, 4);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 14:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
REDUCE(ts_symbol_value, 1);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 15:
|
||||
SET_LEX_STATE(18);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_string:
|
||||
SHIFT(16);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"string"}));
|
||||
}
|
||||
case 16:
|
||||
SET_LEX_STATE(16);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_5:
|
||||
SHIFT(17);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"5"}));
|
||||
}
|
||||
case 17:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(21);
|
||||
case ts_symbol_string:
|
||||
SHIFT(21);
|
||||
case ts_symbol_array:
|
||||
SHIFT(21);
|
||||
case ts_symbol_object:
|
||||
SHIFT(21);
|
||||
case ts_symbol_4:
|
||||
SHIFT(15);
|
||||
case ts_symbol_value:
|
||||
SHIFT(18);
|
||||
case ts_symbol_1:
|
||||
SHIFT(10);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 18:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
SHIFT(19);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 19:
|
||||
SET_LEX_STATE(14);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_7:
|
||||
SHIFT(20);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"7"}));
|
||||
}
|
||||
case 20:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
REDUCE(ts_symbol_object, 6);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 21:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
REDUCE(ts_symbol_value, 1);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 22:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
SHIFT(23);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 23:
|
||||
SET_LEX_STATE(14);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_7:
|
||||
SHIFT(24);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"7"}));
|
||||
}
|
||||
case 24:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
REDUCE(ts_symbol_object, 6);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 25:
|
||||
SET_LEX_STATE(2);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_2:
|
||||
SHIFT(26);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"2"}));
|
||||
}
|
||||
case 26:
|
||||
SET_LEX_STATE(4);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_3:
|
||||
SHIFT(27);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"3"}));
|
||||
}
|
||||
case 27:
|
||||
SET_LEX_STATE(0);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol___END__:
|
||||
REDUCE(ts_symbol_array, 4);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"__END__"}));
|
||||
}
|
||||
case 28:
|
||||
SET_LEX_STATE(18);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_string:
|
||||
SHIFT(29);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"string"}));
|
||||
}
|
||||
case 29:
|
||||
SET_LEX_STATE(16);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_5:
|
||||
SHIFT(30);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"5"}));
|
||||
}
|
||||
case 30:
|
||||
SET_LEX_STATE(8);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_number:
|
||||
SHIFT(21);
|
||||
case ts_symbol_string:
|
||||
SHIFT(21);
|
||||
case ts_symbol_array:
|
||||
SHIFT(21);
|
||||
case ts_symbol_object:
|
||||
SHIFT(21);
|
||||
case ts_symbol_4:
|
||||
SHIFT(15);
|
||||
case ts_symbol_value:
|
||||
SHIFT(31);
|
||||
case ts_symbol_1:
|
||||
SHIFT(10);
|
||||
default:
|
||||
PARSE_ERROR(7, EXPECT({"1", "value", "object", "array", "4", "string", "number"}));
|
||||
}
|
||||
case 31:
|
||||
SET_LEX_STATE(6);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_6:
|
||||
SHIFT(32);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"6"}));
|
||||
}
|
||||
case 32:
|
||||
SET_LEX_STATE(14);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol_7:
|
||||
SHIFT(33);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"7"}));
|
||||
}
|
||||
case 33:
|
||||
SET_LEX_STATE(0);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol___END__:
|
||||
REDUCE(ts_symbol_object, 6);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"__END__"}));
|
||||
}
|
||||
case 34:
|
||||
SET_LEX_STATE(0);
|
||||
switch (LOOKAHEAD_SYM()) {
|
||||
case ts_symbol___END__:
|
||||
REDUCE(ts_symbol_value, 1);
|
||||
default:
|
||||
PARSE_ERROR(1, EXPECT({"__END__"}));
|
||||
}
|
||||
default:
|
||||
PARSE_PANIC();
|
||||
}
|
||||
FINISH_PARSER();
|
||||
}
|
||||
|
||||
TSParseConfig ts_parse_config_json = {
|
||||
.parse_fn = ts_parse,
|
||||
.symbol_names = ts_symbol_names
|
||||
};
|
||||
22
spec/runtime/json_spec.cpp
Normal file
22
spec/runtime/json_spec.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "spec_helper.h"
|
||||
#include "document.h"
|
||||
|
||||
extern TSParseConfig ts_parse_config_json;
|
||||
|
||||
START_TEST
|
||||
|
||||
describe("json", []() {
|
||||
TSDocument *document;
|
||||
|
||||
before_each([&]() {
|
||||
document = TSDocumentMake();
|
||||
TSDocumentSetUp(document, ts_parse_config_json);
|
||||
});
|
||||
|
||||
it("parses strings", [&]() {
|
||||
TSDocumentSetText(document, "\"foo\"");
|
||||
AssertThat(string(TSDocumentToString(document)), Equals("(value (string))"));
|
||||
});
|
||||
});
|
||||
|
||||
END_TEST
|
||||
|
|
@ -6,12 +6,12 @@
|
|||
#include "generate_code/c_code.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
std::string compile(const Grammar &grammar) {
|
||||
std::string compile(const Grammar &grammar, std::string name) {
|
||||
auto grammars = prepare_grammar::perform(grammar);
|
||||
auto tables = build_tables::perform(grammars.first, grammars.second);
|
||||
auto rule_names = grammars.first.rule_names();
|
||||
auto token_names = grammars.second.rule_names();
|
||||
rule_names.insert(rule_names.end(), token_names.begin(), token_names.end());
|
||||
return generate_code::c_code(rule_names, tables.first, tables.second);
|
||||
return generate_code::c_code(name, rule_names, tables.first, tables.second);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
namespace tree_sitter {
|
||||
class Grammar;
|
||||
|
||||
std::string compile(const Grammar &grammar);
|
||||
std::string compile(const Grammar &grammar, std::string name);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -76,8 +76,10 @@ namespace tree_sitter {
|
|||
const vector<string> rule_names;
|
||||
const ParseTable parse_table;
|
||||
const LexTable lex_table;
|
||||
const string name;
|
||||
public:
|
||||
CCodeGenerator(vector<string> rule_names, const ParseTable &parse_table, const LexTable &lex_table) :
|
||||
CCodeGenerator(string name, vector<string> rule_names, const ParseTable &parse_table, const LexTable &lex_table) :
|
||||
name(name),
|
||||
rule_names(rule_names),
|
||||
parse_table(parse_table),
|
||||
lex_table(lex_table)
|
||||
|
|
@ -91,6 +93,8 @@ namespace tree_sitter {
|
|||
switch (character) {
|
||||
case '\0':
|
||||
return "\\0";
|
||||
case '"':
|
||||
return "\\\"";
|
||||
default:
|
||||
return string() + character;
|
||||
}
|
||||
|
|
@ -143,13 +147,18 @@ namespace tree_sitter {
|
|||
return result;
|
||||
}
|
||||
|
||||
string escape_string(string input) {
|
||||
str_replace(input, "\"", "\\\"");
|
||||
return input;
|
||||
}
|
||||
|
||||
string lex_error_call(const unordered_set<CharMatch> &expected_inputs) {
|
||||
string result = "LEX_ERROR(" + to_string(expected_inputs.size()) + ", EXPECT({";
|
||||
bool started = false;
|
||||
for (auto match : expected_inputs) {
|
||||
if (started) result += ", ";
|
||||
started = true;
|
||||
result += "\"" + CharMatchToString(match) + "\"";
|
||||
result += "\"" + escape_string(CharMatchToString(match)) + "\"";
|
||||
}
|
||||
result += "}));";
|
||||
return result;
|
||||
|
|
@ -249,7 +258,7 @@ namespace tree_sitter {
|
|||
|
||||
string parse_config_struct() {
|
||||
return join({
|
||||
"TSParseConfig ts_parse_config_arithmetic = {",
|
||||
"TSParseConfig ts_parse_config_" + name + " = {",
|
||||
indent(".parse_fn = ts_parse,"),
|
||||
indent(".symbol_names = ts_symbol_names"),
|
||||
"};"
|
||||
|
|
@ -268,8 +277,8 @@ namespace tree_sitter {
|
|||
}
|
||||
};
|
||||
|
||||
string c_code(const vector<string> rule_names, const ParseTable &parse_table, const LexTable &lex_table) {
|
||||
return CCodeGenerator(rule_names, parse_table, lex_table).code();
|
||||
string c_code(string name, const vector<string> rule_names, const ParseTable &parse_table, const LexTable &lex_table) {
|
||||
return CCodeGenerator(name, rule_names, parse_table, lex_table).code();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace generate_code {
|
||||
std::string c_code(std::vector<std::string> rule_names, const ParseTable &parse_table, const LexTable &lex_table);
|
||||
std::string c_code(std::string name, std::vector<std::string> rule_names, const ParseTable &parse_table, const LexTable &lex_table);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@
|
|||
12BC470518822B27005AC502 /* parse_config.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12BC470318822A17005AC502 /* parse_config.cpp */; };
|
||||
12BC470718830BC5005AC502 /* first_set_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12BC470618830BC5005AC502 /* first_set_spec.cpp */; };
|
||||
12D136A4183678A2005F3369 /* repeat.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12D136A2183678A2005F3369 /* repeat.cpp */; };
|
||||
12E75A971891BD32001B8F10 /* json.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E75A961891BD32001B8F10 /* json.cpp */; };
|
||||
12E75A9A1891BF57001B8F10 /* json.c in Sources */ = {isa = PBXBuildFile; fileRef = 12E75A981891BF3B001B8F10 /* json.c */; };
|
||||
12E75A9C1891C17D001B8F10 /* json_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12E75A9B1891C17D001B8F10 /* json_spec.cpp */; };
|
||||
12EDCF8A187B498C005A7A07 /* tree_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF89187B498C005A7A07 /* tree_spec.cpp */; };
|
||||
12EDCF8D187C6282005A7A07 /* document.c in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF8C187C6282005A7A07 /* document.c */; };
|
||||
12EDCF981881FCD5005A7A07 /* extract_tokens.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12EDCF8F1881FCCA005A7A07 /* extract_tokens.cpp */; };
|
||||
|
|
@ -94,7 +97,7 @@
|
|||
121D8B3018795CC0003CF44B /* parser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = parser.h; sourceTree = "<group>"; };
|
||||
1225CC6318765693000D4723 /* prepare_grammar_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = prepare_grammar_spec.cpp; path = compiler/prepare_grammar_spec.cpp; sourceTree = "<group>"; };
|
||||
1251209A1830145300C9B56A /* rule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule.cpp; sourceTree = "<group>"; };
|
||||
125120A218307FFD00C9B56A /* arithmetic.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = arithmetic.h; path = spec/fixtures/grammars/arithmetic.h; sourceTree = SOURCE_ROOT; };
|
||||
125120A218307FFD00C9B56A /* test_grammars.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = test_grammars.h; path = spec/fixtures/grammars/test_grammars.h; sourceTree = SOURCE_ROOT; };
|
||||
125120A3183083BD00C9B56A /* arithmetic.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = arithmetic.cpp; path = spec/fixtures/grammars/arithmetic.cpp; sourceTree = SOURCE_ROOT; };
|
||||
12AB465D188BD03E00DE79DF /* follow_sets.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = follow_sets.cpp; sourceTree = "<group>"; };
|
||||
12AB465E188BD03E00DE79DF /* follow_sets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = follow_sets.h; sourceTree = "<group>"; };
|
||||
|
|
@ -109,6 +112,9 @@
|
|||
12D136A3183678A2005F3369 /* repeat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = repeat.h; sourceTree = "<group>"; };
|
||||
12E71794181D02A80051A649 /* compiler_specs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = compiler_specs; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
12E71852181D081C0051A649 /* rules.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = rules.h; path = src/compiler/rules/rules.h; sourceTree = SOURCE_ROOT; };
|
||||
12E75A961891BD32001B8F10 /* json.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = json.cpp; sourceTree = "<group>"; };
|
||||
12E75A981891BF3B001B8F10 /* json.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = json.c; sourceTree = "<group>"; };
|
||||
12E75A9B1891C17D001B8F10 /* json_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = json_spec.cpp; sourceTree = "<group>"; };
|
||||
12EDCF89187B498C005A7A07 /* tree_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tree_spec.cpp; sourceTree = "<group>"; };
|
||||
12EDCF8B187C6251005A7A07 /* document.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = document.h; sourceTree = "<group>"; };
|
||||
12EDCF8C187C6282005A7A07 /* document.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = document.c; sourceTree = "<group>"; };
|
||||
|
|
@ -244,8 +250,9 @@
|
|||
125120A118307FCA00C9B56A /* grammars */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
125120A218307FFD00C9B56A /* arithmetic.h */,
|
||||
125120A218307FFD00C9B56A /* test_grammars.h */,
|
||||
125120A3183083BD00C9B56A /* arithmetic.cpp */,
|
||||
12E75A961891BD32001B8F10 /* json.cpp */,
|
||||
);
|
||||
path = grammars;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -328,6 +335,7 @@
|
|||
12FD4062185E74DF0041A84E /* parsers */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
12E75A981891BF3B001B8F10 /* json.c */,
|
||||
12FD4065185E7C2F0041A84E /* arithmetic.c */,
|
||||
);
|
||||
path = parsers;
|
||||
|
|
@ -398,6 +406,7 @@
|
|||
children = (
|
||||
12FD40DA185FEF0D0041A84E /* arithmetic_spec.cpp */,
|
||||
12EDCF89187B498C005A7A07 /* tree_spec.cpp */,
|
||||
12E75A9B1891C17D001B8F10 /* json_spec.cpp */,
|
||||
);
|
||||
path = runtime;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -498,6 +507,7 @@
|
|||
12AB4661188CB3A300DE79DF /* item_set_closure_spec.cpp in Sources */,
|
||||
12FD40E918641FB70041A84E /* rules.cpp in Sources */,
|
||||
12EDCF981881FCD5005A7A07 /* extract_tokens.cpp in Sources */,
|
||||
12E75A971891BD32001B8F10 /* json.cpp in Sources */,
|
||||
12FD4061185E68470041A84E /* c_code.cpp in Sources */,
|
||||
12FD40D8185FEEDF0041A84E /* rules_spec.cpp in Sources */,
|
||||
12EDCFC318820A70005A7A07 /* item_set_transitions.cpp in Sources */,
|
||||
|
|
@ -527,9 +537,11 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
12EDCF8A187B498C005A7A07 /* tree_spec.cpp in Sources */,
|
||||
12E75A9A1891BF57001B8F10 /* json.c in Sources */,
|
||||
12EDCF8D187C6282005A7A07 /* document.c in Sources */,
|
||||
12FD40DF1860064C0041A84E /* tree.c in Sources */,
|
||||
12FD40D2185EEB970041A84E /* arithmetic.c in Sources */,
|
||||
12E75A9C1891C17D001B8F10 /* json_spec.cpp in Sources */,
|
||||
12FD40DB185FEF0D0041A84E /* arithmetic_spec.cpp in Sources */,
|
||||
12FD40C2185EEB5E0041A84E /* main.cpp in Sources */,
|
||||
12BC470518822B27005AC502 /* parse_config.cpp in Sources */,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue