This generates heterogenous ASTs in Java for the Overture project, and should be applicable to anything else that needs an AST.
<plugin>
<groupId>org.overturetool.astcreator</groupId>
<artifactId>astcreator-plugin</artifactId>
<version>1.6.8</version>
<artifactId>root</artifactId>
<executions>
<execution>
<id>java</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<ast>overtureII.astv2</ast>
</configuration>
</plugin>
The jar
produced from the core/
directory is embeddable; please contact us if you plan to use it.
See https://github.com/overturetool/astcreator/wiki/Release-Procedure
The specification consists of:
-
- Base and analysis package declerations
-
- Tokens section
- Definition of any externally specified nodes. This is nodes that is manually written in Java and implements
INode
- Definition of external java types
enum
- any object
-
- Abstract Syntax Tree: This is the definition of the AST itself it consists of:
- Level 0 nodes prefixed after generation with
P
- Level 1+ nodes prefixed after generation with
S
and in the specification with#
- Leaf nodes prefixed after generation with
A
and specified as {node name} followed by field declerations. Only these nodes can be instantiated.
Both all nodes can speficy fields both only Level X can specify packages.
Packages
base org.overture.ast.node;
analysis org.overture.ast.analysis;
Tokens
LexStringToken = 'java:node:org.overture.ast.intf.lex.ILexStringToken';
location = 'java:org.overture.ast.intf.lex.ILexLocation';
nameScope = 'java:enum:org.overture.ast.typechecker.NameScope';
//Java
java_Boolean = 'java:java.lang.Boolean';
java_Integer = 'java:java.lang.Integer';
Abstract Syntax Tree
exp {-> package='org.overture.ast.expressions'
| (type):type
| [location]:location}
= #Unary
| #Binary
| {apply} [root]:exp [args]:exp* (argtypes):type*
;
#Unary {-> package='org.overture.ast.expressions'
| [exp]:exp}
= {absolute}
;
The ToString extension is used to generate toString
method bodies for AST nodes.
The structure consist of two parts:
-
- Import declerations
-
- ToString specifications
The ToString specification is a fully qualified AST name of the format:
<tostring-body> ::= "%" <path> "=" <body>
<path> ::= <text> <s-name>* "->" <text>
<s-name> ::= "->" <text>
<body> ::= <field> | <java-embeding> | <string> | "+"
<field> ::= "[" <text> "]"
<java-embeding> ::= "$" \.* "$"
<string> ::= "\"" <text> "\""
To String Extensions
// import packages used by external $$ java code
import org.overture.ast.util.Utils;
import org.overture.ast.util.ToStringUtil;
%CG->#decl->classHeader = [name]