UI: RuleTextParser: Simplify nextCharType()

This commit is contained in:
Nodir Temirkhodjaev 2024-06-05 16:44:04 +03:00
parent 2f0f1143cb
commit 34bff830dd
2 changed files with 64 additions and 48 deletions

View File

@ -1,7 +1,50 @@
#include "ruletextparser.h" #include "ruletextparser.h"
#include <QHash>
#include <common/fortconf.h> #include <common/fortconf.h>
namespace {
RuleCharType processChar(const QChar c)
{
if (c.isLetter()) {
return CharNameBegin;
}
if (c.isDigit()) {
return CharValueBegin;
}
static QHash<char, RuleCharType> charTypeMap = {
{ '{', CharListBegin },
{ '}', CharListEnd },
{ '(', CharBracketBegin },
{ ')', CharBracketEnd },
{ '[', CharValueBegin },
{ ',', CharValueSeparator },
{ ':', CharColon },
{ '#', CharComment },
};
return charTypeMap.value(c.unicode(), CharNone);
}
RuleCharType processCharType(RuleCharType charType, const QChar c)
{
if (charType == CharComment) {
if (c == '\n') {
return CharNone;
}
return CharComment;
}
return processChar(c);
}
}
RuleTextParser::RuleTextParser(const QString &text, QObject *parent) : QObject(parent) RuleTextParser::RuleTextParser(const QString &text, QObject *parent) : QObject(parent)
{ {
setupText(text); setupText(text);
@ -20,44 +63,17 @@ bool RuleTextParser::parse()
return false; return false;
} }
RuleTextParser::CharType RuleTextParser::nextCharType() RuleCharType RuleTextParser::nextCharType()
{ {
bool skipLine = false; RuleCharType charType = CharNone;
while (m_p < m_end) { while (m_p < m_end) {
const QChar c = *m_p++; const QChar c = *m_p++;
if (skipLine) { charType = processCharType(charType, c);
skipLine = (c != '\n');
continue;
}
if (c.isLetter()) { if (charType != CharNone) {
return CharNameBegin; return charType;
}
if (c.isDigit()) {
return CharValueBegin;
}
switch (c.unicode()) {
case '{':
return CharListBegin;
case '}':
return CharListEnd;
case '(':
return CharBracketBegin;
case ')':
return CharBracketEnd;
case '[':
return CharValueBegin;
case ',':
return CharValueSeparator;
case ':':
return CharColon;
case '#': {
skipLine = true;
} break;
} }
} }

View File

@ -6,6 +6,21 @@
#include <util/util_types.h> #include <util/util_types.h>
enum RuleCharType {
CharNone = 0,
CharListBegin = (1 << 0), // {
CharListEnd = (1 << 1), // }
CharBracketBegin = (1 << 2), // (
CharBracketEnd = (1 << 3), // )
CharNameBegin = (1 << 4), // a-zA-Z
CharName = (1 << 5), // a-zA-Z0-9_-
CharValueBegin = (1 << 6), // [0-9
CharValue = (1 << 7), // 0-9.:-/
CharValueSeparator = (1 << 8), // ,
CharColon = (1 << 9), // :
CharComment = (1 << 10), // #
};
struct RuleExpr struct RuleExpr
{ {
quint8 flags = 0; quint8 flags = 0;
@ -29,24 +44,9 @@ public:
bool parse(); bool parse();
private: private:
enum CharType {
CharNone = 0,
CharListBegin = (1 << 0), // {
CharListEnd = (1 << 1), // }
CharBracketBegin = (1 << 2), // (
CharBracketEnd = (1 << 3), // )
CharNameBegin = (1 << 4), // a-zA-Z
CharName = (1 << 5), // a-zA-Z0-9_-
CharValueBegin = (1 << 6), // [0-9
CharValue = (1 << 7), // 0-9.:-/
CharValueSeparator = (1 << 8), // ,
CharColon = (1 << 9), // :
CharComment = (1 << 10), // #
};
void setupText(const QString &text); void setupText(const QString &text);
RuleTextParser::CharType nextCharType(); RuleCharType nextCharType();
private: private:
quint8 m_exprType = 0; quint8 m_exprType = 0;