From 3831deaa86fea250c8919c63ede18837a3852052 Mon Sep 17 00:00:00 2001 From: Ken Domino Date: Tue, 8 Aug 2023 22:24:46 -0400 Subject: [PATCH] [plsql] Fix for #3647 -- added TypeScript and JavaScript ports (#3650) * Fix for #3647 The grammar was ported to JS and TS, requiring a few changes to bring it into "target agnostic format". A small subset of "Hello World" examples is used to test the JS and TS ports because the parser is pretty slow. * Update desc.xml to specify minimum required version of Antlr4. --- sql/plsql/PlSqlLexer.g4 | 4 ++-- sql/plsql/PlSqlParser.g4 | 20 +++++++++---------- sql/plsql/TypeScript/PlSqlLexerBase.ts | 5 +++-- sql/plsql/TypeScript/PlSqlParserBase.ts | 4 ++-- sql/plsql/desc.xml | 14 +++++++++++-- sql/plsql/hw-examples/alter_operator.sql | 1 + sql/plsql/hw-examples/alter_outline.sql | 1 + sql/plsql/hw-examples/drop_operator.sql | 1 + .../hw-examples/keywordasidentifier02.sql | 1 + sql/plsql/hw-examples/lexer01.sql | 2 ++ sql/plsql/hw-examples/lexer02.sql | 2 ++ sql/plsql/hw-examples/lexer03.sql | 2 ++ sql/plsql/hw-examples/lexer04.sql | 1 + sql/plsql/hw-examples/lexer05.sql | 1 + sql/plsql/hw-examples/simple11.sql | 1 + sql/plsql/hw-examples/truncate_table.sql | 1 + 16 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 sql/plsql/hw-examples/alter_operator.sql create mode 100644 sql/plsql/hw-examples/alter_outline.sql create mode 100644 sql/plsql/hw-examples/drop_operator.sql create mode 100644 sql/plsql/hw-examples/keywordasidentifier02.sql create mode 100644 sql/plsql/hw-examples/lexer01.sql create mode 100644 sql/plsql/hw-examples/lexer02.sql create mode 100644 sql/plsql/hw-examples/lexer03.sql create mode 100644 sql/plsql/hw-examples/lexer04.sql create mode 100644 sql/plsql/hw-examples/lexer05.sql create mode 100644 sql/plsql/hw-examples/simple11.sql create mode 100644 sql/plsql/hw-examples/truncate_table.sql diff --git a/sql/plsql/PlSqlLexer.g4 b/sql/plsql/PlSqlLexer.g4 index 1106d6e1e6..3c6cc5954e 100644 --- a/sql/plsql/PlSqlLexer.g4 +++ b/sql/plsql/PlSqlLexer.g4 @@ -2449,10 +2449,10 @@ INTRODUCER: '_'; SINGLE_LINE_COMMENT: '--' ~('\r' | '\n')* NEWLINE_EOF -> channel(HIDDEN); MULTI_LINE_COMMENT: '/*' .*? '*/' -> channel(HIDDEN); // https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve034.htm#SQPUG054 -REMARK_COMMENT: 'REM' {self.IsNewlineAtPos(-4)}? 'ARK'? (' ' ~('\r' | '\n')*)? NEWLINE_EOF -> channel(HIDDEN); +REMARK_COMMENT: 'REM' {this.IsNewlineAtPos(-4)}? 'ARK'? (' ' ~('\r' | '\n')*)? NEWLINE_EOF -> channel(HIDDEN); // https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve032.htm#SQPUG052 -PROMPT_MESSAGE: 'PRO' {self.IsNewlineAtPos(-4)}? 'MPT'? (' ' ~('\r' | '\n')*)? NEWLINE_EOF; +PROMPT_MESSAGE: 'PRO' {this.IsNewlineAtPos(-4)}? 'MPT'? (' ' ~('\r' | '\n')*)? NEWLINE_EOF; // TODO: should starts with newline START_CMD diff --git a/sql/plsql/PlSqlParser.g4 b/sql/plsql/PlSqlParser.g4 index 390570433c..089536989e 100644 --- a/sql/plsql/PlSqlParser.g4 +++ b/sql/plsql/PlSqlParser.g4 @@ -2215,7 +2215,7 @@ storage_table_clause // https://docs.oracle.com/database/121/SQLRF/statements_4008.htm#SQLRF56110 unified_auditing - : {self.isVersion12()}? + : {this.isVersion12()}? AUDIT (POLICY policy_name ((BY | EXCEPT) audit_user (',' audit_user)* )? (WHENEVER NOT? SUCCESSFUL)? | CONTEXT NAMESPACE oracle_namespace @@ -2243,11 +2243,11 @@ audit_traditional ; audit_direct_path - : {self.isVersion12()}? DIRECT_PATH auditing_by_clause + : {this.isVersion12()}? DIRECT_PATH auditing_by_clause ; audit_container_clause - : {self.isVersion12()}? (CONTAINER EQUALS_OP (CURRENT | ALL)) + : {this.isVersion12()}? (CONTAINER EQUALS_OP (CURRENT | ALL)) ; audit_operation_clause @@ -2289,7 +2289,7 @@ auditing_on_clause : ON ( object_name | DIRECTORY regular_id | MINING MODEL model_name - | {self.isVersion12()}? SQL TRANSLATION PROFILE profile_name + | {this.isVersion12()}? SQL TRANSLATION PROFILE profile_name | DEFAULT ) ; @@ -2317,7 +2317,7 @@ sql_statement_shortcut | MATERIALIZED VIEW | NOT EXISTS | OUTLINE - | {self.isVersion12()}? PLUGGABLE DATABASE + | {this.isVersion12()}? PLUGGABLE DATABASE | PROCEDURE | PROFILE | PUBLIC DATABASE LINK @@ -2550,11 +2550,11 @@ credential_name ; library_editionable - : {self.isVersion12()}? (EDITIONABLE | NONEDITIONABLE) + : {this.isVersion12()}? (EDITIONABLE | NONEDITIONABLE) ; library_debug - : {self.isVersion12()}? DEBUG + : {this.isVersion12()}? DEBUG ; @@ -2623,7 +2623,7 @@ alter_view ; alter_view_editionable - : {self.isVersion12()}? (EDITIONABLE | NONEDITIONABLE) + : {this.isVersion12()}? (EDITIONABLE | NONEDITIONABLE) ; // https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/CREATE-VIEW.html @@ -4222,7 +4222,7 @@ partial_database_recovery ; partial_database_recovery_10g - : {self.isVersion10()}? STANDBY + : {this.isVersion10()}? STANDBY ( TABLESPACE tablespace (',' tablespace)* | DATAFILE CHAR_STRING | filenumber (',' CHAR_STRING | filenumber)* ) @@ -5046,7 +5046,7 @@ lob_partition_storage ; period_definition - : {self.isVersion12()}? PERIOD FOR column_name + : {this.isVersion12()}? PERIOD FOR column_name ( '(' start_time_column ',' end_time_column ')' )? ; diff --git a/sql/plsql/TypeScript/PlSqlLexerBase.ts b/sql/plsql/TypeScript/PlSqlLexerBase.ts index 7461ad073c..c29aa49b6f 100644 --- a/sql/plsql/TypeScript/PlSqlLexerBase.ts +++ b/sql/plsql/TypeScript/PlSqlLexerBase.ts @@ -1,6 +1,7 @@ -import { Lexer } from "antlr4ts"; +import { CommonToken, Lexer, CharStream, Token } from "antlr4"; +import PlSqlParser from './PlSqlParser'; -export abstract class PlSqlLexerBase extends Lexer { +export default abstract class PlSqlLexerBase extends Lexer { self : PlSqlLexerBase; IsNewlineAtPos(pos: number): boolean { diff --git a/sql/plsql/TypeScript/PlSqlParserBase.ts b/sql/plsql/TypeScript/PlSqlParserBase.ts index e87c9c0cca..56cf756791 100644 --- a/sql/plsql/TypeScript/PlSqlParserBase.ts +++ b/sql/plsql/TypeScript/PlSqlParserBase.ts @@ -1,6 +1,6 @@ -import { Parser, TokenStream } from "antlr4ts" +import { Parser, TokenStream } from "antlr4"; -export abstract class PlSqlParserBase extends Parser { +export default abstract class PlSqlParserBase extends Parser { _isVersion10: boolean; _isVersion12: boolean; diff --git a/sql/plsql/desc.xml b/sql/plsql/desc.xml index 64f0ec5f50..3203a2d0ed 100644 --- a/sql/plsql/desc.xml +++ b/sql/plsql/desc.xml @@ -1,5 +1,15 @@ - ^4.10 - CSharp;Java + ^4.12.0 + CSharp;Java;JavaScript;TypeScript + + hw + JavaScript;TypeScript + hw-examples + + + full + CSharp;Java + examples + diff --git a/sql/plsql/hw-examples/alter_operator.sql b/sql/plsql/hw-examples/alter_operator.sql new file mode 100644 index 0000000000..2176427c5a --- /dev/null +++ b/sql/plsql/hw-examples/alter_operator.sql @@ -0,0 +1 @@ +ALTER OPERATOR eq_op COMPILE; diff --git a/sql/plsql/hw-examples/alter_outline.sql b/sql/plsql/hw-examples/alter_outline.sql new file mode 100644 index 0000000000..67d6aa7c26 --- /dev/null +++ b/sql/plsql/hw-examples/alter_outline.sql @@ -0,0 +1 @@ +ALTER OUTLINE salaries REBUILD; diff --git a/sql/plsql/hw-examples/drop_operator.sql b/sql/plsql/hw-examples/drop_operator.sql new file mode 100644 index 0000000000..952e7c3a41 --- /dev/null +++ b/sql/plsql/hw-examples/drop_operator.sql @@ -0,0 +1 @@ +DROP OPERATOR eq_op; diff --git a/sql/plsql/hw-examples/keywordasidentifier02.sql b/sql/plsql/hw-examples/keywordasidentifier02.sql new file mode 100644 index 0000000000..a2b6c0ae73 --- /dev/null +++ b/sql/plsql/hw-examples/keywordasidentifier02.sql @@ -0,0 +1 @@ +select m.model from model diff --git a/sql/plsql/hw-examples/lexer01.sql b/sql/plsql/hw-examples/lexer01.sql new file mode 100644 index 0000000000..f9eae8c007 --- /dev/null +++ b/sql/plsql/hw-examples/lexer01.sql @@ -0,0 +1,2 @@ +select * from dual where 1 < > 2 and 1 ! = 2 and 1 ^ /*aaa */ = 2 + diff --git a/sql/plsql/hw-examples/lexer02.sql b/sql/plsql/hw-examples/lexer02.sql new file mode 100644 index 0000000000..15aaf848a4 --- /dev/null +++ b/sql/plsql/hw-examples/lexer02.sql @@ -0,0 +1,2 @@ +select 'A' | | 'B' from dual + diff --git a/sql/plsql/hw-examples/lexer03.sql b/sql/plsql/hw-examples/lexer03.sql new file mode 100644 index 0000000000..488fba03a1 --- /dev/null +++ b/sql/plsql/hw-examples/lexer03.sql @@ -0,0 +1,2 @@ +select :1, :X, :1 + 1, 1 + :2 from A where A=:3 and b= : 4 and c= :5and :A = :b + diff --git a/sql/plsql/hw-examples/lexer04.sql b/sql/plsql/hw-examples/lexer04.sql new file mode 100644 index 0000000000..c3931e035d --- /dev/null +++ b/sql/plsql/hw-examples/lexer04.sql @@ -0,0 +1 @@ +select tbl$or$idx$part$num("sys"."wrh$_seg_stat",0,4,0,"rowid") as c1 from t1 diff --git a/sql/plsql/hw-examples/lexer05.sql b/sql/plsql/hw-examples/lexer05.sql new file mode 100644 index 0000000000..8dcc674cfa --- /dev/null +++ b/sql/plsql/hw-examples/lexer05.sql @@ -0,0 +1 @@ +select tbl$or$idx$part$num("sys"."wrh:_seg_stat",0,4,0,"rowid") as c1 from t1 diff --git a/sql/plsql/hw-examples/simple11.sql b/sql/plsql/hw-examples/simple11.sql new file mode 100644 index 0000000000..ed2205330f --- /dev/null +++ b/sql/plsql/hw-examples/simple11.sql @@ -0,0 +1 @@ +select a.* from dual diff --git a/sql/plsql/hw-examples/truncate_table.sql b/sql/plsql/hw-examples/truncate_table.sql new file mode 100644 index 0000000000..3d7f0b9722 --- /dev/null +++ b/sql/plsql/hw-examples/truncate_table.sql @@ -0,0 +1 @@ +truncate table test; \ No newline at end of file