#include <libwccl/parser/Parser.h> #include "ANTLRLexer.hpp" #include "ANTLRParser.hpp" #include <antlr/NoViableAltException.hpp> #include <antlr/MismatchedTokenException.hpp> #include <antlr/TokenStreamRecognitionException.hpp> namespace Wccl { /** * @desc Parser constructor. Default tagset is NULL */ Parser::Parser(const Corpus2::Tagset& t) : tagset_(t) { } /** * */ Parser::~Parser() { // } // ---------------------------------------------------------------------------- /** * @desc Parse a string operator contained in a std::string. Converts the string * to a stream and calls parseStringOperator with it * @arg str operator string * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parseStringOperator(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a string operator. Runs parse_string_operator rule * in the parser grammar. * @arg istr input stream with the operator * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator( std::istream& istr) const { 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 res; } /** * @desc Parse predicates contained in a std::string. Converts the string * to a stream and callis parsePredicate with it * @arg str operator string * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<Bool> > Parser::parseBoolOperator( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parseBoolOperator(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a predicate. Runs parse_predicates rule in the parser grammar. * @arg istr input stream with writed predicate * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<Bool> > Parser::parseBoolOperator( std::istream& istr) const { 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 res; } // ---------------------------------------------------------------------------- /** * @desc Parse a sym set operator contained in a std::string. Converts the * string to a stream and calls parseSymSetOperator with it * @arg str operator string * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parseSymSetOperator(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a sym set operator. Runs parse_sym_set_operator rule * in the parser grammar. * @arg istr input stream with the operator * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator( std::istream& istr) const { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); 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; } // ---------------------------------------------------------------------------- /** * @desc Parse a position operator contained in a std::string. Converts the * string to a stream and calls parseSymSetOperator with it * @arg str operator string * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<Position> > Parser::parsePositionOperator( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parsePositionOperator(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a position operator. Runs parse_sym_set_operator rule * in the parser grammar. * @arg istr input stream with the operator * @return the parsed operator via a shared pointer */ boost::shared_ptr<Operator<Position> > Parser::parsePositionOperator( std::istream& istr) const { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); 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; } /** * @desc Parse any operator contained in a std::string. Converts the string to * to a stream and calls parseAnyOperator with it. * @arg str operator string * @return the parsed operator via a shared pointer */ boost::shared_ptr<FunctionalOperator> Parser::parseAnyOperator( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; return this->parseAnyOperator(ss); } /** * @desc Parse any operator. Runs parse_*_operator rules in sequence * in the parser grammar until one succedes, or all fail. Rethrows * the exception returned by the last parse_* attempt. * @arg istr input stream with the operator * @return the parsed operator via a shared pointer */ boost::shared_ptr<FunctionalOperator> Parser::parseAnyOperator( std::istream& istr) const { std::stringstream ss; ss << istr.rdbuf(); std::stringstream errors; boost::shared_ptr<FunctionalOperator> result; if (!result) { ss.clear(); ss.seekg(0, std::ios::beg); ANTLRLexer lexer(ss); ANTLRParser parser(lexer); try { result = parser.parse_symset_operator(tagset_); } catch (antlr::MismatchedTokenException &e) { errors << std::endl << "(as tset ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::NoViableAltException &e) { errors << std::endl << "(as tset ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::TokenStreamRecognitionException &e) { errors << std::endl << "(as tset ) " << e.getLine() << ":" << e.getColumn() << " " << e.getMessage(); } catch (antlr::ANTLRException& e) { errors << std::endl << "(as tset ) " << e.getMessage(); // ignore, try another type } } if (!result) { ss.clear(); ss.seekg(0, std::ios::beg); ANTLRLexer lexer(ss); ANTLRParser parser(lexer); try { result = parser.parse_strset_operator(tagset_); } catch (antlr::MismatchedTokenException &e) { errors << std::endl << "(as strset ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::NoViableAltException &e) { errors << std::endl << "(as strset ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::TokenStreamRecognitionException &e) { errors << std::endl << "(as strset ) " << e.getLine() << ":" << e.getColumn() << " " << e.getMessage(); } catch (antlr::ANTLRException& e) { errors << std::endl << "(as strset ) " << e.getMessage(); // ignore, try another type } } if (!result) { ss.clear(); ss.seekg(0, std::ios::beg); ANTLRLexer lexer(ss); ANTLRParser parser(lexer); try { result = parser.parse_bool_operator(tagset_); } catch (antlr::MismatchedTokenException &e) { errors << std::endl << "(as bool ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::NoViableAltException &e) { errors << std::endl << "(as bool ) " << e.getFileLineColumnString() << " " << e.getMessage(); } catch(antlr::TokenStreamRecognitionException &e) { errors << std::endl << "(as bool ) " << e.getLine() << ":" << e.getColumn() << " " << e.getMessage(); } catch (antlr::ANTLRException& e) { errors << std::endl << "(as bool ) " << e.getMessage(); // ignore, try another type } } if (!result) { ss.clear(); ss.seekg(0, std::ios::beg); ANTLRLexer lexer(ss); ANTLRParser parser(lexer); try { result = parser.parse_position_operator(tagset_); } catch (antlr::MismatchedTokenException &e) { errors << std::endl << "(as position) " << e.getFileLineColumnString() << " " << e.getMessage() << std::endl; } catch(antlr::TokenStreamRecognitionException &e) { errors << std::endl << "(as position) " << e.getLine() << ":" << e.getColumn() << " " << e.getMessage(); } catch(antlr::NoViableAltException &e) { errors << std::endl << "(as position) " << e.getFileLineColumnString() << " " << e.getMessage() << std::endl; } catch (antlr::ANTLRException& e) { errors << std::endl << "(as position) " << e.getMessage() << std::endl; // ignore, try another type } } if (!result) { throw ParserException(errors.str()); } return result; } // ---------------------------------------------------------------------------- /** * @desc Parse rule sequence contained in a std::string. Converts the string * to a stream and calls parseRuleSequence with it * @arg str rules string * @return the parsed rule sequence via a shared pointer */ boost::shared_ptr<RuleSequence> Parser::parseRuleSequence( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parseRuleSequence(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a sequence rules. Runs parse_rule_sequence rule in the parser * grammar. * @arg istr input stream with writed rules * @return the parsed rule sequence via a shared pointer */ boost::shared_ptr<RuleSequence> Parser::parseRuleSequence( std::istream& istr) const { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); 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; } // ---------------------------------------------------------------------------- /** * @desc Parse single rule contained in a std::string. Converts the string * to a stream and calls parseSingleRule with it * @arg str rule * @return the parsed rule via a shared pointer */ boost::shared_ptr<TagRule> Parser::parseSingleRule( const std::string& str) const { std::stringstream ss (std::stringstream::in | std::stringstream::out); ss << str; try { return this->parseSingleRule(ss); } catch (ParserException&) { throw; } } /** * @desc Parse a single rule. Runs parse_single_rule rule in the parser * grammar. * @arg istr input stream with writed rule * @return the parsed rule via a shared pointer */ boost::shared_ptr<TagRule> Parser::parseSingleRule( std::istream& istr) const { ANTLRLexer lexer(istr); ANTLRParser parser(lexer); boost::shared_ptr<TagRule> 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; } // ---------------------------------------------------------------------------- } // end Wccl ns