Skip to content
Snippets Groups Projects
Select Git revision
  • f7917760f284a799599a9b7bdf5328309f34b554
  • master default protected
  • develop protected
  • feat_remove_attr
  • python2.7
  • python3.8
6 results

token.cpp

Blame
  • Parser.cpp 6.27 KiB
    #include <libwccl/parser/Parser.h>
    #include "ANTLRLexer.hpp"
    #include "ANTLRParser.hpp"
    
    /**
     * @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<ANTLRParserResult<Wccl::StrSet> > Parser::parseStringOperator(
    		const std::string& str) const
    {
    	std::stringstream ss (std::stringstream::in | std::stringstream::out);
    	ss << str;
    
    	return this->parseStringOperator(ss);
    }
    
    /**
     * @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<ANTLRParserResult<Wccl::StrSet> > Parser::parseStringOperator(
    		std::istream& istr) const
    {
    	ANTLRLexer lexer(istr);
    	ANTLRParser parser(lexer);
    
    	return parser.parse_string_operator(tagset_);
    }
    
    // ----------------------------------------------------------------------------
    /**
     * @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<ANTLRParserResult<Wccl::Bool> > Parser::parsePredicate(
    		const std::string& str) const
    {
    	std::stringstream ss (std::stringstream::in | std::stringstream::out);
    	ss << str;
    
    	return this->parsePredicate(ss);
    }
    
    /**
     * @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<ANTLRParserResult<Wccl::Bool> > Parser::parsePredicate(
    		std::istream& istr) const
    {
    	ANTLRLexer lexer(istr);
    	ANTLRParser parser(lexer);
    
    	return parser.parse_predicates(tagset_);
    }
    
    // ----------------------------------------------------------------------------
    /**
     * @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<ANTLRParserResult<Wccl::TSet> > Parser::parseSymSetOperator(
    		const std::string& str) const
    {
    	std::stringstream ss (std::stringstream::in | std::stringstream::out);
    	ss << str;
    
    	return this->parseSymSetOperator(ss);
    }
    
    /**
     * @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<ANTLRParserResult<Wccl::TSet> > Parser::parseSymSetOperator(
    		std::istream& istr) const
    {
    	ANTLRLexer lexer(istr);
    	ANTLRParser parser(lexer);
    	return parser.parse_sym_set_operator(tagset_);
    }
    
    // ----------------------------------------------------------------------------
    /**
     * @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<ANTLRParserResult<Wccl::Position> > Parser::parsePositionOperator(
    		const std::string& str) const
    {
    	std::stringstream ss (std::stringstream::in | std::stringstream::out);
    	ss << str;
    
    	return this->parsePositionOperator(ss);
    }
    
    /**
     * @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<ANTLRParserResult<Wccl::Position> > Parser::parsePositionOperator(
    		std::istream& istr) const
    {
    	ANTLRLexer lexer(istr);
    	ANTLRParser parser(lexer);
    	return parser.parse_position_operator(tagset_);
    }
    
    // ----------------------------------------------------------------------------
    /**
     * @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<ANTLRParserResultBase> 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<ANTLRParserResultBase> Parser::parseAnyOperator(
    		std::istream& istr) const
    {
    	std::stringstream ss;
    	ss << istr.rdbuf();
    	std::stringstream errors;
    	boost::shared_ptr<ANTLRParserResultBase> result;
    	if (!result) {
    		ss.clear();
    		ss.seekg(0, std::ios::beg);
    		ANTLRLexer lexer(ss);
    		ANTLRParser parser(lexer);
    		try {
    			result = parser.parse_sym_set_operator(tagset_);
    		} catch (antlr::ANTLRException& e) {
    			errors << "(as tset) " << e.getMessage() << "\n";
    			// ignore, try another type
    		}
    	}
    	if (!result) {
    		ss.clear();
    		ss.seekg(0, std::ios::beg);
    		ANTLRLexer lexer(ss);
    		ANTLRParser parser(lexer);
    		try {
    			result = parser.parse_string_operator(tagset_);
    		} catch (antlr::ANTLRException& e) {
    			errors << "(as strset) " << e.getMessage() << "\n";
    			// ignore, try another type
    		}
    	}
    	if (!result) {
    		ss.clear();
    		ss.seekg(0, std::ios::beg);
    		ANTLRLexer lexer(ss);
    		ANTLRParser parser(lexer);
    		try {
    			result = parser.parse_predicates(tagset_);
    		} catch (antlr::ANTLRException& e) {
    			errors << "(as predicate) " << e.getMessage() << "\n";
    			// 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::ANTLRException& e) {
    			errors << "(as position) " << e.getMessage() << "\n";
    			// ignore, try another type
    		}
    	}
    	if (!result) {
    		throw ParserException(errors.str());
    	}
    	return result;
    }