From 812f27f43ae940eeeb36354533be395ecd2e7bd1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 12 Feb 2014 23:06:26 -0800 Subject: [PATCH] Clean up using statements --- src/compiler/build_tables/first_set.cpp | 2 +- src/compiler/build_tables/item.cpp | 7 +++--- src/compiler/build_tables/perform.cpp | 10 +++++--- src/compiler/generate_code/c_code.cpp | 14 +++++------ src/compiler/grammar.cpp | 12 ++++----- src/compiler/lex_table.cpp | 19 ++++++-------- src/compiler/parse_table.cpp | 25 ++++++++----------- .../prepare_grammar/expand_repeats.cpp | 10 ++++---- .../prepare_grammar/extract_tokens.cpp | 12 ++++----- src/compiler/prepare_grammar/perform.cpp | 4 +-- .../prepare_grammar/search_for_symbols.cpp | 18 +++++++------ 11 files changed, 66 insertions(+), 67 deletions(-) diff --git a/src/compiler/build_tables/first_set.cpp b/src/compiler/build_tables/first_set.cpp index 63ee4ef4..440725ab 100644 --- a/src/compiler/build_tables/first_set.cpp +++ b/src/compiler/build_tables/first_set.cpp @@ -48,7 +48,7 @@ namespace tree_sitter { } }; - set first_set(const rules::rule_ptr &rule, const Grammar &grammar) { + set first_set(const rule_ptr &rule, const Grammar &grammar) { return FirstSetVisitor::apply(rule, grammar); } } diff --git a/src/compiler/build_tables/item.cpp b/src/compiler/build_tables/item.cpp index d15a2096..e3dcd5fb 100644 --- a/src/compiler/build_tables/item.cpp +++ b/src/compiler/build_tables/item.cpp @@ -8,9 +8,10 @@ namespace tree_sitter { using std::ostream; using std::vector; using rules::Symbol; + using rules::rule_ptr; namespace build_tables { - Item::Item(const Symbol &lhs, const rules::rule_ptr rule) : + Item::Item(const Symbol &lhs, const rule_ptr rule) : lhs(lhs), rule(rule) {}; @@ -58,7 +59,7 @@ namespace tree_sitter { return false; } - LexItem::LexItem(const Symbol &lhs, const rules::rule_ptr rule) : Item(lhs, rule) {} + LexItem::LexItem(const Symbol &lhs, const rule_ptr rule) : Item(lhs, rule) {} bool LexItem::operator==(const LexItem &other) const { bool lhs_eq = other.lhs == lhs; @@ -66,7 +67,7 @@ namespace tree_sitter { return lhs_eq && rules_eq; } - ParseItem::ParseItem(const Symbol &lhs, const rules::rule_ptr rule, const vector &consumed_symbols, const rules::Symbol &lookahead_sym) : + ParseItem::ParseItem(const Symbol &lhs, const rule_ptr rule, const vector &consumed_symbols, const Symbol &lookahead_sym) : Item(lhs, rule), consumed_symbols(consumed_symbols), lookahead_sym(lookahead_sym) {} diff --git a/src/compiler/build_tables/perform.cpp b/src/compiler/build_tables/perform.cpp index 5685e6c2..65314d95 100644 --- a/src/compiler/build_tables/perform.cpp +++ b/src/compiler/build_tables/perform.cpp @@ -9,11 +9,13 @@ namespace tree_sitter { using std::pair; using std::string; using std::map; + using rules::Symbol; + using rules::CharacterSet; namespace build_tables { static int NOT_FOUND = -1; - static rules::Symbol START("start", true); - static rules::Symbol END_OF_INPUT("end", true); + static Symbol START("start", true); + static Symbol END_OF_INPUT("end", true); class TableBuilder { const Grammar grammar; @@ -35,7 +37,7 @@ namespace tree_sitter { void add_shift_actions(const ParseItemSet &item_set, size_t state_index) { for (auto transition : sym_transitions(item_set, grammar)) { - rules::Symbol symbol = transition.first; + Symbol symbol = transition.first; ParseItemSet item_set = transition.second; size_t new_state_index = add_parse_state(item_set); parse_table.add_action(state_index, symbol, ParseAction::Shift(new_state_index)); @@ -44,7 +46,7 @@ namespace tree_sitter { void add_advance_actions(const LexItemSet &item_set, size_t state_index) { for (auto transition : char_transitions(item_set, grammar)) { - rules::CharacterSet rule = transition.first; + CharacterSet rule = transition.first; LexItemSet item_set = transition.second; size_t new_state_index = add_lex_state(item_set); lex_table.add_action(state_index, rule, LexAction::Advance(new_state_index)); diff --git a/src/compiler/generate_code/c_code.cpp b/src/compiler/generate_code/c_code.cpp index ad6da173..c6096df8 100644 --- a/src/compiler/generate_code/c_code.cpp +++ b/src/compiler/generate_code/c_code.cpp @@ -3,14 +3,14 @@ #include #include -using std::string; -using std::to_string; -using std::map; -using std::vector; -using std::set; -using std::pair; - namespace tree_sitter { + using std::string; + using std::to_string; + using std::map; + using std::vector; + using std::set; + using std::pair; + namespace generate_code { static void str_replace(string &input, const string &search, const string &replace) { size_t pos = 0; diff --git a/src/compiler/grammar.cpp b/src/compiler/grammar.cpp index 2b565303..d986d9d6 100644 --- a/src/compiler/grammar.cpp +++ b/src/compiler/grammar.cpp @@ -1,12 +1,12 @@ #include "grammar.h" -using std::vector; -using std::string; -using std::pair; -using std::initializer_list; -using std::ostream; - namespace tree_sitter { + using std::vector; + using std::string; + using std::pair; + using std::initializer_list; + using std::ostream; + Grammar::Grammar(const initializer_list> &rules) : rules(rules), start_rule_name(rules.begin()->first) {} diff --git a/src/compiler/lex_table.cpp b/src/compiler/lex_table.cpp index 59333fd7..8a91725f 100644 --- a/src/compiler/lex_table.cpp +++ b/src/compiler/lex_table.cpp @@ -1,14 +1,13 @@ #include "lex_table.h" -using std::string; -using std::to_string; -using std::map; -using std::set; -using tree_sitter::rules::Symbol; -using tree_sitter::rules::CharacterSet; - namespace tree_sitter { - // Action + using std::string; + using std::to_string; + using std::map; + using std::set; + using rules::Symbol; + using rules::CharacterSet; + LexAction::LexAction(LexActionType type, size_t state_index, Symbol symbol) : type(type), state_index(state_index), @@ -52,15 +51,13 @@ namespace tree_sitter { } } - // State set LexState::expected_inputs() const { set result; - for (auto pair : actions) + for (auto &pair : actions) result.insert(pair.first); return result; } - // Table size_t LexTable::add_state() { states.push_back(LexState()); return states.size() - 1; diff --git a/src/compiler/parse_table.cpp b/src/compiler/parse_table.cpp index bc8c82d7..0950c8a5 100644 --- a/src/compiler/parse_table.cpp +++ b/src/compiler/parse_table.cpp @@ -1,15 +1,14 @@ #include "parse_table.h" -using std::string; -using std::ostream; -using std::to_string; -using std::set; -using std::vector; -using tree_sitter::rules::Symbol; - namespace tree_sitter { - // Action - ParseAction::ParseAction(ParseActionType type, size_t state_index, rules::Symbol symbol, const vector &child_flags) : + using std::string; + using std::ostream; + using std::to_string; + using std::set; + using std::vector; + using rules::Symbol; + + ParseAction::ParseAction(ParseActionType type, size_t state_index, Symbol symbol, const vector &child_flags) : type(type), state_index(state_index), symbol(symbol), @@ -59,11 +58,10 @@ namespace tree_sitter { } } - // State ParseState::ParseState() : lex_state_index(-1) {} - set ParseState::expected_inputs() const { - set result; + set ParseState::expected_inputs() const { + set result; for (auto pair : actions) result.insert(pair.first); return result; @@ -88,13 +86,12 @@ namespace tree_sitter { return stream; } - // Table size_t ParseTable::add_state() { states.push_back(ParseState()); return states.size() - 1; } - void ParseTable::add_action(size_t state_index, rules::Symbol symbol, ParseAction action) { + void ParseTable::add_action(size_t state_index, Symbol symbol, ParseAction action) { symbols.insert(symbol); states[state_index].actions[symbol].insert(action); } diff --git a/src/compiler/prepare_grammar/expand_repeats.cpp b/src/compiler/prepare_grammar/expand_repeats.cpp index 9d5051af..3520a54d 100644 --- a/src/compiler/prepare_grammar/expand_repeats.cpp +++ b/src/compiler/prepare_grammar/expand_repeats.cpp @@ -1,12 +1,12 @@ #include "expand_repeats.h" #include -using std::string; -using std::to_string; -using std::map; -using namespace tree_sitter::rules; - namespace tree_sitter { + using std::string; + using std::to_string; + using std::map; + using namespace rules; + namespace prepare_grammar { class RepeatExpander : rules::Visitor { public: diff --git a/src/compiler/prepare_grammar/extract_tokens.cpp b/src/compiler/prepare_grammar/extract_tokens.cpp index 2e9f8f43..d512db72 100644 --- a/src/compiler/prepare_grammar/extract_tokens.cpp +++ b/src/compiler/prepare_grammar/extract_tokens.cpp @@ -2,13 +2,13 @@ #include "search_for_symbols.h" #include -using std::pair; -using std::string; -using std::to_string; -using std::map; -using namespace tree_sitter::rules; - namespace tree_sitter { + using std::pair; + using std::string; + using std::to_string; + using std::map; + using namespace rules; + namespace prepare_grammar { class TokenExtractor : Visitor { public: diff --git a/src/compiler/prepare_grammar/perform.cpp b/src/compiler/prepare_grammar/perform.cpp index 22d1ddfc..5946a884 100644 --- a/src/compiler/prepare_grammar/perform.cpp +++ b/src/compiler/prepare_grammar/perform.cpp @@ -2,9 +2,9 @@ #include "./extract_tokens.h" #include "./expand_repeats.h" -using std::pair; - namespace tree_sitter { + using std::pair; + namespace prepare_grammar { pair perform(const Grammar &input_grammar) { auto grammars = prepare_grammar::extract_tokens(input_grammar); diff --git a/src/compiler/prepare_grammar/search_for_symbols.cpp b/src/compiler/prepare_grammar/search_for_symbols.cpp index 715559ed..eef4c9ff 100644 --- a/src/compiler/prepare_grammar/search_for_symbols.cpp +++ b/src/compiler/prepare_grammar/search_for_symbols.cpp @@ -1,38 +1,40 @@ #include "search_for_symbols.h" namespace tree_sitter { + using namespace rules; + namespace prepare_grammar { - class SymbolSearcher : rules::Visitor { + class SymbolSearcher : Visitor { public: bool value; - bool apply(const rules::rule_ptr rule) { + bool apply(const rule_ptr rule) { rule->accept(*this); return value; } - void default_visit(const rules::Rule *rule) { + void default_visit(const Rule *rule) { value = false; } - void visit(const rules::Symbol *symbol) { + void visit(const Symbol *symbol) { value = true; } - void visit(const rules::Choice *choice) { + void visit(const Choice *choice) { value = apply(choice->left) || apply(choice->right); } - void visit(const rules::Seq *seq) { + void visit(const Seq *seq) { value = apply(seq->left) || apply(seq->right); } - void visit(const rules::Repeat *rule) { + void visit(const Repeat *rule) { value = apply(rule->content); } }; - bool search_for_symbols(const rules::rule_ptr &rule) { + bool search_for_symbols(const rule_ptr &rule) { return SymbolSearcher().apply(rule); } }