From 6a8addb84f4dd2745e1d1298e66ce1255cd231a3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 23 Jul 2014 18:26:10 -0700 Subject: [PATCH] Fix segfault when grammar consists of a single token --- spec/compiler/compile_spec.cc | 22 +++++++++++++++++++ .../build_tables/build_parse_table.cc | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 spec/compiler/compile_spec.cc diff --git a/spec/compiler/compile_spec.cc b/spec/compiler/compile_spec.cc new file mode 100644 index 00000000..772ce006 --- /dev/null +++ b/spec/compiler/compile_spec.cc @@ -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(nullptr)); + }); + }); +}); + +END_TEST diff --git a/src/compiler/build_tables/build_parse_table.cc b/src/compiler/build_tables/build_parse_table.cc index 6f1cb4de..b11b0967 100644 --- a/src/compiler/build_tables/build_parse_table.cc +++ b/src/compiler/build_tables/build_parse_table.cc @@ -135,7 +135,10 @@ class ParseTableBuilder { conflict_manager(ParseConflictManager(grammar, lex_grammar)) {} pair > build() { - ParseItem start_item(rules::START(), make_shared(0), 0); + auto start_symbol = grammar.rules.empty() ? + make_shared(0, rules::SymbolOptionToken) : + make_shared(0); + ParseItem start_item(rules::START(), start_symbol, 0); add_parse_state( item_set_closure(start_item, { rules::END_OF_INPUT() }, grammar));