Skip to content
Snippets Groups Projects
Parser.cpp 13.6 KiB
Newer Older
#include <libwccl/parser/Parser.h>
#include "ANTLRLexer.hpp"
#include "ANTLRParser.hpp"
rk's avatar
rk committed

#include <antlr/NoViableAltException.hpp>
#include <antlr/MismatchedTokenException.hpp>
#include <antlr/TokenStreamRecognitionException.hpp>
rk's avatar
rk committed
/**
 * @desc Parser constructor. Default tagset is NULL
 */
ilor's avatar
ilor committed
Parser::Parser(const Corpus2::Tagset& t) : tagset_(t)
rk's avatar
rk committed
{
}

/**
 *
 */
Parser::~Parser()
{
	//
}

// ----------------------------------------------------------------------------
/**
ilor's avatar
ilor committed
 * @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
rk's avatar
rk committed
 */
boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator(
		const std::string& str) const
rk's avatar
rk committed
{
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << str;

	try {
		return this->parseStringOperator(ss);
	}
	catch (ParserException&) {
		throw;
	}
rk's avatar
rk committed
}
rk's avatar
rk committed
/**
ilor's avatar
ilor committed
 * @desc Parse a string operator. Runs parse_string_operator rule
ilor's avatar
ilor committed
 * @arg istr input stream with the operator
 * @return the parsed operator via a shared pointer
rk's avatar
rk committed
 */
boost::shared_ptr<Operator<StrSet> > Parser::parseStringOperator(
rk's avatar
rk committed
{
	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());
	}
rk's avatar
rk committed

rk's avatar
rk committed
}

ilor's avatar
ilor committed
 * @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;
	}
ilor's avatar
ilor committed
 * @desc Parse a predicate. Runs parse_predicates rule in the parser grammar.
 * @arg istr input stream with writed predicate
ilor's avatar
ilor committed
 * @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());
	}
// ----------------------------------------------------------------------------
rk's avatar
rk committed
/**
ilor's avatar
ilor committed
 * @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
rk's avatar
rk committed
 */
boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator(
		const std::string& str) const
rk's avatar
rk committed
{
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << str;

	try {
		return this->parseSymSetOperator(ss);
	}
	catch (ParserException&) {
		throw;
	}
rk's avatar
rk committed
}
rk's avatar
rk committed
/**
ilor's avatar
ilor committed
 * @desc Parse a sym set operator. Runs parse_sym_set_operator rule
 *       in the parser grammar.
ilor's avatar
ilor committed
 * @arg istr input stream with the operator
 * @return the parsed operator via a shared pointer
rk's avatar
rk committed
 */
boost::shared_ptr<Operator<TSet> > Parser::parseSymSetOperator(
		std::istream& istr) const
rk's avatar
rk committed
{
	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;
rk's avatar
rk committed
}
ilor's avatar
ilor committed
// ----------------------------------------------------------------------------
/**
 * @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(
ilor's avatar
ilor committed
		const std::string& str) const
{
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << str;

	try {
		return this->parsePositionOperator(ss);
	}
	catch (ParserException&) {
		throw;
	}
ilor's avatar
ilor committed
}

/**
 * @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(
ilor's avatar
ilor committed
		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::stringstream ss;
	ss << istr.rdbuf();
	std::stringstream errors;
	boost::shared_ptr<FunctionalOperator> result;
		ss.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
			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.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
			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.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
			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();
ilor's avatar
ilor committed
	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;
ilor's avatar
ilor committed
		} catch (antlr::ANTLRException& e) {
			errors << std::endl
					<< "(as position) " << e.getMessage() << std::endl;
ilor's avatar
ilor committed
			// ignore, try another type
		}
	}
	if (!result) {
		throw ParserException(errors.str());
	}
// ----------------------------------------------------------------------------

/**
 * @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
 */
Adam Wardynski's avatar
Adam Wardynski committed
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;
	}
Paweł Kędzia's avatar
Paweł Kędzia committed
 * @desc Parse a single rule. Runs parse_single_rule rule in the parser
Paweł Kędzia's avatar
Paweł Kędzia committed
 * @arg istr input stream with writed rule
 * @return the parsed rule via a shared pointer
Adam Wardynski's avatar
Adam Wardynski committed
boost::shared_ptr<TagRule> Parser::parseSingleRule(
		std::istream& istr) const
{
	ANTLRLexer lexer(istr);
	ANTLRParser parser(lexer);
Adam Wardynski's avatar
Adam Wardynski committed
	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;
}

// ----------------------------------------------------------------------------