Skip to content

Commit

Permalink
[postgresql] Fix for #4324; add ports. (#4325)
Browse files Browse the repository at this point in the history
* Fixes for #4321, #4322

* Update README.md

Warnings fixed.

* Fixes for #4324, adding ports.

* Fix Antlr4ng, CSharp, and Java ports. Change Deque to Stack to clarify port code.

* Add TypeScript port, fix symbol conflict for TypeScript port.

The TypeScript port generates `any_name._list()` accessor method for alts with multiple `any_name` references. This conflicts with the symbol reference `any_name_list`, which is another production.

* Fix Cpp target.

* Try to get Mac to behave.

* Not sure what happened. Try again.

* Try3.

* Add Dart target.

* Add Python3 target.

* Add JavaScript port.

* Fix Go port.

* Add blurb on target agnostic to readme.

* Clean up. Force rebuild of Macos Java.
  • Loading branch information
kaby76 authored Nov 17, 2024
1 parent 7965202 commit 5c7ff0b
Show file tree
Hide file tree
Showing 31 changed files with 1,346 additions and 523 deletions.
115 changes: 115 additions & 0 deletions sql/postgresql/Antlr4ng/PostgreSQLLexerBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
PostgreSQL grammar.
The MIT License (MIT).
Copyright (c) 2021-2023, Oleksii Kovalov ([email protected]).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import { CommonToken, Lexer, CharStream, Token, CommonTokenStream } from "antlr4ng";
import { PostgreSQLLexer } from './PostgreSQLLexer.js';


interface IStack<T> {
push(item: T): void;
pop(): T | undefined;
peek(): T | undefined;
size(): number;
}

class Stack<T> implements IStack<T> {
private storage: T[] = [];

constructor(private capacity: number = Infinity) {}

push(item: T): void {
if (this.size() === this.capacity) {
throw Error("Stack has reached max capacity, you cannot add more items");
}
this.storage.push(item);
}

pop(): T | undefined {
return this.storage.pop();
}

peek(): T | undefined {
return this.storage[this.size() - 1];
}

size(): number {
return this.storage.length;
}
}


export abstract class PostgreSQLLexerBase extends Lexer {
protected tags: Stack<string> = new Stack();

constructor(input: CharStream) {
super(input);
}

public PushTag(): void {
this.tags.push(this.text);
}

public IsTag(): boolean {
return this.text === this.tags.peek();
}

public PopTag(): void {
this.tags.pop();
}

public UnterminatedBlockCommentDebugAssert(): void {
console.assert(this.inputStream.LA(1) === -1 /*EOF*/);
}

public CheckLaMinus(): boolean {
return this.inputStream.LA(1) !== '-'.charCodeAt(0);
}

public CheckLaStar(): boolean {
return this.inputStream.LA(1) !== '*'.charCodeAt(0);
}

public CharIsLetter(): boolean {
return /^[a-zA-Z]$/.test(String.fromCharCode(this.inputStream.LA(-1)));
}

public HandleNumericFail(): void {
this.inputStream.seek(this.inputStream.index - 2);
// Assuming PostgreSQLLexer.Integral to be an enum or token type
this.type = PostgreSQLLexer.Integral; // Adjust as per actual token type definition
}

public HandleLessLessGreaterGreater(): void {
if (this.text.toString() === "<<") this.type = PostgreSQLLexer.LESS_LESS;
if (this.text.toString() === ">>") this.type = PostgreSQLLexer.GREATER_GREATER;
}

public CheckIfUtf32Letter(): boolean {
const highSurrogate = String.fromCharCode(this.inputStream.LA(-2));
const lowSurrogate = String.fromCharCode(this.inputStream.LA(-1));
const combined = highSurrogate + lowSurrogate;
return /^[a-zA-Z]$/.test(combined);
}

public IsSemiColon(): boolean {
return ';' === String.fromCharCode(this.inputStream.LA(1));
}
}
111 changes: 111 additions & 0 deletions sql/postgresql/Antlr4ng/PostgreSQLParserBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
PostgreSQL grammar.
The MIT License (MIT).
Copyright (c) 2021-2023, Oleksii Kovalov ([email protected]).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import { Parser, TokenStream, CommonTokenStream, Recognizer } from 'antlr4ng';
//import { ParseTree } from 'antlr4ts/tree/ParseTree';
//import { PostgreSQLLexer } from './PostgreSQLLexer'; // Assuming equivalent lexer is available
//import { PostgreSQLParser } from './PostgreSQLParser'; // Assuming equivalent parser is available

export abstract class PostgreSQLParserBase extends Parser {
constructor(input: TokenStream) {
super(input);
}

// protected getParsedSqlTree(script: string, line: number = 0): ParseTree {
// const ph = this.getPostgreSQLParser(script);
// return ph.root(); // Replace with correct start rule
// }

protected ParseRoutineBody(): void {
// const _localctx = this.context as PostgreSQLParser.Createfunc_opt_listContext;
// const lang = _localctx
// .createfunc_opt_item()
// .find(coi => coi.LANGUAGE() !== null)
// ?.nonreservedword_or_sconst()?.nonreservedword()?.identifier()?.Identifier()?.text;
//
// const func_as = _localctx.createfunc_opt_item().find(coi => coi.func_as() !== null);
// if (func_as) {
// const txt = this.getRoutineBodyString(func_as.func_as().sconst(0));
// switch (lang) {
// case 'plpgsql':
// // Placeholder for mutating tree.
// break;
// case 'sql':
// // Placeholder for mutating tree.
// break;
// }
// }
}

private trimQuotes(s: string): string {
return s ? s.slice(1, -1) : s;
}

private unquote(s: string): string {
let r = '';
for (let i = 0; i < s.length; i++) {
const c = s[i];
r += c;
if (c === "'" && i < s.length - 1 && s[i + 1] === "'") i++;
}
return r;
}

/*
private getRoutineBodyString(rule: PostgreSQLParser.SconstContext): string {
const anysconst = rule.anysconst();
const StringConstant = anysconst.StringConstant();
if (StringConstant) return this.unquote(this.trimQuotes(StringConstant.text));
const UnicodeEscapeStringConstant = anysconst.UnicodeEscapeStringConstant();
if (UnicodeEscapeStringConstant) return this.trimQuotes(UnicodeEscapeStringConstant.text);
const EscapeStringConstant = anysconst.EscapeStringConstant();
if (EscapeStringConstant) return this.trimQuotes(EscapeStringConstant.text);
let result = '';
const dollarText = anysconst.DollarText();
dollarText.forEach(s => result += s);
return result;
}
private getPostgreSQLParser(script: string): PostgreSQLParser {
const charStream = CharStreams.fromString(script);
const lexer = new PostgreSQLLexer(charStream);
const tokens = new CommonTokenStream(lexer);
const parser = new PostgreSQLParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
// Error listeners are assumed to be available in the environment
const listenerLexer = new LexerDispatchingErrorListener(tokens.tokenSource as any); // Adjust as needed
const listenerParser = new ParserDispatchingErrorListener(this);
lexer.addErrorListener(listenerLexer);
parser.addErrorListener(listenerParser);
return parser;
}
*/
public OnlyAcceptableOps(): boolean {
const c = (this.inputStream as CommonTokenStream).LT(1);
const text = c.text;
return text === '!' || text === '!!' || text === '!=-'; // Code for specific example
}
}
33 changes: 33 additions & 0 deletions sql/postgresql/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 { PostgreSQLLexerBase } from "./PostgreSQLLexerBase.js"}', line)
line = re.sub(r"(\/\/ Insert here @header for C\+\+ parser\.)",\
'@header {import { PostgreSQLParserBase } from "./PostgreSQLParserBase.js"}', line)
output_file.write(line)

print("Writing ...")

if __name__ == '__main__':
main()
44 changes: 25 additions & 19 deletions sql/postgresql/CSharp/PostgreSQLLexerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,58 @@ THE SOFTWARE.
using System.Diagnostics;
using System.IO;

public class PostgreSQLLexerBase : Lexer
public abstract class PostgreSQLLexerBase : Lexer
{
protected static Queue<string> tags = new Queue<string>();
protected Stack<string> tags = new Stack<string>();

public PostgreSQLLexerBase(ICharStream input) : base(input)
public PostgreSQLLexerBase(ICharStream input)
: base(input)
{
}

public PostgreSQLLexerBase(ICharStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
: base(input, output, errorOutput)
{
}

private IIntStream getInputStream() { return InputStream; }
public override string[] RuleNames => throw new NotImplementedException();

public override IVocabulary Vocabulary => throw new NotImplementedException();

public override string GrammarFileName => throw new NotImplementedException();

public void pushTag() { tags.Enqueue(this.Text); }
public void PushTag()
{
tags.Push(this.Text);
}

public bool isTag() { return this.Text.Equals(tags.Peek()); }
public bool IsTag()
{
return this.Text.Equals(tags.Peek());
}

public void popTag()
public void PopTag()
{
tags.Dequeue();
tags.Pop();
}

public void UnterminatedBlockCommentDebugAssert()
{
Debug.Assert(InputStream.LA(1) == -1 /*EOF*/);
}

public bool checkLA(int c)
public bool CheckLaMinus()
{
return this.InputStream.LA(1) != '-';
}

public bool CheckLaStar()
{
return getInputStream().LA(1) != c;
return this.InputStream.LA(1) != '*';
}

public bool charIsLetter()
public bool CharIsLetter()
{
return Char.IsLetter((char)InputStream.LA(-1));
}

public void HandleNumericFail()
{
InputStream.Seek(getInputStream().Index - 2);
InputStream.Seek(this.InputStream.Index - 2);
Type = PostgreSQLLexer.Integral;
}

Expand Down
15 changes: 9 additions & 6 deletions sql/postgresql/CSharp/PostgreSQLParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ THE SOFTWARE.

public abstract class PostgreSQLParserBase : Parser
{
public PostgreSQLParserBase(ITokenStream input) : base(input)
public PostgreSQLParserBase(ITokenStream input)
: base(input)
{
}

public PostgreSQLParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput) : base(input, output, errorOutput)
public PostgreSQLParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
{
}

Expand All @@ -43,8 +45,9 @@ internal IParseTree GetParsedSqlTree(string script, int line = 0)
return result;
}

internal void ParseRoutineBody(PostgreSQLParser.Createfunc_opt_listContext _localctx)
internal void ParseRoutineBody()
{
PostgreSQLParser.Createfunc_opt_listContext _localctx = this.Context as PostgreSQLParser.Createfunc_opt_listContext;
var lang =
_localctx
.createfunc_opt_item()
Expand Down Expand Up @@ -81,7 +84,7 @@ private string TrimQuotes(string s)
return string.IsNullOrEmpty(s) ? s : s.Substring(1, s.Length - 2);
}

public string unquote(string s)
private string unquote(string s)
{
var r = new StringBuilder(s.Length);
var i = 0;
Expand All @@ -95,7 +98,7 @@ public string unquote(string s)
return r.ToString();
}

public string GetRoutineBodyString(PostgreSQLParser.SconstContext rule)
private string GetRoutineBodyString(PostgreSQLParser.SconstContext rule)
{
var anysconst = rule.anysconst();
var StringConstant = anysconst.StringConstant();
Expand All @@ -113,7 +116,7 @@ public string GetRoutineBodyString(PostgreSQLParser.SconstContext rule)
return result;
}

public PostgreSQLParser getPostgreSQLParser(string script)
private PostgreSQLParser getPostgreSQLParser(string script)
{
var charStream = CharStreams.fromString(script);
var lexer = new PostgreSQLLexer(charStream);
Expand Down
Loading

0 comments on commit 5c7ff0b

Please sign in to comment.