Advertisement
Guest User

Untitled

a guest
Mar 28th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. tree grammar JavaToGeneric;
  2.  
  3. options {
  4.     memoize = true;
  5.     backtrack = true;
  6.     language = Java;
  7.     output = AST;
  8.     tokenVocab = Java;
  9.     ASTLabelType = CommonTree;
  10.     filter = true;
  11. }
  12.  
  13.  
  14. @treeparser::header {
  15. package org.apiwatch.grammars;
  16. }
  17.  
  18. // Starting point for parsing a Java file.
  19. sourceFile
  20.     :   ^(JAVA_SOURCE annotationList packageDeclaration? importDeclaration* typeDeclaration*)
  21.     ;
  22.  
  23. packageDeclaration
  24.     :   ^(PACKAGE qualifiedIdentifier)
  25.     ;
  26.  
  27. importDeclaration
  28.     :   ^(IMPORT STATIC? qualifiedIdentifier DOTSTAR?)
  29.     ;
  30.  
  31. typeDeclaration
  32.     :   ^(CLASS modifierList IDENT {System.out.println("Class: " + $IDENT.text);} genericTypeParameterList? extendsClause? implementsClause? classTopLevelScope)
  33.     |   ^(INTERFACE modifierList IDENT genericTypeParameterList? extendsClause? interfaceTopLevelScope)
  34.     |   ^(ENUM modifierList IDENT implementsClause? enumTopLevelScope)
  35.     |   ^(AT modifierList IDENT annotationTopLevelScope)
  36.     ;
  37.  
  38. extendsClause // actually 'type' for classes and 'type+' for interfaces, but this has
  39.               // been resolved by the parser grammar.
  40.     :   ^(EXTENDS_CLAUSE type+)
  41.     ;
  42.  
  43. implementsClause
  44.     :   ^(IMPLEMENTS_CLAUSE type+)
  45.     ;
  46.  
  47. genericTypeParameterList
  48.     :   ^(GENERIC_TYPE_PARAM_LIST genericTypeParameter+)
  49.     ;
  50.  
  51. genericTypeParameter
  52.     :   ^(IDENT bound?)
  53.     ;
  54.  
  55. bound
  56.     :   ^(EXTENDS_BOUND_LIST type+)
  57.     ;
  58.  
  59. enumTopLevelScope
  60.     :   ^(ENUM_TOP_LEVEL_SCOPE enumConstant+ classTopLevelScope?)
  61.     ;
  62.  
  63. enumConstant
  64.     :   ^(IDENT annotationList .? classTopLevelScope?)
  65.     ;
  66.  
  67.  
  68. classTopLevelScope
  69.     :   ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*)
  70.     ;
  71.  
  72. classScopeDeclarations
  73.     :   ^(CLASS_INSTANCE_INITIALIZER block)
  74.     |   ^(CLASS_STATIC_INITIALIZER block)
  75.     |   ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT {System.out.println("Method: " + $IDENT.text);} formalParameterList arrayDeclaratorList? throwsClause? block?)
  76.     |   ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT {System.out.println("Method: " + $IDENT.text);} formalParameterList throwsClause? block?)
  77.     |   ^(VAR_DECLARATION modifierList type variableDeclaratorList)
  78.     |   ^(CONSTRUCTOR_DECL modifierList genericTypeParameterList? formalParameterList throwsClause? block)
  79.     |   typeDeclaration
  80.     ;
  81.  
  82. interfaceTopLevelScope
  83.     :   ^(INTERFACE_TOP_LEVEL_SCOPE interfaceScopeDeclarations*)
  84.     ;
  85.  
  86. interfaceScopeDeclarations
  87.     :   ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?)
  88.     |   ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?)
  89.                          // Interface constant declarations have been switched to variable
  90.                          // declarations by 'java.g'; the parser has already checked that
  91.                          // there's an obligatory initializer.
  92.     |   ^(VAR_DECLARATION modifierList type variableDeclaratorList)
  93.     |   typeDeclaration
  94.     ;
  95.  
  96. variableDeclaratorList
  97.     :   ^(VAR_DECLARATOR_LIST variableDeclarator+)
  98.     ;
  99.  
  100. variableDeclarator
  101.     :   ^(VAR_DECLARATOR variableDeclaratorId .?)
  102.     ;
  103.  
  104. variableDeclaratorId
  105.     :   ^(IDENT arrayDeclaratorList?)
  106.     ;
  107.  
  108. arrayDeclarator
  109.     :   LBRACK RBRACK
  110.     ;
  111.  
  112. arrayDeclaratorList
  113.     :   ^(ARRAY_DECLARATOR_LIST ARRAY_DECLARATOR*)
  114.     ;
  115.  
  116.  
  117. throwsClause
  118.     :   ^(THROWS_CLAUSE qualifiedIdentifier+)
  119.     ;
  120.  
  121. modifierList
  122.     :   ^(MODIFIER_LIST modifier*)
  123.     ;
  124.  
  125. modifier
  126.     :   PUBLIC
  127.     |   PROTECTED
  128.     |   PRIVATE
  129.     |   STATIC
  130.     |   ABSTRACT
  131.     |   NATIVE
  132.     |   SYNCHRONIZED
  133.     |   TRANSIENT
  134.     |   VOLATILE
  135.     |   STRICTFP
  136.     |   localModifier
  137.     ;
  138.  
  139. localModifierList
  140.     :   ^(LOCAL_MODIFIER_LIST localModifier*)
  141.     ;
  142.  
  143. localModifier
  144.     :   FINAL
  145.     |   annotation
  146.     ;
  147.  
  148. type
  149.     :   ^(TYPE (primitiveType | qualifiedTypeIdent) arrayDeclaratorList?)
  150.     ;
  151.  
  152. qualifiedTypeIdent
  153.     :   ^(QUALIFIED_TYPE_IDENT typeIdent+)
  154.     ;
  155.  
  156. typeIdent
  157.     :   ^(IDENT genericTypeArgumentList?)
  158.     ;
  159.  
  160. primitiveType
  161.     :   BOOLEAN
  162.     |   CHAR
  163.     |   BYTE
  164.     |   SHORT
  165.     |   INT
  166.     |   LONG
  167.     |   FLOAT
  168.     |   DOUBLE
  169.     ;
  170.  
  171. genericTypeArgumentList
  172.     :   ^(GENERIC_TYPE_ARG_LIST genericTypeArgument+)
  173.     ;
  174.  
  175. genericTypeArgument
  176.     :   type
  177.     |   ^(QUESTION genericWildcardBoundType?)
  178.     ;
  179.  
  180. genericWildcardBoundType
  181.     :   ^(EXTENDS type)
  182.     |   ^(SUPER type)
  183.     ;
  184.  
  185. formalParameterList
  186.     :   ^(FORMAL_PARAM_LIST formalParameterStandardDecl* formalParameterVarargDecl?)
  187.     ;
  188.  
  189. formalParameterStandardDecl
  190.     :   ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId)
  191.     ;
  192.  
  193. formalParameterVarargDecl
  194.     :   ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId)
  195.     ;
  196.  
  197. qualifiedIdentifier
  198.     :   IDENT
  199.     |   ^(DOT qualifiedIdentifier IDENT)
  200.     ;
  201.  
  202. // ANNOTATIONS
  203.  
  204. annotationList
  205.     :   ^(ANNOTATION_LIST annotation*)
  206.     ;
  207.  
  208. annotation
  209.     :   ^(AT qualifiedIdentifier .?)
  210.     ;
  211.  
  212. annotationTopLevelScope
  213.     :   ^(ANNOTATION_TOP_LEVEL_SCOPE annotationScopeDeclarations*)
  214.     ;
  215.  
  216. annotationScopeDeclarations
  217.     :   ^(ANNOTATION_METHOD_DECL modifierList type IDENT .?)
  218.     |   ^(VAR_DECLARATION modifierList type variableDeclaratorList)
  219.     |   typeDeclaration
  220.     ;
  221.  
  222.  
  223. // STATEMENTS / BLOCKS
  224.  
  225. block
  226.     :   ^(BLOCK_SCOPE .*)
  227.     ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement