Newer
Older
#include <libwccl/parser/Parser.h>
#include "ANTLRLexer.hpp"
#include "ANTLRParser.hpp"
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
// ----------------------------------------------------------------------------
/**
* @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
boost::shared_ptr<ANTLRParserResult<Wccl::Bool> > Parser::parsePredicate(
std::istream& istr) const
{
ANTLRLexer lexer(istr);
ANTLRParser parser(lexer);
// ----------------------------------------------------------------------------
* @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
* @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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ----------------------------------------------------------------------------
/**
* @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);
} 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);
} 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);
} catch (antlr::ANTLRException& e) {
errors << "(as predicate) " << e.getMessage() << "\n";
// ignore, try another type
if (!result) {
throw ParserException(errors.str());
}
return result;
}