Commit 7982ba83 authored by Volker Krause's avatar Volker Krause

Refactor rule matching and fix '^' regexp matching

parent 299b74c5
......@@ -141,38 +141,36 @@ ExtractorRule *ExtractorRule::fromXml(QXmlStreamReader &reader)
return rule.release();
}
bool ExtractorVariableRule::match(ExtractorContext *context) const
bool ExtractorRule::match(ExtractorContext *context) const
{
const auto res = m_regexp.match(context->engine()->text(), context->offset());
// use QString::midRef(offset) rather than match(text(), offset) as that makes '^' matches work
const auto res = m_regexp.match(context->engine()->text().midRef(context->offset()));
if (res.hasMatch()) {
qCDebug(SEMANTIC_LOG) << name() << res.captured() << context->offset() << res.capturedEnd() << context->engine()->text().mid(context->offset(), 20);
context->setVariable(name(), value(res, context));
context->setOffset(res.capturedEnd());
qCDebug(SEMANTIC_LOG) << name() << res.captured() << context->offset() << res.capturedEnd() << context->engine()->text().midRef(context->offset(), 20);
processMatch(res, context);
context->setOffset(res.capturedEnd() + context->offset());
}
return res.hasMatch();
}
bool ExtractorClassRule::match(ExtractorContext *context) const
void ExtractorVariableRule::processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const
{
const auto res = m_regexp.match(context->engine()->text(), context->offset());
if (res.hasMatch()) {
context->setOffset(res.capturedEnd());
}
return res.hasMatch();
context->setVariable(name(), value(match, context));
}
bool ExtractorPropertyRule::match(ExtractorContext *context) const
void ExtractorClassRule::processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const
{
const auto res = m_regexp.match(context->engine()->text(), context->offset());
if (res.hasMatch()) {
auto val = value(res, context);
if (type() == QLatin1String("dateTime") && !format().isEmpty()) {
const auto dt = locale().toDateTime(val, format());
val = dt.toString(Qt::ISODate);
}
Q_UNUSED(match);
Q_UNUSED(context);
}
context->setProperty(name(), val);
context->setOffset(res.capturedEnd());
void ExtractorPropertyRule::processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const
{
auto val = value(match, context);
if (type() == QLatin1String("dateTime") && !format().isEmpty()) {
const auto dt = locale().toDateTime(val, format());
val = dt.toString(Qt::ISODate);
}
return res.hasMatch();
context->setProperty(name(), val);
}
......@@ -33,8 +33,7 @@ class ExtractorRule
{
public:
virtual ~ExtractorRule();
virtual bool load(QXmlStreamReader &reader);
virtual bool match(ExtractorContext *context) const = 0;
bool match(ExtractorContext *context) const;
bool hasSubRules() const;
QVector<ExtractorRule *> rules() const;
......@@ -45,6 +44,8 @@ public:
static ExtractorRule *fromXml(QXmlStreamReader &reader);
protected:
bool load(QXmlStreamReader &reader);
virtual void processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const = 0;
QString value(const QRegularExpressionMatch &match, ExtractorContext *context) const;
QString format() const;
QLocale locale() const;
......@@ -64,19 +65,19 @@ private:
class ExtractorVariableRule : public ExtractorRule
{
public:
bool match(ExtractorContext *context) const override;
void processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const override;
};
class ExtractorClassRule : public ExtractorRule
{
public:
bool match(ExtractorContext *context) const override;
void processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const override;
};
class ExtractorPropertyRule : public ExtractorRule
{
public:
bool match(ExtractorContext *context) const override;
void processMatch(const QRegularExpressionMatch &match, ExtractorContext *context) const override;
};
#endif // EXTRACTORRULE_H
Markdown is supported
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