pgLab/sqlhighlighter.cpp

119 lines
3.7 KiB
C++
Raw Normal View History

#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"~"
};
//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"-(as|alter|all|and|any|by|column|create|database|from|group|having|in|not|or|order|select|table|where|(?:(?:inner|(?:left|right|full)(\s+outer)?)\s+)?join)-";
// 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)-";
// 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(128, 128, 255));
// keywordFormat.setFontWeight(QFont::Bold);
// highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat);
// QTextCharFormat typesFormat;
// typesFormat.setForeground(QColor(128, 255, 128));
// 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);
}