From a99edd7b2bd3531d56226bc98e2743d06f29ed73 Mon Sep 17 00:00:00 2001 From: ilor <kailoran@gmail.com> Date: Mon, 29 Nov 2010 14:18:40 +0100 Subject: [PATCH] "all" positions handling in wcclparser --- wcclparser/main.cpp | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/wcclparser/main.cpp b/wcclparser/main.cpp index aed4a47..7bb1a81 100644 --- a/wcclparser/main.cpp +++ b/wcclparser/main.cpp @@ -1,5 +1,7 @@ #include <cstdlib> #include <fstream> +#include <iomanip> + #include <libwccl/values/strset.h> #include <libwccl/parser/Parser.h> @@ -88,7 +90,8 @@ void libedit_read_loop(boost::function<bool (const std::string&)>& line_cb) } #endif -bool process_line(const std::string& line, Parser& parser, Wccl::SentenceContext& sc) +bool process_line(const std::string& line, Parser& parser, + Wccl::SentenceContext& sc, bool all_positions) { if (line.empty() || line == "exit" || line == "quit") { return true; @@ -102,15 +105,28 @@ bool process_line(const std::string& line, Parser& parser, Wccl::SentenceContext try { retOp = parser.parseAnyOperator(line); if (retOp) { - Wccl::FunExecContext cx(sc, retOp->variables); - retVal = retOp->get_op_base()->apply_internal(cx); - if (retVal) { - std::cerr << "Parsed expression: " - << retVal->to_string(parser.tagset()) - << std::endl; + int pb, pe; + if (all_positions) { + pb = 0; + pe = sc.get_sentence().empty() ? 1 : sc.size(); } else { - std::cerr << "Problem while parsing -- " - << "retVal is NULL!" << std::endl; + pb = sc.get_position(); + pe = sc.get_position() + 1; + } + for (int i = pb; i < pe; ++i) { + sc.set_position(i); + Wccl::FunExecContext cx(sc, + boost::shared_ptr<Wccl::Variables>(retOp->variables->clone())); + retVal = retOp->get_op_base()->apply_internal(cx); + if (retVal) { + std::cerr << "[" << std::setw(2) << sc.get_position() << "] " + << "Parsed expression: " + << retVal->to_string(parser.tagset()) + << std::endl; + } else { + std::cerr << "Problem while parsing -- " + << "retVal is NULL!" << std::endl; + } } } else { std::cerr << "Problem while parsing -- " @@ -195,11 +211,12 @@ int main(int argc, char** argv) sc.set_position(pos); Parser parser(tagset); if (!query.empty()) { - process_line(query, parser, sc); + process_line(query, parser, sc, position == "all"); return 0; } boost::function<bool (const std::string&)> f; - f = boost::bind(&process_line, _1, boost::ref(parser), boost::ref(sc)); + f = boost::bind(&process_line, _1, boost::ref(parser), boost::ref(sc), + position == "all"); #ifdef HAVE_LIBEDIT libedit_read_loop(f); #else -- GitLab