From e6f3239bef784185482294071d3b8e0392e3f492 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 10 Sep 2015 10:12:03 -0700 Subject: [PATCH] Move stream operator definitions to spec helpers This is one less thing for users to worry about when compiling and linking the library itself --- include/tree_sitter/compiler.h | 4 - spec/compiler/helpers/stream_methods.cc | 122 ++++++++++++++++++++++ spec/compiler/helpers/stream_methods.h | 30 +++++- src/compiler/build_tables/get_metadata.cc | 8 -- src/compiler/build_tables/get_metadata.h | 2 - src/compiler/build_tables/lex_item.cc | 5 - src/compiler/build_tables/lex_item.h | 2 - src/compiler/build_tables/parse_item.cc | 5 - src/compiler/build_tables/parse_item.h | 2 - src/compiler/grammar.cc | 22 ---- src/compiler/lex_table.cc | 15 --- src/compiler/lex_table.h | 2 - src/compiler/parse_table.cc | 39 ------- src/compiler/parse_table.h | 4 - src/compiler/rule.cc | 12 --- src/compiler/rule.h | 3 - src/compiler/rules/character_range.cc | 4 - 17 files changed, 147 insertions(+), 134 deletions(-) create mode 100644 spec/compiler/helpers/stream_methods.cc diff --git a/include/tree_sitter/compiler.h b/include/tree_sitter/compiler.h index 5534e47c..6f3d47a5 100644 --- a/include/tree_sitter/compiler.h +++ b/include/tree_sitter/compiler.h @@ -62,10 +62,6 @@ class GrammarError { std::pair compile(const Grammar &, std::string); -std::ostream &operator<<(std::ostream &stream, const Grammar &grammar); -std::ostream &operator<<(std::ostream &stream, const GrammarError *error); -std::ostream &operator<<(std::ostream &stream, const rule_ptr &rule); - } // namespace tree_sitter #endif // TREE_SITTER_COMPILER_H_ diff --git a/spec/compiler/helpers/stream_methods.cc b/spec/compiler/helpers/stream_methods.cc new file mode 100644 index 00000000..46cde8dc --- /dev/null +++ b/spec/compiler/helpers/stream_methods.cc @@ -0,0 +1,122 @@ +#include "compiler/helpers/stream_methods.h" +#include "compiler/compiler_spec_helper.h" +#include "tree_sitter/compiler.h" +#include "compiler/parse_table.h" +#include "compiler/build_tables/parse_item.h" +#include "compiler/build_tables/lex_item.h" +#include "compiler/build_tables/get_metadata.h" + +namespace tree_sitter { + +ostream &operator<<(ostream &stream, const Grammar &grammar) { + stream << string("# "); + stream << pair.second; + started = true; + } + return stream << string("}>"); +} + +ostream &operator<<(ostream &stream, const GrammarError *error) { + if (error) + return stream << (string("#message + "'>"); + else + return stream << string("#"); +} + +ostream &operator<<(ostream &stream, const Rule &rule) { + return stream << rule.to_string(); +} + +ostream &operator<<(ostream &stream, const rule_ptr &rule) { + if (rule.get()) + stream << *rule; + else + stream << string("(null-rule)"); + return stream; +} + +ostream &operator<<(ostream &stream, const RuleEntry &entry) { + return stream << string("{") << entry.name << string(", ") << entry.rule << string(", ") << to_string(entry.type) << string("}"); +} + +std::ostream &operator<<(std::ostream &stream, const LexAction &action) { + switch (action.type) { + case LexActionTypeError: + return stream << string("#"); + case LexActionTypeAccept: + return stream << string("#"; + case LexActionTypeAdvance: + return stream << string("#"; + default: + return stream; + } +} + +ostream &operator<<(ostream &stream, const ParseAction &action) { + switch (action.type) { + case ParseActionTypeError: + return stream << string("#"); + case ParseActionTypeAccept: + return stream << string("#"); + case ParseActionTypeShift: + return stream << (string("#"); + case ParseActionTypeShiftExtra: + return stream << string("#"); + case ParseActionTypeReduce: + return stream << ("#"); + default: + return stream; + } +} + +ostream &operator<<(ostream &stream, const ParseState &state) { + stream << string("# {"); + for (auto &action : pair.second) { + stream << string(" ") << action; + } + stream << string("}"); + started = true; + } + stream << string(">"); + return stream; +} + +namespace build_tables { + +ostream &operator<<(ostream &stream, const build_tables::LexItem &item) { + return stream << string("(item ") << item.lhs << string(" ") << *item.rule + << string(")"); +} + +ostream &operator<<(ostream &stream, const build_tables::ParseItem &item) { + return stream << string("(item ") << item.lhs << string(" ") << *item.rule + << string(")"); +} + +std::ostream &operator<<(std::ostream &stream, const MetadataRange &range) { + return stream << string("{") << to_string(range.min) << string(", ") + << to_string(range.max) << string("}"); +} + +} // namespace build_tables + +} // namespace tree_sitter diff --git a/spec/compiler/helpers/stream_methods.h b/spec/compiler/helpers/stream_methods.h index 54c39fec..d7d6cb41 100644 --- a/spec/compiler/helpers/stream_methods.h +++ b/spec/compiler/helpers/stream_methods.h @@ -7,7 +7,7 @@ #include #include #include -#include "compiler/prepared_grammar.h" +#include "tree_sitter/compiler.h" using std::cout; @@ -89,11 +89,31 @@ namespace tree_sitter { using std::ostream; using std::string; using std::to_string; +struct RuleEntry; +class LexAction; +class ParseAction; +class ParseState; -inline ostream &operator<<(ostream &stream, const RuleEntry &entry) { - return stream << string("{") << entry.name << string(", ") << entry.rule << string(", ") << to_string(entry.type) << string("}"); -} +ostream &operator<<(ostream &, const Grammar &); +ostream &operator<<(ostream &, const GrammarError &); +ostream &operator<<(ostream &, const Rule &); +ostream &operator<<(ostream &, const rule_ptr &); +ostream &operator<<(ostream &, const RuleEntry &); +std::ostream &operator<<(ostream &stream, const LexAction &); +std::ostream &operator<<(ostream &stream, const ParseAction &); +std::ostream &operator<<(ostream &stream, const ParseState &); -} +namespace build_tables { + +struct MetadataRange; +class LexItem; +class ParseItem; + +ostream &operator<<(ostream &stream, const MetadataRange &); +ostream &operator<<(ostream &stream, const LexItem &); +ostream &operator<<(ostream &stream, const ParseItem &); + +} // namespace build_tables +} // namespace tree_sitter #endif diff --git a/src/compiler/build_tables/get_metadata.cc b/src/compiler/build_tables/get_metadata.cc index 11f60349..1adf4f89 100644 --- a/src/compiler/build_tables/get_metadata.cc +++ b/src/compiler/build_tables/get_metadata.cc @@ -1,6 +1,5 @@ #include "compiler/build_tables/get_metadata.h" #include -#include #include "compiler/rules/visitor.h" #include "compiler/rules/seq.h" #include "compiler/rules/repeat.h" @@ -11,13 +10,6 @@ namespace tree_sitter { namespace build_tables { using std::pair; -using std::string; -using std::to_string; - -std::ostream &operator<<(std::ostream &stream, const MetadataRange &range) { - return stream << string("{") << to_string(range.min) << string(", ") - << to_string(range.max) << string("}"); -} MetadataRange get_metadata(const rule_ptr &rule, rules::MetadataKey key) { class GetMetadata : public rules::RuleFn> { diff --git a/src/compiler/build_tables/get_metadata.h b/src/compiler/build_tables/get_metadata.h index fa3ecd02..a30a4915 100644 --- a/src/compiler/build_tables/get_metadata.h +++ b/src/compiler/build_tables/get_metadata.h @@ -19,8 +19,6 @@ struct MetadataRange { int max; }; -std::ostream &operator<<(std::ostream &stream, const MetadataRange &range); - MetadataRange get_metadata(const rule_ptr &, rules::MetadataKey); } // namespace build_tables diff --git a/src/compiler/build_tables/lex_item.cc b/src/compiler/build_tables/lex_item.cc index 9db5a03d..2b523651 100644 --- a/src/compiler/build_tables/lex_item.cc +++ b/src/compiler/build_tables/lex_item.cc @@ -22,10 +22,5 @@ bool LexItem::is_token_start() const { return get_metadata(rule, rules::START_TOKEN).max > 0; } -ostream &operator<<(ostream &stream, const LexItem &item) { - return stream << string("(item ") << item.lhs << string(" ") << *item.rule - << string(")"); -} - } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/lex_item.h b/src/compiler/build_tables/lex_item.h index 1de0f886..2a9724c4 100644 --- a/src/compiler/build_tables/lex_item.h +++ b/src/compiler/build_tables/lex_item.h @@ -15,8 +15,6 @@ class LexItem : public Item { bool is_token_start() const; }; -std::ostream &operator<<(std::ostream &stream, const LexItem &item); - typedef std::unordered_set LexItemSet; } // namespace build_tables diff --git a/src/compiler/build_tables/parse_item.cc b/src/compiler/build_tables/parse_item.cc index 61b95361..7c215b0a 100644 --- a/src/compiler/build_tables/parse_item.cc +++ b/src/compiler/build_tables/parse_item.cc @@ -30,10 +30,5 @@ bool ParseItem::operator<(const ParseItem &other) const { return rule < other.rule; } -ostream &operator<<(ostream &stream, const ParseItem &item) { - return stream << string("(item ") << item.lhs << string(" ") << *item.rule - << string(")"); -} - } // namespace build_tables } // namespace tree_sitter diff --git a/src/compiler/build_tables/parse_item.h b/src/compiler/build_tables/parse_item.h index 45a3daed..f2f31b8e 100644 --- a/src/compiler/build_tables/parse_item.h +++ b/src/compiler/build_tables/parse_item.h @@ -19,8 +19,6 @@ class ParseItem : public Item { std::vector consumed_symbols; }; -std::ostream &operator<<(std::ostream &stream, const ParseItem &item); - typedef std::map> ParseItemSet; } // namespace build_tables diff --git a/src/compiler/grammar.cc b/src/compiler/grammar.cc index 52e83fe7..50dfcaa0 100644 --- a/src/compiler/grammar.cc +++ b/src/compiler/grammar.cc @@ -33,21 +33,6 @@ Grammar &Grammar::expected_conflicts(const vector> &expected_conf return *this; } -ostream &operator<<(ostream &stream, const Grammar &grammar) { - stream << string("# "); - stream << pair.second; - started = true; - } - return stream << string("}>"); -} - GrammarError::GrammarError(GrammarErrorType type, string message) : type(type), message(message) {} @@ -55,11 +40,4 @@ bool GrammarError::operator==(const GrammarError &other) const { return type == other.type && message == other.message; } -ostream &operator<<(ostream &stream, const GrammarError *error) { - if (error) - return stream << (string("#message + "'>"); - else - return stream << string("#"); -} - } // namespace tree_sitter diff --git a/src/compiler/lex_table.cc b/src/compiler/lex_table.cc index 9fecc716..88e07b7f 100644 --- a/src/compiler/lex_table.cc +++ b/src/compiler/lex_table.cc @@ -41,21 +41,6 @@ bool LexAction::operator==(const LexAction &other) const { (symbol == other.symbol); } -std::ostream &operator<<(std::ostream &stream, const LexAction &action) { - switch (action.type) { - case LexActionTypeError: - return stream << string("#"); - case LexActionTypeAccept: - return stream << string("#"; - case LexActionTypeAdvance: - return stream << string("#"; - default: - return stream; - } -} - LexState::LexState() : is_token_start(false) {} set LexState::expected_inputs() const { diff --git a/src/compiler/lex_table.h b/src/compiler/lex_table.h index f68533c5..8d433598 100644 --- a/src/compiler/lex_table.h +++ b/src/compiler/lex_table.h @@ -33,8 +33,6 @@ class LexAction { std::set precedence_values; }; -std::ostream &operator<<(std::ostream &stream, const LexAction &item); - } // namespace tree_sitter namespace std { diff --git a/src/compiler/parse_table.cc b/src/compiler/parse_table.cc index 9890ff71..ef1cb1b7 100644 --- a/src/compiler/parse_table.cc +++ b/src/compiler/parse_table.cc @@ -90,28 +90,6 @@ bool ParseAction::operator<(const ParseAction &other) const { return consumed_symbol_count < other.consumed_symbol_count; } -ostream &operator<<(ostream &stream, const ParseAction &action) { - switch (action.type) { - case ParseActionTypeError: - return stream << string("#"); - case ParseActionTypeAccept: - return stream << string("#"); - case ParseActionTypeShift: - return stream << (string("#"); - case ParseActionTypeShiftExtra: - return stream << string("#"); - case ParseActionTypeReduce: - return stream << ("#"); - default: - return stream; - } -} - ParseState::ParseState() : lex_state_id(-1) {} set ParseState::expected_inputs() const { @@ -121,23 +99,6 @@ set ParseState::expected_inputs() const { return result; } -ostream &operator<<(ostream &stream, const ParseState &state) { - stream << string("# {"); - for (auto &action : pair.second) { - stream << string(" ") << action; - } - stream << string("}"); - started = true; - } - stream << string(">"); - return stream; -} - ParseStateId ParseTable::add_state() { states.push_back(ParseState()); return states.size() - 1; diff --git a/src/compiler/parse_table.h b/src/compiler/parse_table.h index d13de3b6..de492604 100644 --- a/src/compiler/parse_table.h +++ b/src/compiler/parse_table.h @@ -50,8 +50,6 @@ class ParseAction { int production_id; }; -std::ostream &operator<<(std::ostream &stream, const ParseAction &item); - } // namespace tree_sitter namespace std { @@ -79,8 +77,6 @@ class ParseState { LexStateId lex_state_id; }; -std::ostream &operator<<(std::ostream &stream, const ParseState &state); - class ParseTable { public: ParseStateId add_state(); diff --git a/src/compiler/rule.cc b/src/compiler/rule.cc index 828e26d0..b4617257 100644 --- a/src/compiler/rule.cc +++ b/src/compiler/rule.cc @@ -10,18 +10,6 @@ bool Rule::operator!=(const Rule &other) const { return !this->operator==(other); } -ostream &operator<<(ostream &stream, const Rule &rule) { - return stream << rule.to_string(); -} - -ostream &operator<<(ostream &stream, const rule_ptr &rule) { - if (rule.get()) - stream << *rule; - else - stream << string("(null-rule)"); - return stream; -} - Rule::~Rule() {} } // namespace tree_sitter diff --git a/src/compiler/rule.h b/src/compiler/rule.h index e42dd523..1eb06c1a 100644 --- a/src/compiler/rule.h +++ b/src/compiler/rule.h @@ -24,9 +24,6 @@ class Rule { virtual ~Rule(); }; -std::ostream &operator<<(std::ostream &stream, const Rule &rule); -std::ostream &operator<<(std::ostream &stream, const rule_ptr &rule); - } // namespace tree_sitter namespace std { diff --git a/src/compiler/rules/character_range.cc b/src/compiler/rules/character_range.cc index fb99d0bb..0b6ab7cc 100644 --- a/src/compiler/rules/character_range.cc +++ b/src/compiler/rules/character_range.cc @@ -35,9 +35,5 @@ string CharacterRange::to_string() const { return string() + util::escape_char(min) + "-" + util::escape_char(max); } -ostream &operator<<(ostream &stream, const CharacterRange &range) { - return stream << range.to_string(); -} - } // namespace rules } // namespace tree_sitter