Commit 851393ce authored by Volker Krause's avatar Volker Krause

Move sub-rule loading to rule base class

parent eeecab8c
......@@ -61,21 +61,10 @@ bool Extractor::load(const QString &fileName)
continue;
}
std::unique_ptr<ExtractorRule> rule;
QStringRef readerName = reader.name();
if (readerName == QLatin1String("variable")) {
rule.reset(new ExtractorVariableRule);
} else if (readerName == QLatin1String("class")) {
rule.reset(new ExtractorClassRule);
} else if (readerName == QLatin1String("property")) {
rule.reset(new ExtractorPropertyRule);
} else {
return false;
auto rule = ExtractorRule::fromXml(reader);
if (rule) {
m_rules.push_back(rule);
}
if (!rule->load(reader)) {
return false;
}
m_rules.push_back(rule.release());
}
qCDebug(SEMANTIC_LOG) << fileName << "loaded!";
......
......@@ -27,7 +27,15 @@
#include <memory>
ExtractorRule::~ExtractorRule() = default;
ExtractorRule::~ExtractorRule()
{
qDeleteAll(m_rules);
}
QVector<ExtractorRule*> ExtractorRule::rules() const
{
return m_rules;
}
QString ExtractorRule::name() const
{
......@@ -90,30 +98,6 @@ bool ExtractorRule::load(QXmlStreamReader &reader)
if (reader.attributes().hasAttribute(QLatin1String("locale"))) {
m_locale = QLocale(reader.attributes().value(QLatin1String("locale")).toString());
}
return true;
}
bool ExtractorVariableRule::match(ExtractorContext *context) const
{
const auto res = m_regexp.match(context->engine()->text(), 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());
}
return res.hasMatch();
}
ExtractorClassRule::~ExtractorClassRule()
{
qDeleteAll(m_rules);
}
bool ExtractorClassRule::load(QXmlStreamReader &reader)
{
if (!ExtractorRule::load(reader)) {
return false;
}
while (!reader.atEnd()) {
reader.readNext();
......@@ -123,46 +107,53 @@ bool ExtractorClassRule::load(QXmlStreamReader &reader)
if (reader.tokenType() != QXmlStreamReader::StartElement) {
continue;
}
std::unique_ptr<ExtractorRule> rule;
if (reader.name() == QLatin1String("variable")) {
rule.reset(new ExtractorVariableRule);
if (!rule->load(reader)) {
return false;
}
reader.skipCurrentElement();
} else if (reader.name() == QLatin1String("class")) {
rule.reset(new ExtractorClassRule);
if (!rule->load(reader)) {
return false;
}
} else if (reader.name() == QLatin1String("property")) {
rule.reset(new ExtractorPropertyRule);
if (!rule->load(reader)) {
return false;
}
reader.skipCurrentElement();
} else {
auto rule = fromXml(reader);
if (!rule) {
return false;
}
m_rules.push_back(rule.release());
m_rules.push_back(rule);
}
return true;
return false;
}
bool ExtractorClassRule::match(ExtractorContext *context) const
ExtractorRule *ExtractorRule::fromXml(QXmlStreamReader &reader)
{
std::unique_ptr<ExtractorRule> rule;
QStringRef readerName = reader.name();
if (readerName == QLatin1String("variable")) {
rule.reset(new ExtractorVariableRule);
} else if (readerName == QLatin1String("class")) {
rule.reset(new ExtractorClassRule);
} else if (readerName == QLatin1String("property")) {
rule.reset(new ExtractorPropertyRule);
} else {
return nullptr;
}
if (!rule->load(reader)) {
return nullptr;
}
return rule.release();
}
bool ExtractorVariableRule::match(ExtractorContext *context) const
{
const auto res = m_regexp.match(context->engine()->text(), 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());
}
return res.hasMatch();
}
QVector<ExtractorRule *> ExtractorClassRule::rules() const
bool ExtractorClassRule::match(ExtractorContext *context) const
{
return m_rules;
const auto res = m_regexp.match(context->engine()->text(), context->offset());
if (res.hasMatch()) {
context->setOffset(res.capturedEnd());
}
return res.hasMatch();
}
bool ExtractorPropertyRule::match(ExtractorContext *context) const
......
......@@ -36,10 +36,13 @@ public:
virtual bool load(QXmlStreamReader &reader);
virtual bool match(ExtractorContext *context) const = 0;
QVector<ExtractorRule *> rules() const;
QString name() const;
QString type() const;
bool repeats() const;
static ExtractorRule *fromXml(QXmlStreamReader &reader);
protected:
QString value(const QRegularExpressionMatch &match, ExtractorContext *context) const;
QString format() const;
......@@ -48,6 +51,7 @@ protected:
QRegularExpression m_regexp;
private:
QVector<ExtractorRule *> m_rules;
QString m_name;
QString m_type;
QString m_value;
......@@ -65,12 +69,7 @@ public:
class ExtractorClassRule : public ExtractorRule
{
public:
~ExtractorClassRule();
bool load(QXmlStreamReader &reader) override;
bool match(ExtractorContext *context) const override;
QVector<ExtractorRule *> rules() const;
private:
QVector<ExtractorRule *> m_rules;
};
class ExtractorPropertyRule : public ExtractorRule
......
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