Search code examples
javascriptnode.jsplsqlantlr4

About hard-coded `self`-references in antlr4 grammar files


Got a question about antlr4-grammars:

After generating a PL/SQL-parser and lexer using:

antlr4 -Dlanguage=JavaScript PlSqlParser.g4 PlSqlLexer.g4,

I find that the resulting PlSqlParser.js and PlSqlLexer.js contains the variable self which produces ReferenceError: self is not defined, when run with the antlr4 node runtime.

The grammar files can be found here: https://github.com/antlr/grammars-v4/tree/master/sql/plsql

As you'll see, both the the *.g4 files in this listing, actually contains this variable hard-coded in some of its statements, e.g. in PlSqlParser.g4 at line 1377:

alter_view_editionable
    : {self.isVersion12()}? (EDITIONABLE | NONEDITIONABLE)
    ;

Is it correct to assume this grammar, then, is hard-coded to be run in a browser-context, in which self would reference the Window object? So all I need to do, is replace self with this in the *.g4-files to make it compatible with node?


Solution

  • As explained by @kaby76, there was a grammar transformation step (replacing self with this) missing when generating the JavaScript target for PL/SQL.

    It's now been fixed, and merged into the master branch of antlr/grammars-v4.