Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plsql] Added Antlr4ng, Cpp, Dart, Go ports. Dart-specific .g4 files removed. #4362

Merged
merged 23 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion sql/plsql/.trgen-ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
examples/
examples-sql-script/
more/
11 changes: 11 additions & 0 deletions sql/plsql/Antlr4ng/PlSqlLexerBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommonToken, Lexer, CharStream, Token, CommonTokenStream } from "antlr4ng";

export abstract class PlSqlLexerBase extends Lexer {
self : PlSqlLexerBase;

IsNewlineAtPos(pos: number): boolean {
const la = this.inputStream.LA(pos);
return la == -1 || String.fromCharCode(la) == '\n';
}

}
30 changes: 30 additions & 0 deletions sql/plsql/Antlr4ng/PlSqlParserBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Parser, TokenStream, CommonTokenStream, Recognizer } from 'antlr4ng';

export abstract class PlSqlParserBase extends Parser {

_isVersion10: boolean;
_isVersion12: boolean;

constructor(input: TokenStream) {
super(input);
this._isVersion10 = true;
this._isVersion12 = true;
}

isVersion10(): boolean {
return this._isVersion10;
}

isVersion12(): boolean {
return this._isVersion12;
}

setVersion10(value: boolean): void {
this._isVersion10 = value;
}

setVersion12(value: boolean): void {
this._isVersion12 = value;
}

}
33 changes: 33 additions & 0 deletions sql/plsql/Antlr4ng/transformGrammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""The script transforms the grammar to fit for the c++ target """
import sys
import re
import shutil
from glob import glob
from pathlib import Path

def main():
"""Executes the script."""
for file in glob("./*.g4"):
transform_grammar(file)

def transform_grammar(file_path):
"""Transforms the grammar to fit for the target"""
print("Altering " + file_path)
if not Path(file_path).is_file:
print(f"Could not find file: {file_path}")
sys.exit(1)

shutil.move(file_path, file_path + ".bak")
with open(file_path + ".bak",'r', encoding="utf-8") as input_file:
with open(file_path, 'w', encoding="utf-8") as output_file:
for line in input_file:
line = re.sub(r"(\/\/ Insert here @header for C\+\+ lexer\.)",\
'@header {import { PlSqlLexerBase } from "./PlSqlLexerBase.js"}', line)
line = re.sub(r"(\/\/ Insert here @header for C\+\+ parser\.)",\
'@header {import { PlSqlParserBase } from "./PlSqlParserBase.js"}', line)
output_file.write(line)

print("Writing ...")

if __name__ == '__main__':
main()
3 changes: 0 additions & 3 deletions sql/plsql/CSharp/PlSqlLexerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
public class PlSqlLexerBase : Lexer
{
ICharStream myinput;
public PlSqlLexerBase self;

public override string[] RuleNames => throw new NotImplementedException();

Expand All @@ -19,14 +18,12 @@ protected PlSqlLexerBase(ICharStream input, TextWriter output, TextWriter errorO
: base(input, output, errorOutput)
{
myinput = input;
self = this;
}

public PlSqlLexerBase(ICharStream input)
: base(input)
{
myinput = input;
self = this;
}

public bool IsNewlineAtPos(int pos)
Expand Down
2 changes: 0 additions & 2 deletions sql/plsql/CSharp/PlSqlParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ public abstract class PlSqlParserBase : Parser
{
private bool _isVersion10 = false;
private bool _isVersion12 = true;
public PlSqlParserBase self;

protected PlSqlParserBase(ITokenStream input)
: base(input)
{
self = this;
}

public PlSqlParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput) : this(input)
Expand Down
13 changes: 3 additions & 10 deletions sql/plsql/Cpp/PlSqlLexerBase.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
#ifndef PLSQLLEXERBASE_H
#define PLSQLLEXERBASE_H

#pragma once
#include "antlr4-runtime.h"

class PlSqlLexerBase : public antlr4::Lexer
{
public:
PlSqlLexerBase(antlr4::CharStream *input) : Lexer(input), self(*this) { }

public:
PlSqlLexerBase & self;
PlSqlLexerBase(antlr4::CharStream *input) : Lexer(input) { };

public:
bool IsNewlineAtPos(int pos)
{
int la = _input->LA(pos);
return la == -1 || la == '\n';
}
};
};

#endif
4 changes: 1 addition & 3 deletions sql/plsql/Cpp/PlSqlParserBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ class PlSqlParserBase : public antlr4::Parser
{
bool _isVersion12 = true;
bool _isVersion10 = true;
public:
PlSqlParserBase & self;

public:
PlSqlParserBase(antlr4::TokenStream *input) : Parser(input), self(*this) { }
PlSqlParserBase(antlr4::TokenStream *input) : Parser(input) { }

bool isVersion12()
{
Expand Down
73 changes: 73 additions & 0 deletions sql/plsql/Cpp/st.CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Generated from trgen <version>

cmake_minimum_required (VERSION 3.14)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

set(CMAKE_CXX_STANDARD 17)
set(ANTLR4_TAG 4.13.1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
#SET(GCC_COVERAGE_COMPILE_FLAGS "-g -pg")
#SET(GCC_COVERAGE_LINK_FLAGS "-g -pg")
<if(os_win)>
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT /bigobj")
<endif>
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

add_definitions(-DANTLR4CPP_STATIC)

include(ExternalAntlr4Cpp)
include_directories(${ANTLR4_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/)
find_package(ANTLR REQUIRED)

<if(is_combined_grammar)>
antlr_target(
<grammar_name>
<grammar_file>
)

include_directories(${ANTLR4_INCLUDE_DIRS})
include_directories(${ANTLR_<grammar_name>_OUTPUT_DIR})

add_executable(Test
Test.cpp
ErrorListener.cpp
ErrorListener.h
<additional_sources:{x | <x>
} >${ANTLR_<grammar_name>_CXX_OUTPUTS}
)

<else>
antlr_target(
<lexer_name>
<lexer_grammar_file>
LEXER
)
antlr_target(
<parser_name>
<parser_grammar_file>
PARSER
DEPENDS_ANTLR <lexer_name>
COMPILE_FLAGS -lib ${ANTLR_<lexer_name>_OUTPUT_DIR}
)

include_directories(${ANTLR_<lexer_name>_OUTPUT_DIR})
include_directories(${ANTLR_<parser_name>_OUTPUT_DIR})

add_executable(Test
Test.cpp
ErrorListener.cpp
ErrorListener.h
<additional_sources:{x | <x>
} >${ANTLR_<lexer_name>_CXX_OUTPUTS}
${ANTLR_<parser_name>_CXX_OUTPUTS}
)

<endif>

<if(os_win)>
target_compile_options(Test PUBLIC /MT /bigobj)
<endif>

target_link_libraries(Test antlr4_static Threads::Threads)
34 changes: 34 additions & 0 deletions sql/plsql/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 C++ lexer.' in x:
x = x.replace('// Insert here @header for C++ lexer.', '@header {#include "PlSqlLexerBase.h"}')
if '// Insert here @header for C++ parser.' in x:
x = x.replace('// Insert here @header for C++ parser.', '@header {#include "PlSqlParserBase.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)
Loading
Loading