C++ (Qt)
class SQLSyntaxHighlighter : public QSyntaxHighlighter
{
public:
SQLSyntaxHighlighter (QTextEdit *te) : QSyntaxHighlighter (te) {}
virtual void highlightBlock (const QString& text)
{
int pos = 0;
static QRegExp rgx ("^\\s*(--)");
if ((pos = rgx.search (text, pos)) != -1)
{
setFormat (pos, text.length(), Qt::green);
return;
}
setFormat (0, text.length(), Qt::black);
static QRegExp rw_rgx ("\\b(?:select|from|where|and|case|when|then|else|distinct|all|null|"
"is|like|between|not|count|group|by|having|order|inner|outer|right|left|"
"join|on|using|union|exists|in|as|intersect|except|coalesce|insert|delete|into|update)\\b", false);
pos = 0;
while ((pos = rw_rgx.search (text, pos)) != -1)
{
setFormat (pos, rw_rgx.matchedLength(), Qt::blue);
pos += rw_rgx.matchedLength();
}
static QRegExp o_rgx (":(\\w*)");
pos = 0;
while ((pos = o_rgx.search (text, pos)) != -1)
{
setFormat (pos, o_rgx.matchedLength(), Qt::red);
pos += o_rgx.matchedLength();
}
}
};