diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp index 3551d42d7b2c6cf9b9c6edda703412e7f6a6ed39..389c8a425eafc3ee1f0f2a5d13b4aaf947ec7c53 100644 --- a/libwccl/parser/Parser.cpp +++ b/libwccl/parser/Parser.cpp @@ -108,6 +108,36 @@ boost::shared_ptr<ANTLRParserResult<Wccl::TSet> > Parser::parseSymSetOperator( 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 @@ -174,6 +204,18 @@ boost::shared_ptr<ANTLRParserResultBase> Parser::parseAnyOperator( // 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()); } diff --git a/libwccl/parser/Parser.h b/libwccl/parser/Parser.h index 58ef0824f37c078a120da705cd35b2cf956f869a..1428c295cd56fb843f0ed0c8c5854c76ea49784c 100644 --- a/libwccl/parser/Parser.h +++ b/libwccl/parser/Parser.h @@ -45,6 +45,13 @@ public: boost::shared_ptr<ANTLRParserResult<Wccl::TSet> > parseSymSetOperator(std::istream&) const; + // --------------------------------------------------------------------------- + // methods for parsing position operators + boost::shared_ptr<ANTLRParserResult<Wccl::Position> > + parsePositionOperator(const std::string&) const; + boost::shared_ptr<ANTLRParserResult<Wccl::Position> > + parsePositionOperator(std::istream&) const; + // --------------------------------------------------------------------------- // methods for parsing any operators boost::shared_ptr<ANTLRParserResultBase> diff --git a/libwccl/parser/grammar.g b/libwccl/parser/grammar.g index 8518295c19eb4fd2035cef73907a6055b424b5e6..4a08f6a4ef23cc1010fa7497c64007a8adef65aa 100644 --- a/libwccl/parser/grammar.g +++ b/libwccl/parser/grammar.g @@ -147,6 +147,21 @@ parse_sym_set_operator } ; +// ---------------------------------------------------------------------------- +// Rules for parsing position operators +// Returns boost::shared_ptr<Wccl::Function<Wccl::Position> > +parse_position_operator + [const Corpus2::Tagset &tagset] + returns [boost::shared_ptr<ANTLRParserResult<Wccl::Position> > res] +{ + res.reset(new ANTLRParserResult<Wccl::Position>()); + boost::shared_ptr<Wccl::Function<Wccl::Position> > op; +} + : op = position_operators [tagset, *res->variables.get()] { + res->op = op; + } +; + /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // VALUES @@ -265,7 +280,7 @@ position_literal val.reset(new Wccl::Position(Wccl::Position(Wccl::Position::Nowhere))); } ; -// Constat position value +// Constant position value // Returns boost::shared_ptr<Wccl::Constant<Wccl::Position> > position_value returns [boost::shared_ptr<Wccl::Constant<Wccl::Position> > val]