Move stream operator definitions to spec helpers

This is one less thing for users to worry about when compiling and linking
the library itself
This commit is contained in:
Max Brunsfeld 2015-09-10 10:12:03 -07:00
parent f5f24a708e
commit e6f3239bef
17 changed files with 147 additions and 134 deletions

View file

@ -62,10 +62,6 @@ class GrammarError {
std::pair<std::string, const GrammarError *> 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_

View file

@ -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("#<grammar");
stream << string(" rules: {");
bool started = false;
for (auto pair : grammar.rules()) {
if (started)
stream << string(", ");
stream << pair.first;
stream << string(" => ");
stream << pair.second;
started = true;
}
return stream << string("}>");
}
ostream &operator<<(ostream &stream, const GrammarError *error) {
if (error)
return stream << (string("#<grammar-error '") + error->message + "'>");
else
return stream << string("#<null>");
}
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("#<error>");
case LexActionTypeAccept:
return stream << string("#<accept ") + to_string(action.symbol.index) +
">";
case LexActionTypeAdvance:
return stream << string("#<advance ") + to_string(action.state_index) +
">";
default:
return stream;
}
}
ostream &operator<<(ostream &stream, const ParseAction &action) {
switch (action.type) {
case ParseActionTypeError:
return stream << string("#<error>");
case ParseActionTypeAccept:
return stream << string("#<accept>");
case ParseActionTypeShift:
return stream << (string("#<shift ") + to_string(action.state_index) +
">");
case ParseActionTypeShiftExtra:
return stream << string("#<shift_extra");
case ParseActionTypeReduceExtra:
return stream << ("#<reduce_extra sym" + to_string(action.symbol.index) +
">");
case ParseActionTypeReduce:
return stream << ("#<reduce sym" + to_string(action.symbol.index) + " " +
to_string(action.consumed_symbol_count) + ">");
default:
return stream;
}
}
ostream &operator<<(ostream &stream, const ParseState &state) {
stream << string("#<parse_state ");
bool started = false;
for (auto pair : state.actions) {
if (started)
stream << string(", ");
stream << pair.first << 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

View file

@ -7,7 +7,7 @@
#include <map>
#include <unordered_set>
#include <vector>
#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

View file

@ -1,6 +1,5 @@
#include "compiler/build_tables/get_metadata.h"
#include <utility>
#include <string>
#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<pair<MetadataRange, bool>> {

View file

@ -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

View file

@ -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

View file

@ -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<LexItem> LexItemSet;
} // namespace build_tables

View file

@ -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

View file

@ -19,8 +19,6 @@ class ParseItem : public Item {
std::vector<rules::Symbol> consumed_symbols;
};
std::ostream &operator<<(std::ostream &stream, const ParseItem &item);
typedef std::map<ParseItem, std::set<rules::Symbol>> ParseItemSet;
} // namespace build_tables

View file

@ -33,21 +33,6 @@ Grammar &Grammar::expected_conflicts(const vector<vector<string>> &expected_conf
return *this;
}
ostream &operator<<(ostream &stream, const Grammar &grammar) {
stream << string("#<grammar");
stream << string(" rules: {");
bool started = false;
for (auto pair : grammar.rules()) {
if (started)
stream << string(", ");
stream << pair.first;
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("#<grammar-error '") + error->message + "'>");
else
return stream << string("#<null>");
}
} // namespace tree_sitter

View file

@ -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("#<error>");
case LexActionTypeAccept:
return stream << string("#<accept ") + to_string(action.symbol.index) +
">";
case LexActionTypeAdvance:
return stream << string("#<advance ") + to_string(action.state_index) +
">";
default:
return stream;
}
}
LexState::LexState() : is_token_start(false) {}
set<CharacterSet> LexState::expected_inputs() const {

View file

@ -33,8 +33,6 @@ class LexAction {
std::set<int> precedence_values;
};
std::ostream &operator<<(std::ostream &stream, const LexAction &item);
} // namespace tree_sitter
namespace std {

View file

@ -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("#<error>");
case ParseActionTypeAccept:
return stream << string("#<accept>");
case ParseActionTypeShift:
return stream << (string("#<shift ") + to_string(action.state_index) +
">");
case ParseActionTypeShiftExtra:
return stream << string("#<shift_extra");
case ParseActionTypeReduceExtra:
return stream << ("#<reduce_extra sym" + to_string(action.symbol.index) +
">");
case ParseActionTypeReduce:
return stream << ("#<reduce sym" + to_string(action.symbol.index) + " " +
to_string(action.consumed_symbol_count) + ">");
default:
return stream;
}
}
ParseState::ParseState() : lex_state_id(-1) {}
set<Symbol> ParseState::expected_inputs() const {
@ -121,23 +99,6 @@ set<Symbol> ParseState::expected_inputs() const {
return result;
}
ostream &operator<<(ostream &stream, const ParseState &state) {
stream << string("#<parse_state ");
bool started = false;
for (auto pair : state.actions) {
if (started)
stream << string(", ");
stream << pair.first << 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;

View file

@ -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();

View file

@ -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

View file

@ -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 {

View file

@ -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