Merge pull request #42 from tree-sitter/mergeable-symbols

Mergeable symbols
This commit is contained in:
Max Brunsfeld 2016-10-25 14:59:22 -07:00 committed by GitHub
commit f11b729dfb
6 changed files with 16 additions and 8 deletions

View file

@ -27,7 +27,7 @@ describe("recovery_tokens(rule)", []() {
})),
};
AssertThat(recovery_tokens(grammar), Equals<vector<Symbol>>({
AssertThat(recovery_tokens(grammar), Equals<set<Symbol>>({
Symbol(1, true),
}));
});

View file

@ -75,6 +75,8 @@ class ParseTableBuilder {
for (const auto &pair2 : state.entries)
parse_table.symbols[pair1.first].compatible_symbols.insert(pair2.first);
parse_table.mergeable_symbols = recovery_tokens(lexical_grammar);
build_error_parse_state();
allow_any_conflict = true;
@ -112,7 +114,7 @@ class ParseTableBuilder {
void build_error_parse_state() {
ParseState error_state;
for (const Symbol &symbol : recovery_tokens(lexical_grammar))
for (auto &symbol : parse_table.mergeable_symbols)
add_out_of_context_parse_state(&error_state, symbol);
for (const Symbol &symbol : grammar.extra_tokens)

View file

@ -11,7 +11,7 @@ namespace tree_sitter {
namespace build_tables {
using rules::Symbol;
using std::vector;
using std::set;
template <bool left, bool right>
class CharacterAggregator : public rules::RuleFn<void> {
@ -47,8 +47,8 @@ class FirstCharacters : public CharacterAggregator<true, false> {};
class LastCharacters : public CharacterAggregator<false, true> {};
class AllCharacters : public CharacterAggregator<true, true> {};
vector<Symbol> recovery_tokens(const LexicalGrammar &grammar) {
vector<Symbol> result;
set<Symbol> recovery_tokens(const LexicalGrammar &grammar) {
set<Symbol> result;
AllCharacters all_separator_characters;
for (const rule_ptr &separator : grammar.separators)
@ -79,7 +79,7 @@ vector<Symbol> recovery_tokens(const LexicalGrammar &grammar) {
!all_characters.result.intersects(all_separator_characters.result);
if ((has_distinct_start && has_distinct_end) || has_no_separators)
result.push_back(Symbol(i, true));
result.insert(Symbol(i, true));
}
return result;

View file

@ -3,7 +3,7 @@
#include "compiler/rule.h"
#include "compiler/rules/symbol.h"
#include <vector>
#include <set>
namespace tree_sitter {
@ -11,7 +11,7 @@ struct LexicalGrammar;
namespace build_tables {
std::vector<rules::Symbol> recovery_tokens(const LexicalGrammar &);
std::set<rules::Symbol> recovery_tokens(const LexicalGrammar &);
} // namespace build_tables
} // namespace tree_sitter

View file

@ -206,6 +206,8 @@ bool ParseTable::merge_state(size_t i, size_t j) {
const auto &other_entry = other.entries.find(symbol);
if (other_entry == other.entries.end()) {
if (mergeable_symbols.count(symbol) == 0)
return false;
if (actions.back().type != ParseActionTypeReduce)
return false;
if (!has_entry(other, entry.second))
@ -222,6 +224,8 @@ bool ParseTable::merge_state(size_t i, size_t j) {
const vector<ParseAction> &actions = entry.second.actions;
if (!state.entries.count(symbol)) {
if (mergeable_symbols.count(symbol) == 0)
return false;
if (actions.back().type != ParseActionTypeReduce)
return false;
if (!has_entry(state, entry.second))

View file

@ -97,6 +97,8 @@ class ParseTable {
std::vector<ParseState> states;
std::map<rules::Symbol, ParseTableSymbolMetadata> symbols;
std::set<rules::Symbol> mergeable_symbols;
};
} // namespace tree_sitter