New syntax highlighter not complete.

- Supports comments
- more efficient as it scans the text block instead of repeatedly searching throught the whole block
- type matching based on catalog (but need to add aliases manually)
- added many keywords

todo:
- heap corruption bug
- symbol stops at special char like parenthese or operator or something similar.
This commit is contained in:
Eelke Klein 2017-02-07 21:39:45 +01:00
parent 4364f427bf
commit 37e8882a3c
11 changed files with 311 additions and 72 deletions

View file

@ -74,64 +74,6 @@ SqlHighlighter::SqlHighlighter(QTextDocument *parent)
// }
}
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) {
@ -146,3 +88,5 @@ void SqlHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(0);
}