/* * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has * intellectual property rights relating to technology embodied in the product * that is described in this document. In particular, and without limitation, * these intellectual property rights may include one or more of the U.S. * patents listed at http://www.sun.com/patents and one or more additional * patents or pending patent applications in the U.S. and in other countries. * U.S. Government Rights - Commercial software. Government users are subject * to the Sun Microsystems, Inc. standard license agreement and applicable * provisions of the FAR and its supplements. Use is subject to license terms. * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This * product is covered and controlled by U.S. Export Control laws and may be * subject to the export or import laws in other countries. Nuclear, missile, * chemical biological weapons or nuclear maritime end uses or end users, * whether direct or indirect, are strictly prohibited. Export or reexport * to countries subject to U.S. embargo or to entities identified on U.S. * export exclusion lists, including, but not limited to, the denied persons * and specially designated nationals lists is strictly prohibited. */ options { JAVA_UNICODE_ESCAPE = true; ERROR_REPORTING = true; STATIC = false; OUTPUT_DIRECTORY="generated_java15"; STATIC = false; } PARSER_BEGIN(Java15Parser) package tmp.generated_java15; import java.io.*; import java.util.*; import cide.gast.*; import cide.gparser.*; /** * Grammar to parse Java version 1.5 * @author Sreenivasa Viswanadha - Simplified and enhanced for 1.5 */ public class Java15Parser { public Java15Parser(String fileName) { this(System.in); try { ReInit(new FileInputStream(new File(fileName))); } catch(Exception e) { e.printStackTrace(); } } public static void main(String args[]) { Java15Parser parser; if (args.length == 0) { System.out.println("Java Parser Version 1.1: Reading from standard input . . ."); parser = new Java15Parser(System.in); } else if (args.length == 1) { System.out.println("Java Parser Version 1.1: Reading from file " + args[0] + " . . ."); try { parser = new Java15Parser(new java.io.FileInputStream(args[0])); } catch (java.io.FileNotFoundException e) { System.out.println("Java Parser Version 1.1: File " + args[0] + " not found."); return; } } else { System.out.println("Java Parser Version 1.1: Usage is one of:"); System.out.println(" java JavaParser < inputfile"); System.out.println("OR"); System.out.println(" java JavaParser inputfile"); return; } try { parser.CompilationUnit(); System.out.println("Java Parser Version 1.1: Java program parsed successfully."); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println("Java Parser Version 1.1: Encountered errors during parse."); e.printStackTrace(System.out); } } public Java15Parser(InputStream inputStream) { this(new OffsetCharStream(inputStream)); } private void ReInit(InputStream inputStream) { ReInit(new OffsetCharStream(inputStream)); } public ISourceFile getRoot() throws ParseException { return CompilationUnit(); } } PARSER_END(Java15Parser) /* WHITE SPACE */ SKIP : { " " | "\t" | "\n" | "\r" | "\f" } /* COMMENTS */ MORE : { "//" : IN_SINGLE_LINE_COMMENT | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } MORE : { < ~[] > } /* RESERVED WORDS AND LITERALS */ TOKEN : { < ABSTRACT: "abstract" > | < ASSERT: "assert" > | < BOOLEAN: "boolean" > | < BREAK: "break" > | < BYTE: "byte" > | < CASE: "case" > | < CATCH: "catch" > | < CHAR: "char" > | < CLASS: "class" > | < CONST: "const" > | < CONTINUE: "continue" > | < _DEFAULT: "default" > | < DO: "do" > | < DOUBLE: "double" > | < ELSE: "else" > | < ENUM: "enum" > | < EXTENDS: "extends" > | < FALSE: "false" > | < FINAL: "final" > | < FINALLY: "finally" > | < FLOAT: "float" > | < FOR: "for" > | < GOTO: "goto" > | < IF: "if" > | < IMPLEMENTS: "implements" > | < IMPORT: "import" > | < INSTANCEOF: "instanceof" > | < INT: "int" > | < INTERFACE: "interface" > | < LONG: "long" > | < NATIVE: "native" > | < NEW: "new" > | < NULL: "null" > | < PACKAGE: "package"> | < PRIVATE: "private" > | < PROTECTED: "protected" > | < PUBLIC: "public" > | < RETURN: "return" > | < SHORT: "short" > | < STATIC: "static" > | < STRICTFP: "strictfp" > | < SUPER: "super" > | < SWITCH: "switch" > | < SYNCHRONIZED: "synchronized" > | < THIS: "this" > | < THROW: "throw" > | < THROWS: "throws" > | < TRANSIENT: "transient" > | < TRUE: "true" > | < TRY: "try" > | < VOID: "void" > | < VOLATILE: "volatile" > | < WHILE: "while" > } /* LITERALS */ TOKEN : { < INTEGER_LITERAL: (["l","L"])? | (["l","L"])? | (["l","L"])? > | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < CHARACTER_LITERAL: "'" ( (~["'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > } /* IDENTIFIERS */ TOKEN : { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } /* SEPARATORS */ TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < LBRACKET: "[" > | < RBRACKET: "]" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > | < AT: "@" > } /* OPERATORS */ TOKEN : { < ASSIGN: "=" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < INCR: "++" > | < DECR: "--" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > | < LSHIFT: "<<" > | < PLUSASSIGN: "+=" > | < MINUSASSIGN: "-=" > | < STARASSIGN: "*=" > | < SLASHASSIGN: "/=" > | < ANDASSIGN: "&=" > | < ORASSIGN: "|=" > | < XORASSIGN: "^=" > | < REMASSIGN: "%=" > | < LSHIFTASSIGN: "<<=" > | < RSIGNEDSHIFTASSIGN: ">>=" > | < RUNSIGNEDSHIFTASSIGN: ">>>=" > | < ELLIPSIS: "..." > } /* >'s need special attention due to generics syntax. */ TOKEN : { < RUNSIGNEDSHIFT: ">>>" > | < RSIGNEDSHIFT: ">>" > | < GT: ">" > } GRAMMARSTART //grammar /***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ /* * Program structuring syntax follows. */ CompilationUnit: [ PackageDeclaration ] ( ImportDeclaration )* ( TypeDeclaration )* ; PackageDeclaration: "package" Name ";" @! ; ImportDeclaration: "import" [ "static" ] Name [ "." "*" ] ";" @! ; Modifiers: (Modifier)* ; Modifier: "public" | "static" | "protected" | "private" | "final" | "abstract" | "synchronized" | "native" | "transient" | "volatile" | "strictfp" | Annotation ; TypeDeclaration: ";" | LOOK_AHEAD(2) Modifiers ClassOrInterfaceDeclaration | LOOK_AHEAD(2) Modifiers EnumDeclaration | LOOK_AHEAD(2) Modifiers AnnotationTypeDeclaration ; ClassOrInterfaceDeclaration: ClassOrInterface [ TypeParameters ] [ ExtendsList ] [ ImplementsList ] ClassOrInterfaceBody ; ClassOrInterface: "class" | "interface"; ExtendsList: "extends" ClassOrInterfaceType ( "," ClassOrInterfaceType )* ; ImplementsList: "implements" ClassOrInterfaceType ( "," ClassOrInterfaceType )* ; EnumDeclaration: "enum" [ ImplementsList ] EnumBody ; EnumBody: "{" EnumConstant ( "," EnumConstant )* [ EnumBodyInternal ] "}" ; EnumBodyInternal: ";" ( ClassOrInterfaceBodyDeclaration )* ; EnumConstant: [ Arguments ] [ ClassOrInterfaceBody ] ; TypeParameters: "<" TypeParameter ( "," TypeParameter )* ">" ; TypeParameter: [ TypeBound ] ; TypeBound: "extends" ClassOrInterfaceType ( "&" ClassOrInterfaceType )* ; ClassOrInterfaceBody: "{" @+ @! ( ClassOrInterfaceBodyDeclaration @! @! )* @- @! "}" ; ClassOrInterfaceBodyDeclaration: LOOK_AHEAD("Modifiers() [ \"static\" ] \"{\"") Initializer | LOOK_AHEAD("Modifiers() ClassOrInterface()") Modifiers ClassOrInterfaceDeclaration | LOOK_AHEAD("Modifiers() \"enum\"") Modifiers EnumDeclaration | LOOK_AHEAD(" Modifiers() [ TypeParameters() ] \"(\"" ) Modifiers ConstructorDeclaration | LOOK_AHEAD( "Modifiers() Type() ( \"[\" \"]\" )* ( \",\" | \"=\" | \";\" ) " ) Modifiers FieldDeclaration | LOOK_AHEAD(2) Modifiers MethodDeclaration | ";" ; FieldDeclaration: Type VariableDeclarator ( "," VariableDeclarator )* ";" ; VariableDeclarator: VariableDeclaratorId [ "=" VariableInitializer ] ; VariableDeclaratorId: ( "[" "]" )* ; VariableInitializer: ArrayInitializer | Expression ; ArrayInitializer: "{" [ ArrayInitializerInternal ] [ "," ] "}" ; ArrayInitializerInternal: VariableInitializer ( LOOK_AHEAD(2) "," VariableInitializer )*; MethodDeclaration: [ TypeParameters ] ResultType MethodDeclarator [ "throws" NameList ] MethodDeclarationBody ; MethodDeclarationBody: Block | ";"; MethodDeclarator: FormalParameters ( "[" "]" )* ; FormalParameters: "(" [ FormalParametersInternal ] ")" ; FormalParametersInternal: &LI FormalParameter ( "," &LI FormalParameter )*; FormalParameter: [ "final" ] Type [ "..." ] VariableDeclaratorId ; ConstructorDeclaration: [ TypeParameters ] FormalParameters [ "throws" NameList ] "{" [ LOOK_AHEAD("ExplicitConstructorInvocation()") ExplicitConstructorInvocation ] ( BlockStatement )* "}" ; ExplicitConstructorInvocation: LOOK_AHEAD("\"this\" Arguments() \";\"") "this" Arguments ";" @! | [ LOOK_AHEAD(2) PrimaryExpression "." ] "super" Arguments ";" @! ; Initializer: [ "static" ] Block ; /* * Type, name and expression syntax follows. */ Type: LOOK_AHEAD(2) ReferenceType | PrimitiveType ; ReferenceType: PrimitiveType ( LOOK_AHEAD(2) "[" "]" )+ | ClassOrInterfaceType ( LOOK_AHEAD(2) "[" "]" )* ; ClassOrInterfaceType: [ LOOK_AHEAD(2) TypeArguments ] ( LOOK_AHEAD(2) ClassOrInterfaceTypeIntern )* ; ClassOrInterfaceTypeIntern: "." [ LOOK_AHEAD(2) TypeArguments ] ; TypeArguments: "<" TypeArgument ( "," TypeArgument )* ">" ; TypeArgument: ReferenceType | "?" [ WildcardBounds ] ; WildcardBounds: "extends" ReferenceType | "super" ReferenceType ; PrimitiveType: "boolean" | "char" | "byte" | "short" | "int" | "long" | "float" | "double" ; ResultType: "void" | Type ; Name: ( LOOK_AHEAD(2) "." )* ; NameList: &LI Name ( "," &LI Name )* ; /* * Expression syntax follows. */ /* * This expansion has been written this way instead of: * Assignment | ConditionalExpression * for performance reasons. * However, it is a weakening of the grammar for it allows the LHS of * assignments to be any conditional expression whereas it can only be * a primary expression. Consider adding a semantic predicate to work * around this. */ Expression: ConditionalExpression [ LOOK_AHEAD(2) AssignExp ] ; AssignExp : AssignmentOperator Expression ; AssignmentOperator: "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>="| ">>>=" | "&=" | "^=" | "|=" ; ConditionalExpression: LOOK_AHEAD("ConditionalOrExpression() \"?\"") ConditionalExpressionFull{Expression} | ConditionalOrExpression ; ConditionalExpressionFull: ConditionalOrExpression "?" Expression! ":" Expression ; ConditionalOrExpression: ConditionalAndExpression ( "||" ConditionalAndExpression )* ; ConditionalAndExpression: InclusiveOrExpression ( "&&" InclusiveOrExpression )* ; InclusiveOrExpression: ExclusiveOrExpression ( "|" ExclusiveOrExpression )* ; ExclusiveOrExpression: AndExpression ( "^" AndExpression )* ; AndExpression: EqualityExpression ( "&" EqualityExpression )* ; EqualityExpression: InstanceOfExpression ( EqualityExpressionIntern )* ; EqualityExpressionIntern: EqualityOp InstanceOfExpression; EqualityOp: "==" | "!="; InstanceOfExpression: RelationalExpression [ "instanceof" Type ] ; RelationalExpression: ShiftExpression ( RelationalExpressionIntern )* ; RelationalExpressionIntern:RelationalOp ShiftExpression; RelationalOp: "<" | ">" | "<=" | ">=" ; ShiftExpression: AdditiveExpression ( ShiftExpressionRight )* ; ShiftExpressionRight: ShiftOp AdditiveExpression ; ShiftOp: "<<" | LOOK_AHEAD(3) ">" ">" ">" | ">" ">" ; AdditiveExpression: MultiplicativeExpression (AdditiveExpressionIntern )* ; AdditiveExpressionIntern:AdditiveOp MultiplicativeExpression ; AdditiveOp: "+" | "-"; MultiplicativeExpression: UnaryExpression (MultiplicativeExpressionIntern)* ; MultiplicativeExpressionIntern: MultiplicativeOp UnaryExpression ; MultiplicativeOp: "*" | "/" | "%"; UnaryExpression: AdditiveOp UnaryExpression | PreIncrementExpression | PreDecrementExpression | UnaryExpressionNotPlusMinus ; PreIncrementExpression: "++" PrimaryExpression ; PreDecrementExpression: "--" PrimaryExpression ; UnaryExpressionNotPlusMinus: UnaryOp UnaryExpression | LOOK_AHEAD( "CastLookahead()" ) CastExpression | PostfixExpression ; UnaryOp: "~" | "!"; // This production is to determine LOOK_AHEAD only. The LOOK_AHEAD specifications // below are not used, but they are there just to indicate that we know about // this. CastLookahead: LOOK_AHEAD(2) "(" PrimitiveType | LOOK_AHEAD("\"(\" Type() \"[\"") "(" Type "[" "]" | "(" Type ")" CastLAOp ; CastLAOp: "~" | "!" | "(" | "this" | "super" | "new" | | Literal; PostfixExpression: PrimaryExpression [ PostfixOp ] ; PostfixOp: "++" | "--"; CastExpression: LOOK_AHEAD("\"(\" PrimitiveType()") "(" Type ")" UnaryExpression | "(" Type ")" UnaryExpressionNotPlusMinus ; PrimaryExpression: PrimaryPrefix ( LOOK_AHEAD(2) PrimarySuffix )* ; MemberSelector: "." TypeArguments ; PrimaryPrefix: Literal | "this" | "super" "." | "(" Expression ")" | AllocationExpression | LOOK_AHEAD(" ResultType() \".\" \"class\"" ) ResultType "." "class" | Name ; PrimarySuffix: LOOK_AHEAD(2) "." "this" | LOOK_AHEAD(2) "." AllocationExpression | LOOK_AHEAD(3) MemberSelector | "[" Expression "]" | "." | Arguments ; Literal: | | | | BooleanLiteral | NullLiteral ; BooleanLiteral: "true" | "false" ; NullLiteral: "null" ; Arguments: "(" [ ArgumentList ] ")" ; ArgumentList: Expression ( "," Expression )* ; AllocationExpression: LOOK_AHEAD(2) "new" PrimitiveType ArrayDimsAndInits | "new" ClassOrInterfaceType [ TypeArguments ] AllocationExpressionInit ; AllocationExpressionInit: ArrayDimsAndInits | Arguments [ ClassOrInterfaceBody ] ; /* * The third LOOK_AHEAD specification below is to parse to PrimarySuffix * if there is an expression between the "[...]". */ ArrayDimsAndInits: LOOK_AHEAD(2) "[" Expression "]" ( LOOK_AHEAD(2) "[" Expression "]" )* ( LOOK_AHEAD(2) "[" "]" )* | ( "[" "]" )+ ArrayInitializer ; /* * Statement syntax follows. */ Statement: LOOK_AHEAD(2) LabeledStatement | AssertStatement @! | Block | EmptyStatement | StatementExpression ";" @! | SwitchStatement @! | IfStatement{Statement} @! | WhileStatement{Statement} @! | DoStatement{Statement} @! | ForStatement{Statement} @! | BreakStatement @! | ContinueStatement @! | ReturnStatement @! | ThrowStatement @! | SynchronizedStatement{Statement} @! | TryStatement{Statement} @! ; AssertStatement: "assert" Expression [ ":" Expression ] ";" ; LabeledStatement: ":" Statement ; Block: "{" @+ @! ( BlockStatement )* @- "}" @! ; BlockStatement: LOOK_AHEAD("[ \"final\" ] Type() ") LocalVariableDeclaration ";" | Statement | ClassOrInterfaceDeclaration ; LocalVariableDeclaration: [ "final" ] Type VariableDeclarator ( "," VariableDeclarator )* ; EmptyStatement: ";" ; StatementExpression :/* * The last expansion of this production accepts more than the legal * Java expansions for StatementExpression. This expansion does not * use PostfixExpression for performance reasons. */ PreIncrementExpression | PreDecrementExpression | PrimaryExpression [ StatementExpressionAssignment ] ; StatementExpressionAssignment: "++" | "--" | AssignmentOperator Expression ; SwitchStatement: "switch" "(" Expression ")" "{" @+ @! ( SwitchStatementLabel )* @- "}" @! ; SwitchStatementLabel :SwitchLabel ( BlockStatement )*; SwitchLabel: "case" Expression ":" | "default" ":" ; IfStatement: /* * The disambiguating algorithm of JavaCC automatically binds dangling * else's to the innermost if statement. The LOOK_AHEAD specification * is to tell JavaCC that we know what we are doing. */ "if" "(" Expression ")" @! @+ Statement! @- [ LOOK_AHEAD(1) "else" @! @+ Statement @- ] ; WhileStatement: "while" "(" Expression ")" @! @+ Statement! @- ; DoStatement: "do" @! @+ Statement! @- "while" "(" Expression ")" ";" ; ForStatement: "for" "(" ForStatementInternal ")" @! @+ Statement! @- ; ForStatementInternal: LOOK_AHEAD("Type() \":\"") Type ":" Expression | [ ForInit ] ";" [ Expression ] ";" [ ForUpdate ] ; ForInit: LOOK_AHEAD( "[ \"final\" ] Type() " ) LocalVariableDeclaration | StatementExpressionList ; StatementExpressionList: StatementExpression ( "," StatementExpression )* ; ForUpdate: StatementExpressionList ; BreakStatement: "break" [ ] ";" ; ContinueStatement: "continue" [ ] ";" ; ReturnStatement: "return" [ Expression ] ";" ; ThrowStatement: "throw" Expression ";" ; SynchronizedStatement: "synchronized" "(" Expression ")" Block! ; TryStatement: /* * Semantic check required here to make sure that at least one * finally/catch is present. */ "try" Block! TryStatementEnd ; TryStatementEnd: (CatchBlock )+ | "finally" Block ::FinallyBlock ; CatchBlock: "catch" "(" FormalParameter ")" Block ; /* We use productions to match >>>, >> and > so that we can keep the * type declaration syntax with generics clean */ /* Annotation syntax follows. */ Annotation: LOOK_AHEAD( "\"@\" Name() \"(\" ( \"=\" | \")\" )") NormalAnnotation | LOOK_AHEAD( "\"@\" Name() \"(\"" ) SingleMemberAnnotation | MarkerAnnotation ; NormalAnnotation: "@" Name "(" [ MemberValuePairs ] ")" @! ; MarkerAnnotation: "@" Name @! ; SingleMemberAnnotation: "@" Name "(" MemberValue ")" @! ; MemberValuePairs: MemberValuePair ( "," MemberValuePair )* ; MemberValuePair: "=" MemberValue ; MemberValue: Annotation | MemberValueArrayInitializer | ConditionalExpression ; MemberValueArrayInitializer: "{" MemberValue ( LOOK_AHEAD(2) "," MemberValue )* [ "," ] "}" ; /* Annotation Types. */ AnnotationTypeDeclaration: "@" "interface" AnnotationTypeBody ; AnnotationTypeBody: "{" ( AnnotationTypeMemberDeclaration )* "}" ; AnnotationTypeMemberDeclaration: LOOK_AHEAD("Modifiers() Type() \"(\"") Modifiers Type "(" ")" [ DefaultValue ] ";" | LOOK_AHEAD(1) Modifiers ClassOrInterfaceDeclaration | LOOK_AHEAD(2) Modifiers EnumDeclaration | LOOK_AHEAD(2) Modifiers AnnotationTypeDeclaration | LOOK_AHEAD(1) Modifiers FieldDeclaration | ";" ; DefaultValue: "default" MemberValue ;