Rename type ts_lr_parser -> TSStateMachine
This commit is contained in:
parent
27f6eb725d
commit
0ec3faba3e
5 changed files with 96 additions and 95 deletions
75
spec/runtime/state_machine_spec.cc
Normal file
75
spec/runtime/state_machine_spec.cc
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include "runtime/runtime_spec_helper.h"
|
||||
#include "runtime/helpers/spy_reader.h"
|
||||
#include "runtime/helpers/dummy_parser.h"
|
||||
#include "tree_sitter/parser/state_machine.h"
|
||||
|
||||
TSTree *lex_fn_node_to_return;
|
||||
TSStateId lex_fn_state_received;
|
||||
TSLexer *lex_fn_lexer_received;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
START_TEST
|
||||
|
||||
describe("LR Parsers", [&]() {
|
||||
TSStateMachine *parser;
|
||||
SpyReader *reader;
|
||||
|
||||
before_each([&]() {
|
||||
reader = new SpyReader("some structured text", 5);
|
||||
parser = ts_state_machine_make(dummy_parser.symbol_count,
|
||||
(const TSParseAction *)dummy_parser.parse_table,
|
||||
dummy_parser.lex_states,
|
||||
fake_lex,
|
||||
dummy_parser.hidden_symbols);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
delete reader;
|
||||
});
|
||||
|
||||
describe("when starting at the beginning of the input (edit is NULL)", [&]() {
|
||||
before_each([&]() {
|
||||
ts_state_machine_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(dummy_sym2, 5, 1);
|
||||
ts_state_machine_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(dummy_sym2, 5, 1);
|
||||
});
|
||||
|
||||
it("advances to the state specified in the action", [&]() {
|
||||
ts_state_machine_parse(parser, nullptr);
|
||||
AssertThat(ts_stack_top_state(&parser->stack), Equals(12));
|
||||
});
|
||||
|
||||
it("continues parsing (returns NULL)", [&]() {
|
||||
auto result = ts_state_machine_parse(parser, nullptr);
|
||||
AssertThat(result, Equals((TSTree *)nullptr));
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the returned symbol indicates an error", [&]() {
|
||||
before_each([&]() {
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1);
|
||||
});
|
||||
|
||||
it("ends the parse, returning an error tree", [&]() {
|
||||
auto result = ts_state_machine_parse(parser, nullptr);
|
||||
AssertThat(ts_tree_symbol(result), Equals(ts_builtin_sym_error));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
END_TEST
|
||||
Loading…
Add table
Add a link
Reference in a new issue