From 091040f13f78ee3a5d6c3e5935b29c6338d324d5 Mon Sep 17 00:00:00 2001 From: eelke Date: Tue, 26 Dec 2017 07:32:52 +0100 Subject: [PATCH] Split big function in sql lexer into several functions. --- core/SqlLexer.cpp | 189 +++++++++++++++++++++++++--------------------- core/SqlLexer.h | 4 +- 2 files changed, 106 insertions(+), 87 deletions(-) diff --git a/core/SqlLexer.cpp b/core/SqlLexer.cpp index f28eedb..e211996 100644 --- a/core/SqlLexer.cpp +++ b/core/SqlLexer.cpp @@ -59,57 +59,11 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent } else if (c == '\'') { // Single quoted string so it's an SQL text literal - while (true) { - 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; - } - } - } - } + return parseSingleQuotedString(startpos, length, tokentype); } else if (c == '"') { // Double quoted identifier - while (true) { - 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; - } - } - } - } + return parseDoubleQuotedIdentifier(startpos, length, tokentype); } // else if (c == '/' && peekChar() == '*') { // nextChar(); @@ -119,44 +73,7 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent break; } else if (c == '$') { - 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 parseDollarQuote(startpos, length, tokentype, out); } else { // Undetermined symbol @@ -201,3 +118,103 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent 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; +} diff --git a/core/SqlLexer.h b/core/SqlLexer.h index 2ad3405..18bbd35 100644 --- a/core/SqlLexer.h +++ b/core/SqlLexer.h @@ -44,7 +44,9 @@ private: int m_pos = 0; 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