Added tests - restore target es6 - transpile base into es5
This commit is contained in:
parent
f79202a5b7
commit
ea0f061ff9
@ -10,4 +10,5 @@ export * from './lib/plsql/PlSqlParserListener';
|
|||||||
export * from './lib/plsql/PlSqlParserVisitor';
|
export * from './lib/plsql/PlSqlParserVisitor';
|
||||||
export * from './lib/spark/SparkSqlVisitor';
|
export * from './lib/spark/SparkSqlVisitor';
|
||||||
export * from './lib/spark/SparkSqlListener';
|
export * from './lib/spark/SparkSqlListener';
|
||||||
|
export * from './lib/pgsql/PostgreSQLParserListener';
|
||||||
|
export * from './lib/pgsql/PostgreSQLParserVisitor';
|
||||||
|
67
src/lib/pgsql/base.es6/PostgreSQLLexerBase.js
Normal file
67
src/lib/pgsql/base.es6/PostgreSQLLexerBase.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
const antlr4 = require('antlr4/index');
|
||||||
|
const Lexer = antlr4.Lexer;
|
||||||
|
function isLetter(str) {
|
||||||
|
return str.length === 1 && str.match(/[a-z]/i);
|
||||||
|
}
|
||||||
|
export class PostgreSQLLexerBase extends Lexer {
|
||||||
|
tags = [];
|
||||||
|
|
||||||
|
constructor(input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
pushTag() {
|
||||||
|
this.tags.push(getText());
|
||||||
|
}
|
||||||
|
|
||||||
|
isTag() {
|
||||||
|
return this.getText().equals(this.tags.peek());
|
||||||
|
}
|
||||||
|
|
||||||
|
popTag() {
|
||||||
|
tags.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
getInputStream() {
|
||||||
|
return this._input;
|
||||||
|
}
|
||||||
|
checkLA( c) {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
return this.getInputStream().LA(1) !== c;
|
||||||
|
}
|
||||||
|
|
||||||
|
charIsLetter() {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
return isLetter(this.getInputStream().LA(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
HandleNumericFail() {
|
||||||
|
this.getInputStream().seek(this.getInputStream().index() - 2);
|
||||||
|
const Integral = 535;
|
||||||
|
this.setType(Integral);
|
||||||
|
}
|
||||||
|
|
||||||
|
HandleLessLessGreaterGreater() {
|
||||||
|
const LESS_LESS = 18;
|
||||||
|
const GREATER_GREATER = 19;
|
||||||
|
if (this.getText() === '<<') this.setType(LESS_LESS);
|
||||||
|
if (this.getText() === '>>') this.setType(GREATER_GREATER);
|
||||||
|
}
|
||||||
|
|
||||||
|
UnterminatedBlockCommentDebugAssert() {
|
||||||
|
// Debug.Assert(InputStream.LA(1) == -1 /*EOF*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckIfUtf32Letter() {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
let codePoint = this.getInputStream().LA(-2) << 8 + this.getInputStream().LA(-1);
|
||||||
|
let c;
|
||||||
|
if (codePoint < 0x10000) {
|
||||||
|
c = String.fromCharCode(codePoint);
|
||||||
|
} else {
|
||||||
|
codePoint -= 0x10000;
|
||||||
|
c = String.fromCharCode(codePoint / 0x400 + 0xd800, codePoint % 0x400 + 0xdc00);
|
||||||
|
}
|
||||||
|
return isLetter(c[0]);
|
||||||
|
}
|
||||||
|
}
|
114
src/lib/pgsql/base.es6/PostgreSQLParserBase.js
Normal file
114
src/lib/pgsql/base.es6/PostgreSQLParserBase.js
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* eslint-disable new-cap */
|
||||||
|
import { PostgreSQLLexer } from '../PostgreSQLLexer';
|
||||||
|
import { PostgreSQLParser } from '../PostgreSQLParser';
|
||||||
|
|
||||||
|
|
||||||
|
const antlr4 = require('antlr4/index');
|
||||||
|
const CharStreams = antlr4.CharStreams;
|
||||||
|
const CommonTokenStream = antlr4.CommonTokenStream;
|
||||||
|
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
export class PostgreSQLParserBase extends antlr4.Parser {
|
||||||
|
constructor( input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
GetParsedSqlTree( script, line) {
|
||||||
|
const ph = this.getPostgreSQLParser(script);
|
||||||
|
return ph.program();
|
||||||
|
}
|
||||||
|
|
||||||
|
ParseRoutineBody( _localctx) {
|
||||||
|
let lang = null;
|
||||||
|
for (const coi of _localctx.createfunc_opt_item()) {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
if (!coi.LANGUAGE()) {
|
||||||
|
if (!coi.nonreservedword_or_sconst()) {
|
||||||
|
if (!coi.nonreservedword_or_sconst().nonreservedword()) {
|
||||||
|
if (!coi.nonreservedword_or_sconst().nonreservedword().identifier()) {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
if (!coi.nonreservedword_or_sconst().nonreservedword().identifier().Identifier()) {
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
lang = coi.nonreservedword_or_sconst().nonreservedword().identifier().Identifier().getText();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!lang) return;
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
let func_as = null;
|
||||||
|
for (const a of _localctx.createfunc_opt_item()) {
|
||||||
|
if (!a.func_as()) {
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
func_as = a;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
if (!func_as) {
|
||||||
|
const txt = this.GetRoutineBodyString(func_as.func_as().sconst(0));
|
||||||
|
const line = func_as.func_as().sconst(0).start.getLine();
|
||||||
|
const ph = this.getPostgreSQLParser(txt);
|
||||||
|
switch (lang) {
|
||||||
|
case 'plpgsql':
|
||||||
|
func_as.func_as().Definition = ph.plsqlroot();
|
||||||
|
break;
|
||||||
|
case 'sql':
|
||||||
|
func_as.func_as().Definition = ph.program();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TrimQuotes( s) {
|
||||||
|
return (!s) ? s : s.substring(1, s.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unquote( s) {
|
||||||
|
const slength = s.length();
|
||||||
|
const r = '';
|
||||||
|
let i = 0;
|
||||||
|
while (i < slength) {
|
||||||
|
const c = s.charAt(i);
|
||||||
|
r.append(c);
|
||||||
|
if (c === '\'' && i < slength - 1 && (s.charAt(i + 1) === '\'')) i++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return r.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
GetRoutineBodyString( rule) {
|
||||||
|
const anysconst = rule.anysconst();
|
||||||
|
// eslint-disable-next-line new-cap
|
||||||
|
const StringConstant = anysconst.StringConstant();
|
||||||
|
if (null !== StringConstant) return this.unquote(this.TrimQuotes(StringConstant.getText()));
|
||||||
|
const UnicodeEscapeStringConstant = anysconst.UnicodeEscapeStringConstant();
|
||||||
|
if (null !== UnicodeEscapeStringConstant) return this.TrimQuotes(UnicodeEscapeStringConstant.getText());
|
||||||
|
const EscapeStringConstant = anysconst.EscapeStringConstant();
|
||||||
|
if (null !== EscapeStringConstant) return this.TrimQuotes(EscapeStringConstant.getText());
|
||||||
|
let result = '';
|
||||||
|
const dollartext = anysconst.DollarText();
|
||||||
|
for (const s of dollartext) {
|
||||||
|
result += s.getText();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static getPostgreSQLParser( script) {
|
||||||
|
const charStream = CharStreams.fromString(script);
|
||||||
|
const lexer = new PostgreSQLLexer(charStream);
|
||||||
|
const tokens = new CommonTokenStream(lexer);
|
||||||
|
const parser = new PostgreSQLParser(tokens);
|
||||||
|
lexer.removeErrorListeners();
|
||||||
|
parser.removeErrorListeners();
|
||||||
|
// LexerDispatchingErrorListener listener_lexer = new LexerDispatchingErrorListener((Lexer)(((CommonTokenStream)(this.getInputStream())).getTokenSource()));
|
||||||
|
// ParserDispatchingErrorListener listener_parser = new ParserDispatchingErrorListener(this);
|
||||||
|
// lexer.addErrorListener(listener_lexer);
|
||||||
|
// parser.addErrorListener(listener_parser);
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
}
|
@ -1,67 +1,82 @@
|
|||||||
const antlr4 = require('antlr4/index');
|
"use strict";
|
||||||
const Lexer = antlr4.Lexer;
|
var __extends = (this && this.__extends) || (function () {
|
||||||
|
var extendStatics = function (d, b) {
|
||||||
|
extendStatics = Object.setPrototypeOf ||
|
||||||
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||||
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||||
|
return extendStatics(d, b);
|
||||||
|
};
|
||||||
|
return function (d, b) {
|
||||||
|
if (typeof b !== "function" && b !== null)
|
||||||
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||||
|
extendStatics(d, b);
|
||||||
|
function __() { this.constructor = d; }
|
||||||
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.PostgreSQLLexerBase = void 0;
|
||||||
|
var antlr4 = require('antlr4/index');
|
||||||
|
var Lexer = antlr4.Lexer;
|
||||||
function isLetter(str) {
|
function isLetter(str) {
|
||||||
return str.length === 1 && str.match(/[a-z]/i);
|
return str.length === 1 && str.match(/[a-z]/i);
|
||||||
}
|
}
|
||||||
export class PostgreSQLLexerBase extends Lexer {
|
var PostgreSQLLexerBase = /** @class */ (function (_super) {
|
||||||
tags = [];
|
__extends(PostgreSQLLexerBase, _super);
|
||||||
|
function PostgreSQLLexerBase(input) {
|
||||||
constructor(input) {
|
var _this = _super.call(this, input) || this;
|
||||||
super(input);
|
_this.tags = [];
|
||||||
|
return _this;
|
||||||
}
|
}
|
||||||
|
PostgreSQLLexerBase.prototype.pushTag = function () {
|
||||||
pushTag() {
|
|
||||||
this.tags.push(getText());
|
this.tags.push(getText());
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.isTag = function () {
|
||||||
isTag() {
|
|
||||||
return this.getText().equals(this.tags.peek());
|
return this.getText().equals(this.tags.peek());
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.popTag = function () {
|
||||||
popTag() {
|
|
||||||
tags.pop();
|
tags.pop();
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.getInputStream = function () {
|
||||||
getInputStream() {
|
|
||||||
return this._input;
|
return this._input;
|
||||||
}
|
};
|
||||||
checkLA( c) {
|
PostgreSQLLexerBase.prototype.checkLA = function (c) {
|
||||||
// eslint-disable-next-line new-cap
|
// eslint-disable-next-line new-cap
|
||||||
return this.getInputStream().LA(1) !== c;
|
return this.getInputStream().LA(1) !== c;
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.charIsLetter = function () {
|
||||||
charIsLetter() {
|
|
||||||
// eslint-disable-next-line new-cap
|
// eslint-disable-next-line new-cap
|
||||||
return isLetter(this.getInputStream().LA(-1));
|
return isLetter(this.getInputStream().LA(-1));
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.HandleNumericFail = function () {
|
||||||
HandleNumericFail() {
|
|
||||||
this.getInputStream().seek(this.getInputStream().index() - 2);
|
this.getInputStream().seek(this.getInputStream().index() - 2);
|
||||||
const Integral = 535;
|
var Integral = 535;
|
||||||
this.setType(Integral);
|
this.setType(Integral);
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.HandleLessLessGreaterGreater = function () {
|
||||||
HandleLessLessGreaterGreater() {
|
var LESS_LESS = 18;
|
||||||
const LESS_LESS = 18;
|
var GREATER_GREATER = 19;
|
||||||
const GREATER_GREATER = 19;
|
if (this.getText() === '<<')
|
||||||
if (this.getText() === '<<') this.setType(LESS_LESS);
|
this.setType(LESS_LESS);
|
||||||
if (this.getText() === '>>') this.setType(GREATER_GREATER);
|
if (this.getText() === '>>')
|
||||||
}
|
this.setType(GREATER_GREATER);
|
||||||
|
};
|
||||||
UnterminatedBlockCommentDebugAssert() {
|
PostgreSQLLexerBase.prototype.UnterminatedBlockCommentDebugAssert = function () {
|
||||||
// Debug.Assert(InputStream.LA(1) == -1 /*EOF*/);
|
// Debug.Assert(InputStream.LA(1) == -1 /*EOF*/);
|
||||||
}
|
};
|
||||||
|
PostgreSQLLexerBase.prototype.CheckIfUtf32Letter = function () {
|
||||||
CheckIfUtf32Letter() {
|
|
||||||
// eslint-disable-next-line new-cap
|
// eslint-disable-next-line new-cap
|
||||||
let codePoint = this.getInputStream().LA(-2) << 8 + this.getInputStream().LA(-1);
|
var codePoint = this.getInputStream().LA(-2) << 8 + this.getInputStream().LA(-1);
|
||||||
let c;
|
var c;
|
||||||
if (codePoint < 0x10000) {
|
if (codePoint < 0x10000) {
|
||||||
c = String.fromCharCode(codePoint);
|
c = String.fromCharCode(codePoint);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
codePoint -= 0x10000;
|
codePoint -= 0x10000;
|
||||||
c = String.fromCharCode(codePoint / 0x400 + 0xd800, codePoint % 0x400 + 0xdc00);
|
c = String.fromCharCode(codePoint / 0x400 + 0xd800, codePoint % 0x400 + 0xdc00);
|
||||||
}
|
}
|
||||||
return isLetter(c[0]);
|
return isLetter(c[0]);
|
||||||
}
|
};
|
||||||
}
|
return PostgreSQLLexerBase;
|
||||||
|
}(Lexer));
|
||||||
|
exports.PostgreSQLLexerBase = PostgreSQLLexerBase;
|
@ -1,27 +1,41 @@
|
|||||||
|
"use strict";
|
||||||
|
var __extends = (this && this.__extends) || (function () {
|
||||||
|
var extendStatics = function (d, b) {
|
||||||
|
extendStatics = Object.setPrototypeOf ||
|
||||||
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||||
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||||
|
return extendStatics(d, b);
|
||||||
|
};
|
||||||
|
return function (d, b) {
|
||||||
|
if (typeof b !== "function" && b !== null)
|
||||||
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||||
|
extendStatics(d, b);
|
||||||
|
function __() { this.constructor = d; }
|
||||||
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.PostgreSQLParserBase = void 0;
|
||||||
/* eslint-disable new-cap */
|
/* eslint-disable new-cap */
|
||||||
import { PostgreSQLLexer } from '../PostgreSQLLexer';
|
var PostgreSQLLexer_1 = require("../PostgreSQLLexer");
|
||||||
import { PostgreSQLParser } from '../PostgreSQLParser';
|
var PostgreSQLParser_1 = require("../PostgreSQLParser");
|
||||||
|
var antlr4 = require('antlr4/index');
|
||||||
|
var CharStreams = antlr4.CharStreams;
|
||||||
const antlr4 = require('antlr4/index');
|
var CommonTokenStream = antlr4.CommonTokenStream;
|
||||||
const CharStreams = antlr4.CharStreams;
|
|
||||||
const CommonTokenStream = antlr4.CommonTokenStream;
|
|
||||||
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
export class PostgreSQLParserBase extends antlr4.Parser {
|
var PostgreSQLParserBase = /** @class */ (function (_super) {
|
||||||
constructor( input) {
|
__extends(PostgreSQLParserBase, _super);
|
||||||
super(input);
|
function PostgreSQLParserBase(input) {
|
||||||
|
return _super.call(this, input) || this;
|
||||||
}
|
}
|
||||||
|
PostgreSQLParserBase.prototype.GetParsedSqlTree = function (script, line) {
|
||||||
GetParsedSqlTree( script, line) {
|
var ph = this.getPostgreSQLParser(script);
|
||||||
const ph = this.getPostgreSQLParser(script);
|
|
||||||
return ph.program();
|
return ph.program();
|
||||||
}
|
};
|
||||||
|
PostgreSQLParserBase.prototype.ParseRoutineBody = function (_localctx) {
|
||||||
ParseRoutineBody( _localctx) {
|
var lang = null;
|
||||||
let lang = null;
|
for (var _i = 0, _a = _localctx.createfunc_opt_item(); _i < _a.length; _i++) {
|
||||||
for (const coi of _localctx.createfunc_opt_item()) {
|
var coi = _a[_i];
|
||||||
// eslint-disable-next-line new-cap
|
// eslint-disable-next-line new-cap
|
||||||
if (!coi.LANGUAGE()) {
|
if (!coi.LANGUAGE()) {
|
||||||
if (!coi.nonreservedword_or_sconst()) {
|
if (!coi.nonreservedword_or_sconst()) {
|
||||||
@ -38,10 +52,12 @@ export class PostgreSQLParserBase extends antlr4.Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!lang) return;
|
if (!lang)
|
||||||
|
return;
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
let func_as = null;
|
var func_as = null;
|
||||||
for (const a of _localctx.createfunc_opt_item()) {
|
for (var _b = 0, _c = _localctx.createfunc_opt_item(); _b < _c.length; _b++) {
|
||||||
|
var a = _c[_b];
|
||||||
if (!a.func_as()) {
|
if (!a.func_as()) {
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
func_as = a;
|
func_as = a;
|
||||||
@ -50,59 +66,60 @@ export class PostgreSQLParserBase extends antlr4.Parser {
|
|||||||
}
|
}
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
if (!func_as) {
|
if (!func_as) {
|
||||||
const txt = this.GetRoutineBodyString(func_as.func_as().sconst(0));
|
var txt = this.GetRoutineBodyString(func_as.func_as().sconst(0));
|
||||||
const line = func_as.func_as().sconst(0).start.getLine();
|
var line = func_as.func_as().sconst(0).start.getLine();
|
||||||
const ph = this.getPostgreSQLParser(txt);
|
var ph = this.getPostgreSQLParser(txt);
|
||||||
switch (lang) {
|
switch (lang) {
|
||||||
case 'plpgsql':
|
case 'plpgsql':
|
||||||
func_as.func_as().Definition = ph.plsqlroot();
|
func_as.func_as().Definition = ph.plsqlroot();
|
||||||
break;
|
break;
|
||||||
case 'sql':
|
case 'sql':
|
||||||
func_as.func_as().Definition = ph.program();
|
func_as.func_as().Definition = ph.program();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
PostgreSQLParserBase.prototype.TrimQuotes = function (s) {
|
||||||
TrimQuotes( s) {
|
|
||||||
return (!s) ? s : s.substring(1, s.length() - 1);
|
return (!s) ? s : s.substring(1, s.length() - 1);
|
||||||
}
|
};
|
||||||
|
PostgreSQLParserBase.prototype.unquote = function (s) {
|
||||||
unquote( s) {
|
var slength = s.length();
|
||||||
const slength = s.length();
|
var r = '';
|
||||||
const r = '';
|
var i = 0;
|
||||||
let i = 0;
|
|
||||||
while (i < slength) {
|
while (i < slength) {
|
||||||
const c = s.charAt(i);
|
var c = s.charAt(i);
|
||||||
r.append(c);
|
r.append(c);
|
||||||
if (c === '\'' && i < slength - 1 && (s.charAt(i + 1) === '\'')) i++;
|
if (c === '\'' && i < slength - 1 && (s.charAt(i + 1) === '\''))
|
||||||
|
i++;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return r.toString();
|
return r.toString();
|
||||||
}
|
};
|
||||||
|
PostgreSQLParserBase.prototype.GetRoutineBodyString = function (rule) {
|
||||||
GetRoutineBodyString( rule) {
|
var anysconst = rule.anysconst();
|
||||||
const anysconst = rule.anysconst();
|
|
||||||
// eslint-disable-next-line new-cap
|
// eslint-disable-next-line new-cap
|
||||||
const StringConstant = anysconst.StringConstant();
|
var StringConstant = anysconst.StringConstant();
|
||||||
if (null !== StringConstant) return this.unquote(this.TrimQuotes(StringConstant.getText()));
|
if (null !== StringConstant)
|
||||||
const UnicodeEscapeStringConstant = anysconst.UnicodeEscapeStringConstant();
|
return this.unquote(this.TrimQuotes(StringConstant.getText()));
|
||||||
if (null !== UnicodeEscapeStringConstant) return this.TrimQuotes(UnicodeEscapeStringConstant.getText());
|
var UnicodeEscapeStringConstant = anysconst.UnicodeEscapeStringConstant();
|
||||||
const EscapeStringConstant = anysconst.EscapeStringConstant();
|
if (null !== UnicodeEscapeStringConstant)
|
||||||
if (null !== EscapeStringConstant) return this.TrimQuotes(EscapeStringConstant.getText());
|
return this.TrimQuotes(UnicodeEscapeStringConstant.getText());
|
||||||
let result = '';
|
var EscapeStringConstant = anysconst.EscapeStringConstant();
|
||||||
const dollartext = anysconst.DollarText();
|
if (null !== EscapeStringConstant)
|
||||||
for (const s of dollartext) {
|
return this.TrimQuotes(EscapeStringConstant.getText());
|
||||||
|
var result = '';
|
||||||
|
var dollartext = anysconst.DollarText();
|
||||||
|
for (var _i = 0, dollartext_1 = dollartext; _i < dollartext_1.length; _i++) {
|
||||||
|
var s = dollartext_1[_i];
|
||||||
result += s.getText();
|
result += s.getText();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
PostgreSQLParserBase.getPostgreSQLParser = function (script) {
|
||||||
static getPostgreSQLParser( script) {
|
var charStream = CharStreams.fromString(script);
|
||||||
const charStream = CharStreams.fromString(script);
|
var lexer = new PostgreSQLLexer_1.PostgreSQLLexer(charStream);
|
||||||
const lexer = new PostgreSQLLexer(charStream);
|
var tokens = new CommonTokenStream(lexer);
|
||||||
const tokens = new CommonTokenStream(lexer);
|
var parser = new PostgreSQLParser_1.PostgreSQLParser(tokens);
|
||||||
const parser = new PostgreSQLParser(tokens);
|
|
||||||
lexer.removeErrorListeners();
|
lexer.removeErrorListeners();
|
||||||
parser.removeErrorListeners();
|
parser.removeErrorListeners();
|
||||||
// LexerDispatchingErrorListener listener_lexer = new LexerDispatchingErrorListener((Lexer)(((CommonTokenStream)(this.getInputStream())).getTokenSource()));
|
// LexerDispatchingErrorListener listener_lexer = new LexerDispatchingErrorListener((Lexer)(((CommonTokenStream)(this.getInputStream())).getTokenSource()));
|
||||||
@ -110,5 +127,7 @@ export class PostgreSQLParserBase extends antlr4.Parser {
|
|||||||
// lexer.addErrorListener(listener_lexer);
|
// lexer.addErrorListener(listener_lexer);
|
||||||
// parser.addErrorListener(listener_parser);
|
// parser.addErrorListener(listener_parser);
|
||||||
return parser;
|
return parser;
|
||||||
}
|
};
|
||||||
}
|
return PostgreSQLParserBase;
|
||||||
|
}(antlr4.Parser));
|
||||||
|
exports.PostgreSQLParserBase = PostgreSQLParserBase;
|
@ -3,3 +3,4 @@ export { default as PLSQL } from './plsql';
|
|||||||
export { default as HiveSQL } from './hive';
|
export { default as HiveSQL } from './hive';
|
||||||
export { default as FlinkSQL } from './flinksql';
|
export { default as FlinkSQL } from './flinksql';
|
||||||
export { default as SparkSQL } from './spark';
|
export { default as SparkSQL } from './spark';
|
||||||
|
export { default as PostgresSQL } from './pgsql';
|
||||||
|
@ -4,7 +4,7 @@ import { PostgreSQLParser } from '../lib/pgsql/PostgreSQLParser';
|
|||||||
|
|
||||||
import BasicParser from './common/basicParser';
|
import BasicParser from './common/basicParser';
|
||||||
|
|
||||||
export default class PLSQLParser extends BasicParser {
|
export default class PostgresSQL extends BasicParser {
|
||||||
public createLexer(input: string): Lexer {
|
public createLexer(input: string): Lexer {
|
||||||
const chars = new InputStream(input.toUpperCase());
|
const chars = new InputStream(input.toUpperCase());
|
||||||
const lexer = <unknown> new PostgreSQLLexer(chars) as Lexer;
|
const lexer = <unknown> new PostgreSQLLexer(chars) as Lexer;
|
||||||
|
12
test/parser/pgsql/lexer.test.ts
Normal file
12
test/parser/pgsql/lexer.test.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { PostgresSQL } from '../../../src/';
|
||||||
|
|
||||||
|
describe('PostgresSQL Lexer tests', () => {
|
||||||
|
const mysqlParser = new PostgresSQL();
|
||||||
|
|
||||||
|
const sql = 'select id,name,sex from user1;';
|
||||||
|
const tokens = mysqlParser.getAllTokens(sql);
|
||||||
|
|
||||||
|
test('token counts', () => {
|
||||||
|
expect(tokens.length).toBe(12);
|
||||||
|
});
|
||||||
|
});
|
25
test/parser/pgsql/listener.test.ts
Normal file
25
test/parser/pgsql/listener.test.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { PostgresSQL, PostgreSQLParserListener } from '../../../src';
|
||||||
|
|
||||||
|
describe('PostgresSQL Listener Tests', () => {
|
||||||
|
const expectTableName = 'user1';
|
||||||
|
const sql = `select id,name,sex from ${expectTableName};`;
|
||||||
|
const parser = new PostgresSQL();
|
||||||
|
|
||||||
|
const parserTree = parser.parse(sql);
|
||||||
|
|
||||||
|
console.log('Parser tree string:', parserTree);
|
||||||
|
|
||||||
|
test('Listener enterTableName', async () => {
|
||||||
|
let result = '';
|
||||||
|
class MyListener extends PostgreSQLParserListener {
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
enterTable_ref(ctx): void {
|
||||||
|
result = ctx.getText().toLowerCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const listenTableName: any = new MyListener();
|
||||||
|
|
||||||
|
await parser.listen(listenTableName, parserTree);
|
||||||
|
expect(result).toBe(expectTableName);
|
||||||
|
});
|
||||||
|
});
|
18
test/parser/pgsql/syntax.test.ts
Normal file
18
test/parser/pgsql/syntax.test.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { PostgresSQL } from '../../../src';
|
||||||
|
|
||||||
|
describe('Generic SQL Syntax Tests', () => {
|
||||||
|
const parser = new PostgresSQL();
|
||||||
|
|
||||||
|
test('Select Statement', () => {
|
||||||
|
const sql = 'select id,name from user1;';
|
||||||
|
const result = parser.validate(sql);
|
||||||
|
|
||||||
|
expect(result.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Select 1+1', () => {
|
||||||
|
const sql = 'SELECT 1+1;';
|
||||||
|
const result = parser.validate(sql);
|
||||||
|
expect(result.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
26
test/parser/pgsql/visitor.test.ts
Normal file
26
test/parser/pgsql/visitor.test.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { PostgresSQL, PostgreSQLParserVisitor } from '../../../src';
|
||||||
|
|
||||||
|
describe('Generic SQL Visitor Tests', () => {
|
||||||
|
const expectTableName = 'user1';
|
||||||
|
const sql = `select id,name,sex from ${expectTableName};`;
|
||||||
|
const parser = new PostgresSQL();
|
||||||
|
|
||||||
|
const parserTree = parser.parse(sql, (error) => {
|
||||||
|
console.log('Parse error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Visitor visitTableName', () => {
|
||||||
|
let result = '';
|
||||||
|
class MyVisitor extends PostgreSQLParserVisitor {
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
visitTable_ref(ctx): void {
|
||||||
|
result = ctx.getText().toLowerCase();
|
||||||
|
super.visitTable_ref(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const visitor: any = new MyVisitor();
|
||||||
|
visitor.visit(parserTree);
|
||||||
|
|
||||||
|
expect(result).toBe(expectTableName);
|
||||||
|
});
|
||||||
|
});
|
@ -3,7 +3,7 @@
|
|||||||
"outDir": "./dist/",
|
"outDir": "./dist/",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"allowJs":true,
|
"allowJs":true,
|
||||||
"target": "es5",
|
"target": "es6",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
|
Loading…
Reference in New Issue
Block a user