Remove generated parsers' dependency on the runtime library

Generated parsers no longer export a parser constructor function.
They now export an opaque Language object which can be set on
Documents directly. This way, the logic for constructing parsers
lives entirely in the runtime. The Languages are just structs which
have no load-time dependency on the runtime
This commit is contained in:
Max Brunsfeld 2014-07-30 23:40:02 -07:00
parent 048a479b5f
commit eecbcccee0
21 changed files with 219 additions and 173 deletions

View file

@ -1,15 +1,16 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/helpers/spy_reader.h"
#include "runtime/helpers/dummy_parser.h"
#include "runtime/helpers/dummy_language.h"
#include "runtime/tree.h"
#include "runtime/parser.h"
#include "tree_sitter/parser.h"
TSTree *lex_fn_node_to_return;
TSStateId lex_fn_state_received;
TSParser *lex_fn_parser_received;
TSLexer *lex_fn_lexer_received;
TSTree * fake_lex(TSParser *parser, TSStateId state_id) {
lex_fn_parser_received = parser;
TSTree * fake_lex(TSLexer *lexer, TSStateId state_id) {
lex_fn_lexer_received = lexer;
lex_fn_state_received = state_id;
return lex_fn_node_to_return;
}
@ -17,31 +18,29 @@ TSTree * fake_lex(TSParser *parser, TSStateId state_id) {
START_TEST
describe("LR Parsers", [&]() {
TSParser *parser;
TSParser parser;
SpyReader *reader;
before_each([&]() {
TSParserConfig config = dummy_parser;
config.lex_fn = fake_lex;
parser = ts_parser_make(config);
dummy_language->lex_fn = fake_lex;
parser = ts_parser_make(dummy_language);
reader = new SpyReader("some structured text", 5);
});
after_each([&]() {
ts_parser_free(parser);
ts_parser_destroy(&parser);
delete reader;
});
describe("when starting at the beginning of the input (edit is NULL)", [&]() {
before_each([&]() {
ts_parser_start(parser, reader->input, nullptr);
ts_parser_start(&parser, reader->input, nullptr);
});
it("runs the lexer with the lex state corresponding to the initial state", [&]() {
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1, 0);
ts_parser_step(parser);
ts_parser_step(&parser);
AssertThat(lex_fn_state_received, Equals(100));
});
@ -51,12 +50,12 @@ describe("LR Parsers", [&]() {
});
it("advances to the state specified in the action", [&]() {
ts_parser_step(parser);
AssertThat(ts_stack_top_state(&parser->stack), Equals(12));
ts_parser_step(&parser);
AssertThat(ts_stack_top_state(&parser.stack), Equals(12));
});
it("continues parsing (returns NULL)", [&]() {
auto result = ts_parser_step(parser);
auto result = ts_parser_step(&parser);
AssertThat(result, Equals((TSTree *)nullptr));
});
});
@ -67,7 +66,7 @@ describe("LR Parsers", [&]() {
});
it("ends the parse, returning an error tree", [&]() {
auto result = ts_parser_step(parser);
auto result = ts_parser_step(&parser);
AssertThat(result->symbol, Equals(ts_builtin_sym_error));
});
});