From 71cc7a2dc2e1c3699c9d31f5b8f313de55828087 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 12 Oct 2014 19:10:02 -0700 Subject: [PATCH] Tidy up remaining files in build_tables namespace --- src/compiler/build_tables/first_symbols.cc | 2 +- src/compiler/build_tables/item.cc | 2 +- src/compiler/build_tables/item.h | 2 +- src/compiler/build_tables/item_set_closure.cc | 6 +-- src/compiler/build_tables/item_set_closure.h | 15 ++++--- .../build_tables/item_set_transitions.cc | 7 ++-- .../build_tables/lex_conflict_manager.cc | 26 +++++------- src/compiler/build_tables/lex_item.cc | 1 - .../build_tables/parse_conflict_manager.cc | 7 ++-- .../build_tables/parse_conflict_manager.h | 18 ++++----- src/compiler/build_tables/parse_item.cc | 4 -- src/compiler/build_tables/parse_item.h | 3 +- .../build_tables/rule_can_be_blank.cc | 4 +- src/compiler/build_tables/rule_transitions.cc | 40 ++++++++++--------- 14 files changed, 64 insertions(+), 73 deletions(-) diff --git a/src/compiler/build_tables/first_symbols.cc b/src/compiler/build_tables/first_symbols.cc index 421745b3..8144610c 100644 --- a/src/compiler/build_tables/first_symbols.cc +++ b/src/compiler/build_tables/first_symbols.cc @@ -21,7 +21,7 @@ class FirstSymbols : public rules::RuleFn> { public: explicit FirstSymbols(const SyntaxGrammar *grammar) : grammar(grammar) {} - protected: + private: set apply_to(const Symbol *rule) { auto insertion_result = visited_symbols.insert(*rule); if (!insertion_result.second) diff --git a/src/compiler/build_tables/item.cc b/src/compiler/build_tables/item.cc index e3313c9b..85d2f2eb 100644 --- a/src/compiler/build_tables/item.cc +++ b/src/compiler/build_tables/item.cc @@ -1,7 +1,7 @@ +#include "compiler/build_tables/get_metadata.h" #include "compiler/build_tables/item.h" #include "compiler/build_tables/rule_can_be_blank.h" #include "compiler/rules/metadata.h" -#include "compiler/build_tables/get_metadata.h" #include "tree_sitter/compiler.h" namespace tree_sitter { diff --git a/src/compiler/build_tables/item.h b/src/compiler/build_tables/item.h index 72b72269..4c8df462 100644 --- a/src/compiler/build_tables/item.h +++ b/src/compiler/build_tables/item.h @@ -1,8 +1,8 @@ #ifndef COMPILER_BUILD_TABLES_ITEM_H_ #define COMPILER_BUILD_TABLES_ITEM_H_ -#include #include "compiler/rules/symbol.h" +#include "tree_sitter/compiler.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/item_set_closure.cc b/src/compiler/build_tables/item_set_closure.cc index 464a4b2a..6bd55a9f 100644 --- a/src/compiler/build_tables/item_set_closure.cc +++ b/src/compiler/build_tables/item_set_closure.cc @@ -22,10 +22,8 @@ const ParseItemSet item_set_closure(const ParseItem &starting_item, const set &starting_lookahead_symbols, const SyntaxGrammar &grammar) { ParseItemSet result; - - vector>> items_to_process = { - { starting_item, starting_lookahead_symbols } - }; + vector>> items_to_process; + items_to_process.push_back({ starting_item, starting_lookahead_symbols }); while (!items_to_process.empty()) { ParseItem item = items_to_process.back().first; diff --git a/src/compiler/build_tables/item_set_closure.h b/src/compiler/build_tables/item_set_closure.h index f7633879..b16fc99a 100644 --- a/src/compiler/build_tables/item_set_closure.h +++ b/src/compiler/build_tables/item_set_closure.h @@ -2,17 +2,20 @@ #define COMPILER_BUILD_TABLES_ITEM_SET_CLOSURE_H_ #include -#include "compiler/rules/symbol.h" #include "compiler/build_tables/parse_item.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { + class SyntaxGrammar; namespace build_tables { -const ParseItemSet item_set_closure( - const ParseItem &item, const std::set &lookahead_symbols, - const SyntaxGrammar &grammar); -} -} + +const ParseItemSet item_set_closure(const ParseItem &, + const std::set &, + const SyntaxGrammar &); + +} // namespace build_tables +} // namespace tree_sitter #endif // COMPILER_BUILD_TABLES_ITEM_SET_CLOSURE_H_ diff --git a/src/compiler/build_tables/item_set_transitions.cc b/src/compiler/build_tables/item_set_transitions.cc index e90d0a3b..a23a5e06 100644 --- a/src/compiler/build_tables/item_set_transitions.cc +++ b/src/compiler/build_tables/item_set_transitions.cc @@ -1,10 +1,11 @@ -#include "compiler/build_tables/item_set_transitions.h" #include #include "compiler/build_tables/item_set_closure.h" -#include "compiler/build_tables/rule_transitions.h" +#include "compiler/build_tables/lex_item.h" #include "compiler/build_tables/merge_transitions.h" -#include "compiler/rules/symbol.h" +#include "compiler/build_tables/parse_item.h" +#include "compiler/build_tables/rule_transitions.h" #include "compiler/prepared_grammar.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/lex_conflict_manager.cc b/src/compiler/build_tables/lex_conflict_manager.cc index 0e346dad..e875b162 100644 --- a/src/compiler/build_tables/lex_conflict_manager.cc +++ b/src/compiler/build_tables/lex_conflict_manager.cc @@ -1,20 +1,10 @@ #include "compiler/build_tables/lex_conflict_manager.h" -#include -#include -#include -#include #include "compiler/util/string_helpers.h" #include "compiler/prepared_grammar.h" namespace tree_sitter { namespace build_tables { -using std::string; -using std::to_string; -using std::map; -using std::set; -using std::vector; - LexConflictManager::LexConflictManager(const LexicalGrammar &grammar) : grammar(grammar) {} @@ -26,26 +16,30 @@ bool LexConflictManager::resolve_lex_action(const LexAction &old_action, switch (old_action.type) { case LexActionTypeError: return true; + case LexActionTypeAccept: { int old_precedence = *old_action.precedence_values.begin(); + switch (new_action.type) { case LexActionTypeAccept: { int new_precedence = *new_action.precedence_values.begin(); - if (new_precedence > old_precedence) { + if (new_precedence > old_precedence) return true; - } else if (new_precedence < old_precedence) { + else if (new_precedence < old_precedence) return false; - } else { + else return new_action.symbol.index < old_action.symbol.index; - } } - case LexActionTypeAdvance: { return true; } + + case LexActionTypeAdvance: + return true; + default: return false; } - return true; } + default: return false; } diff --git a/src/compiler/build_tables/lex_item.cc b/src/compiler/build_tables/lex_item.cc index 8db92401..fd550f80 100644 --- a/src/compiler/build_tables/lex_item.cc +++ b/src/compiler/build_tables/lex_item.cc @@ -10,7 +10,6 @@ namespace build_tables { using std::string; using std::ostream; -using std::vector; LexItem::LexItem(const rules::Symbol &lhs, const rules::rule_ptr rule) : Item(lhs, rule) {} diff --git a/src/compiler/build_tables/parse_conflict_manager.cc b/src/compiler/build_tables/parse_conflict_manager.cc index e867c636..e66eef1d 100644 --- a/src/compiler/build_tables/parse_conflict_manager.cc +++ b/src/compiler/build_tables/parse_conflict_manager.cc @@ -1,8 +1,6 @@ #include "compiler/build_tables/parse_conflict_manager.h" #include -#include #include -#include #include "compiler/util/string_helpers.h" #include "compiler/rules/built_in_symbols.h" #include "compiler/prepared_grammar.h" @@ -12,8 +10,6 @@ namespace build_tables { using std::string; using std::to_string; -using std::map; -using std::set; using std::vector; ParseConflictManager::ParseConflictManager(const SyntaxGrammar &parse_grammar, @@ -29,6 +25,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol, switch (old_action.type) { case ParseActionTypeError: return true; + case ParseActionTypeShift: { int min_precedence = *old_action.precedence_values.begin(); int max_precedence = *old_action.precedence_values.rbegin(); @@ -50,6 +47,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol, return false; } } + case ParseActionTypeReduce: switch (new_action.type) { case ParseActionTypeReduce: { @@ -67,6 +65,7 @@ bool ParseConflictManager::resolve_parse_action(const rules::Symbol &symbol, default: return false; } + default: return false; } diff --git a/src/compiler/build_tables/parse_conflict_manager.h b/src/compiler/build_tables/parse_conflict_manager.h index 245abb34..db04cc98 100644 --- a/src/compiler/build_tables/parse_conflict_manager.h +++ b/src/compiler/build_tables/parse_conflict_manager.h @@ -1,14 +1,13 @@ #ifndef COMPILER_BUILD_TABLES_PARSE_CONFLICT_MANAGER_H_ #define COMPILER_BUILD_TABLES_PARSE_CONFLICT_MANAGER_H_ -#include -#include -#include #include -#include "tree_sitter/compiler.h" +#include +#include #include "compiler/parse_table.h" -#include "compiler/rules/symbol.h" #include "compiler/prepared_grammar.h" +#include "compiler/rules/symbol.h" +#include "tree_sitter/compiler.h" namespace tree_sitter { namespace build_tables { @@ -19,17 +18,16 @@ class ParseConflictManager { std::set conflicts_; public: - ParseConflictManager(const SyntaxGrammar &parse_grammar, - const LexicalGrammar &lex_grammar); + ParseConflictManager(const SyntaxGrammar &, const LexicalGrammar &); bool resolve_parse_action(const rules::Symbol &symbol, const ParseAction &old_action, const ParseAction &new_action); const std::vector conflicts() const; private: - std::string symbol_name(const rules::Symbol &symbol); - void record_conflict(const rules::Symbol &symbol, const ParseAction &left, - const ParseAction &right); + std::string symbol_name(const rules::Symbol &); + void record_conflict(const rules::Symbol &, const ParseAction &, + const ParseAction &); }; } // namespace build_tables diff --git a/src/compiler/build_tables/parse_item.cc b/src/compiler/build_tables/parse_item.cc index 4deda6fb..840eb1d4 100644 --- a/src/compiler/build_tables/parse_item.cc +++ b/src/compiler/build_tables/parse_item.cc @@ -1,14 +1,10 @@ #include "compiler/build_tables/parse_item.h" -#include #include "tree_sitter/compiler.h" namespace tree_sitter { namespace build_tables { -using std::pair; -using std::set; using std::string; -using std::to_string; using std::ostream; ParseItem::ParseItem(const rules::Symbol &lhs, const rules::rule_ptr rule, diff --git a/src/compiler/build_tables/parse_item.h b/src/compiler/build_tables/parse_item.h index e5ccec9b..f3ec4413 100644 --- a/src/compiler/build_tables/parse_item.h +++ b/src/compiler/build_tables/parse_item.h @@ -2,10 +2,9 @@ #define COMPILER_BUILD_TABLES_PARSE_ITEM_H_ #include -#include #include -#include "compiler/rules/symbol.h" #include "compiler/build_tables/item.h" +#include "compiler/rules/symbol.h" namespace tree_sitter { namespace build_tables { diff --git a/src/compiler/build_tables/rule_can_be_blank.cc b/src/compiler/build_tables/rule_can_be_blank.cc index d17dcba8..c22c21d8 100644 --- a/src/compiler/build_tables/rule_can_be_blank.cc +++ b/src/compiler/build_tables/rule_can_be_blank.cc @@ -40,10 +40,12 @@ class CanBeBlankRecursive : public CanBeBlank { using CanBeBlank::visit; public: - using CanBeBlank::apply_to; explicit CanBeBlankRecursive(const SyntaxGrammar *grammar) : grammar(grammar) {} + private: + using CanBeBlank::apply_to; + bool apply_to(const rules::Symbol *rule) { if (visited_symbols.find(*rule) == visited_symbols.end()) { visited_symbols.insert(*rule); diff --git a/src/compiler/build_tables/rule_transitions.cc b/src/compiler/build_tables/rule_transitions.cc index 14622e3e..cbc6c4c8 100644 --- a/src/compiler/build_tables/rule_transitions.cc +++ b/src/compiler/build_tables/rule_transitions.cc @@ -4,11 +4,9 @@ #include "compiler/rules/blank.h" #include "compiler/rules/choice.h" #include "compiler/rules/seq.h" -#include "compiler/rules/string.h" #include "compiler/rules/repeat.h" #include "compiler/rules/metadata.h" #include "compiler/rules/symbol.h" -#include "compiler/rules/pattern.h" #include "compiler/rules/character_set.h" #include "compiler/rules/visitor.h" @@ -17,48 +15,52 @@ namespace build_tables { using std::map; using std::make_shared; -using rules::rule_ptr; -using rules::Symbol; using rules::CharacterSet; +using rules::Choice; +using rules::Symbol; +using rules::rule_ptr; template -void merge_transitions(map *left, const map &right); +void merge_transitions(map *, const map &); + +struct MergeAsChoice { + void operator()(rule_ptr *left, const rule_ptr *right) { + *left = Choice::Build({ *left, *right }); + } +}; template <> void merge_transitions(map *left, const map &right) { for (auto &pair : right) - merge_char_transition(left, pair, - [](rule_ptr *left, const rule_ptr *right) { - *left = rules::Choice::Build({ *left, *right }); - }); + merge_char_transition(left, pair, MergeAsChoice()); } template <> void merge_transitions(map *left, const map &right) { for (auto &pair : right) - merge_sym_transition(left, pair, - [](rule_ptr *left, const rule_ptr *right) { - *left = rules::Choice::Build({ *left, *right }); - }); + merge_sym_transition(left, pair, MergeAsChoice()); } template class RuleTransitions : public rules::RuleFn> { - map apply_to_atom(const rules::Rule *rule) { - auto atom = dynamic_cast(rule); - if (atom) - return map({ { *atom, make_shared() } }); + private: + map apply_to_primitive(const rules::Rule *rule) { + auto primitive = dynamic_cast(rule); + if (primitive) + return map({ { *primitive, make_shared() } }); else return map(); } map apply_to(const CharacterSet *rule) { - return apply_to_atom(rule); + return apply_to_primitive(rule); } - map apply_to(const Symbol *rule) { return apply_to_atom(rule); } + map apply_to(const Symbol *rule) { + return apply_to_primitive(rule); + } map apply_to(const rules::Choice *rule) { map result;