Skip to content
Snippets Groups Projects
Commit 6f4e5b27 authored by Adam Wardynski's avatar Adam Wardynski
Browse files

OptionalMatch - "optional" match condition.

parent ee023bf3
Branches
No related merge requests found
......@@ -55,6 +55,7 @@ SET(libwccl_STAT_SRC
ops/functions/tset/getsymbols.cpp
ops/functions/tset/getsymbolsinrange.cpp
ops/match/conditions/conjconditions.cpp
ops/match/conditions/optionalmatch.cpp
ops/match/conditions/repeatedmatch.cpp
ops/match/conditions/tokencondition.cpp
ops/match/applyoperator.cpp
......
#include <libwccl/ops/match/conditions/optionalmatch.h>
#include <libwccl/values/matchvector.h>
#include <sstream>
namespace Wccl {
OptionalMatch::OptionalMatch(const boost::shared_ptr<ConjConditions>& conditions)
: _conditions(conditions)
{
BOOST_ASSERT(_conditions);
}
MatchResult OptionalMatch::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const
{
int orig_pos = iter_pos->get_value();
MatchResult res = _conditions->apply(iter_pos, context);
if (res.matched()) {
return res;
}
iter_pos->set_value(orig_pos);
return MatchResult(boost::make_shared<Match>());
}
std::string OptionalMatch::to_string(const Corpus2::Tagset& tagset) const
{
std::ostringstream ostream;
ostream << name() << _conditions->to_string(tagset);
return ostream.str();
}
std::ostream& OptionalMatch::write_to(std::ostream& ostream) const
{
return ostream << name() << *_conditions;
}
} /* end ns Wccl */
#ifndef LIBWCCL_OPS_MATCH_CONDITIONS_OPTIONALMATCH_H
#define LIBWCCL_OPS_MATCH_CONDITIONS_OPTIONALMATCH_H
#include <libwccl/ops/match/conditions/conjconditions.h>
namespace Wccl {
/**
* Class for "optional" condition of match
*/
class OptionalMatch : public MatchCondition
{
public:
OptionalMatch(const boost::shared_ptr<ConjConditions>& conditions);
/**
* @returns Name of the condition.
*/
std::string name() const {
return "optional";
}
/**
* Applies the condition to the given execution context.
* Inner conditions are executed once.
* If the match, was successfull the result consists of "true"
* and the match.
* If there was no match, "true" is returned anyway, but with a default Match.
* If match is found, the current iter Position "$_" is increased
* as to point one token after all the matched tokens, otherwise
* it stays unchanged.
*/
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const;
/**
* @returns String representation of the MatchCondition
*/
std::string to_string(const Corpus2::Tagset& tagset) const;
protected:
/**
* Writes string representation of the MatchCondition to
* an output stream.
* @returns Stream written to.
* @note May be incomplete and/or containt internal info.
*/
std::ostream& write_to(std::ostream& ostream) const;
private:
const boost::shared_ptr<ConjConditions> _conditions;
};
} /* end ns Wccl */
#endif // LIBWCCL_OPS_MATCH_CONDITIONS_OPTIONALMATCH_H
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment