Split big function in sql lexer into several functions.
This commit is contained in:
parent
81c4449d31
commit
091040f13f
2 changed files with 106 additions and 87 deletions
|
|
@ -59,57 +59,11 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
|
||||||
}
|
}
|
||||||
else if (c == '\'') {
|
else if (c == '\'') {
|
||||||
// Single quoted string so it's an SQL text literal
|
// Single quoted string so it's an SQL text literal
|
||||||
while (true) {
|
return parseSingleQuotedString(startpos, length, tokentype);
|
||||||
c = peekChar();
|
|
||||||
if (c == QChar::Null || c == '\n') {
|
|
||||||
// unexpected end, pretend nothings wrong
|
|
||||||
length = m_pos - startpos;
|
|
||||||
tokentype = BasicTokenType::QuotedString;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nextChar();
|
|
||||||
if (c == '\'') {
|
|
||||||
// maybe end of string literal
|
|
||||||
if (peekChar() == '\'') {
|
|
||||||
// Nope, just double quote to escape quote
|
|
||||||
nextChar(); // eat it
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
length = m_pos - startpos;
|
|
||||||
tokentype = BasicTokenType::QuotedString;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (c == '"') {
|
else if (c == '"') {
|
||||||
// Double quoted identifier
|
// Double quoted identifier
|
||||||
while (true) {
|
return parseDoubleQuotedIdentifier(startpos, length, tokentype);
|
||||||
c = peekChar();
|
|
||||||
if (c == QChar::Null || c == '\n') {
|
|
||||||
// unexpected end, pretend nothings wrong
|
|
||||||
length = m_pos - startpos;
|
|
||||||
tokentype = BasicTokenType::QuotedIdentifier;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nextChar();
|
|
||||||
if (c == '"') {
|
|
||||||
// maybe end of string literal
|
|
||||||
if (peekChar() == '"') {
|
|
||||||
// Nope, just double quote to escape quote
|
|
||||||
nextChar(); // eat it
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
length = m_pos - startpos;
|
|
||||||
tokentype = BasicTokenType::QuotedIdentifier;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// else if (c == '/' && peekChar() == '*') {
|
// else if (c == '/' && peekChar() == '*') {
|
||||||
// nextChar();
|
// nextChar();
|
||||||
|
|
@ -119,44 +73,7 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (c == '$') {
|
else if (c == '$') {
|
||||||
c = nextChar();
|
return parseDollarQuote(startpos, length, tokentype, out);
|
||||||
if (c.isDigit()) {
|
|
||||||
for (;;) {
|
|
||||||
c = peekChar();
|
|
||||||
if (c.isDigit())
|
|
||||||
nextChar();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
tokentype = BasicTokenType::Parameter;
|
|
||||||
length = m_pos - startpos;
|
|
||||||
QStringRef sr(&m_block, startpos, length);
|
|
||||||
out = sr.toString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (c.isLetter()) {
|
|
||||||
// is this a dollar quote?
|
|
||||||
while (true) {
|
|
||||||
c = nextChar();
|
|
||||||
if (c == '$') {
|
|
||||||
// Found valid dollar quote
|
|
||||||
tokentype = BasicTokenType::DollarQuote;
|
|
||||||
length = m_pos - startpos;
|
|
||||||
QStringRef sr(&m_block, startpos, length);
|
|
||||||
out = sr.toString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (!c.isLetter()) {
|
|
||||||
// ERROR, unallowed character
|
|
||||||
tokentype = BasicTokenType::None;
|
|
||||||
length = m_pos - startpos;
|
|
||||||
QStringRef sr(&m_block, startpos, length);
|
|
||||||
out = sr.toString();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Undetermined symbol
|
// Undetermined symbol
|
||||||
|
|
@ -201,3 +118,103 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool SqlLexer::parseSingleQuotedString(int startpos, int &length, BasicTokenType &tokentype)
|
||||||
|
{
|
||||||
|
while (true) {
|
||||||
|
QChar c = peekChar();
|
||||||
|
if (c == QChar::Null || c == '\n') {
|
||||||
|
// unexpected end, pretend nothings wrong
|
||||||
|
length = m_pos - startpos;
|
||||||
|
tokentype = BasicTokenType::QuotedString;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
nextChar();
|
||||||
|
if (c == '\'') {
|
||||||
|
// maybe end of string literal
|
||||||
|
if (peekChar() == '\'') {
|
||||||
|
// Nope, just double quote to escape quote
|
||||||
|
nextChar(); // eat it
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
length = m_pos - startpos;
|
||||||
|
tokentype = BasicTokenType::QuotedString;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SqlLexer::parseDoubleQuotedIdentifier(int startpos, int &length, BasicTokenType &tokentype)
|
||||||
|
{
|
||||||
|
while (true) {
|
||||||
|
QChar c = peekChar();
|
||||||
|
if (c == QChar::Null || c == '\n') {
|
||||||
|
// unexpected end, pretend nothings wrong
|
||||||
|
length = m_pos - startpos;
|
||||||
|
tokentype = BasicTokenType::QuotedIdentifier;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
nextChar();
|
||||||
|
if (c == '"') {
|
||||||
|
// maybe end of string literal
|
||||||
|
if (peekChar() == '"') {
|
||||||
|
// Nope, just double quote to escape quote
|
||||||
|
nextChar(); // eat it
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
length = m_pos - startpos;
|
||||||
|
tokentype = BasicTokenType::QuotedIdentifier;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SqlLexer::parseDollarQuote(int startpos, int &length, BasicTokenType &tokentype, QString &out)
|
||||||
|
{
|
||||||
|
QChar c = nextChar();
|
||||||
|
if (c.isDigit()) {
|
||||||
|
for (;;) {
|
||||||
|
c = peekChar();
|
||||||
|
if (c.isDigit())
|
||||||
|
nextChar();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokentype = BasicTokenType::Parameter;
|
||||||
|
length = m_pos - startpos;
|
||||||
|
QStringRef sr(&m_block, startpos, length);
|
||||||
|
out = sr.toString();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (c.isLetter()) {
|
||||||
|
// is this a dollar quote?
|
||||||
|
while (true) {
|
||||||
|
c = nextChar();
|
||||||
|
if (c == '$') {
|
||||||
|
// Found valid dollar quote
|
||||||
|
tokentype = BasicTokenType::DollarQuote;
|
||||||
|
length = m_pos - startpos;
|
||||||
|
QStringRef sr(&m_block, startpos, length);
|
||||||
|
out = sr.toString();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (!c.isLetter()) {
|
||||||
|
// ERROR, unallowed character
|
||||||
|
tokentype = BasicTokenType::None;
|
||||||
|
length = m_pos - startpos;
|
||||||
|
QStringRef sr(&m_block, startpos, length);
|
||||||
|
out = sr.toString();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,9 @@ private:
|
||||||
int m_pos = 0;
|
int m_pos = 0;
|
||||||
LexerState m_state;
|
LexerState m_state;
|
||||||
|
|
||||||
|
bool parseSingleQuotedString(int startpos, int &length, BasicTokenType &tokentype);
|
||||||
|
bool parseDoubleQuotedIdentifier(int startpos, int &length, BasicTokenType &tokentype);
|
||||||
|
bool parseDollarQuote(int startpos, int &length, BasicTokenType &tokentype, QString &out);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SQLLEXER_H
|
#endif // SQLLEXER_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue