-
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.
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
Showing
31 changed files
with
1,346 additions
and
523 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,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)); | ||
} | ||
} |
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,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 | ||
} | ||
} |
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,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() |
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
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
Oops, something went wrong.