Skip to content
Snippets Groups Projects
regex.h 1.94 KiB
Newer Older
#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_REGEX_H
#define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_REGEX_H
Adam Wardynski's avatar
Adam Wardynski committed

#include <unicode/regex.h>

#include <libwccl/exception.h>
#include <libwccl/values/strset.h>
#include <libwccl/ops/functions/bool/predicate.h>
Adam Wardynski's avatar
Adam Wardynski committed

namespace Wccl {

/**
 * Operator that applies a regular expression to a set of
 * strings and returns true if all of the strings match the
 * expression
 */
class Regex : public Predicate {
public:
	typedef boost::shared_ptr<Function<StrSet> > StrSetFunctionPtr;

	Regex(const StrSetFunctionPtr& strset_expr, const UnicodeString& patstr);

	/**
	 * @returns String representation of the function in form of:
Adam Wardynski's avatar
Adam Wardynski committed
	 * "regex(strset_expr_string, regex_string)"
	 */
	std::string to_string(const Corpus2::Tagset& tagset) const;
	 * @returns String representation of the function in form of:
Adam Wardynski's avatar
Adam Wardynski committed
	 * "regex(strset_expr_raw_string, regex_string)"
	 * @note This version does not require a tagset, but may be inclomplete
Adam Wardynski's avatar
Adam Wardynski committed
	 * and/or contain internal info.
	 */
	std::string to_raw_string() const;
	/**
	 * @returns Name of the function: "regex"
	 */
	std::string raw_name() const {
Adam Wardynski's avatar
Adam Wardynski committed
		return "regex";
	}

protected:	
	/**
	 * Get a string set from the argument expression,
	 * apply regular expression to each string one by one,
	 * return false if a string not matching expression is found.
	 * Return true if all strings matched the regular espression.
	 */
	BaseRetValPtr apply_internal(const FunExecContext& context) const;
Adam Wardynski's avatar
Adam Wardynski committed

private:
	const StrSetFunctionPtr strset_expr_;
	const UnicodeString patstr_;
	const boost::shared_ptr<const RegexPattern> pattern_;
};

class RegexParseError : public WcclError
Adam Wardynski's avatar
Adam Wardynski committed
{
public:
	RegexParseError(
		const UnicodeString& pattern,
		const UErrorCode& status_code,
		const UParseError& parse_error);

	~RegexParseError() throw();
	
Adam Wardynski's avatar
Adam Wardynski committed
	std::string info() const;

ilor's avatar
ilor committed
	UnicodeString pattern;
	std::string status;
	UParseError upe;
Adam Wardynski's avatar
Adam Wardynski committed
};

} /* end ns Wccl */


#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_REGEX_H