From f532b738be5bc3b13b2625bd1435e6b3aa53c5a5 Mon Sep 17 00:00:00 2001 From: Adam Wardynski <award@.(B-4.4.46a)> Date: Fri, 18 Mar 2011 17:46:43 +0100 Subject: [PATCH] Rewrite MatchOperator to use ConjConditions. --- libwccl/ops/match/applyoperator.cpp | 3 ++- libwccl/ops/match/matchoperator.cpp | 37 ++++++----------------------- libwccl/ops/match/matchoperator.h | 8 +++---- libwccl/parser/grammar.g | 1 + 4 files changed, 14 insertions(+), 35 deletions(-) diff --git a/libwccl/ops/match/applyoperator.cpp b/libwccl/ops/match/applyoperator.cpp index cdac967..e40ddd8 100644 --- a/libwccl/ops/match/applyoperator.cpp +++ b/libwccl/ops/match/applyoperator.cpp @@ -1,4 +1,5 @@ #include <libwccl/ops/match/applyoperator.h> +#include <libwccl/values/matchvector.h> #include <libpwrutils/foreach.h> namespace Wccl { @@ -35,7 +36,7 @@ void ApplyOperator::execute(const ActionExecContext &context) const should_act = _conditions[i]->apply(context)->get_value(); } if (should_act) { - matches->append(match); + matches->append(match); // the match goes to $m:_M[0] foreach (const boost::shared_ptr<const MatchAction>& action, _actions) { action->execute(context); } diff --git a/libwccl/ops/match/matchoperator.cpp b/libwccl/ops/match/matchoperator.cpp index af29636..4cb8ec9 100644 --- a/libwccl/ops/match/matchoperator.cpp +++ b/libwccl/ops/match/matchoperator.cpp @@ -1,5 +1,4 @@ #include <libwccl/ops/match/matchoperator.h> -#include <libpwrutils/foreach.h> #include <sstream> namespace Wccl { @@ -8,47 +7,25 @@ boost::shared_ptr<Match> MatchOperator::apply( boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const { - boost::shared_ptr<MatchVector> matches(new MatchVector()); int orig_iter_pos = iter_pos->get_value(); - - foreach (const boost::shared_ptr<const MatchCondition>& cond, _conditions) { - MatchResult res = cond->apply(iter_pos, context); - if(res.matched()) { - matches->append(res.get_match()); - } else { - matches.reset(new MatchVector()); - iter_pos->set_value(orig_iter_pos + 1); - return matches; - } + MatchResult res = _conditions->apply(iter_pos, context); + if(res.matched()) { + return res.get_match(); } - return matches; + iter_pos->set_value(orig_iter_pos + 1); + return boost::make_shared<Match>(); } std::string MatchOperator::to_string(const Corpus2::Tagset& tagset) const { std::ostringstream ostream; - ostream << name() << "("; - for(size_t i = 0; i < _conditions.size(); ++i) { - if (i != 0) { - ostream << ", "; - } - ostream << _conditions[i]->to_string(tagset); - } - ostream << ")"; + ostream << name() << _conditions->to_string(tagset); return ostream.str(); } std::ostream& MatchOperator::write_to(std::ostream &ostream) const { - ostream << name() << "("; - for(size_t i = 0; i < _conditions.size(); ++i) { - if (i != 0) { - ostream << ", "; - } - ostream << *_conditions[i]; - } - ostream << ")"; - return ostream; + return ostream << name() << *_conditions; } } /* end ns Wccl */ diff --git a/libwccl/ops/match/matchoperator.h b/libwccl/ops/match/matchoperator.h index ff679ca..dd6745b 100644 --- a/libwccl/ops/match/matchoperator.h +++ b/libwccl/ops/match/matchoperator.h @@ -1,8 +1,7 @@ #ifndef LIBWCCL_OPS_MATCH_MATCHOPERATOR_H #define LIBWCCL_OPS_MATCH_MATCHOPERATOR_H -#include <libwccl/ops/match/matchcondition.h> -#include <libwccl/values/matchvector.h> +#include <libwccl/ops/match/conditions/conjconditions.h> namespace Wccl { @@ -12,8 +11,9 @@ namespace Wccl { class MatchOperator : public Expression { public: - MatchOperator(const std::vector< boost::shared_ptr<const MatchCondition> >& conditions) + MatchOperator(const boost::shared_ptr<ConjConditions>& conditions) : _conditions(conditions) { + BOOST_ASSERT(_conditions); } /** @@ -52,7 +52,7 @@ protected: std::ostream& write_to(std::ostream& ostream) const; private: - const std::vector<boost::shared_ptr<const MatchCondition> > _conditions; + const boost::shared_ptr<ConjConditions> _conditions; }; } /* end ns Wccl */ diff --git a/libwccl/parser/grammar.g b/libwccl/parser/grammar.g index 712d2c0..a026023 100644 --- a/libwccl/parser/grammar.g +++ b/libwccl/parser/grammar.g @@ -70,6 +70,7 @@ header { // Match operators #include <libwccl/values/tokenmatch.h> #include <libwccl/values/annotationmatch.h> + #include <libwccl/values/matchvector.h> #include <libwccl/ops/match/applyoperator.h> // Unicode String -- GitLab