#include "SqlHighlighter.h" static const wchar_t *keywords[] = { L"as", L"alter", L"all", L"and", L"any", L"by", L"char", L"column", L"create", L"database", L"date", L"from", L"full", L"group", L"having", L"in", L"inner", L"int", L"join", L"left", L"not", L"numeric", L"or", L"order", L"outer", L"right", L"select", L"smallint", L"table", L"time", L"timestamp", L"timestamptz", L"varchar", L"where" }; static const wchar_t *operators[] = { L"+", L"-", L"*", L"/", L"<", L">", L"<=", L">=", L"<>", L"!=", L"~" }; /* + - * / < > = ~ ! @ # % ^ & | ` ? There are a few restrictions on your choice of name: -- and /* cannot appear anywhere in an operator name, since they will be taken as the start of a comment. A multicharacter operator name cannot end in + or -, unless the name also contains at least one of these characters: ~ ! @ # % ^ & | ` ? For example, @- is an allowed operator name, but *- is not. This restriction allows PostgreSQL to parse SQL-compliant commands without requiring spaces between tokens. The use of => as an operator name is deprecated. It may be disallowed altogether in a future release. The operator != is mapped to <> on input, so these two names are always equivalent. + - * / < > = */ //static auto types = R"-(bigint|boolean|char|character varying|date|int[248]|integer|numeric|smallint|time|timestamp(?:tz)?|timestamp(?:\s+with\s+timezone)?|varchar)-"; //static auto err = R"-(left|right|inner|outer)-"; // static_assert(sizeof(keywords)/4 == 25, // "sizeof keywords"); SqlHighlighter::SqlHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { // { static auto keywords = R"-(\balter\b|\ball\b|\band\b|\bany\b|\bas\b|\bby\b|\bcascade\b|\bcheck\b|\bcolumn\b|\bcopy\b|\bcreate\b|\bdatabase\b|\bdefault\b|\bdelete\b)-" R"-(|\foreign\b|\bfrom\b|\bgroup\b|\bhaving\b|\bin\b|\bindex\b|\bis\b|\bkey\b|\blimit\b|\bnatural\b|\bnot\b|\bnull\b|\boffset\b|\bon\b)-" R"-(|\bor\b|\border\b|\bover\b|\bparition\b|\bprimary\b|\breferences\b|\brestrict\b|\bselect\b|\btable\b|\btruncate\b|\bunique\b|\bupdate\b|\busing\b)-" R"-(|\bwhere\b|\bwith\b|(?:(?:inner|(?:left|right|full)(\s+outer)?)\s+)?join)-"; // into temp DISTINCT true false static auto types = R"-(\bbigint\b|\bboolean\b|\bchar\b|\bcharacter varying\b|\bdate\b|\bint[248]\b|\binteger\b|\bnumeric\b|\bsmallint\b)-" R"-(|\btime\b|\btimestamp(?:tz)?\b|\btimestamp(?:\s+with\s+time\s+zone)?\b|\bvarchar\b)-"; // static auto err = R"-(left|right|inner|outer)-"; // QTextCharFormat errFormat; // errFormat.setForeground(QColor(255, 128, 128)); // errFormat.setFontWeight(QFont::Bold); // highlightingRules.emplace_back(QRegExp(err, Qt::CaseInsensitive), errFormat); QTextCharFormat keywordFormat; keywordFormat.setForeground(QColor(64, 64, 192)); keywordFormat.setFontWeight(QFont::Bold); highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat); QTextCharFormat typesFormat; typesFormat.setForeground(QColor(64, 192, 64)); typesFormat.setFontWeight(QFont::Bold); highlightingRules.emplace_back(QRegExp(types, Qt::CaseInsensitive), typesFormat); // } } void SqlHighlighter::highlightBlock(const QString &text) { foreach (const HighlightingRule &rule, highlightingRules) { QRegExp expression(rule.pattern); int index = expression.indexIn(text); while (index >= 0) { int length = expression.matchedLength(); setFormat(index, length, rule.format); index = expression.indexIn(text, index + length); } } setCurrentBlockState(0); }