From 2d5eed1e687dcbda07ad74a6924c6bb9959e6642 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20K=C4=99dzia?= <pawel.kedzia@pwr.wroc.pl>
Date: Fri, 1 Apr 2011 14:13:53 +0200
Subject: [PATCH] Added parseMatchRule to the user Parser. parseMatchRule
 returns parsed expression.

---
 libwccl/parser/Parser.cpp | 52 +++++++++++++++++++++++++++++++++++++++
 libwccl/parser/Parser.h   | 10 ++++++++
 2 files changed, 62 insertions(+)

diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp
index a8bdddf..8a08f26 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 645caba..c062733 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_;
-- 
GitLab