From 4602690c1a1e0ca07d93eb8194c1ce330bf3c27c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 8 Jun 2014 16:31:45 -0700 Subject: [PATCH] Fix lr parser spec for gcc --- spec/runtime/helpers/dummy_parser.c | 38 +++++++++++++++++ spec/runtime/helpers/dummy_parser.h | 30 +++++++++++++ spec/runtime/helpers/tree_helpers.h | 7 +++- spec/runtime/lr_parser_spec.cc | 65 ++++++++--------------------- tree_sitter.gyp | 2 +- 5 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 spec/runtime/helpers/dummy_parser.c create mode 100644 spec/runtime/helpers/dummy_parser.h diff --git a/spec/runtime/helpers/dummy_parser.c b/spec/runtime/helpers/dummy_parser.c new file mode 100644 index 00000000..1f1654f6 --- /dev/null +++ b/spec/runtime/helpers/dummy_parser.c @@ -0,0 +1,38 @@ +#include "helpers/dummy_parser.h" + +const ts_parse_action parse_table[3][5] = { + [0] = { + [dummy_sym2] = SHIFT(12), + [dummy_sym3] = SHIFT(12), + }, + [1] = { + [dummy_sym1] = ACCEPT_INPUT(), + [dummy_sym2] = SHIFT(2), + [dummy_sym3] = SHIFT(4), + }, + [2] = { + [dummy_sym1] = SHIFT(3), + [dummy_sym2] = SHIFT(12), + [dummy_sym3] = SHIFT(12), + }, +}; + +const ts_state_id lex_states[3] = { + [0] = 100, + [1] = 101, + [2] = 102, +}; + +const int hidden_symbols[5] = { + [dummy_sym1] = 0, + [dummy_sym2] = 0, + [dummy_sym3] = 1, +}; + +struct test_parser dummy_parser = { + .state_count = 3, + .symbol_count = 5, + .parse_table = (const ts_parse_action **)parse_table, + .lex_states = lex_states, + .hidden_symbols = hidden_symbols, +}; diff --git a/spec/runtime/helpers/dummy_parser.h b/spec/runtime/helpers/dummy_parser.h new file mode 100644 index 00000000..c84e1b9b --- /dev/null +++ b/spec/runtime/helpers/dummy_parser.h @@ -0,0 +1,30 @@ +#ifndef HELPERS_DUMMY_PARSER_H_ +#define HELPERS_DUMMY_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "tree_sitter/parser/lr_parser.h" + +enum { + dummy_sym1 = 2, + dummy_sym2 = 3, + dummy_sym3 = 4, +}; + +struct test_parser { + size_t state_count; + size_t symbol_count; + const ts_parse_action **parse_table; + const ts_state_id *lex_states; + const int *hidden_symbols; +}; + +extern struct test_parser dummy_parser; + +#ifdef __cplusplus +} +#endif + +#endif // HELPERS_DUMMY_PARSER_H_ diff --git a/spec/runtime/helpers/tree_helpers.h b/spec/runtime/helpers/tree_helpers.h index 383d066f..cc0bfa2f 100644 --- a/spec/runtime/helpers/tree_helpers.h +++ b/spec/runtime/helpers/tree_helpers.h @@ -1,5 +1,10 @@ +#ifndef HELPERS_TREE_HELPERS_H_ +#define HELPERS_TREE_HELPERS_H_ + #include "tree_sitter/runtime.h" #include -ts_tree ** tree_array(std::vector trees); \ No newline at end of file +ts_tree ** tree_array(std::vector trees); + +#endif // HELPERS_TREE_HELPERS_H_ \ No newline at end of file diff --git a/spec/runtime/lr_parser_spec.cc b/spec/runtime/lr_parser_spec.cc index 26d36a02..52701148 100644 --- a/spec/runtime/lr_parser_spec.cc +++ b/spec/runtime/lr_parser_spec.cc @@ -1,42 +1,8 @@ #include "runtime_spec_helper.h" #include "helpers/spy_reader.h" +#include "helpers/dummy_parser.h" #include "tree_sitter/parser/lr_parser.h" -enum { - sym1 = 2, - sym2 = 3, - hidden_sym = 4, -}; - -const ts_parse_action parse_table[3][5] = { - [0] = { - [sym2] = SHIFT(12), - [hidden_sym] = SHIFT(12), - }, - [1] = { - [sym1] = ACCEPT_INPUT(), - [sym2] = SHIFT(2), - [hidden_sym] = SHIFT(4), - }, - [2] = { - [sym1] = SHIFT(3), - [sym2] = SHIFT(12), - [hidden_sym] = SHIFT(12), - }, -}; - -ts_state_id lex_states[3] = { - [0] = 100, - [1] = 101, - [2] = 102, -}; - -int hidden_symbols[5] = { - [sym1] = 0, - [sym2] = 0, - [hidden_sym] = 1, -}; - ts_tree *lex_fn_node_to_return; ts_state_id lex_fn_state_received; ts_lexer *lex_fn_lexer_received; @@ -52,48 +18,53 @@ START_TEST describe("LR Parsers", [&]() { ts_lr_parser *parser; SpyReader *reader; - + before_each([&]() { reader = new SpyReader("some structured text", 5); - parser = ts_lr_parser_make(3, (const ts_parse_action *)parse_table, lex_states, fake_lex, hidden_symbols, nullptr); + parser = ts_lr_parser_make(dummy_parser.symbol_count, + (const ts_parse_action *)dummy_parser.parse_table, + dummy_parser.lex_states, + fake_lex, + dummy_parser.hidden_symbols, + nullptr); }); - + after_each([&]() { delete reader; }); - + describe("when starting at the beginning of the input (edit is NULL)", [&]() { before_each([&]() { ts_lr_parser_initialize(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(sym2, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1); ts_lr_parser_parse(parser, nullptr); AssertThat(lex_fn_state_received, Equals(100)); }); describe("when the returned symbol indicates a shift action", [&]() { before_each([&]() { - lex_fn_node_to_return = ts_tree_make_leaf(sym2, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1); }); - + it("advances to the state specified in the action", [&]() { ts_lr_parser_parse(parser, nullptr); AssertThat(ts_stack_top_state(&parser->stack), Equals(12)); }); - + it("continues parsing (returns NULL)", [&]() { auto result = ts_lr_parser_parse(parser, nullptr); AssertThat(result, Equals((ts_tree *)nullptr)); }); }); - + describe("when the returned symbol indicates an error", [&]() { before_each([&]() { - lex_fn_node_to_return = ts_tree_make_leaf(sym1, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1); }); - + it("ends the parse, returning an error tree", [&]() { auto result = ts_lr_parser_parse(parser, nullptr); AssertThat(ts_tree_symbol(result), Equals(ts_builtin_sym_error)); diff --git a/tree_sitter.gyp b/tree_sitter.gyp index c033b56e..1e989769 100644 --- a/tree_sitter.gyp +++ b/tree_sitter.gyp @@ -73,7 +73,7 @@ }] ], 'sources': [ - '