diff --git a/alib2/src/factory/RegExpFactory.cpp b/alib2/src/factory/RegExpFactory.cpp index 8b4943e6c0c5d71e0d448cfad33b687919ceb991..093a77e3a43bdb040df33c0a81acc209664aa88a 100644 --- a/alib2/src/factory/RegExpFactory.cpp +++ b/alib2/src/factory/RegExpFactory.cpp @@ -9,7 +9,7 @@ #include "../sax/SaxInterface.h" #include "RegExpFactory.h" -#include "../regexp/RegExpParser.h" +#include "../regexp/RegExpFromXMLParser.h" namespace regexp { @@ -35,7 +35,7 @@ RegExp RegExpFactory::fromStream(std::istream& in) { } RegExp RegExpFactory::parse(list<sax::Token> tokens) { - RegExpParser parser; + RegExpFromXMLParser parser; return parser.parse(tokens); } diff --git a/alib2/src/regexp/RegExpFromXMLParser.cpp b/alib2/src/regexp/RegExpFromXMLParser.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53c730e5c3d6e72c6a9b27ac542929e7812eb0e0 --- /dev/null +++ b/alib2/src/regexp/RegExpFromXMLParser.cpp @@ -0,0 +1,123 @@ +/* + * RegExpFromXMLParser.cpp + * + * Created on: Nov 23, 2013 + * Author: Martin Zak + */ + +#include "RegExpFromXMLParser.h" +#include "../sax/ParserException.h" + +namespace regexp { + +RegExp RegExpFromXMLParser::parse(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "regexp"); + RegExp regexp; + regexp.setRegExp(parseElement(input)); + popToken(input, sax::Token::END_ELEMENT, "regexp"); + + return regexp; +} + +RegExpElement* RegExpFromXMLParser::parseElement(list<sax::Token>& input) { + if (isToken(input, sax::Token::START_ELEMENT, "symbol")) { + return parseSymbol(input); + } else if (isToken(input, sax::Token::START_ELEMENT, "empty")) { + return parseEmpty(input); + } else if (isToken(input, sax::Token::START_ELEMENT, "epsilon")) { + return parseEpsilon(input); + } else if (isToken(input, sax::Token::START_ELEMENT, "iteration")) { + return parseIteration(input); + } else if (isToken(input, sax::Token::START_ELEMENT, "alternation")) { + return parseAlternation(input); + } else if (isToken(input, sax::Token::START_ELEMENT, "concatenation")) { + return parseConcatenation(input); + } else { + return NULL; + } +} + +Alternation* RegExpFromXMLParser::parseAlternation(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "alternation"); + + Alternation* alternation = new Alternation; + parseContent(input, alternation->getElements()); + + popToken(input, sax::Token::END_ELEMENT, "alternation"); + return alternation; +} + +Concatenation* RegExpFromXMLParser::parseConcatenation(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "concatenation"); + + Concatenation* concatenation = new Concatenation(); + parseContent(input, concatenation->getElements()); + + popToken(input, sax::Token::END_ELEMENT, "concatenation"); + return concatenation; + +} + +Iteration* RegExpFromXMLParser::parseIteration(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "iteration"); + + Iteration* iteration = new Iteration(); + iteration->setElement(parseElement(input)); + + popToken(input, sax::Token::END_ELEMENT, "iteration"); + return iteration; +} + +void RegExpFromXMLParser::parseContent(list<sax::Token>& input, list<RegExpElement*>& elements) { + while (true) { + RegExpElement* element = parseElement(input); + if(!element) return; + elements.push_back(element); + } +} + +RegExpEpsilon* RegExpFromXMLParser::parseEpsilon(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "epsilon"); + + RegExpEpsilon* epsilon = new RegExpEpsilon(); + + popToken(input, sax::Token::END_ELEMENT, "epsilon"); + return epsilon; +} + +RegExpEmpty* RegExpFromXMLParser::parseEmpty(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "empty"); + + RegExpEmpty* empty = new RegExpEmpty(); + + popToken(input, sax::Token::END_ELEMENT, "empty"); + return empty; +} + +RegExpSymbol* RegExpFromXMLParser::parseSymbol(list<sax::Token>& input) { + popToken(input, sax::Token::START_ELEMENT, "symbol"); + + if (input.front().getType() == sax::Token::CHARACTER) { + RegExpSymbol* symbol = new RegExpSymbol(input.front().getData()); + input.pop_front(); + popToken(input, sax::Token::END_ELEMENT, "symbol"); + return symbol; + } else { + throw sax::ParserException(sax::Token("", sax::Token::CHARACTER), input.front()); + } +} + +bool RegExpFromXMLParser::isToken(list<sax::Token>& input, sax::Token::TokenType type, string data) { + return input.front().getType() == type && input.front().getData() == data; +} + +void RegExpFromXMLParser::popToken(list<sax::Token>& input, sax::Token::TokenType type, string data) { + if (isToken(input, type, data)) { + input.pop_front(); + } else { + throw sax::ParserException(sax::Token(data, type), input.front()); + } +} + +} /* namespace regexp */ + diff --git a/alib2/src/regexp/RegExpFromXMLParser.h b/alib2/src/regexp/RegExpFromXMLParser.h new file mode 100644 index 0000000000000000000000000000000000000000..20ce98fe8270ea50a8970b8d44620bd0371e7d71 --- /dev/null +++ b/alib2/src/regexp/RegExpFromXMLParser.h @@ -0,0 +1,44 @@ +/* + * RegExpFromXMLParser.h + * + * Created on: Nov 23, 2013 + * Author: martin + */ + +#ifndef REG_EXP_FROM_XML_PARSER_H_ +#define REG_EXP_FROM_XML_PARSER_H_ + +#include "RegExp.h" +#include "RegExpElements.h" +#include "../sax/Token.h" + +namespace regexp { + +/** + * Parser used to get RegExp from XML parsed into list of tokens. + */ +class RegExpFromXMLParser { + void parseContent(list<sax::Token>& input, list<RegExpElement*>& elements); + RegExpElement* parseElement(list<sax::Token>& input); + + RegExpEpsilon* parseEpsilon(list<sax::Token>& input); + RegExpEmpty* parseEmpty(list<sax::Token>& input); + RegExpSymbol* parseSymbol(list<sax::Token> &input); + Iteration* parseIteration(list<sax::Token> &input); + Alternation* parseAlternation(list<sax::Token> &input); + Concatenation* parseConcatenation(list<sax::Token> &input); + bool isToken(list<sax::Token> &input, sax::Token::TokenType type, string data); + void popToken(list<sax::Token> &input, sax::Token::TokenType type, string data); + +public: + /** + * Parses the XML and returns regular expression. The input is destroyed in the process. + * @param input XML represented as list of tokens + * @return RegExp + * @throws ParserException when an error occurs + */ + RegExp parse(list<sax::Token>& input); +}; + +} /* namespace regexp */ +#endif /* REG_EXP_FROM_PARSER_H_ */ diff --git a/alib2/src/regexp/RegExpParser.cpp b/alib2/src/regexp/RegExpParser.cpp deleted file mode 100644 index 5bcad371450a35c23584ebbfc6d5e26ca3c80e05..0000000000000000000000000000000000000000 --- a/alib2/src/regexp/RegExpParser.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * RegExpParser.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "RegExpParser.h" -#include "../sax/ParserException.h" - -namespace regexp { - -RegExp RegExpParser::parse(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "regexp"); - RegExp regexp; - regexp.setRegExp(parseElement(input)); - popToken(input, Token::END_ELEMENT, "regexp"); - - return regexp; -} - -RegExpElement* RegExpParser::parseElement(list<Token>& input) { - if (isToken(input, Token::START_ELEMENT, "symbol")) { - return parseSymbol(input); - } else if (isToken(input, Token::START_ELEMENT, "empty")) { - return parseEmpty(input); - } else if (isToken(input, Token::START_ELEMENT, "epsilon")) { - return parseEpsilon(input); - } else if (isToken(input, Token::START_ELEMENT, "iteration")) { - return parseIteration(input); - } else if (isToken(input, Token::START_ELEMENT, "alternation")) { - return parseAlternation(input); - } else if (isToken(input, Token::START_ELEMENT, "concatenation")) { - return parseConcatenation(input); - } else { - return NULL; - } -} - -Alternation* RegExpParser::parseAlternation(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "alternation"); - - Alternation* alternation = new Alternation; - parseContent(input, alternation->getElements()); - - popToken(input, Token::END_ELEMENT, "alternation"); - return alternation; -} - -Concatenation* RegExpParser::parseConcatenation(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "concatenation"); - - Concatenation* concatenation = new Concatenation(); - parseContent(input, concatenation->getElements()); - - popToken(input, Token::END_ELEMENT, "concatenation"); - return concatenation; - -} - -Iteration* RegExpParser::parseIteration(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "iteration"); - - Iteration* iteration = new Iteration(); - iteration->setElement(parseElement(input)); - - popToken(input, Token::END_ELEMENT, "iteration"); - return iteration; -} - -void RegExpParser::parseContent(list<Token>& input, list<RegExpElement*>& elements) { - while (true) { - RegExpElement* element = parseElement(input); - if(!element) return; - elements.push_back(element); - } -} - -RegExpEpsilon* RegExpParser::parseEpsilon(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "epsilon"); - - RegExpEpsilon* epsilon = new RegExpEpsilon(); - - popToken(input, Token::END_ELEMENT, "epsilon"); - return epsilon; -} - -RegExpEmpty* RegExpParser::parseEmpty(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "empty"); - - RegExpEmpty* empty = new RegExpEmpty(); - - popToken(input, Token::END_ELEMENT, "empty"); - return empty; -} - -RegExpSymbol* RegExpParser::parseSymbol(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "symbol"); - - if (input.front().getType() == Token::CHARACTER) { - RegExpSymbol* symbol = new RegExpSymbol(input.front().getData()); - input.pop_front(); - popToken(input, Token::END_ELEMENT, "symbol"); - return symbol; - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } -} - -bool RegExpParser::isToken(list<Token>& input, Token::TokenType type, string data) { - return input.front().getType() == type && input.front().getData() == data; -} - -void RegExpParser::popToken(list<Token>& input, Token::TokenType type, string data) { - if (isToken(input, type, data)) { - input.pop_front(); - } else { - throw ParserException(Token(data, type), input.front()); - } -} - -} /* namespace regexp */ - diff --git a/alib2/src/regexp/RegExpParser.h b/alib2/src/regexp/RegExpParser.h deleted file mode 100644 index 654d3fe719c1f1a533f1cf929b27d879356c12a9..0000000000000000000000000000000000000000 --- a/alib2/src/regexp/RegExpParser.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * RegExpParser.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXPPARSER_H_ -#define REGEXPPARSER_H_ - -#include "RegExp.h" -#include "RegExpElements.h" -#include "../sax/Token.h" - - -namespace regexp { - -using namespace sax; - -/** - * Parser used to get RegExp from XML parsed into list of tokens. - */ -class RegExpParser { -protected: - static void parseContent(list<Token>& input, list<RegExpElement*>& elements); - static RegExpElement* parseElement(list<Token>& input); - - static RegExpEpsilon* parseEpsilon(list<Token>& input); - static RegExpEmpty* parseEmpty(list<Token>& input); - static RegExpSymbol* parseSymbol(list<Token> &input); - static Iteration* parseIteration(list<Token> &input); - static Alternation* parseAlternation(list<Token> &input); - static Concatenation* parseConcatenation(list<Token> &input); - static bool isToken(list<Token> &input, Token::TokenType type, string data); - static void popToken(list<Token> &input, Token::TokenType type, string data); - -public: - /** - * Parses the XML and returns regular expression. The input is destroyed in the process. - * @param input XML represented as list of tokens - * @return RegExp - * @throws ParserException when an error occurs - */ - static RegExp parse(list<Token>& input); -}; - -} /* namespace regexp */ -#endif /* REGEXPPARSER_H_ */