Fix segfault when grammar consists of a single token

This commit is contained in:
Max Brunsfeld 2014-07-23 18:26:10 -07:00
parent df3613d511
commit 6a8addb84f
2 changed files with 26 additions and 1 deletions

View file

@ -0,0 +1,22 @@
#include "compiler/compiler_spec_helper.h"
#include "tree_sitter/compiler.h"
using namespace rules;
START_TEST
describe("Compile", []() {
describe("when the grammar's start symbol is a token", [&]() {
it("does not fail", [&]() {
Grammar grammar({
{ "rule1", str("the-value") }
});
auto result = compile(grammar, "test_grammar");
const GrammarError *error = get<2>(result);
AssertThat(error, Equals<const GrammarError *>(nullptr));
});
});
});
END_TEST

View file

@ -135,7 +135,10 @@ class ParseTableBuilder {
conflict_manager(ParseConflictManager(grammar, lex_grammar)) {}
pair<ParseTable, vector<Conflict> > build() {
ParseItem start_item(rules::START(), make_shared<Symbol>(0), 0);
auto start_symbol = grammar.rules.empty() ?
make_shared<Symbol>(0, rules::SymbolOptionToken) :
make_shared<Symbol>(0);
ParseItem start_item(rules::START(), start_symbol, 0);
add_parse_state(
item_set_closure(start_item, { rules::END_OF_INPUT() }, grammar));