Fix lr parser spec for gcc

This commit is contained in:
Max Brunsfeld 2014-06-08 16:31:45 -07:00 committed by vagrant
parent 652fa2f8a5
commit 4602690c1a
5 changed files with 93 additions and 49 deletions

View file

@ -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,
};

View file

@ -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_

View file

@ -1,5 +1,10 @@
#ifndef HELPERS_TREE_HELPERS_H_
#define HELPERS_TREE_HELPERS_H_
#include "tree_sitter/runtime.h"
#include <vector>
ts_tree ** tree_array(std::vector<ts_tree *> trees);
ts_tree ** tree_array(std::vector<ts_tree *> trees);
#endif // HELPERS_TREE_HELPERS_H_

View file

@ -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));

View file

@ -73,7 +73,7 @@
}]
],
'sources': [
'<!@(find spec/runtime -name "*.h" -or -name "*.cc")',
'<!@(find spec/runtime -name "*.h" -or -name "*.cc" -or -name "*.c")',
'<!@(find examples/parsers -name "*.c")',
'<!@(find spec/runtime/languages -name "*.txt")',
],