From a44bf8658fc6a7a33c5464882fd43830d1f50304 Mon Sep 17 00:00:00 2001
From: ilor <kailoran@gmail.com>
Date: Thu, 5 May 2011 13:20:36 +0200
Subject: [PATCH] add parseWcclFile to Parser

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

diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp
index 0e9643c..7a5703c 100644
--- a/libwccl/parser/Parser.cpp
+++ b/libwccl/parser/Parser.cpp
@@ -554,4 +554,58 @@ boost::shared_ptr<MatchRule> Parser::parseMatchRule(std::istream& istr) const
 	return res;
 }
 
+// ----------------------------------------------------------------------------
+
+/**
+ * @desc Parse an entire WCCL file contained in a std::string.
+ *       Converts the string to a stream and calls parseWcclFile with it
+ * @arg istr input stream with writed rule
+ * @return the parsed file via a shared pointer
+ */
+boost::shared_ptr<WcclFile> Parser::parseWcclFile(
+		const std::string& str) const
+{
+	std::stringstream ss (std::stringstream::in | std::stringstream::out);
+	ss << str;
+
+	try {
+		return this->parseWcclFile(ss);
+	}
+	catch (ParserException&) {
+		throw;
+	}
+}
+
+/**
+ * @desc Parse an entire WCCL file. Runs parse_wccl_file rule from the parser grammar.
+ * @arg istr input stream with writed rule
+ * @return the parsed file via a shared pointer
+ */
+boost::shared_ptr<WcclFile> Parser::parseWcclFile(std::istream& istr) const
+{
+	ANTLRLexer lexer(istr);
+	ANTLRParser parser(lexer);
+	boost::shared_ptr<WcclFile> res;
+
+	try {
+		res = parser.parse_wccl_file(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 cc36272..6873365 100644
--- a/libwccl/parser/Parser.h
+++ b/libwccl/parser/Parser.h
@@ -20,6 +20,9 @@
 // match actions
 #include <libwccl/ops/matchrule.h>
 
+// match actions
+#include <libwccl/wcclfile.h>
+
 // exceptions
 #include <libwccl/parser/ParserException.h>
 
@@ -88,6 +91,13 @@ public:
 	boost::shared_ptr<MatchRule>
 			parseMatchRule(std::istream& is) const;
 
+	// ---------------------------------------------------------------------------
+	// WCCL file parsing
+	boost::shared_ptr<WcclFile>
+			parseWcclFile(const std::string& file_contents_string) const;
+	boost::shared_ptr<WcclFile>
+			parseWcclFile(std::istream& is) const;
+
 	// ---------------------------------------------------------------------------
 	const Corpus2::Tagset& tagset() const {
 		return tagset_;
-- 
GitLab