From f6af1524d991a132074a01b7949fbbb5203f5388 Mon Sep 17 00:00:00 2001
From: ilor <kailoran@gmail.com>
Date: Mon, 29 Nov 2010 12:27:06 +0100
Subject: [PATCH] add parsing of position operators

---
 libwccl/parser/Parser.cpp | 42 +++++++++++++++++++++++++++++++++++++++
 libwccl/parser/Parser.h   |  7 +++++++
 libwccl/parser/grammar.g  | 17 +++++++++++++++-
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp
index 3551d42..389c8a4 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 58ef082..1428c29 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 8518295..4a08f6a 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]
-- 
GitLab