Skip to content

Commit

Permalink
Add in Cpp port (incomplete).
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Nov 29, 2024
1 parent 4c3d911 commit 8bf6c26
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 2 deletions.
121 changes: 121 additions & 0 deletions sql/mysql/Oracle/Cpp/MySQLLexerBase.h
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();
};
41 changes: 41 additions & 0 deletions sql/mysql/Oracle/Cpp/MySQLParserBase.h
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();
};
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
}
};
14 changes: 14 additions & 0 deletions sql/mysql/Oracle/Cpp/SqlModes.h
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);
}
34 changes: 34 additions & 0 deletions sql/mysql/Oracle/Cpp/transformGrammar.py
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)

0 comments on commit 8bf6c26

Please sign in to comment.