From 608d1666eb7bd582ffa4ab3e7f55e3c9a54f7f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20K=C4=99dzia?= <pawel.kedzia@pwr.wroc.pl> Date: Tue, 22 Feb 2011 19:35:33 +0100 Subject: [PATCH] The parser throws ParserException (ParserException inherits from WcclError) --- libwccl/parser/Parser.cpp | 172 ++++++++++++++++++++++++++++--- libwccl/parser/ParserException.h | 11 +- 2 files changed, 166 insertions(+), 17 deletions(-) diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp index 1a822aa..b7dca25 100644 --- a/libwccl/parser/Parser.cpp +++ b/libwccl/parser/Parser.cpp @@ -36,7 +36,12 @@ boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parseStringOperator(ss); + try { + return this->parseStringOperator(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -50,8 +55,27 @@ boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); + boost::shared_ptr<Operator<StrSet> > res; + + try { + res = parser.parse_strset_operator(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } - return parser.parse_strset_operator(tagset_); + return res; } /** @@ -66,7 +90,12 @@ boost::shared_ptr<Operator<Bool> > Parser::parseBoolOperator( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parseBoolOperator(ss); + try { + return this->parseBoolOperator(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -79,8 +108,27 @@ boost::shared_ptr<Operator<Bool> > Parser::parseBoolOperator( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); + boost::shared_ptr<Operator<Bool> > res; + + try { + res = parser.parse_bool_operator(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } - return parser.parse_bool_operator(tagset_); + return res; } // ---------------------------------------------------------------------------- @@ -96,7 +144,12 @@ boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parseSymSetOperator(ss); + try { + return this->parseSymSetOperator(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -110,7 +163,27 @@ boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); - return parser.parse_symset_operator(tagset_); + boost::shared_ptr<Operator<TSet> > res; + + try { + res = parser.parse_symset_operator(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } + + return res; } // ---------------------------------------------------------------------------- @@ -126,7 +199,12 @@ boost::shared_ptr<Operator<Position> > Parser::parsePositionOperator( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parsePositionOperator(ss); + try { + return this->parsePositionOperator(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -140,7 +218,27 @@ boost::shared_ptr<Operator<Position> > Parser::parsePositionOperator( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); - return parser.parse_position_operator(tagset_); + boost::shared_ptr<Operator<Position> > res; + + try { + res = parser.parse_position_operator(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } + + return res; } /** @@ -304,7 +402,12 @@ boost::shared_ptr<RuleSequence> Parser::parseRuleSequence( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parseRuleSequence(ss); + try { + return this->parseRuleSequence(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -318,7 +421,27 @@ boost::shared_ptr<RuleSequence> Parser::parseRuleSequence( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); - return parser.parse_rule_sequence(tagset_); + boost::shared_ptr<RuleSequence> res; + + try { + res = parser.parse_rule_sequence(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } + + return res; } // ---------------------------------------------------------------------------- @@ -335,7 +458,12 @@ boost::shared_ptr<Rule> Parser::parseSingleRule( std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; - return this->parseSingleRule(ss); + try { + return this->parseSingleRule(ss); + } + catch (ParserException&) { + throw; + } } /** @@ -349,7 +477,27 @@ boost::shared_ptr<Rule> Parser::parseSingleRule( { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); - return parser.parse_single_rule(tagset_); + boost::shared_ptr<Rule> res; + + try { + res = parser.parse_single_rule(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } + + return res; } // ---------------------------------------------------------------------------- diff --git a/libwccl/parser/ParserException.h b/libwccl/parser/ParserException.h index a421a67..d5d0ca2 100644 --- a/libwccl/parser/ParserException.h +++ b/libwccl/parser/ParserException.h @@ -8,12 +8,13 @@ class ParserException : public Wccl::WcclError { public: - ParserException(const std::string); - ~ParserException() throw() { - } + ParserException(const std::string); - // - std::string info() const { return this->msg; } + ~ParserException() throw() { + } + + // + std::string info() const { return this->msg; } private: std::string msg; -- GitLab