为什么 Java AST 中的表达式“super()”是 Rascal 中的?

2023-12-26

中的表达方式之一Java AST 声明 http://tutor.rascal-mpl.org/Rascal/Libraries/lang/java/m3/AST/Declaration/Declaration.html is Expression::super()。 Java 表达式是super() used?

以这个示例类为例:

import java.util.ArrayList;
import java.util.List;

public class SuperTests<T> extends ArrayList<T> {

    public SuperTests() {
        super();
    }
    public SuperTests(int capacity) {
        super(capacity);
    }

    @Override
    public void clear() {
        super.clear();
    }

    public <T extends Integer> void addSupers(List<? super T> list) {

    }
}

rascal 中的 AST 为:

compilationUnit(
  [
    import("java.util.ArrayList")[
      @src=|project://TestThing/src/SuperTests.java|(0,27,<1,0>,<1,27>)
    ],
    import("java.util.List")[
      @src=|project://TestThing/src/SuperTests.java|(28,22,<2,0>,<2,22>)
    ]
  ],
  [class(
      "SuperTests",
      [parameterizedType(simpleType(simpleName("ArrayList")[
              @src=|project://TestThing/src/SuperTests.java|(87,9,<4,35>,<4,44>),
              @decl=|java+class:///java/util/ArrayList|,
              @typ=class(
                |java+class:///java/util/ArrayList|,
                [typeArgument(|java+typeVariable:///SuperTests/T|)])
            ]))],
      [],
      [
        constructor(
          "SuperTests",
          [],
          [],
          block([constructorCall(
                true,
                [])[
                @src=|project://TestThing/src/SuperTests.java|(128,8,<7,2>,<7,10>),
                @decl=|java+constructor:///java/util/ArrayList/ArrayList()|
              ]])[
            @src=|project://TestThing/src/SuperTests.java|(124,15,<6,21>,<8,2>)
          ])[
          @modifiers=[public()],
          @src=|project://TestThing/src/SuperTests.java|(104,35,<6,1>,<8,2>),
          @decl=|java+constructor:///SuperTests/SuperTests()|,
          @typ=constructor(
            |java+constructor:///SuperTests/SuperTests()|,
            [])
        ],
        constructor(
          "SuperTests",
          [parameter(
              int(),
              "capacity",
              0)[
              @src=|project://TestThing/src/SuperTests.java|(159,12,<9,19>,<9,31>),
              @decl=|java+parameter:///SuperTests/SuperTests(int)/capacity|,
              @typ=int()
            ]],
          [],
          block([constructorCall(
                true,
                [simpleName("capacity")[
                    @src=|project://TestThing/src/SuperTests.java|(183,8,<10,8>,<10,16>),
                    @decl=|java+parameter:///SuperTests/SuperTests(int)/capacity|,
                    @typ=int()
                  ]])[
                @src=|project://TestThing/src/SuperTests.java|(177,16,<10,2>,<10,18>),
                @decl=|java+constructor:///java/util/ArrayList/ArrayList(int)|
              ]])[
            @src=|project://TestThing/src/SuperTests.java|(173,23,<9,33>,<11,2>)
          ])[
          @modifiers=[public()],
          @src=|project://TestThing/src/SuperTests.java|(141,55,<9,1>,<11,2>),
          @decl=|java+constructor:///SuperTests/SuperTests(int)|,
          @typ=constructor(
            |java+constructor:///SuperTests/SuperTests(int)|,
            [int()])
        ],
        method(
          void(),
          "clear",
          [],
          [],
          block([expressionStatement(methodCall(
                  true,
                  "clear",
                  [])[
                  @src=|project://TestThing/src/SuperTests.java|(234,13,<15,2>,<15,15>),
                  @decl=|java+method:///java/util/ArrayList/clear()|,
                  @typ=void()
                ])[
                @src=|project://TestThing/src/SuperTests.java|(234,14,<15,2>,<15,16>)
              ]])[
            @src=|project://TestThing/src/SuperTests.java|(230,21,<14,21>,<16,2>)
          ])[
          @modifiers=[
            annotation(markerAnnotation("Override")[
                @src=|project://TestThing/src/SuperTests.java|(199,9,<13,1>,<13,10>),
                @typ=interface(
                  |java+interface:///java/lang/Override|,
                  [])
              ]),
            public()
          ],
          @src=|project://TestThing/src/SuperTests.java|(199,52,<13,1>,<16,2>),
          @decl=|java+method:///SuperTests/clear()|,
          @typ=method(
            |java+method:///SuperTests/clear()|,
            [],
            void(),
            [])
        ],
        method(
          void(),
          "addSupers",
          [parameter(
              parameterizedType(simpleType(simpleName("List")[
                    @src=|project://TestThing/src/SuperTests.java|(297,4,<18,43>,<18,47>),
                    @decl=|java+interface:///java/util/List|,
                    @typ=interface(
                      |java+interface:///java/util/List|,
                      [wildcard(super([typeArgument(|java+typeVariable:///SuperTests/addSupers(java/util/List)/T|)]))])
                  ])),
              "list",
              0)[
              @src=|project://TestThing/src/SuperTests.java|(297,20,<18,43>,<18,63>),
              @decl=|java+parameter:///SuperTests/addSupers(java.util.List)/list|,
              @typ=interface(
                |java+interface:///java/util/List|,
                [wildcard(super([typeArgument(|java+typeVariable:///SuperTests/addSupers(java/util/List)/T|)]))])
            ]],
          [],
          block([])[
            @src=|project://TestThing/src/SuperTests.java|(319,7,<18,65>,<20,2>)
          ])[
          @modifiers=[public()],
          @src=|project://TestThing/src/SuperTests.java|(255,71,<18,1>,<20,2>),
          @decl=|java+method:///SuperTests/addSupers(java.util.List)|,
          @typ=method(
            |java+method:///SuperTests/addSupers(java.util.List)|,
            [typeParameter(
                |java+typeVariable:///SuperTests/addSupers(java/util/List)/T|,
                extends([class(
                      |java+class:///java/lang/Integer|,
                      [])]))],
            void(),
            [interface(
                |java+interface:///java/util/List|,
                [wildcard(super([typeArgument(|java+typeVariable:///SuperTests/addSupers(java/util/List)/T|)]))])])
        ]
      ])[
      @modifiers=[public()],
      @src=|project://TestThing/src/SuperTests.java|(52,278,<4,0>,<23,1>),
      @decl=|java+class:///SuperTests|,
      @typ=class(
        |java+class:///SuperTests|,
        [typeParameter(
            |java+typeVariable:///SuperTests/T|,
            unbounded())])
    ]])[
  @src=|project://TestThing/src/SuperTests.java|(0,331,<1,0>,<23,2>),
  @decl=|java+compilationUnit:///src/SuperTests.java|,
  @messages=[
    warning(
      "The serializable class SuperTests does not declare a static final serialVersionUID field of type long",
      |project://TestThing/src/SuperTests.java|(65,10,<4,0>,<4,0>)),
    warning(
      "The type parameter T is hiding the type T",
      |project://TestThing/src/SuperTests.java|(263,1,<18,0>,<18,0>)),
    warning(
      "The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended",
      |project://TestThing/src/SuperTests.java|(273,7,<18,0>,<18,0>))
  ]
]

它不包含super()没有参数的构造函数。仅在通配符绑定的情况下(wildcard(super() 有没有super(_)但它有一个参数,并在lang::java::m3::TypeSymbol module.

那么到底发生了什么,我是否缺少 Java 构造,或者 Rascal ADT 定义相对于 AST 中生成的内容是否不正确?


它不再用于表示任何 Java 表达式。在我们决定更改 Rascal 中 Java AST 中超级构造函数调用和超级方法调用的表示方式之前,它是代码的一部分。

所有构造函数调用均表示为constructorCall(bool isSuper, _)。中的 true 值isSuper字段表明这是一个超级调用。类似地,所有方法调用都表示为methodCall(bool isSuper,_)用同样的逻辑。

The wildcard(_) and super(_) from lang::java::m3::TypeSymbol用于表示 AST 以及 M3 中使用的类型信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Java AST 中的表达式“super()”是 Rascal 中的? 的相关文章

  • Microsoft CCI - 编写编译器的资源和参考

    前一段时间 我在研究编译器 我使用过System Reflection从我的 AST 生成代码 IL 现在 我有了另一个我想要开发的编译器的想法 这将是另一个宠物项目 不会在生产代码中使用任何内容 至少现在不会 如您所知 与生产代码相比 宠
  • 在嵌套字典上使用 ast.literal_eval

    我使用 ast literal eval 将从 json loads 收到的数据更改为 Python 字典 然而 如果我应该以一种完全不同的方式来解决这个问题 也请随意指出这一点 Authentication buf StringIO St
  • ANTLR 4 和 AST 访客

    我正在尝试将 AST 与 ANTLR4 一起使用 并使用以下文件 生成器 java import org antlr v4 runtime ANTLRInputStream import org antlr v4 runtime CharS
  • 何时使用抽象语法树或具体语法树?

    我一直在研究编译器 词法分析器似乎非常简单 取一个 句子 并将其分解为单词 或标记 为了确保语法正确 需要解析器 解析器通常采用标记并构建一棵树 该树产生根节点 单词到句子 段落 页面等 From 这个问题 https stackoverf
  • 如何在 Rascal 中将值类型转换为 Map?

    我有一个存储映射的值类型变量 但我无法通过提供键来访问这些值 rascal gt a value s s rascal gt a s stdin 2 3 lt 1 2 gt lt 1 5 gt subscript not supported
  • 将节点插入抽象语法树

    ast模块的文档 https docs python org 3 library ast html解释如何使用以下命令替换 AST 中的节点节点转换器 https docs python org 3 library ast html ast
  • 在侦听器中使用 ParserRuleContext 遍历令牌 - ANTLR4

    在使用侦听器迭代令牌时 我想知道如何使用 ParserRuleContext 来查看令牌流中的下一个令牌或接下来的几个令牌 在下面的代码中 我试图查看当前标记之后直到 EOF 的所有标记 Override public void enter
  • clang-query:检查函数参数类型的模板参数名称

    我有一个大项目 以及大量以下形式的 C 类成员函数 Return CClass MemberFunction Arg1 arg1 std weak ptr
  • LL 自顶向下解析器,从 CST 到 AST

    我目前正在学习语法分析 尤其是自上而下的解析 我知道术语以及与自下而上的 LR 解析器的区别 并且由于自上而下的 LL 解析器更容易手动实现 所以我期待着制作自己的解析器 我见过两种方法 递归下降使用一组递归函数 基于堆栈和表驱动的自动机为
  • 从 Python AST 生成 .pyc?

    如何从 Python AST 生成 pyc 文件以便可以从 Python 导入该文件 我用过compile创建一个代码对象 然后编写co code属性到文件 但是当我尝试从 Python 导入文件时 我得到一个ImportError Bad
  • Eclipse 抽象语法树差异

    在 Eclipse 中给出以下代码 import org eclipse jdt core dom AST import org eclipse jdt core dom ASTParser import org eclipse jdt c
  • 何时在 Python 的 AST 中使用 ExtSlice 节点?

    绿树蛇 http greentreesnakes readthedocs org en latest index html gives 一个例子 http greentreesnakes readthedocs org en latest
  • 数据流图构建

    我被要求编写一个程序 在给定抽象语法树的情况下构建输入程序代码的数据流图 我在网上搜索了数据流图的定义 发现在代码段的数据流分析中发生了很多事情 我想知道我到底需要绘制什么来为给定的代码构建数据流图 很感谢任何形式的帮助 给定 AST 要生
  • 为什么 MATLAB 中允许使用多个连续的加号语法?

    有谁知道为什么这在 MATLAB 中有效 gt gt 1 2 ans 3 来自 C Python Java 等语言的编码 我发现这根本就行得通 这是最违反直觉的 想必解析器有一些我不明白的重要内容 有一个区别plus https mathw
  • 使用 GraphViz 理解 -fdump-tree 输出 gcc

    我创建了一个树转储 如下所述 如何将 gcc 生成的抽象语法树转储到 dot 文件中 https stackoverflow com questions 15800230 how can i dump an abstract syntax
  • 使用带有返回值的访问者模式实现 AST 的最佳方法是什么?

    我正在尝试使用访问者模式在 C 中实现一个简单的抽象语法树 AST 通常访问者模式不处理返回值 但在我的 AST 中 有一些表达式节点关心其子节点的返回类型和值 例如 我有一个这样的 Node 结构 class AstNode public
  • 生成 C / C++ 代码时表达式的结合性和优先级?

    我编写了一个生成 AST 的基本编译器 正确考虑了表达式中运算符的优先级 但是 在执行代码生成以生成 C 代码时 我不确定如何处理括号的使用 对于这个表达式 A B c AST如下 A B C 应该正确生成包含括号的前一个表达式 但是如果第
  • 修改现有的yaml文件并添加新的数据和注释

    我最近看到了yaml https github com go yaml yaml tree v3lib 有新版本 V3 与nodes https github com go yaml yaml blob v3 yaml go L348功能
  • 如何在 Scala 用户定义注释中使用命名参数?

    在下面的代码中 我能够获得第一个注释对象 Publishable 但不能获得第二个 第二个使用命名参数 它转换为 x 2 x 3 x 1 作为 AST 中的参数 我该如何正确地做到这一点 class Publishable val path
  • 使用 Python ast 模块访问语法树中的节点

    我正在玩 python ast 抽象语法树 我编写了以下内容 它访问了 AST 的所有节点 import ast class Py2Neko ast NodeVisitor def generic visit self node print

随机推荐