From 04d18b56ed61ba0e2daf49b156c64564e45d1e8e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 4 Jan 2014 15:30:05 -0800 Subject: [PATCH] Cleanup --- spec/compiler/generate_parsers.cpp | 1 + spec/compiler/lr/item_spec.cpp | 2 ++ spec/compiler/lr/table_builder_spec.cpp | 1 + spec/compiler/spec_helper.h | 17 ++++++----------- src/compiler/grammar/grammar.cpp | 15 ++++++++++----- src/compiler/lr/item.cpp | 18 ++++++++++-------- src/compiler/lr/lex_table.cpp | 3 --- src/compiler/lr/lex_table.h | 1 - src/compiler/lr/parse_table.cpp | 10 ++++------ src/compiler/lr/table_builder.cpp | 13 +++++-------- 10 files changed, 39 insertions(+), 42 deletions(-) diff --git a/spec/compiler/generate_parsers.cpp b/spec/compiler/generate_parsers.cpp index 99843d41..d26744cc 100644 --- a/spec/compiler/generate_parsers.cpp +++ b/spec/compiler/generate_parsers.cpp @@ -3,6 +3,7 @@ #include "parse_table.h" #include "prepare_grammar.h" #include "c_code.h" +#include "../fixtures/grammars/arithmetic.h" #include START_TEST diff --git a/spec/compiler/lr/item_spec.cpp b/spec/compiler/lr/item_spec.cpp index 903950ce..9b1df071 100644 --- a/spec/compiler/lr/item_spec.cpp +++ b/spec/compiler/lr/item_spec.cpp @@ -1,4 +1,6 @@ #include "spec_helper.h" +#include "item.h" +#include "../../fixtures/grammars/arithmetic.h" using namespace tree_sitter::lr; diff --git a/spec/compiler/lr/table_builder_spec.cpp b/spec/compiler/lr/table_builder_spec.cpp index 10a8c9a2..4087b461 100644 --- a/spec/compiler/lr/table_builder_spec.cpp +++ b/spec/compiler/lr/table_builder_spec.cpp @@ -1,5 +1,6 @@ #include "spec_helper.h" #include +#include "table_builder.h" using namespace tree_sitter::lr; using namespace tree_sitter::rules; diff --git a/spec/compiler/spec_helper.h b/spec/compiler/spec_helper.h index 548f4df3..9462927b 100644 --- a/spec/compiler/spec_helper.h +++ b/spec/compiler/spec_helper.h @@ -3,14 +3,9 @@ #include "bandit/bandit.h" #include -#include "transition_map.h" -#include "rules.h" -#include "item.h" -#include "item_set.h" +#include +#include #include "grammar.h" -#include "parse_table.h" -#include "table_builder.h" -#include "../fixtures/grammars/arithmetic.h" using namespace tree_sitter; using namespace std; @@ -21,7 +16,7 @@ using namespace bandit; namespace std { template - inline std::ostream& operator<<(std::ostream &stream, const unordered_set &set) { + inline ostream& operator<<(ostream &stream, const unordered_set &set) { stream << string("# - inline std::ostream& operator<<(std::ostream &stream, const unordered_map &map) { + inline ostream& operator<<(ostream &stream, const unordered_map &map) { stream << string("# struct Stringizer> { - static std::string ToString(const EqualsPointerConstraint& constraint) { - std::ostringstream builder; + static string ToString(const EqualsPointerConstraint& constraint) { + ostringstream builder; builder << "pointer to " << snowhouse::Stringize(constraint.expected); return builder.str(); } diff --git a/src/compiler/grammar/grammar.cpp b/src/compiler/grammar/grammar.cpp index de3afa59..ca1ad9e7 100644 --- a/src/compiler/grammar/grammar.cpp +++ b/src/compiler/grammar/grammar.cpp @@ -1,18 +1,23 @@ #include "grammar.h" -using namespace std; +using std::unordered_map; +using std::vector; +using std::string; +using std::pair; +using std::initializer_list; +using std::ostream; namespace tree_sitter { - Grammar::Grammar(const std::initializer_list> &rules) : + Grammar::Grammar(const initializer_list> &rules) : rules(rules), start_rule_name(rules.begin()->first) {} - Grammar::Grammar(std::string start_rule_name, const std::unordered_map &rules) : + Grammar::Grammar(std::string start_rule_name, const unordered_map &rules) : rules(rules), start_rule_name(start_rule_name) {} - const rules::rule_ptr Grammar::rule(const std::string &name) const { + const rules::rule_ptr Grammar::rule(const string &name) const { auto iter = rules.find(name); return (iter == rules.end()) ? rules::rule_ptr(nullptr) : @@ -42,7 +47,7 @@ namespace tree_sitter { return rules.find(symbol.name) != rules.end(); } - std::ostream& operator<<(std::ostream &stream, const Grammar &grammar) { + ostream& operator<<(ostream &stream, const Grammar &grammar) { stream << string("# - -using namespace std; +using std::string; +using std::vector; +using std::dynamic_pointer_cast; +using std::make_shared; +using std::ostream; namespace tree_sitter { namespace lr { - Item::Item(const std::string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) : + Item::Item(const string &rule_name, const rules::rule_ptr rule, int consumed_sym_count) : rule_name(rule_name), rule(rule), consumed_sym_count(consumed_sym_count) {}; - Item Item::at_beginning_of_rule(const std::string &rule_name, const Grammar &grammar) { + Item Item::at_beginning_of_rule(const string &rule_name, const Grammar &grammar) { return Item(rule_name, grammar.rule(rule_name), 0); } - Item Item::at_beginning_of_token(const std::string &rule_name, const Grammar &grammar) { + Item Item::at_beginning_of_token(const string &rule_name, const Grammar &grammar) { return Item(rule_name, grammar.rule(rule_name), -1); } transition_map Item::transitions() const { return lr::transitions(rule).map([&](rules::rule_ptr to_rule) -> item_ptr { int next_sym_count = (consumed_sym_count == -1) ? -1 : (consumed_sym_count + 1); - return std::make_shared(rule_name, to_rule, next_sym_count); + return make_shared(rule_name, to_rule, next_sym_count); }); }; @@ -51,7 +53,7 @@ namespace tree_sitter { return false; } - std::ostream& operator<<(ostream &stream, const Item &item) { + ostream& operator<<(ostream &stream, const Item &item) { return stream << string("#>()) {} - // Table size_t LexTable::add_state() { states.push_back(LexState()); diff --git a/src/compiler/lr/lex_table.h b/src/compiler/lr/lex_table.h index 5982af0e..e7f4295b 100644 --- a/src/compiler/lr/lex_table.h +++ b/src/compiler/lr/lex_table.h @@ -48,7 +48,6 @@ namespace tree_sitter { class LexState { public: - LexState(); std::unordered_map> actions; std::unordered_set default_actions; }; diff --git a/src/compiler/lr/parse_table.cpp b/src/compiler/lr/parse_table.cpp index 3e2d8c2d..1b10a753 100644 --- a/src/compiler/lr/parse_table.cpp +++ b/src/compiler/lr/parse_table.cpp @@ -1,6 +1,8 @@ #include "parse_table.h" -using namespace std; +using std::string; +using std::ostream; +using std::to_string; namespace tree_sitter { namespace lr { @@ -48,11 +50,7 @@ namespace tree_sitter { } // State - ParseState::ParseState() : - actions(unordered_map>()), - default_actions(unordered_set()), - lex_state_index(-1) - {} + ParseState::ParseState() : lex_state_index(-1) {} // Table size_t ParseTable::add_state() { diff --git a/src/compiler/lr/table_builder.cpp b/src/compiler/lr/table_builder.cpp index caf5cf60..77ac31cd 100644 --- a/src/compiler/lr/table_builder.cpp +++ b/src/compiler/lr/table_builder.cpp @@ -1,13 +1,10 @@ #include "table_builder.h" -#include #include "item_set.h" #include "rules.h" -#include "item_set.h" #include "grammar.h" -#include - -using namespace std; +using std::pair; +using std::vector; namespace tree_sitter { namespace lr { @@ -109,15 +106,15 @@ namespace tree_sitter { grammar(grammar), lex_grammar(lex_grammar) {}; - std::pair build() { + pair build() { auto item = Item(ParseTable::START, rules::sym(grammar.start_rule_name), 0); auto item_set = ItemSet(item, grammar); add_parse_state(item_set); - return std::pair(parse_table, lex_table); + return pair(parse_table, lex_table); } }; - std::pair build_tables(const Grammar &grammar, const Grammar &lex_grammar) { + pair build_tables(const Grammar &grammar, const Grammar &lex_grammar) { return TableBuilder(grammar, lex_grammar).build(); } }