-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for parenthesis and operators in post-filters (fix #…
- Loading branch information
Showing
3 changed files
with
59 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,63 @@ | ||
#include "post-filter.h" | ||
#include <QRegularExpression> | ||
#include <QStringList> | ||
#include "filename/ast/filename-node-condition.h" | ||
#include "filename/filename-parser.h" | ||
#include "filename/visitors/filename-condition-visitor.h" | ||
#include "filter.h" | ||
#include "filter-factory.h" | ||
#include "logger.h" | ||
|
||
|
||
PostFilter::PostFilter(const QStringList &filters) | ||
PostFilter::PostFilter(const QString &filters) | ||
{ | ||
for (const QString &filter : filters) { | ||
auto fil = QSharedPointer<Filter>(FilterFactory::build(filter)); | ||
if (!fil.isNull()) { | ||
m_filters.append(fil); | ||
if (filters.isEmpty()) { | ||
return; | ||
} | ||
|
||
// Use advanced mode if the filter contains a parenthesis or conditional operator | ||
if (filters.contains(QRegularExpression("[()&|]"))) { | ||
FilenameParser parser(filters); | ||
FilenameNodeCondition *ast = parser.parseCondition(); | ||
if (!parser.error().isEmpty()) { | ||
log(QString("Error parsing post-filter '%1': %2").arg(filters, parser.error()), Logger::Error); | ||
return; | ||
} | ||
m_ast = ast; | ||
} else { | ||
for (const QString &filter : filters.split(' ', Qt::SkipEmptyParts)) { | ||
auto fil = QSharedPointer<Filter>(FilterFactory::build(filter)); | ||
if (!fil.isNull()) { | ||
m_filters.append(fil); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int PostFilter::count() const | ||
{ | ||
return m_filters.count(); | ||
} | ||
PostFilter::PostFilter(const QStringList &filters) | ||
: PostFilter(filters.join(' ')) | ||
{} | ||
|
||
|
||
QStringList PostFilter::match(const QMap<QString, Token> &tokens) const | ||
{ | ||
QStringList ret; | ||
|
||
// Advanced mode | ||
if (m_ast != nullptr) { | ||
FilenameConditionVisitor conditionVisitor(tokens, nullptr); // TODO(Bionus): properly pass settings | ||
if (!conditionVisitor.run(*m_ast)) { | ||
ret.append(QStringLiteral("post-filter")); | ||
} | ||
} | ||
|
||
// Simple mode | ||
for (const auto &filter : m_filters) { | ||
QString err = filter->match(tokens); | ||
if (!err.isEmpty()) { | ||
ret.append(err); | ||
} | ||
} | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters