From 9a73915f1b7c097ede66685e2f13f70dd1ee3ffc Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 6 May 2014 15:00:19 +0200 Subject: [PATCH] base class for all xml parsers --- alib2/src/sax/FromXMLParser.cpp | 39 +++++++++++++++++++++++++++++++++ alib2/src/sax/FromXMLParser.h | 31 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 alib2/src/sax/FromXMLParser.cpp create mode 100644 alib2/src/sax/FromXMLParser.h diff --git a/alib2/src/sax/FromXMLParser.cpp b/alib2/src/sax/FromXMLParser.cpp new file mode 100644 index 0000000000..782b395a94 --- /dev/null +++ b/alib2/src/sax/FromXMLParser.cpp @@ -0,0 +1,39 @@ +/* + * FromXMLParser.cpp + * + * Created on: Oct 12, 2013 + * Author: Jan Travnicek + */ + +#include "FromXMLParser.h" +#include "ParserException.h" + +namespace sax { + +bool FromXMLParser::isToken(std::list<Token>& input, Token::TokenType type, std::string data) { + return input.front().getType() == type && input.front().getData() == data; +} + +bool FromXMLParser::isTokenType(std::list<Token>& input, Token::TokenType type) { + return input.front().getType() == type; +} + +void FromXMLParser::popToken(std::list<Token>& input, Token::TokenType type, std::string data) { + if (isToken(input, type, data)) { + input.pop_front(); + } else { + throw ParserException(Token(data, type), input.front()); + } +} + +std::string FromXMLParser::popTokenType(std::list<Token>& input, Token::TokenType type) { + if(isTokenType(input, type)) { + std::string result = input.front().getData(); + input.pop_front(); + return std::move(result); + } else { + throw ParserException(Token("?", type), input.front()); + } +} + +} /* namespace sax */ diff --git a/alib2/src/sax/FromXMLParser.h b/alib2/src/sax/FromXMLParser.h new file mode 100644 index 0000000000..d6d55b0b4d --- /dev/null +++ b/alib2/src/sax/FromXMLParser.h @@ -0,0 +1,31 @@ +/* + * FromXMLParser.h + * + * Created on: Oct 12, 2013 + * Author: Jan Travnicek + */ + +#ifndef FROM_XML_PARSER_H_ +#define FROM_XML_PARSER_H_ + +#include <list> +#include "Token.h" + +namespace sax { + +/** + * Parser used to get UnknownAutomaton from XML parsed into list of Tokens. + */ +class FromXMLParser { +protected: + bool isToken(std::list<Token> &input, Token::TokenType type, std::string data); + bool isTokenType(std::list<Token>& input, Token::TokenType type); + void popToken(std::list<Token> &input, Token::TokenType type, std::string data); + std::string popTokenType(std::list<Token>& input, Token::TokenType type); + +}; + +} /* namespace sax */ + +#endif /* FROM_XML_PARSER_H_ */ + -- GitLab