Skip to content
Snippets Groups Projects
Parser.cpp 5.01 KiB
Newer Older
#include <libwccl/parser/Parser.h>
#include "ANTLRLexer.hpp"
#include "ANTLRParser.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<ANTLRParserResult<Wccl::StrSet> > Parser::parseStringOperator(
		const std::string& str) const
rk's avatar
rk committed
{
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << str;

	return this->parseStringOperator(ss);
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<ANTLRParserResult<Wccl::StrSet> > Parser::parseStringOperator(
rk's avatar
rk committed
{
	ANTLRLexer lexer(istr);
	ANTLRParser parser(lexer);

ilor's avatar
ilor committed
	return parser.parse_string_operator(tagset_);
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<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);
}

/**
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<ANTLRParserResult<Wccl::Bool> > Parser::parsePredicate(
		std::istream& istr) const
{
	ANTLRLexer lexer(istr);
	ANTLRParser parser(lexer);

ilor's avatar
ilor committed
	return parser.parse_predicates(tagset_);
// ----------------------------------------------------------------------------
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<ANTLRParserResult<Wccl::TSet> > Parser::parseSymSetOperator(
		const std::string& str) const
rk's avatar
rk committed
{
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << str;

	return this->parseSymSetOperator(ss);
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<ANTLRParserResult<Wccl::TSet> > Parser::parseSymSetOperator(
		std::istream& istr) const
rk's avatar
rk committed
{
	ANTLRLexer lexer(istr);
	ANTLRParser parser(lexer);
ilor's avatar
ilor committed
	return parser.parse_sym_set_operator(tagset_);
rk's avatar
rk committed
}

// ----------------------------------------------------------------------------
/**
 * @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.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
ilor's avatar
ilor committed
			result = parser.parse_sym_set_operator(tagset_);
		} catch (antlr::ANTLRException& e) {
			errors << "(as tset) " << e.getMessage() << "\n";
			// ignore, try another type
		}
	}
	if (!result) {
		ss.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
ilor's avatar
ilor committed
			result = parser.parse_string_operator(tagset_);
		} catch (antlr::ANTLRException& e) {
			errors << "(as strset) " << e.getMessage() << "\n";
			// ignore, try another type
		}
	}
	if (!result) {
		ss.seekg(0, std::ios::beg);
		ANTLRLexer lexer(ss);
		ANTLRParser parser(lexer);
ilor's avatar
ilor committed
			result = parser.parse_predicates(tagset_);
		} catch (antlr::ANTLRException& e) {
			errors << "(as predicate) " << e.getMessage() << "\n";
			// ignore, try another type
	if (!result) {
		throw ParserException(errors.str());
	}