Advertisement
Guest User

Untitled

a guest
Mar 19th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.29 KB | None | 0 0
  1. /**
  2. * For more information see the head comment within the 'java.g' grammar file
  3. * that defines the input for this tree grammar.
  4. *
  5. * BSD licence
  6. *
  7. * Copyright (c) 2007-2008 by HABELITZ Software Developments
  8. *
  9. * All rights reserved.
  10. *
  11. * http://www.habelitz.com
  12. *
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  35. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. tree grammar JavaTreeParser;
  39.  
  40. options {
  41. backtrack = true;
  42. memoize = true;
  43. tokenVocab = Java;
  44. ASTLabelType = CommonTree;
  45. output = AST;
  46. rewrite = true;
  47. }
  48.  
  49.  
  50. @treeparser::header {
  51. package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
  52. }
  53.  
  54. @treeparser::members {
  55.  
  56. boolean mMessageCollectionEnabled = false;
  57. private boolean mHasErrors = false;
  58. List<String> mMessages;
  59.  
  60. /**
  61. * Switches error message collection on or of.
  62. *
  63. * The standard destination for parser error messages is <code>System.err</code>.
  64. * However, if <code>true</code> gets passed to this method this default
  65. * behaviour will be switched off and all error messages will be collected
  66. * instead of written to anywhere.
  67. *
  68. * The default value is <code>false</code>.
  69. *
  70. * @param pNewState <code>true</code> if error messages should be collected.
  71. */
  72. public void enableErrorMessageCollection(boolean pNewState) {
  73. mMessageCollectionEnabled = pNewState;
  74. if (mMessages == null && mMessageCollectionEnabled) {
  75. mMessages = new ArrayList<String>();
  76. }
  77. }
  78.  
  79. /**
  80. * Collects an error message or passes the error message to <code>
  81. * super.emitErrorMessage(...)</code>.
  82. *
  83. * The actual behaviour depends on whether collecting error messages
  84. * has been enabled or not.
  85. *
  86. * @param pMessage The error message.
  87. */
  88. @Override
  89. public void emitErrorMessage(String pMessage) {
  90. if (mMessageCollectionEnabled) {
  91. mMessages.add(pMessage);
  92. } else {
  93. super.emitErrorMessage(pMessage);
  94. }
  95. }
  96.  
  97. /**
  98. * Returns collected error messages.
  99. *
  100. * @return A list holding collected error messages or <code>null</code> if
  101. * collecting error messages hasn't been enabled. Of course, this
  102. * list may be empty if no error message has been emited.
  103. */
  104. public List<String> getMessages() {
  105. return mMessages;
  106. }
  107.  
  108. /**
  109. * Tells if parsing a Java source has caused any error messages.
  110. *
  111. * @return <code>true</code> if parsing a Java source has caused at least one error message.
  112. */
  113. public boolean hasErrors() {
  114. return mHasErrors;
  115. }
  116. }
  117.  
  118. // Starting point for parsing a Java file.
  119. javaSource
  120. : ^(JAVA_SOURCE annotationList packageDeclaration? importDeclaration* typeDeclaration*)
  121. ;
  122.  
  123. packageDeclaration
  124. : ^(PACKAGE qualifiedIdentifier) ->
  125. ;
  126.  
  127. importDeclaration
  128. : ^(IMPORT STATIC? qualifiedIdentifier DOTSTAR?) ->
  129. ;
  130.  
  131. typeDeclaration
  132. : ^(CLASS modifierList IDENT genericTypeParameterList? extendsClause? implementsClause? classTopLevelScope) ->
  133. | ^(INTERFACE modifierList IDENT genericTypeParameterList? extendsClause? interfaceTopLevelScope) ->
  134. | ^(ENUM modifierList IDENT implementsClause? enumTopLevelScope) ->
  135. | ^(AT modifierList IDENT annotationTopLevelScope) ->
  136. ;
  137.  
  138. extendsClause // actually 'type' for classes and 'type+' for interfaces, but this has
  139. // been resolved by the parser grammar.
  140. : ^(EXTENDS_CLAUSE type+) ->
  141. ;
  142.  
  143. implementsClause
  144. : ^(IMPLEMENTS_CLAUSE type+) ->
  145. ;
  146.  
  147. genericTypeParameterList
  148. : ^(GENERIC_TYPE_PARAM_LIST genericTypeParameter+) ->
  149. ;
  150.  
  151. genericTypeParameter
  152. : ^(IDENT bound?) ->
  153. ;
  154.  
  155. bound
  156. : ^(EXTENDS_BOUND_LIST type+) ->
  157. ;
  158.  
  159. enumTopLevelScope
  160. : ^(ENUM_TOP_LEVEL_SCOPE enumConstant+ classTopLevelScope?) ->
  161. ;
  162.  
  163. enumConstant
  164. : ^(IDENT annotationList arguments? classTopLevelScope?) ->
  165. ;
  166.  
  167. classTopLevelScope
  168. : ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*) ->
  169. ;
  170.  
  171. classScopeDeclarations
  172. : ^(CLASS_INSTANCE_INITIALIZER block) ->
  173. | ^(CLASS_STATIC_INITIALIZER block) ->
  174. | ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause? block?) ->
  175. | ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause? block?) ->
  176. | ^(VAR_DECLARATION modifierList type variableDeclaratorList) ->
  177. | ^(CONSTRUCTOR_DECL modifierList genericTypeParameterList? formalParameterList throwsClause? block) ->
  178. | typeDeclaration
  179. ;
  180.  
  181. interfaceTopLevelScope
  182. : ^(INTERFACE_TOP_LEVEL_SCOPE interfaceScopeDeclarations*) ->
  183. ;
  184.  
  185. interfaceScopeDeclarations
  186. : ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?) ->
  187. | ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?) ->
  188. // Interface constant declarations have been switched to variable
  189. // declarations by 'java.g'; the parser has already checked that
  190. // there's an obligatory initializer.
  191. | ^(VAR_DECLARATION modifierList type variableDeclaratorList) ->
  192. | typeDeclaration
  193. ;
  194.  
  195. variableDeclaratorList
  196. : ^(VAR_DECLARATOR_LIST variableDeclarator+) ->
  197. ;
  198.  
  199. variableDeclarator
  200. : ^(VAR_DECLARATOR variableDeclaratorId variableInitializer?) ->
  201. ;
  202.  
  203. variableDeclaratorId
  204. : ^(IDENT arrayDeclaratorList?) ->
  205. ;
  206.  
  207. variableInitializer
  208. : arrayInitializer
  209. | expression
  210. ;
  211.  
  212. arrayDeclarator
  213. : LBRACK RBRACK
  214. ;
  215.  
  216. arrayDeclaratorList
  217. : ^(ARRAY_DECLARATOR_LIST ARRAY_DECLARATOR*) ->
  218. ;
  219.  
  220. arrayInitializer
  221. : ^(ARRAY_INITIALIZER variableInitializer*) ->
  222. ;
  223.  
  224. throwsClause
  225. : ^(THROWS_CLAUSE qualifiedIdentifier+) ->
  226. ;
  227.  
  228. modifierList
  229. : ^(MODIFIER_LIST modifier*) ->
  230. ;
  231.  
  232. modifier
  233. : PUBLIC
  234. | PROTECTED
  235. | PRIVATE
  236. | STATIC
  237. | ABSTRACT
  238. | NATIVE
  239. | SYNCHRONIZED
  240. | TRANSIENT
  241. | VOLATILE
  242. | STRICTFP
  243. | localModifier
  244. ;
  245.  
  246. localModifierList
  247. : ^(LOCAL_MODIFIER_LIST localModifier*) ->
  248. ;
  249.  
  250. localModifier
  251. : FINAL
  252. | annotation
  253. ;
  254.  
  255. type
  256. : ^(TYPE (primitiveType | qualifiedTypeIdent) arrayDeclaratorList?) ->
  257. ;
  258.  
  259. qualifiedTypeIdent
  260. : ^(QUALIFIED_TYPE_IDENT typeIdent+) ->
  261. ;
  262.  
  263. typeIdent
  264. : ^(IDENT genericTypeArgumentList?) ->
  265. ;
  266.  
  267. primitiveType
  268. : BOOLEAN
  269. | CHAR
  270. | BYTE
  271. | SHORT
  272. | INT
  273. | LONG
  274. | FLOAT
  275. | DOUBLE
  276. ;
  277.  
  278. genericTypeArgumentList
  279. : ^(GENERIC_TYPE_ARG_LIST genericTypeArgument+) ->
  280. ;
  281.  
  282. genericTypeArgument
  283. : type
  284. | ^(QUESTION genericWildcardBoundType?) ->
  285. ;
  286.  
  287. genericWildcardBoundType
  288. : ^(EXTENDS type) ->
  289. | ^(SUPER type) ->
  290. ;
  291.  
  292. formalParameterList
  293. : ^(FORMAL_PARAM_LIST formalParameterStandardDecl* formalParameterVarargDecl?) ->
  294. ;
  295.  
  296. formalParameterStandardDecl
  297. : ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId) ->
  298. ;
  299.  
  300. formalParameterVarargDecl
  301. : ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId) ->
  302. ;
  303.  
  304. qualifiedIdentifier
  305. : IDENT
  306. | ^(DOT qualifiedIdentifier IDENT) ->
  307. ;
  308.  
  309. // ANNOTATIONS
  310.  
  311. annotationList
  312. : ^(ANNOTATION_LIST annotation*) ->
  313. ;
  314.  
  315. annotation
  316. : ^(AT qualifiedIdentifier annotationInit?) ->
  317. ;
  318.  
  319. annotationInit
  320. : ^(ANNOTATION_INIT_BLOCK annotationInitializers) ->
  321. ;
  322.  
  323. annotationInitializers
  324. : ^(ANNOTATION_INIT_KEY_LIST annotationInitializer+) ->
  325. | ^(ANNOTATION_INIT_DEFAULT_KEY annotationElementValue) ->
  326. ;
  327.  
  328. annotationInitializer
  329. : ^(IDENT annotationElementValue) ->
  330. ;
  331.  
  332. annotationElementValue
  333. : ^(ANNOTATION_INIT_ARRAY_ELEMENT annotationElementValue*) ->
  334. | annotation
  335. | expression
  336. ;
  337.  
  338. annotationTopLevelScope
  339. : ^(ANNOTATION_TOP_LEVEL_SCOPE annotationScopeDeclarations*) ->
  340. ;
  341.  
  342. annotationScopeDeclarations
  343. : ^(ANNOTATION_METHOD_DECL modifierList type IDENT annotationDefaultValue?) ->
  344. | ^(VAR_DECLARATION modifierList type variableDeclaratorList) ->
  345. | typeDeclaration
  346. ;
  347.  
  348. annotationDefaultValue
  349. : ^(DEFAULT annotationElementValue) ->
  350. ;
  351.  
  352. // STATEMENTS / BLOCKS
  353.  
  354. block
  355. : ^(BLOCK_SCOPE blockStatement*) ->
  356. ;
  357.  
  358. blockStatement
  359. : localVariableDeclaration
  360. | typeDeclaration
  361. | statement
  362. ;
  363.  
  364. localVariableDeclaration
  365. : ^(VAR_DECLARATION localModifierList type variableDeclaratorList) ->
  366. ;
  367.  
  368.  
  369. statement
  370. : block
  371. | ^(ASSERT expression expression?) ->
  372. | ^(IF parenthesizedExpression statement statement?) ->
  373. | ^(FOR forInit forCondition forUpdater statement) ->
  374. | ^(FOR_EACH localModifierList type IDENT expression statement) ->
  375. | ^(WHILE parenthesizedExpression statement) ->
  376. | ^(DO statement parenthesizedExpression) ->
  377. | ^(TRY block catches? block?) -> // The second optional block is the optional finally block.
  378. | ^(SWITCH parenthesizedExpression switchBlockLabels) ->
  379. | ^(SYNCHRONIZED parenthesizedExpression block) ->
  380. | ^(RETURN expression?) ->
  381. | ^(THROW expression) ->
  382. | ^(BREAK IDENT?) ->
  383. | ^(CONTINUE IDENT?) ->
  384. | ^(LABELED_STATEMENT IDENT statement) ->
  385. | expression
  386. | SEMI // Empty statement.
  387. ;
  388.  
  389. catches
  390. : ^(CATCH_CLAUSE_LIST catchClause+) ->
  391. ;
  392.  
  393. catchClause
  394. : ^(CATCH formalParameterStandardDecl block) ->
  395. ;
  396.  
  397. switchBlockLabels
  398. : ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel* switchDefaultLabel? switchCaseLabel*) ->
  399. ;
  400.  
  401. switchCaseLabel
  402. : ^(CASE expression blockStatement*) ->
  403. ;
  404.  
  405. switchDefaultLabel
  406. : ^(DEFAULT blockStatement*) ->
  407. ;
  408.  
  409. forInit
  410. : ^(FOR_INIT (localVariableDeclaration | expression*)?) ->
  411. ;
  412.  
  413. forCondition
  414. : ^(FOR_CONDITION expression?) ->
  415. ;
  416.  
  417. forUpdater
  418. : ^(FOR_UPDATE expression*) ->
  419. ;
  420.  
  421. // EXPRESSIONS
  422.  
  423. parenthesizedExpression
  424. : ^(PARENTESIZED_EXPR expression) ->
  425. ;
  426.  
  427. expression
  428. : ^(EXPR expr) ->
  429. ;
  430.  
  431. expr
  432. : ^(ASSIGN expr expr) ->
  433. | ^(PLUS_ASSIGN expr expr) ->
  434. | ^(MINUS_ASSIGN expr expr) ->
  435. | ^(STAR_ASSIGN expr expr) ->
  436. | ^(DIV_ASSIGN expr expr) ->
  437. | ^(AND_ASSIGN expr expr) ->
  438. | ^(OR_ASSIGN expr expr) ->
  439. | ^(XOR_ASSIGN expr expr) ->
  440. | ^(MOD_ASSIGN expr expr) ->
  441. | ^(BIT_SHIFT_RIGHT_ASSIGN expr expr) ->
  442. | ^(SHIFT_RIGHT_ASSIGN expr expr) ->
  443. | ^(SHIFT_LEFT_ASSIGN expr expr) ->
  444. | ^(QUESTION expr expr expr) ->
  445. | ^(LOGICAL_OR expr expr) ->
  446. | ^(LOGICAL_AND expr expr) ->
  447. | ^(OR expr expr) ->
  448. | ^(XOR expr expr) ->
  449. | ^(AND expr expr) ->
  450. | ^(EQUAL expr expr) ->
  451. | ^(NOT_EQUAL expr expr) ->
  452. | ^(INSTANCEOF expr type) ->
  453. | ^(LESS_OR_EQUAL expr expr) ->
  454. | ^(GREATER_OR_EQUAL expr expr) ->
  455. | ^(BIT_SHIFT_RIGHT expr expr) ->
  456. | ^(SHIFT_RIGHT expr expr) ->
  457. | ^(GREATER_THAN expr expr) ->
  458. | ^(SHIFT_LEFT expr expr) ->
  459. | ^(LESS_THAN expr expr) ->
  460. | ^(PLUS expr expr) ->
  461. | ^(MINUS expr expr) ->
  462. | ^(STAR expr expr) ->
  463. | ^(DIV expr expr) ->
  464. | ^(MOD expr expr) ->
  465. | ^(UNARY_PLUS expr) ->
  466. | ^(UNARY_MINUS expr) ->
  467. | ^(PRE_INC expr) ->
  468. | ^(PRE_DEC expr) ->
  469. | ^(POST_INC expr) ->
  470. | ^(POST_DEC expr) ->
  471. | ^(NOT expr) ->
  472. | ^(LOGICAL_NOT expr) ->
  473. | ^(CAST_EXPR type expr) ->
  474. | primaryExpression
  475. ;
  476.  
  477. primaryExpression
  478. : ^( DOT
  479. ( primaryExpression
  480. ( IDENT
  481. | THIS
  482. | SUPER
  483. | innerNewExpression
  484. | CLASS
  485. )
  486. | primitiveType CLASS
  487. | VOID CLASS
  488. )
  489. ) ->
  490. | parenthesizedExpression
  491. | IDENT
  492. | ^(METHOD_CALL primaryExpression genericTypeArgumentList? arguments)
  493. | explicitConstructorCall
  494. | ^(ARRAY_ELEMENT_ACCESS primaryExpression expression)
  495. | literal
  496. | newExpression
  497. | THIS
  498. | arrayTypeDeclarator
  499. | SUPER
  500. ;
  501.  
  502. explicitConstructorCall
  503. : ^(THIS_CONSTRUCTOR_CALL genericTypeArgumentList? arguments) ->
  504. | ^(SUPER_CONSTRUCTOR_CALL primaryExpression? genericTypeArgumentList? arguments) ->
  505. ;
  506.  
  507. arrayTypeDeclarator
  508. : ^(ARRAY_DECLARATOR (arrayTypeDeclarator | qualifiedIdentifier | primitiveType)) ->
  509. ;
  510.  
  511. newExpression
  512. : ^( STATIC_ARRAY_CREATOR
  513. ( primitiveType newArrayConstruction
  514. | genericTypeArgumentList? qualifiedTypeIdent newArrayConstruction
  515. )
  516. ) ->
  517. | ^(CLASS_CONSTRUCTOR_CALL genericTypeArgumentList? qualifiedTypeIdent arguments classTopLevelScope?) ->
  518. ;
  519.  
  520. innerNewExpression // something like 'InnerType innerType = outer.new InnerType();'
  521. : ^(CLASS_CONSTRUCTOR_CALL genericTypeArgumentList? IDENT arguments classTopLevelScope?) ->
  522. ;
  523.  
  524. newArrayConstruction
  525. : arrayDeclaratorList arrayInitializer
  526. | expression+ arrayDeclaratorList?
  527. ;
  528.  
  529. arguments
  530. : ^(ARGUMENT_LIST expression*) ->
  531. ;
  532.  
  533. literal
  534. : HEX_LITERAL
  535. | OCTAL_LITERAL
  536. | DECIMAL_LITERAL
  537. | FLOATING_POINT_LITERAL
  538. | CHARACTER_LITERAL
  539. | STRING_LITERAL
  540. | TRUE
  541. | FALSE
  542. | NULL
  543. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement