Search code examples
javasyntaxabstract-syntax-treerascal

Modifying Java syntax


I am looking to use Rascal's existing Java AST provided in the m3 library but I would like to extend this to add some features. We are working with a Java-like language (about 95% Java and 5% our specific features). Ideally, I'd like to be able to take the Java syntax and AST code (src/org/rascalmpl/library/lang/java/syntax/Java15.rsc and src/org/rascalmpl/library/lang/java/m3/AST.rsc, respectively) modify the syntax a bit and add some new AST nodes. Going through and exploring how this all works has shown this all doesn't work the same as the other language examples I've found.

For example, the constructors in Java15.rsc don't match the constructors in AST.rsc which is how all the other language examples appear to work. My current understanding of how this all works is when a user makes a call to createAstFromFile() what actually happens is Rascal calls into some Java code which produces the AST.

I've tried a number of things, the most successful being started to rename the constructors in Java15.rsc to match those in AST.rsc but didn't make it too far before I realised this was going to be a daunting task and perhaps not the best way to approach this problem.

Is there anyway I can "easily" extract the syntax and AST portions here and modify them on my own?

Thanks.


Solution

  • M3 (and it's AST) is disconnected from the Java15 grammar. It used eclipse JDT for parsing and name binding. It you want to extend the AST, you can add new constructors to the ADT.

    data Expression = delayedCall(Expression receiver, str name, list[Expression] arguments);
    

    Adds a new alternative to the Expression ADT.

    However, we do not have a java printer for the AST, so depending on what you want to do, you might actually want a grammar for your language and work with parse trees. You can extend grammars in a similar way as ADTs.