From d46cb5f0ee8fb7a821a5e944c47ce7eecb867a47 Mon Sep 17 00:00:00 2001 From: Martin Zak <zakmart1@fit.cvut.cz> Date: Wed, 25 Dec 2013 11:22:57 +0100 Subject: [PATCH] Adds operator << to automaton, grammar and regexp --- acat/src/acat.cpp | 10 +++++----- alib/src/automaton/Automaton.cpp | 7 +++++++ alib/src/automaton/Automaton.h | 9 ++++++++- alib/src/grammar/Grammar.cpp | 5 +++++ alib/src/grammar/Grammar.h | 7 +++++++ alib/src/grammar/Rule.cpp | 4 ++-- alib/src/grammar/Rule.h | 2 +- alib/src/regexp/RegExp.cpp | 5 +++++ alib/src/regexp/RegExp.h | 7 +++++++ 9 files changed, 47 insertions(+), 9 deletions(-) diff --git a/acat/src/acat.cpp b/acat/src/acat.cpp index f1ab25b513..92ee523d54 100644 --- a/acat/src/acat.cpp +++ b/acat/src/acat.cpp @@ -34,21 +34,21 @@ void getAutomaton(list<Token>& tokens, bool complexTypes) { UnknownAutomaton automaton = AutomatonParser::parse(tokens); if(complexTypes) { - Automaton* concreateAutomaton = AutomatonFactory::buildAutomaton(automaton); - concreateAutomaton->toXML(cout); + Automaton* concreteAutomaton = AutomatonFactory::buildAutomaton(automaton); + cout << *concreteAutomaton; } else { - automaton.toXML(cout); + cout << automaton; } } void getGrammar(list<Token>& tokens) { UnknownGrammar grammar = GrammarParser::parse(tokens); - grammar.toXML(cout); + cout << grammar; } void getRegExp(list<Token>& tokens) { RegExp regexp = RegExpParser::parse(tokens); - regexp.toXML(cout); + cout << regexp; } int main(int argc, char** argv) { diff --git a/alib/src/automaton/Automaton.cpp b/alib/src/automaton/Automaton.cpp index 519b65c7c2..27235c0138 100644 --- a/alib/src/automaton/Automaton.cpp +++ b/alib/src/automaton/Automaton.cpp @@ -77,4 +77,11 @@ const set<State>& Automaton::getFinalStates() const { return finalStates; } +ostream& operator <<(ostream& out, const Automaton& automaton) { + automaton.toXML(out); + return out; +} + } /* namespace automaton */ + + diff --git a/alib/src/automaton/Automaton.h b/alib/src/automaton/Automaton.h index bfbfd4cfb8..7686fe04e9 100644 --- a/alib/src/automaton/Automaton.h +++ b/alib/src/automaton/Automaton.h @@ -111,10 +111,17 @@ public: const set<State>& getFinalStates() const; /** - * Prints XML representation of the automaton. + * Prints XML representation of the automaton to the ostream. * @param out output stream to which print the automaton */ virtual void toXML(ostream& out) const = 0; + + /** + * Prints XML representation of the automaton to the ostream. + * @param out output stream to which print the automaton + * @param automaton automaton to print + */ + friend ostream& operator<<(ostream& out, const Automaton& automaton); }; } /* namespace automaton */ diff --git a/alib/src/grammar/Grammar.cpp b/alib/src/grammar/Grammar.cpp index be42c3f2d1..c1d28daed3 100644 --- a/alib/src/grammar/Grammar.cpp +++ b/alib/src/grammar/Grammar.cpp @@ -114,4 +114,9 @@ void Grammar::toXML(ostream& out) const { GrammarPrinter::toXML(*this, out); } +ostream& operator <<(ostream& out, const Grammar& grammar) { + grammar.toXML(out); + return out; +} + } /* namespace grammar */ diff --git a/alib/src/grammar/Grammar.h b/alib/src/grammar/Grammar.h index a6833a74f7..fd4c2fe856 100644 --- a/alib/src/grammar/Grammar.h +++ b/alib/src/grammar/Grammar.h @@ -116,6 +116,13 @@ public: * @param out output stream to print to */ void toXML(ostream& out) const; + + /** + * Prints the XML representation of grammar to the output stream. + * @param out output stream to print to + * @param grammar grammar to print + */ + friend ostream& operator<<(ostream& out, const Grammar& grammar); }; } /* namespace grammar */ diff --git a/alib/src/grammar/Rule.cpp b/alib/src/grammar/Rule.cpp index 6aa296a25f..1abe676b25 100644 --- a/alib/src/grammar/Rule.cpp +++ b/alib/src/grammar/Rule.cpp @@ -102,7 +102,7 @@ bool Rule::operator !=(const Rule& other) const { || !equal(rightSide.begin(), rightSide.end(), other.rightSide.begin()); } -std::ostream& operator<<(std::ostream& out, const Rule& rule) { +ostream& operator<<(ostream& out, const Rule& rule) { bool first; out << " leftSide = ["; @@ -113,7 +113,7 @@ std::ostream& operator<<(std::ostream& out, const Rule& rule) { out << *iter; } - out << "] rightSize = ["; + out << "] rightSide = ["; first = true; for(list<Symbol>::const_iterator iter = rule.rightSide.begin(); iter != rule.rightSide.end(); iter++) { diff --git a/alib/src/grammar/Rule.h b/alib/src/grammar/Rule.h index ab2235a836..f708aa71f5 100644 --- a/alib/src/grammar/Rule.h +++ b/alib/src/grammar/Rule.h @@ -66,7 +66,7 @@ public: bool operator ==(const Rule& other) const; bool operator !=(const Rule& other) const; - friend std::ostream& operator<<(std::ostream&, const Rule&); + friend ostream& operator<<(ostream&, const Rule&); }; } /* namespace grammar */ diff --git a/alib/src/regexp/RegExp.cpp b/alib/src/regexp/RegExp.cpp index 92641f64ab..6cec78b363 100644 --- a/alib/src/regexp/RegExp.cpp +++ b/alib/src/regexp/RegExp.cpp @@ -70,4 +70,9 @@ void RegExp::toXML(ostream& out) { RegExpPrinter::toXML(*this, out); } +ostream& operator <<(ostream& out, RegExp& regexp) { + regexp.toXML(out); + return out; +} + } /* namespace regexp */ diff --git a/alib/src/regexp/RegExp.h b/alib/src/regexp/RegExp.h index f26f94fbf7..73507c1fa4 100644 --- a/alib/src/regexp/RegExp.h +++ b/alib/src/regexp/RegExp.h @@ -54,6 +54,13 @@ public: * @param out output stream to which print the RegExp */ void toXML(ostream& out); + + /** + * Prints XML representation of the RegExp to the output stream. + * @param out output stream to which print the RegExp + * @param regexp RegExp to print + */ + friend ostream& operator<<(ostream& out, RegExp& regexp); }; } /* namespace regexp */ -- GitLab