Compile time syntax trees for the Java Teaching Machine. ======================================================== This document describes the "compile time syntax trees," that is the output of the pass 1 (parsing pass) and the input to passes 2 and 4. Pass 1 parses the input string and produces two kinds of output. A marked up representation of the source (the codelines) and a tree -- the "compile time syntax tree". The tree is passed on to passes 2 and 4 for further processing. Pass 2 populates the compile time symbol table and pass 4 generates the so-called AST code. (Technically the compile time syntax tree is an "abstract syntax tree", however, Ill avoid calling them such to prevent confusion with the AST code interpreted during run time.) Between pass 2 and pass 4, there is a pass 3, which is actually a pass over the compile time symbol table that produces all the type nodes for the user declared types. When we start supporting multiple source files there will actually be one compile time syntax tree for each source file. In that case we must compute all the compile time syntax trees (and execute pass 2 on them) prior to the start of pass 3. Each compilation unit is represented as a tree of SimpleNodes. SimpleNodes have been augmented with the attributes of types int, bool, string, ScopedName, and Java_SpecifierSet. By default, these are 0, false, null, null, and null respectively. The roughly 60 kinds of nodes are described in the sections below. In the following: an "expression node" is a node of kind JJTBINOP, JJTCONDITIONAL, JJTINSTANCEOF, JJTUNARY_PREFIX, JJTUNARY_POSTFIX, JJTCASTEXPRESSION, JJTCASTEXPRESSION, JJTDOT, JJTPARENS, JJTTHISEXP, JJTCLASSEXP, JJTNAMEEXP, JJTSUBSCRIPT, JJTCALL, JJTLITERAL, or JJTALLOCATIONEXPRESSION. a "statement node" is a node of kind JJTVARIABLEDECLARATOR, JJTUNMODIFIEDCLASSDECLARATION, JJTUNMODIFIEDINTERFACEDECLARATION, JJTLABELEDSTATEMENT, JJTBLOCK, JJTEMPTYSTATEMENT, JJTEXPRESSIONSTATEMENT, JJTSWITCHSTATEMENT, JJTIFSTATEMENT, JJTWHILESTATEMENT, JJTDOSTATEMENT, JJTFORSTATEMENT, JJTBREAKSTATEMENT, JJTCONTINUESTATEMENT, JJTRETURNSTATEMENT, JJTTHROWSTATEMENT, JJTSYNCHRONIZEDSTATEMENT, or JJTTRYSTATEMENT. a "type declaration node" is a node of kind JJTUNMODIFIEDCLASSDECLARATION or JJTUNMODIFIEDINTERFACEDECLARATION. a "class member declaration" node is a node of kind JJTUNMODIFIEDCLASSDECLARATION, JJTUNMODIFIEDINTERFACEDECLARATION, JJTINITIALIZER, JJTCONSTRUCTORDECLARATION, JJTMETHODDECLARATION or JJTVARIABLEDECLARATOR. an "interface member declaration" node is a node of kind JJTUNMODIFIEDCLASSDECLARATION, JJTUNMODIFIEDINTERFACEDECLARATION, JJTMETHODDECLARATION or JJTVARIABLEDECLARATOR. an "explicit constructor invocation" node is a node of kind JJTTHISEXPLICITCONSTRUCTORINVOCATION, or JJTSUPEREXPLICITCONSTRUCTORINVOCATION. DECLARATIONS ------------ JJTCOMPILATIONUNIT Represents the compilation unit Child(0) A JJTOPTPACKAGEDECLARATION Child(1) A JJTIMPORTDECLARATIONS Chilren( 2.. ) type declaration nodes JJTOPTPACKAGEDECLARATION Represents a package declaration: "package Name ;" bool attribute: indicates whether a package declaration is present. ScopedName attribute: The name of the package, if any, otherwise null. JJTIMPORTDECLARATIONS Represents the sequence of import declarations Children: JJTIMPORTDECLARATION nodes JJTIMPORTDECLARATION Represents a single import declaration of the form either import Name ; or import Name.* ; ScopedName attribute: The name of the package or class being imported. bool attribute: indicates the presence of the ".*". JJTUNMODIFIEDCLASSDECLARATION Represents a class declaration. Either top-level or nested. Java_SpecifierSet: The specifiers for the class declaration. String: The name of the class Child(0) A JJTOPTCLASSEXTENDSCLAUSE Child(1) A JJTOPTIMPLEMENTSCLAUSE Child(2) A JJTCLASSBODY JJTOPTCLASSEXTENDSCLAUSE Represents a (possibly absent) "extends" clause for a class: "extends Name;" bool attribute: indicates the presence of the clause ScopedName attribute: the name of the super class if any, otherwise null. JJTOPTIMPLEMENTSCLAUSE Represents a (possibly absent) "implements" clause for a class: "implements NameList;" bool attribute: indicates the presence of an implements clause Children JJTNAMEEXP nodes JJTCLASSBODY Represents the sequence of declarations in a class. Children: class member declaration nodes JJTUNMODIFIEDINTERFACEDECLARATION Represents an interface declaration. Either top-level or nested. Java_SpecifierSet attribute: The specifiers for the interface declaration. String: The name of the interface Child(0) A JJTOPTINTERFACEEXTENDSCLAUSE Child(1) A JJTINTERFACEBODY JJTOPTINTERFACEEXTENDSCLAUSE Represents a (possibly absent) "extends" clause for an interface: "extends NameList;" bool attribute: indicates the presence of an extends clause Children JJTNAMEEXP nodes JJTINTERFACEBODY Represents the sequence of declarations in an interface. Children: interface member declaration nodes JJTVARIABLEDECLARATOR Represents the declaration of a single variable, either in a type (class or interface) or local. Java_SpecifierSet attribute: The specifiers for the declaration String attribute: The name of the variable int attribute: The number of extra array dimensions associated with the declaration. For example if we declare Foo[] a[][], b[][][]. The the specSet will have 1 dimension, the int attribute for a's declaration will be 2 and the int attribute for b's attribute is 3. Child(0) [Optional]. Either an expression node, or a JJTARRAYINITIALIZER node. JJTARRAYINITIALIZER Represents the initialization of an array. E.g. "{{0,-1},{+1,0}}" Children: 0 or more nodes. Each is either an expression node, or a JJTARRAYINITIALIZER node. JJTCONSTRUCTORDECLARATION Represents the declaration of a constructor. Child(0) a JJTCONSTRUCTORDECLARATOR node Child(1) a JJTOPTTHROWSCLAUSE node Child(2) a JJTCONSTRUCTORBLOCK node JJTCONSTRUCTORDECLARATOR Represents the header of a constructor. Java_SpecifierSet attribute: The specifiers for the constructor String attribute: The name of the constructor Child(0) A JJTFORMALPARAMETERS node JJTMETHODDECLARATION Represents the declaration of a method. Child(0) a JJTMETHODDECLARATOR node Child(1) a JJTOPTTHROWSCLAUSE node Child(2) (optional) a JJTBLOCK node (absent for abstract declarations) JJTMETHODDECLARATOR Represents the header of a method. Java_SpecifierSet attribute: The specifiers for the method String attribute: The name of the method int attribute: The number of extra array dimensions not mentioned in the specSet. For example "A[][] foo()[][][] ;" The specSet will mention only 2 dimensions, the int attribute will be 3. Child(0) A JJTFORMALPARAMETERS node JJTFORMALPARAMETERS Children: 0 or more JJTFORMALPARAMETER nodes. JJTFORMALPARAMETER Represents a parameter in a method or constructor declaration, or a catch. Java_SpecifierSet attribute: The specifiers for the parameter. String attribute: The name of the parameter int attribute: The number of extra array dimensions not mentioned in the specSet. For example "void foo(int[][] p[][][]) ;" The specSet will mention only 2 dimensions, the int attribute will be 3. JJTOPTTHROWSCLAUSE Represents a throws clause, that may not be there. bool attribute: indicates the presence of a throws clause Children JJTNAMEEXP nodes JJTCONSTRUCTORBLOCK Represents the body of a constructor. bool attribute: indicates the presence of an explicit constructor invocation. Child(0): if the bool attribute is true this is an explicit constructor invocation node. Otherwise it is an (optional statement nodes). Children(1..): statement nodes. JJTTHISEXPLICITCONSTRUCTORINVOCATION Represents a explicit constructor invocation of the form "this( ... )" Child(0): a JJTARGUMENTS node JJTSUPEREXPLICITCONSTRUCTORINVOCATION Represents a explicit constructor invocation of the form "super( ... )" or PrimaryExpression.super( ... ). bool attribute: indicates the presence of the primary expression If the bool attribute is true, then Child(0): An expression Child(1): a JJTARGUMENTS node Else Child(0): a JJTARGUMENTS node JJTINITIALIZER Represents an initializer in a class body. bool attribute: indicates the initialization is "static" Child(0) A JJTBLOCK Expressions ----------- JJTBINOP Represents the application of a binary operator int attribute: The token kind of the operator. String attribute: The image of the operator. Child(0): The left operand. An expression node. Child(1): The right operand. An expression node. JJTCONDITIONAL Represents a conditional expression: E0 ? E1 : E2 Child(0): The first operand. An expression node. Child(1): The second operand. An expression node. Child(2): The third operand. An expression node. JJTINSTANCEOF Represents an instanceof expression. Java_SpecifierSet attribute: represents the right operand Child(0): The left operand. An expression node. JJTUNARY_PREFIX Represents a prefix operation. int attribute: The token kind of the operator. String attribute: The image of the operator. Child(0): The operand. An expression node. JJTUNARY_POSTFIX Represents a postfix operation. int attribute: The token kind of the operator. String attribute: The image of the operator. Child(0): The operand. An expression node. JJTCASTEXPRESSION Represents a cast expression. Java_SpecifierSet attribute: represents the type (the left operand) Child(0): The right operand. An expression node. JJTDOT Represents the dot (.) operation. But note that NameExp's also represents dots, so not all dots become dot operations. Put simply, if there is an identifier on each side of the dot, then a JJTNAMEEXP is used and a ScopedName represents the dot. Otherwise a JJTDOT. For example a.b.c will be a JJTNAMEEXP, whereas this.a, super.a, and (a).b will be JJTDOTs. Child(0) One of An expression node representing the left operand. A JJTSUPEREXP Child(1) One of the following: A JJTIDENTIFIERNODE representing the identifier on the right. A JJTTHISEXP representing .this A JJTALLOCATIONEXPRESSION representing .new ... Note that if Child(0) is a JJTSUPEREXP, then Child(1) must be a JJTIDENTIFIERNODE. JJTPARENS Represents a parenthesized expression. Child(0) The operand, an expression node. JJTTHISEXP Represents "this". JJTSUPEREXP Represents "super" JJTCLASSEXP Represents an expression of the form "ResultType.class". Java_SpecifierSet attribute: represents the type. JJTNAMEEXP Represents a name used as an expression or in a list or names (such as a throws clause, an implements clause, or an extends clause for an interface.) ScopedName attribute. The name. JJTSUBSCRIPT Represents a subscript expression a[b]. Child(0): The left operand. An expression node. Child(1): The right operand. An expression node. JJTCALL Represents a function call expression: a(...). (But not explicit constructor invocations.) Child(0): The left operand. An expression node. Child(1): The arguments. A JJTARGUMENTS node. JJTLITERAL Represents a literal. int parameter: The token kind of the literal. One of DECIMAL_LITERAL, HEX_LITERAL, OCTAL_LITERAL, FLOATING_POINT_LITERAL, CHARACTER_LITERAL, STRING_LITERAL, TRUE, FALSE, or NULL. String parameter: The image of the literal. JJTIDENTIFIERNODE Represents an Identifier. Used in JJTDOT. String attribute. The identifier. JJTARGUMENTS Represents an argument list. Children: The arguments. Expression nodes. JJTALLOCATIONEXPRESSION Represents a "new" expression. These come in four forms new PrimitiveType ArrayDimsAndInit new Name ArrayDimsAndInits new Name ( ... ) new Name ( ... ) ClassBody Java_SpecifierSet: The base type (including the Name for nonprimitive types) bool attribute: indicates that arguments are present, i.e the third or fourth form. If the bool attribute is true: Child(0): The arguments. A JJTARGUMENTS node. Child(1): A JJTCLASSBODY representing the body of an anonymous class. Otherwise: Child(0): The array dimensions and initializer. A JJTARRAYDIMSANDINITS node. JJTARRAYDIMSANDINITS Represents the array dimensions and initialization part of an allocation ("new") expression. These come in two basic forms: Those without initializers may have explicit dimensions. E.g. [i][j][k][][] Those with initializers must not have explicit dimensions. E.g. [][] ArrayInitializer int attribute: The number of "empty" bracket pairs. In the above examples 2. bool attribute: Indicates the presence of an array initializer. If bool attribute then Children: The explicit dimensioning expressions. Expression nodes. Else Child(0): An array initializer, a JJTARRAYINITIALIZER. Statements ---------- Note that besides the nodes below, JJTVARIABLEDECLARATOR, JJTUNMODIFIEDCLASSDECLARATION, and JJTUNMODIFIEDINTERFACEDECLARATION are statement nodes. However these may appear only directly within a JJTBLOCK. JJTLABELEDSTATEMENT Represents statements of the form "identifier:Statement". String attribute: The label. Child(0) : The statement JJTBLOCK Represents a sequence of statements. Children: Statement nodes JJTEMPTYSTATEMENT Represents the empty statement: ";". JJTEXPRESSIONSTATEMENT Represents an expression statement: "e;" Child(0) An expression node. JJTSWITCHSTATEMENT Represents a switch statement. There is no syntactic check that there is only one default. Child(0) An Expression node. Children(1..) JJTCASE nodes. JJTCASE Represents one case of a switch statement. Either "case Expression: Statements" or "default: Statements". Child(0) A JJTSWITCHLABEL node. Chilren(1..) Statement nodes. JJTSWITCHLABEL Represent either "case Expression:" or "default:" bool attribute: indicates the presence of an expression. If bool attribute then Child(0) An expression node. JJTIFSTATEMENT Represents an if statement. Child(0): The guard, an expression node, Child(1): The then statement, a statement node. Child(2) [optional]. The else statement, a statement node. JJTWHILESTATEMENT Represents a while statement. Child(0): The guard, an expression node. Child(1): The body, a statement node. JJTDOSTATEMENT Represents a do-while statement. Child(0): The body, a statement node. Child(1): The guard, an expression node. JJTFORSTATEMENT Represents a for statement: Child(0): a JJTOPTFORINIT node Child(1): a JJTOPTEXPRESSION node Child(2): a JJTOPTFORUPDATE Child(3): the body, a statement node. JJTOPTFORINIT bool attribute: indicates this part is present. If bool attribute Child(0): A JJTFORINIT node. JJTFORINIT When present, ForInits come in two forms: a LocalVariableDeclaration or a list of expressions. bool attribute: indicates a LocalVariableDeclaration If bool attribute: Children: JJTVARIABLEDECLARATOR nodes. Else Children: expression nodes. JJTOPTEXPRESSION bool attribute: indicates the guard expression is present. If bool attribute Child(0): The guard, an expression node. JJTOPTFORUPDATE Children: expression nodes. JJTBREAKSTATEMENT Represents a break statement: "break;" or "break label;" bool attribute: indicates the label is present. String attribute: the label, if present. JJTCONTINUESTATEMENT Represents a continue statement: "continue;" or "continue label;" bool attribute: indicates the label is present. String attribute: the label, if present. JJTRETURNSTATEMENT Represents a return statement: "return;" or "return Expression;" bool attribute: indicates the expression is present. if bool attribute: Child(0): An expression node JJTTHROWSTATEMENT Represents a throw statement: "throw Expression;" Child(0): An expression node JJTSYNCHRONIZEDSTATEMENT Represents a synchronized statement: "synchronized(Expression) Block" Child(0): An expression node. Child(1): A JJTBLOCK node. JJTTRYSTATEMENT Represents a try statement. bool attribute: indicates whether there is a finally clause. Child( 0 ) a JJTCATCHES node. If bool attribute Child(1) a JJTBLOCK representing the finally block. JJTCATCHES Represents the sequence of "catch"es in a "try" statement. Children: JJCATCH nodes. JJTCATCH Represents a single "catch( FormalParameter ) Block" clause. child(0) A JJTFORMALPARAMETER node child(1) A JJTBLOCK node