关于antlr4语法文件中硬编码的`self`引用

huangapple go评论74阅读模式
英文:

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

问题

在生成 PL/SQL 解析器和词法分析器后,使用以下命令:

antlr4 -Dlanguage=JavaScript PlSqlParser.g4 PlSqlLexer.g4

我发现生成的 PlSqlParser.jsPlSqlLexer.js 包含变量 self,当在 antlr4 的节点运行时产生 ReferenceError: self is not defined 错误。

语法文件可以在这里找到:https://github.com/antlr/grammars-v4/tree/master/sql/plsql

正如您所看到的,实际上,这个列表中的 *.g4 文件都在某些语句中硬编码了这个变量,例如在 PlSqlParser.g4 的第 1377 行:

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

可以正确地假设这个语法是硬编码为在浏览器环境中运行的吗,其中 self 将引用 Window 对象?因此,我需要做的只是将 *.g4 文件中的 self 替换为 this,以使其与节点兼容吗?

英文:

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?

答案1

得分: 1

根据 @kaby76 的解释,在为 PL/SQL 生成 JavaScript 目标时,缺少了一个语法转换步骤(将 self 替换为 this)。

现在已经被 修复,并合并到 antlr/grammars-v4 的主分支中。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月8日 23:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定