148 lines
4.9 KiB
C++
148 lines
4.9 KiB
C++
#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);
|
|
|
|
// }
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Advances ofs to first whitespace or end of string, returns false at end of string
|
|
void skipWhiteSpace(const QString &in, int &ofs)
|
|
{
|
|
const int l = in.length();
|
|
while (ofs < l && in.at(ofs).isSpace()) ++ofs;
|
|
}
|
|
|
|
enum class BasicTokenType {
|
|
None,
|
|
End, // End of input
|
|
Symbol, // can be many things, keyword, object name, operator, ..
|
|
Comment,
|
|
QuotedString,
|
|
DollarQuotedString,
|
|
QuotedIdentifier
|
|
};
|
|
|
|
/**
|
|
* @brief NextBasicToken
|
|
* @param in
|
|
* @param ofs
|
|
* @param start
|
|
* @param length
|
|
* @return false when input seems invalid, it will return what it did recognize but something wasn't right, parser should try to recover
|
|
*/
|
|
bool NextBasicToken(const QString &in, int ofs, int &start, int &length, BasicTokenType &tokentype)
|
|
{
|
|
// Basically chops based on white space
|
|
// it does also recognize comments and quoted strings/identifiers
|
|
bool result = false;
|
|
|
|
skipWhiteSpace(in, ofs);
|
|
const int len = in.length();
|
|
while (ofs < len) {
|
|
if (ofs+1 < len && in.at(ofs) == L'-' && in.at(ofs+1) == L'-') {
|
|
// Start of comment, end of line is end of comment
|
|
|
|
}
|
|
else if (in.at(ofs) == L'\'') {
|
|
// Start of quoted string
|
|
|
|
}
|
|
else if (in.at(ofs) == L'"') {
|
|
// Start of quoted identifier
|
|
|
|
}
|
|
else if (in.at(ofs) == L'/' && ofs+1 < len && in.at(ofs+1) == L'*') {
|
|
// Start of C style comment, scan for end
|
|
}
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
|