diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp index a8bdddf4054cd6308a82d17c7525616e681afe1a..8a08f26678461ebc0ec5b844f712428e274002bd 100644 --- a/libwccl/parser/Parser.cpp +++ b/libwccl/parser/Parser.cpp @@ -502,4 +502,56 @@ boost::shared_ptr<TagRule> Parser::parseSingleRule( // ---------------------------------------------------------------------------- +/** + * @desc Parse single match rule contained in a std::string. Converts the string + * to a stream and calls parseMatchRule with it + * @arg istr input stream with writed rule + * @return the parsed rule via a shared pointer + */ +boost::shared_ptr<Expression> Parser::parseMatchRule( + const std::string& str) const +{ + std::stringstream ss (std::stringstream::in | std::stringstream::out); + ss << str; + + try { + return this->parseMatchRule(ss); + } + catch (ParserException&) { + throw; + } +} + +/** + * @desc Parse match rule. Runs parse_match_rule rule from the parser grammar. + * @arg istr input stream with writed rule + * @return the parsed rule via a shared pointer + */ +boost::shared_ptr<Expression> Parser::parseMatchRule(std::istream& istr) const +{ + ANTLRLexer lexer(istr); + ANTLRParser parser(lexer); + boost::shared_ptr<Expression> res; + + try { + res = parser.parse_match_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; +} + } // end Wccl ns diff --git a/libwccl/parser/Parser.h b/libwccl/parser/Parser.h index 645caba176f5d5ec2f9522a1c8f08b4365115d31..c06273348e9b3bcb5d49d0a71e75b5fffb12baf7 100644 --- a/libwccl/parser/Parser.h +++ b/libwccl/parser/Parser.h @@ -17,6 +17,9 @@ // rules #include <libwccl/ops/rulesequence.h> +// match actions +#include <libwccl/ops/match/applyoperator.h> + // exceptions #include <libwccl/parser/ParserException.h> @@ -78,6 +81,13 @@ public: boost::shared_ptr<TagRule> parseSingleRule(std::istream&) const; + // --------------------------------------------------------------------------- + // Parsing match rule from input string + boost::shared_ptr<Expression> + parseMatchRule(const std::string&) const; + boost::shared_ptr<Expression> + parseMatchRule(std::istream&) const; + // --------------------------------------------------------------------------- const Corpus2::Tagset& tagset() const { return tagset_;