antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.16k stars 3.7k forks source link

[PL/SQL] PlSqlParser $$ or Predefined Inquiry Directives is not supported #3626

Open misaya98 opened 1 year ago

misaya98 commented 1 year ago

When i use the g4 file https://github.com/antlr/grammars-v4/blob/master/sql/plsql/PlSqlParser.g4

and parse the sql like below

CREATE OR REPLACE PROCEDURE p
 AUTHID DEFINER IS
   i PLS_INTEGER;
 BEGIN
  DBMS_OUTPUT.PUT_LINE('Inside p');
    i := $$PLSQL_LINE;
    DBMS_OUTPUT.PUT_LINE('i = ' || i);
   DBMS_OUTPUT.PUT_LINE('$$PLSQL_LINE = ' || $$PLSQL_LINE);
   DBMS_OUTPUT.PUT_LINE('$$PLSQL_UNIT = ' || $$PLSQL_UNIT);
   DBMS_OUTPUT.PUT_LINE('$$PLSQL_UNIT_OWNER = ' || $$PLSQL_UNIT_OWNER);
    DBMS_OUTPUT.PUT_LINE('$$PLSQL_UNIT_TYPE = ' || $$PLSQL_UNIT_TYPE);
  END;

I got the error:

line 11:51 token recognition error at: '$'
line 11:52 token recognition error at: '$'

Can anyone help to fix the issue?

misaya98 commented 1 year ago

you guys can reference this link

https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-language-fundamentals.html#GUID-6CDF1EB6-913D-48E7-AFDA-DB4DE45209CE

misaya98 commented 1 year ago

temporary workaroud solution change parser to

constant
    : TIMESTAMP (quoted_string | bind_variable) (AT TIME ZONE quoted_string)?
    | INTERVAL (quoted_string | bind_variable | general_element_part)
      (YEAR | MONTH | DAY | HOUR | MINUTE | SECOND)
      ('(' (UNSIGNED_INTEGER | bind_variable) (',' (UNSIGNED_INTEGER | bind_variable) )? ')')?
      (TO ( DAY | HOUR | MINUTE | SECOND ('(' (UNSIGNED_INTEGER | bind_variable) ')')?))?
    | numeric
    | DATE quoted_string
    | quoted_string
    | NULL_
    | TRUE
    | FALSE
    | DBTIMEZONE
    | SESSIONTIMEZONE
    | MINVALUE
    | MAXVALUE
    | DEFAULT
    | STATIC_EXPRESSION
    ;

and add to lexer : STATIC_EXPRESSION: '$''$' SIMPLE_LETTER (SIMPLE_LETTER | '$' | '_' | '#' | [0-9])*;