-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2024, Oracle and/or its affiliates | ||
*/ | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
/* cspell: ignore antlr, longlong, ULONGLONG, MAXDB */ | ||
|
||
#pragma once | ||
#include "antlr4-runtime.h" | ||
#include <string> | ||
#include "SqlMode.h" | ||
#include <queue> | ||
|
||
/** The base lexer class provides a number of functions needed in actions in the lexer (grammar). */ | ||
class MySQLLexerBase : public antlr4::Lexer | ||
{ | ||
|
||
public: | ||
MySQLLexerBase(antlr4::CharStream * input); | ||
int serverVersion; | ||
std::set<SqlMode> sqlModes; | ||
|
||
/** Enable Multi Language Extension support. */ | ||
bool supportMle; | ||
|
||
std::set<std::string> charSets; | ||
|
||
protected: | ||
bool inVersionComment; | ||
|
||
private: | ||
std::queue<antlr4::Token> pendingTokens; | ||
static std::string longString; | ||
static int longLength; | ||
static std::string signedLongString; | ||
static std::string longLongString; | ||
static int longLongLength; | ||
static std::string signedLongLongString; | ||
static int signedLongLongLength; | ||
static std::string unsignedLongLongString; | ||
static int unsignedLongLongLength; | ||
|
||
bool justEmitedDot; | ||
|
||
/** | ||
* Determines if the given SQL mode is currently active in the lexer. | ||
* | ||
* @param mode The mode to check. | ||
* | ||
* @returns True if the mode is one of the currently active modes. | ||
*/ | ||
public: | ||
bool isSqlModeActive(SqlMode mode); | ||
void reset(); | ||
std::unique_ptr<antlr4::Token> nextToken() override; | ||
|
||
protected: | ||
bool checkMySQLVersion(std::string text); | ||
int determineFunction(int proposed); | ||
int determineNumericType(std::string text); | ||
int checkCharset(std::string text); | ||
void emitDot(); | ||
|
||
public: | ||
bool isServerVersionLt80024(); | ||
bool isServerVersionGe80024(); | ||
bool isServerVersionGe80011(); | ||
bool isServerVersionGe80013(); | ||
bool isServerVersionLt80014(); | ||
bool isServerVersionGe80014(); | ||
bool isServerVersionGe80017(); | ||
bool isServerVersionGe80018(); | ||
bool isMasterCompressionAlgorithm(); | ||
bool isServerVersionLt80031(); | ||
void doLogicalOr(); | ||
void doIntNumber(); | ||
void doAdddate(); | ||
void doBitAnd(); | ||
void doBitOr(); | ||
void doBitXor(); | ||
void doCast(); | ||
void doCount(); | ||
void doCurdate(); | ||
void doCurrentDate(); | ||
void doCurrentTime(); | ||
void doCurtime(); | ||
void doDateAdd(); | ||
void doDateSub(); | ||
void doExtract(); | ||
void doGroupConcat(); | ||
void doMax(); | ||
void doMid(); | ||
void doMin(); | ||
void doNot(); | ||
void doNow(); | ||
void doPosition(); | ||
void doSessionUser(); | ||
void doStddevSamp(); | ||
void doStddev(); | ||
void doStddevPop(); | ||
void doStd(); | ||
void doSubdate(); | ||
void doSubstr(); | ||
void doSubstring(); | ||
void doSum(); | ||
void doSysdate(); | ||
void doSystemUser(); | ||
void doTrim(); | ||
void doVariance(); | ||
void doVarPop(); | ||
void doVarSamp(); | ||
void doUnderscoreCharset(); | ||
bool isVersionComment(); | ||
bool isBackTickQuotedId(); | ||
bool isDoubleQuotedText(); | ||
bool isSingleQuotedText(); | ||
antlr4::Token* emit() override; | ||
void startInVersionComment(); | ||
void endInVersionComment(); | ||
bool isInVersionComment(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024, Oracle and/or its affiliates | ||
*/ | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
/* cspell: ignore antlr, longlong, ULONGLONG, MAXDB */ | ||
|
||
#pragma once | ||
#include "antlr4-runtime.h" | ||
#include <string> | ||
#include "SqlMode.h" | ||
#include <set> | ||
|
||
class MySQLParserBase : public antlr4::Parser { | ||
|
||
// To parameterize the parsing process. | ||
public: | ||
int serverVersion; | ||
std::set<SqlMode> sqlModes; | ||
|
||
public: | ||
/** Enable Multi Language Extension support. */ | ||
bool supportMle; | ||
|
||
protected: | ||
MySQLParserBase(antlr4::TokenStream* input); | ||
|
||
public: | ||
/** | ||
* Determines if the given SQL mode is currently active in the lexer. | ||
* | ||
* @param mode The mode to check. | ||
* | ||
* @returns True if the mode is one of the currently active modes. | ||
*/ | ||
bool isSqlModeActive(SqlMode mode); | ||
bool isPureIdentifier(); | ||
bool isTextStringLiteral(); | ||
bool isStoredRoutineBody(); | ||
bool isSelectStatementWithInto(); | ||
}; |
6 changes: 4 additions & 2 deletions
6
sql/mysql/Oracle/Cpp/SqlMode.java → sql/mysql/Oracle/Cpp/SqlMode.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
#pragma once | ||
|
||
/** SQL modes that control parsing behavior. */ | ||
public enum SqlMode { | ||
enum SqlMode { | ||
NoMode, | ||
AnsiQuotes, | ||
HighNotPrecedence, | ||
PipesAsConcat, | ||
IgnoreSpace, | ||
NoBackslashEscapes | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** SQL modes that control parsing behavior. */ | ||
#include once | ||
#include "SqlMode.h" | ||
|
||
class SqlModes { | ||
|
||
/** | ||
* Converts a mode string into individual mode flags. | ||
* | ||
* @param modes The input string to parse. | ||
*/ | ||
public: | ||
static std::set<SqlMode> sqlModeFromString(std::string modes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import sys, os, re, shutil | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
def main(argv): | ||
for file in glob("./*.g4"): | ||
fix(file) | ||
|
||
def fix(file_path): | ||
print("Altering " + file_path) | ||
if not os.path.exists(file_path): | ||
print(f"Could not find file: {file_path}") | ||
sys.exit(1) | ||
parts = os.path.split(file_path) | ||
file_name = parts[-1] | ||
shutil.move(file_path, file_path + ".bak") | ||
input_file = open(file_path + ".bak",'r') | ||
output_file = open(file_path, 'w') | ||
for x in input_file: | ||
if '// Insert here @header for lexer.' in x: | ||
x = x.replace('// Insert here @header for lexer.', '@header {#include "MySQLLexerBase.h"}') | ||
if '// Insert here @header for parser.' in x: | ||
x = x.replace('// Insert here @header for parser.', '@header {#include "MySQLParserBase.h"}') | ||
if 'this.' in x: | ||
x = x.replace('this.', 'this->') | ||
output_file.write(x) | ||
output_file.flush() | ||
|
||
print("Writing ...") | ||
input_file.close() | ||
output_file.close() | ||
|
||
if __name__ == '__main__': | ||
main(sys.argv) |