Skip to content

DumpAst Ast nodes without a constructor with parameters are not recognized as such

Problem:

AST nodes are not shown as such, when they do not have any children.

Example:

Grammar:

Root ::= SimpleNode;
SimpleNode;

Results in (slightly untested like this, I abstracted from my use case):

@startuml
scale 1.0

object "Root@2040495657" as node0 {
}

node0 *-- node1 : SimpleNode
@enduml

Analysis:

Currently, AST nodes are recognized based on their constructors: dumpAst/src/main/jastadd/GenerationBackend.jadd:413

  // --- isAstNode ---
  syn boolean DumpNode.isAstNode() {
    Class<?> clazz = getObject().getClass();
    for (java.lang.reflect.Constructor<?> constructor : clazz.getConstructors()) {
      for (java.lang.annotation.Annotation annotation : constructor.getAnnotations()) {
        if (annotation.annotationType().getCanonicalName().startsWith(astNodeAnnotationPrefix()) &&
              annotation.annotationType().getSimpleName().equals("Constructor")) {
          return true;
        }
      }
    }
    return false;
  }

However, the generated node only has one constructor without annotations:

  public SimpleNode() {
    super();
  }

Thoughts:

  • An AST node is a node that inherits from the ASTNode superclass.
  • We do not know the name of this class (it can be changed by JastAdd) and its package.
  • Assuming we know that the Dumper is called with an AST node argument, we maybe can figure this out, but it is still not trivial, since then we have to figure out which class in the superclass hierarchy is the ASTNode class.
  • Thus, I suggest a "Duck-Typing" approach, check for a method that any ASTNode has, but probably not that many other nodes.
  • For this, I suggest public void init$Children().

MR will follow.