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 <QHash>
#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)
{
setupText(text);
@ -20,44 +63,17 @@ bool RuleTextParser::parse()
return false;
}
RuleTextParser::CharType RuleTextParser::nextCharType()
RuleCharType RuleTextParser::nextCharType()
{
bool skipLine = false;
RuleCharType charType = CharNone;
while (m_p < m_end) {
const QChar c = *m_p++;
if (skipLine) {
skipLine = (c != '\n');
continue;
}
charType = processCharType(charType, c);
if (c.isLetter()) {
return CharNameBegin;
}
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;
if (charType != CharNone) {
return charType;
}
}

View File

@ -6,6 +6,21 @@
#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
{
quint8 flags = 0;
@ -29,24 +44,9 @@ public:
bool parse();
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);
RuleTextParser::CharType nextCharType();
RuleCharType nextCharType();
private:
quint8 m_exprType = 0;