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
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
137
138
139
140
// ----------------------------------------------------------------------------
/**
* @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_);
}
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ----------------------------------------------------------------------------
/**
* @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) {
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;
}