fork download
  1. /**
  2. * An ANTLRv3 capable Java 1.5 grammar for building ASTs.
  3. *
  4. * Note that there's also the tree grammar 'JavaTreeParser.g' that can be fed
  5. * with this grammer's output.
  6. *
  7. *
  8. * Please report any detected errors or even suggestions regarding this grammar
  9. * to
  10. *
  11. * dieter [D O T] habelitz [A T] habelitz [D O T] com
  12. *
  13. * with the subject
  14. *
  15. * jsom grammar: [your subject note]
  16. *
  17. * To generate a parser based on this grammar you'll need ANTLRv3, which you can
  18. * get from 'http://w...content-available-to-author-only...r.org'.
  19. *
  20. *
  21. * This grammar is published under the ...
  22. *
  23. * BSD licence
  24. *
  25. * Copyright (c) 2007-2008 by HABELITZ Software Developments
  26. *
  27. * All rights reserved.
  28. *
  29. * http://w...content-available-to-author-only...z.com
  30. *
  31. *
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions
  34. * are met:
  35. *
  36. * 1. Redistributions of source code must retain the above copyright
  37. * notice, this list of conditions and the following disclaimer.
  38. * 2. Redistributions in binary form must reproduce the above copyright
  39. * notice, this list of conditions and the following disclaimer in the
  40. * documentation and/or other materials provided with the distribution.
  41. * 3. The name of the author may not be used to endorse or promote products
  42. * derived from this software without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS''
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  50. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  51. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  52. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  53. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. */
  56.  
  57. grammar Java;
  58.  
  59. options {
  60. backtrack = true;
  61. memoize = true;
  62. output = AST;
  63. ASTLabelType = CommonTree;
  64. }
  65.  
  66. tokens {
  67.  
  68. // operators and other special chars
  69.  
  70. AND = '&' ;
  71. AND_ASSIGN = '&=' ;
  72. ASSIGN = '=' ;
  73. AT = '@' ;
  74. BIT_SHIFT_RIGHT = '>>>' ;
  75. BIT_SHIFT_RIGHT_ASSIGN = '>>>=' ;
  76. COLON = ':' ;
  77. COMMA = ',' ;
  78. DEC = '--' ;
  79. DIV = '/' ;
  80. DIV_ASSIGN = '/=' ;
  81. DOT = '.' ;
  82. DOTSTAR = '.*' ;
  83. ELLIPSIS = '...' ;
  84. EQUAL = '==' ;
  85. GREATER_OR_EQUAL = '>=' ;
  86. GREATER_THAN = '>' ;
  87. INC = '++' ;
  88. LBRACK = '[' ;
  89. LCURLY = '{' ;
  90. LESS_OR_EQUAL = '<=' ;
  91. LESS_THAN = '<' ;
  92. LOGICAL_AND = '&&' ;
  93. LOGICAL_NOT = '!' ;
  94. LOGICAL_OR = '||' ;
  95. LPAREN = '(' ;
  96. MINUS = '-' ;
  97. MINUS_ASSIGN = '-=' ;
  98. MOD = '%' ;
  99. MOD_ASSIGN = '%=' ;
  100. NOT = '~' ;
  101. NOT_EQUAL = '!=' ;
  102. OR = '|' ;
  103. OR_ASSIGN = '|=' ;
  104. PLUS = '+' ;
  105. PLUS_ASSIGN = '+=' ;
  106. QUESTION = '?' ;
  107. RBRACK = ']' ;
  108. RCURLY = '}' ;
  109. RPAREN = ')' ;
  110. SEMI = ';' ;
  111. SHIFT_LEFT = '<<' ;
  112. SHIFT_LEFT_ASSIGN = '<<=' ;
  113. SHIFT_RIGHT = '>>' ;
  114. SHIFT_RIGHT_ASSIGN = '>>=' ;
  115. STAR = '*' ;
  116. STAR_ASSIGN = '*=' ;
  117. XOR = '^' ;
  118. XOR_ASSIGN = '^=' ;
  119.  
  120. // keywords
  121.  
  122. ABSTRACT = 'abstract' ;
  123. ASSERT = 'assert' ;
  124. BOOLEAN = 'boolean' ;
  125. BREAK = 'break' ;
  126. BYTE = 'byte' ;
  127. CASE = 'case' ;
  128. CATCH = 'catch' ;
  129. CHAR = 'char' ;
  130. CLASS = 'class' ;
  131. CONTINUE = 'continue' ;
  132. DEFAULT = 'default' ;
  133. DO = 'do' ;
  134. DOUBLE = 'double' ;
  135. ELSE = 'else' ;
  136. ENUM = 'enum' ;
  137. EXTENDS = 'extends' ;
  138. FALSE = 'false' ;
  139. FINAL = 'final' ;
  140. FINALLY = 'finally' ;
  141. FLOAT = 'float' ;
  142. FOR = 'for' ;
  143. IF = 'if' ;
  144. IMPLEMENTS = 'implements' ;
  145. INSTANCEOF = 'instanceof' ;
  146. INTERFACE = 'interface' ;
  147. IMPORT = 'import' ;
  148. INT = 'int' ;
  149. LONG = 'long' ;
  150. NATIVE = 'native' ;
  151. NEW = 'new' ;
  152. NULL = 'null' ;
  153. PACKAGE = 'package' ;
  154. PRIVATE = 'private' ;
  155. PROTECTED = 'protected' ;
  156. PUBLIC = 'public' ;
  157. RETURN = 'return' ;
  158. SHORT = 'short' ;
  159. STATIC = 'static' ;
  160. STRICTFP = 'strictfp' ;
  161. SUPER = 'super' ;
  162. SWITCH = 'switch' ;
  163. SYNCHRONIZED = 'synchronized' ;
  164. THIS = 'this' ;
  165. THROW = 'throw' ;
  166. THROWS = 'throws' ;
  167. TRANSIENT = 'transient' ;
  168. TRUE = 'true' ;
  169. TRY = 'try' ;
  170. VOID = 'void' ;
  171. VOLATILE = 'volatile' ;
  172. WHILE = 'while' ;
  173.  
  174. // tokens for imaginary nodes
  175.  
  176. ANNOTATION_INIT_ARRAY_ELEMENT;
  177. ANNOTATION_INIT_BLOCK;
  178. ANNOTATION_INIT_DEFAULT_KEY;
  179. ANNOTATION_INIT_KEY_LIST;
  180. ANNOTATION_LIST;
  181. ANNOTATION_METHOD_DECL;
  182. ANNOTATION_SCOPE;
  183. ANNOTATION_TOP_LEVEL_SCOPE;
  184. ARGUMENT_LIST;
  185. ARRAY_DECLARATOR;
  186. ARRAY_DECLARATOR_LIST;
  187. ARRAY_ELEMENT_ACCESS;
  188. ARRAY_INITIALIZER;
  189. BLOCK_SCOPE;
  190. CAST_EXPR;
  191. CATCH_CLAUSE_LIST;
  192. CLASS_CONSTRUCTOR_CALL;
  193. CLASS_INSTANCE_INITIALIZER;
  194. CLASS_STATIC_INITIALIZER;
  195. CLASS_TOP_LEVEL_SCOPE;
  196. CONSTRUCTOR_DECL;
  197. ENUM_TOP_LEVEL_SCOPE;
  198. EXPR;
  199. EXTENDS_BOUND_LIST;
  200. EXTENDS_CLAUSE;
  201. FOR_CONDITION;
  202. FOR_EACH;
  203. FOR_INIT;
  204. FOR_UPDATE;
  205. FORMAL_PARAM_LIST;
  206. FORMAL_PARAM_STD_DECL;
  207. FORMAL_PARAM_VARARG_DECL;
  208. FUNCTION_METHOD_DECL;
  209. GENERIC_TYPE_ARG_LIST;
  210. GENERIC_TYPE_PARAM_LIST;
  211. INTERFACE_TOP_LEVEL_SCOPE;
  212. IMPLEMENTS_CLAUSE;
  213. LABELED_STATEMENT;
  214. LOCAL_MODIFIER_LIST;
  215. JAVA_SOURCE;
  216. METHOD_CALL;
  217. MODIFIER_LIST;
  218. PARENTESIZED_EXPR;
  219. POST_DEC;
  220. POST_INC;
  221. PRE_DEC;
  222. PRE_INC;
  223. QUALIFIED_TYPE_IDENT;
  224. STATIC_ARRAY_CREATOR;
  225. SUPER_CONSTRUCTOR_CALL;
  226. SWITCH_BLOCK_LABEL_LIST;
  227. THIS_CONSTRUCTOR_CALL;
  228. THROWS_CLAUSE;
  229. TYPE;
  230. UNARY_MINUS;
  231. UNARY_PLUS;
  232. VAR_DECLARATION;
  233. VAR_DECLARATOR;
  234. VAR_DECLARATOR_LIST;
  235. VOID_METHOD_DECL;
  236. }
  237.  
  238.  
  239.  
  240. @members {
  241.  
  242. private boolean mMessageCollectionEnabled = false;
  243. private boolean mHasErrors = false;
  244. private List<String> mMessages;
  245.  
  246. /**
  247. * Switches error message collection on or of.
  248. *
  249. * The standard destination for parser error messages is <code>System.err</code>.
  250. * However, if <code>true</code> gets passed to this method this default
  251. * behaviour will be switched off and all error messages will be collected
  252. * instead of written to anywhere.
  253. *
  254. * The default value is <code>false</code>.
  255. *
  256. * @param pNewState <code>true</code> if error messages should be collected.
  257. */
  258. public void enableErrorMessageCollection(boolean pNewState) {
  259. mMessageCollectionEnabled = pNewState;
  260. if (mMessages == null && mMessageCollectionEnabled) {
  261. mMessages = new ArrayList<String>();
  262. }
  263. }
  264.  
  265. /**
  266. * Collects an error message or passes the error message to <code>
  267. * super.emitErrorMessage(...)</code>.
  268. *
  269. * The actual behaviour depends on whether collecting error messages
  270. * has been enabled or not.
  271. *
  272. * @param pMessage The error message.
  273. */
  274. @Override
  275. public void emitErrorMessage(String pMessage) {
  276. if (mMessageCollectionEnabled) {
  277. mMessages.add(pMessage);
  278. } else {
  279. super.emitErrorMessage(pMessage);
  280. }
  281. }
  282.  
  283. /**
  284. * Returns collected error messages.
  285. *
  286. * @return A list holding collected error messages or <code>null</code> if
  287. * collecting error messages hasn't been enabled. Of course, this
  288. * list may be empty if no error message has been emited.
  289. */
  290. public List<String> getMessages() {
  291. return mMessages;
  292. }
  293.  
  294. /**
  295. * Tells if parsing a Java source has caused any error messages.
  296. *
  297. * @return <code>true</code> if parsing a Java source has caused at least one error message.
  298. */
  299. public boolean hasErrors() {
  300. return mHasErrors;
  301. }
  302. }
  303.  
  304.  
  305.  
  306. @lexer::members {
  307. /**
  308. * Determines if whitespaces and comments should be preserved or thrown away.
  309. *
  310. * If <code>true</code> whitespaces and comments will be preserved within the
  311. * hidden channel, otherwise the appropriate tokens will be skiped. This is
  312. * a 'little bit' expensive, of course. If only one of the two behaviours is
  313. * needed forever the lexer part of the grammar should be changed by replacing
  314. * the 'if-else' stuff within the approprate lexer grammar actions.
  315. */
  316. public boolean preserveWhitespacesAndComments = false;
  317. }
  318.  
  319. // Starting point for parsing a Java file.
  320. javaSource
  321. : compilationUnit
  322. -> ^(JAVA_SOURCE compilationUnit)
  323. ;
  324.  
  325. compilationUnit
  326. : annotationList
  327. packageDeclaration?
  328. importDeclaration*
  329. typeDecls*
  330. ;
  331.  
  332. typeDecls
  333. : typeDeclaration
  334. | SEMI!
  335. ;
  336.  
  337. packageDeclaration
  338. : PACKAGE^ qualifiedIdentifier SEMI!
  339. ;
  340.  
  341. importDeclaration
  342. : IMPORT^ STATIC? qualifiedIdentifier DOTSTAR? SEMI!
  343. ;
  344.  
  345. typeDeclaration
  346. : modifierList!
  347. ( classTypeDeclaration[$modifierList.tree]
  348. | interfaceTypeDeclaration[$modifierList.tree]
  349. | enumTypeDeclaration[$modifierList.tree]
  350. | annotationTypeDeclaration[$modifierList.tree]
  351. )
  352. ;
  353.  
  354. classTypeDeclaration[CommonTree modifiers]
  355. : CLASS IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody
  356. -> ^(CLASS {$modifiers} IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody)
  357. ;
  358.  
  359. classExtendsClause
  360. : EXTENDS type
  361. -> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] type)
  362. ;
  363.  
  364. interfaceExtendsClause
  365. : EXTENDS typeList
  366. -> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] typeList)
  367. ;
  368.  
  369. implementsClause
  370. : IMPLEMENTS typeList
  371. -> ^(IMPLEMENTS_CLAUSE[$IMPLEMENTS, "IMPLEMENTS_CLAUSE"] typeList)
  372. ;
  373.  
  374. genericTypeParameterList
  375. : LESS_THAN genericTypeParameter (COMMA genericTypeParameter)* genericTypeListClosing
  376. -> ^(GENERIC_TYPE_PARAM_LIST[$LESS_THAN, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
  377. ;
  378.  
  379. genericTypeListClosing // This 'trick' is fairly dirty - if there's some time a better solution should
  380. // be found to resolve the problem with nested generic type parameter lists
  381. // (i.e. <T1 extends AnyType<T2>> for generic type parameters or <T1<T2>> for
  382. // generic type arguments etc).
  383. : GREATER_THAN
  384. | SHIFT_RIGHT
  385. | BIT_SHIFT_RIGHT
  386. | // nothing
  387. ;
  388.  
  389. genericTypeParameter
  390. : IDENT bound?
  391. -> ^(IDENT bound?)
  392. ;
  393.  
  394. bound
  395. : EXTENDS type (AND type)*
  396. -> ^(EXTENDS_BOUND_LIST[$EXTENDS, "EXTENDS_BOUND_LIST"] type+)
  397. ;
  398.  
  399. enumTypeDeclaration[CommonTree modifiers]
  400. : ENUM IDENT implementsClause? enumBody
  401. -> ^(ENUM {$modifiers} IDENT implementsClause? enumBody)
  402. ;
  403.  
  404. enumBody
  405. : LCURLY enumScopeDeclarations RCURLY
  406. -> ^(ENUM_TOP_LEVEL_SCOPE[$LCURLY, "ENUM_TOP_LEVEL_SCOPE"] enumScopeDeclarations)
  407. ;
  408.  
  409. enumScopeDeclarations
  410. : enumConstants (COMMA!)? enumClassScopeDeclarations?
  411. ;
  412.  
  413. enumClassScopeDeclarations
  414. : SEMI classScopeDeclarations*
  415. -> ^(CLASS_TOP_LEVEL_SCOPE[$SEMI, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
  416. ;
  417.  
  418. enumConstants
  419. : enumConstant (COMMA! enumConstant)*
  420. ;
  421.  
  422. enumConstant
  423. : annotationList IDENT^ arguments? classBody?
  424. ;
  425.  
  426. interfaceTypeDeclaration[CommonTree modifiers]
  427. : INTERFACE IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody
  428. -> ^(INTERFACE {$modifiers} IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody)
  429. ;
  430.  
  431. typeList
  432. : type (COMMA! type)*
  433. ;
  434.  
  435. classBody
  436. : LCURLY classScopeDeclarations* RCURLY
  437. -> ^(CLASS_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
  438. ;
  439.  
  440. interfaceBody
  441. : LCURLY interfaceScopeDeclarations* RCURLY
  442. -> ^(INTERFACE_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] interfaceScopeDeclarations*)
  443. ;
  444.  
  445. classScopeDeclarations
  446. : block -> ^(CLASS_INSTANCE_INITIALIZER block)
  447. | STATIC block -> ^(CLASS_STATIC_INITIALIZER[$STATIC, "CLASS_STATIC_INITIALIZER"] block)
  448. | modifierList
  449. ( genericTypeParameterList?
  450. ( type IDENT formalParameterList arrayDeclaratorList? throwsClause? (block | SEMI)
  451. -> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause? block?)
  452. | VOID IDENT formalParameterList throwsClause? (block | SEMI)
  453. -> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause? block?)
  454. | ident=IDENT formalParameterList throwsClause? block
  455. -> ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList genericTypeParameterList? formalParameterList throwsClause? block)
  456. )
  457. | type classFieldDeclaratorList SEMI
  458. -> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
  459. )
  460. | typeDeclaration
  461. | SEMI!
  462. ;
  463.  
  464. interfaceScopeDeclarations
  465. : modifierList
  466. ( genericTypeParameterList?
  467. ( type IDENT formalParameterList arrayDeclaratorList? throwsClause? SEMI
  468. -> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?)
  469. | VOID IDENT formalParameterList throwsClause? SEMI
  470. -> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?)
  471. )
  472. | type interfaceFieldDeclaratorList SEMI
  473. -> ^(VAR_DECLARATION modifierList type interfaceFieldDeclaratorList)
  474. )
  475. | typeDeclaration
  476. | SEMI!
  477. ;
  478.  
  479. classFieldDeclaratorList
  480. : classFieldDeclarator (COMMA classFieldDeclarator)*
  481. -> ^(VAR_DECLARATOR_LIST classFieldDeclarator+)
  482. ;
  483.  
  484. classFieldDeclarator
  485. : variableDeclaratorId (ASSIGN variableInitializer)?
  486. -> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer?)
  487. ;
  488.  
  489. interfaceFieldDeclaratorList
  490. : interfaceFieldDeclarator (COMMA interfaceFieldDeclarator)*
  491. -> ^(VAR_DECLARATOR_LIST interfaceFieldDeclarator+)
  492. ;
  493.  
  494. interfaceFieldDeclarator
  495. : variableDeclaratorId ASSIGN variableInitializer
  496. -> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer)
  497. ;
  498.  
  499. variableDeclaratorId
  500. : IDENT^ arrayDeclaratorList?
  501. ;
  502.  
  503. variableInitializer
  504. : arrayInitializer
  505. | expression
  506. ;
  507.  
  508. arrayDeclarator
  509. : LBRACK RBRACK
  510. -> ^(ARRAY_DECLARATOR)
  511. ;
  512.  
  513. arrayDeclaratorList
  514. : arrayDeclarator+
  515. -> ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)
  516. ;
  517.  
  518. arrayInitializer
  519. : LCURLY (variableInitializer (COMMA variableInitializer)* COMMA?)? RCURLY
  520. -> ^(ARRAY_INITIALIZER[$LCURLY, "ARRAY_INITIALIZER"] variableInitializer*)
  521. ;
  522.  
  523. throwsClause
  524. : THROWS qualifiedIdentList
  525. -> ^(THROWS_CLAUSE[$THROWS, "THROWS_CLAUSE"] qualifiedIdentList)
  526. ;
  527.  
  528. modifierList
  529. : modifier*
  530. -> ^(MODIFIER_LIST modifier*)
  531. ;
  532.  
  533. modifier
  534. : PUBLIC
  535. | PROTECTED
  536. | PRIVATE
  537. | STATIC
  538. | ABSTRACT
  539. | NATIVE
  540. | SYNCHRONIZED
  541. | TRANSIENT
  542. | VOLATILE
  543. | STRICTFP
  544. | localModifier
  545. ;
  546.  
  547. localModifierList
  548. : localModifier*
  549. -> ^(LOCAL_MODIFIER_LIST localModifier*)
  550. ;
  551.  
  552. localModifier
  553. : FINAL
  554. | annotation
  555. ;
  556.  
  557. type
  558. : simpleType
  559. | objectType
  560. ;
  561.  
  562. simpleType // including static arrays of simple type elements
  563. : primitiveType arrayDeclaratorList?
  564. -> ^(TYPE primitiveType arrayDeclaratorList?)
  565. ;
  566.  
  567. objectType // including static arrays of object type reference elements
  568. : qualifiedTypeIdent arrayDeclaratorList?
  569. -> ^(TYPE qualifiedTypeIdent arrayDeclaratorList?)
  570. ;
  571.  
  572. objectTypeSimplified
  573. : qualifiedTypeIdentSimplified arrayDeclaratorList?
  574. -> ^(TYPE qualifiedTypeIdentSimplified arrayDeclaratorList?)
  575. ;
  576.  
  577. qualifiedTypeIdent
  578. : typeIdent (DOT typeIdent)*
  579. -> ^(QUALIFIED_TYPE_IDENT typeIdent+)
  580. ;
  581.  
  582. qualifiedTypeIdentSimplified
  583. : typeIdentSimplified (DOT typeIdentSimplified)*
  584. -> ^(QUALIFIED_TYPE_IDENT typeIdentSimplified+)
  585. ;
  586.  
  587. typeIdent
  588. : IDENT^ genericTypeArgumentList?
  589. ;
  590.  
  591. typeIdentSimplified
  592. : IDENT^ genericTypeArgumentListSimplified?
  593. ;
  594.  
  595. primitiveType
  596. : BOOLEAN
  597. | CHAR
  598. | BYTE
  599. | SHORT
  600. | INT
  601. | LONG
  602. | FLOAT
  603. | DOUBLE
  604. ;
  605.  
  606. genericTypeArgumentList
  607. : LESS_THAN genericTypeArgument (COMMA genericTypeArgument)* genericTypeListClosing
  608. -> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
  609. ;
  610.  
  611. genericTypeArgument
  612. : type
  613. | QUESTION genericWildcardBoundType?
  614. -> ^(QUESTION genericWildcardBoundType?)
  615. ;
  616.  
  617. genericWildcardBoundType
  618. : (EXTENDS | SUPER)^ type
  619. ;
  620.  
  621. genericTypeArgumentListSimplified
  622. : LESS_THAN genericTypeArgumentSimplified (COMMA genericTypeArgumentSimplified)* genericTypeListClosing
  623. -> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgumentSimplified+)
  624. ;
  625.  
  626. genericTypeArgumentSimplified
  627. : type
  628. | QUESTION
  629. ;
  630.  
  631. qualifiedIdentList
  632. : qualifiedIdentifier (COMMA! qualifiedIdentifier)*
  633. ;
  634.  
  635. formalParameterList
  636. : LPAREN
  637. ( // Contains at least one standard argument declaration and optionally a variable argument declaration.
  638. formalParameterStandardDecl (COMMA formalParameterStandardDecl)* (COMMA formalParameterVarArgDecl)?
  639. -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterStandardDecl+ formalParameterVarArgDecl?)
  640. // Contains a variable argument declaration only.
  641. | formalParameterVarArgDecl
  642. -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterVarArgDecl)
  643. // Contains nothing.
  644. | -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"])
  645. )
  646. RPAREN
  647. ;
  648.  
  649. formalParameterStandardDecl
  650. : localModifierList type variableDeclaratorId
  651. -> ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId)
  652. ;
  653.  
  654. formalParameterVarArgDecl
  655. : localModifierList type ELLIPSIS variableDeclaratorId
  656. -> ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId)
  657. ;
  658.  
  659. qualifiedIdentifier
  660. : ( IDENT -> IDENT
  661. )
  662. ( DOT ident=IDENT -> ^(DOT $qualifiedIdentifier $ident)
  663. )*
  664. ;
  665.  
  666. // ANNOTATIONS
  667.  
  668. annotationList
  669. : annotation*
  670. -> ^(ANNOTATION_LIST annotation*)
  671. ;
  672.  
  673. annotation
  674. : AT^ qualifiedIdentifier annotationInit?
  675. ;
  676.  
  677. annotationInit
  678. : LPAREN annotationInitializers RPAREN
  679. -> ^(ANNOTATION_INIT_BLOCK[$LPAREN, "ANNOTATION_INIT_BLOCK"] annotationInitializers)
  680. ;
  681.  
  682. annotationInitializers
  683. : annotationInitializer (COMMA annotationInitializer)*
  684. -> ^(ANNOTATION_INIT_KEY_LIST annotationInitializer+)
  685. | annotationElementValue // implicite initialization of the annotation field 'value'
  686. -> ^(ANNOTATION_INIT_DEFAULT_KEY annotationElementValue)
  687. ;
  688.  
  689. annotationInitializer
  690. : IDENT^ ASSIGN! annotationElementValue
  691. ;
  692.  
  693. annotationElementValue
  694. : annotationElementValueExpression
  695. | annotation
  696. | annotationElementValueArrayInitializer
  697. ;
  698.  
  699. annotationElementValueExpression
  700. : conditionalExpression
  701. -> ^(EXPR conditionalExpression)
  702. ;
  703.  
  704. annotationElementValueArrayInitializer
  705. : LCURLY (annotationElementValue (COMMA annotationElementValue)*)? (COMMA)? RCURLY
  706. -> ^(ANNOTATION_INIT_ARRAY_ELEMENT[$LCURLY, "ANNOTATION_ELEM_VALUE_ARRAY_INIT"] annotationElementValue*)
  707. ;
  708.  
  709. annotationTypeDeclaration[CommonTree modifiers]
  710. : AT INTERFACE IDENT annotationBody
  711. -> ^(AT {$modifiers} IDENT annotationBody)
  712. ;
  713.  
  714. annotationBody
  715. : LCURLY annotationScopeDeclarations* RCURLY
  716. -> ^(ANNOTATION_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] annotationScopeDeclarations*)
  717. ;
  718.  
  719. annotationScopeDeclarations
  720. : modifierList type
  721. ( IDENT LPAREN RPAREN annotationDefaultValue? SEMI
  722. -> ^(ANNOTATION_METHOD_DECL modifierList type IDENT annotationDefaultValue?)
  723. | classFieldDeclaratorList SEMI
  724. -> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
  725. )
  726. | typeDeclaration
  727. ;
  728.  
  729. annotationDefaultValue
  730. : DEFAULT^ annotationElementValue
  731. ;
  732.  
  733. // STATEMENTS / BLOCKS
  734.  
  735. block
  736. : LCURLY blockStatement* RCURLY
  737. -> ^(BLOCK_SCOPE[$LCURLY, "BLOCK_SCOPE"] blockStatement*)
  738. ;
  739.  
  740. blockStatement
  741. : localVariableDeclaration SEMI!
  742. | typeDeclaration
  743. | statement
  744. ;
  745.  
  746. localVariableDeclaration
  747. : localModifierList type classFieldDeclaratorList
  748. -> ^(VAR_DECLARATION localModifierList type classFieldDeclaratorList)
  749. ;
  750.  
  751.  
  752. statement
  753. : block
  754. | ASSERT expr1=expression
  755. ( COLON expr2=expression SEMI -> ^(ASSERT $expr1 $expr2)
  756. | SEMI -> ^(ASSERT $expr1)
  757. )
  758. | IF parenthesizedExpression ifStat=statement
  759. ( ELSE elseStat=statement -> ^(IF parenthesizedExpression $ifStat $elseStat)
  760. | -> ^(IF parenthesizedExpression $ifStat)
  761. )
  762. | FOR LPAREN
  763. ( forInit SEMI forCondition SEMI forUpdater RPAREN statement -> ^(FOR forInit forCondition forUpdater statement)
  764. | localModifierList type IDENT COLON expression RPAREN statement
  765. -> ^(FOR_EACH[$FOR, "FOR_EACH"] localModifierList type IDENT expression statement)
  766. )
  767. | WHILE parenthesizedExpression statement -> ^(WHILE parenthesizedExpression statement)
  768. | DO statement WHILE parenthesizedExpression SEMI -> ^(DO statement parenthesizedExpression)
  769. | TRY block (catches finallyClause? | finallyClause) -> ^(TRY block catches? finallyClause?)
  770. | SWITCH parenthesizedExpression LCURLY switchBlockLabels RCURLY -> ^(SWITCH parenthesizedExpression switchBlockLabels)
  771. | SYNCHRONIZED parenthesizedExpression block -> ^(SYNCHRONIZED parenthesizedExpression block)
  772. | RETURN expression? SEMI -> ^(RETURN expression?)
  773. | THROW expression SEMI -> ^(THROW expression)
  774. | BREAK IDENT? SEMI -> ^(BREAK IDENT?)
  775. | CONTINUE IDENT? SEMI -> ^(CONTINUE IDENT?)
  776. | IDENT COLON statement -> ^(LABELED_STATEMENT IDENT statement)
  777. | expression SEMI!
  778. | SEMI // Preserve empty statements.
  779. ;
  780.  
  781. catches
  782. : catchClause+
  783. -> ^(CATCH_CLAUSE_LIST catchClause+)
  784. ;
  785.  
  786. catchClause
  787. : CATCH^ LPAREN! formalParameterStandardDecl RPAREN! block
  788. ;
  789.  
  790. finallyClause
  791. : FINALLY block
  792. -> block
  793. ;
  794.  
  795. switchBlockLabels
  796. : switchCaseLabels switchDefaultLabel? switchCaseLabels
  797. -> ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabels switchDefaultLabel? switchCaseLabels)
  798. ;
  799.  
  800. switchCaseLabels
  801. : switchCaseLabel*
  802. ;
  803.  
  804. switchCaseLabel
  805. : CASE^ expression COLON! blockStatement*
  806. ;
  807.  
  808. switchDefaultLabel
  809. : DEFAULT^ COLON! blockStatement*
  810. ;
  811.  
  812. forInit
  813. : localVariableDeclaration -> ^(FOR_INIT localVariableDeclaration)
  814. | expressionList -> ^(FOR_INIT expressionList)
  815. | -> ^(FOR_INIT)
  816. ;
  817.  
  818. forCondition
  819. : expression?
  820. -> ^(FOR_CONDITION expression?)
  821. ;
  822.  
  823. forUpdater
  824. : expressionList?
  825. -> ^(FOR_UPDATE expressionList?)
  826. ;
  827.  
  828. // EXPRESSIONS
  829.  
  830. parenthesizedExpression
  831. : LPAREN expression RPAREN
  832. -> ^(PARENTESIZED_EXPR[$LPAREN, "PARENTESIZED_EXPR"] expression)
  833. ;
  834.  
  835. expressionList
  836. : expression (COMMA! expression)*
  837. ;
  838.  
  839. expression
  840. : assignmentExpression
  841. -> ^(EXPR assignmentExpression)
  842. ;
  843.  
  844. assignmentExpression
  845. : conditionalExpression
  846. ( ( ASSIGN^
  847. | PLUS_ASSIGN^
  848. | MINUS_ASSIGN^
  849. | STAR_ASSIGN^
  850. | DIV_ASSIGN^
  851. | AND_ASSIGN^
  852. | OR_ASSIGN^
  853. | XOR_ASSIGN^
  854. | MOD_ASSIGN^
  855. | SHIFT_LEFT_ASSIGN^
  856. | SHIFT_RIGHT_ASSIGN^
  857. | BIT_SHIFT_RIGHT_ASSIGN^
  858. )
  859. assignmentExpression)?
  860. ;
  861.  
  862. conditionalExpression
  863. : logicalOrExpression (QUESTION^ assignmentExpression COLON! conditionalExpression)?
  864. ;
  865.  
  866. logicalOrExpression
  867. : logicalAndExpression (LOGICAL_OR^ logicalAndExpression)*
  868. ;
  869.  
  870. logicalAndExpression
  871. : inclusiveOrExpression (LOGICAL_AND^ inclusiveOrExpression)*
  872. ;
  873.  
  874. inclusiveOrExpression
  875. : exclusiveOrExpression (OR^ exclusiveOrExpression)*
  876. ;
  877.  
  878. exclusiveOrExpression
  879. : andExpression (XOR^ andExpression)*
  880. ;
  881.  
  882. andExpression
  883. : equalityExpression (AND^ equalityExpression)*
  884. ;
  885.  
  886. equalityExpression
  887. : instanceOfExpression
  888. ( ( EQUAL^
  889. | NOT_EQUAL^
  890. )
  891. instanceOfExpression
  892. )*
  893. ;
  894.  
  895. instanceOfExpression
  896. : relationalExpression (INSTANCEOF^ type)?
  897. ;
  898.  
  899. relationalExpression
  900. : shiftExpression
  901. ( ( LESS_OR_EQUAL^
  902. | GREATER_OR_EQUAL^
  903. | LESS_THAN^
  904. | GREATER_THAN^
  905. )
  906. shiftExpression
  907. )*
  908. ;
  909.  
  910. shiftExpression
  911. : additiveExpression
  912. ( ( BIT_SHIFT_RIGHT^
  913. | SHIFT_RIGHT^
  914. | SHIFT_LEFT^
  915. )
  916. additiveExpression
  917. )*
  918. ;
  919.  
  920. additiveExpression
  921. : multiplicativeExpression
  922. ( ( PLUS^
  923. | MINUS^
  924. )
  925. multiplicativeExpression
  926. )*
  927. ;
  928.  
  929. multiplicativeExpression
  930. : unaryExpression
  931. ( ( STAR^
  932. | DIV^
  933. | MOD^
  934. )
  935. unaryExpression
  936. )*
  937. ;
  938.  
  939. unaryExpression
  940. : PLUS unaryExpression -> ^(UNARY_PLUS[$PLUS, "UNARY_PLUS"] unaryExpression)
  941. | MINUS unaryExpression -> ^(UNARY_MINUS[$MINUS, "UNARY_MINUS"] unaryExpression)
  942. | INC postfixedExpression -> ^(PRE_INC[$INC, "PRE_INC"] postfixedExpression)
  943. | DEC postfixedExpression -> ^(PRE_DEC[$DEC, "PRE_DEC"] postfixedExpression)
  944. | unaryExpressionNotPlusMinus
  945. ;
  946.  
  947. unaryExpressionNotPlusMinus
  948. : NOT unaryExpression -> ^(NOT unaryExpression)
  949. | LOGICAL_NOT unaryExpression -> ^(LOGICAL_NOT unaryExpression)
  950. | LPAREN type RPAREN unaryExpression -> ^(CAST_EXPR[$LPAREN, "CAST_EXPR"] type unaryExpression)
  951. | postfixedExpression
  952. ;
  953.  
  954. postfixedExpression
  955. // At first resolve the primary expression ...
  956. : ( primaryExpression -> primaryExpression
  957. )
  958. // ... and than the optional things that may follow a primary expression 0 or more times.
  959. ( outerDot=DOT
  960. ( ( genericTypeArgumentListSimplified? // Note: generic type arguments are only valid for method calls, i.e. if there
  961. // is an argument list.
  962. IDENT -> ^(DOT $postfixedExpression IDENT)
  963. )
  964. ( arguments -> ^(METHOD_CALL $postfixedExpression genericTypeArgumentListSimplified? arguments)
  965. )?
  966. | THIS -> ^(DOT $postfixedExpression THIS)
  967. | Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] $postfixedExpression arguments)
  968. | ( SUPER innerDot=DOT IDENT -> ^($innerDot ^($outerDot $postfixedExpression SUPER) IDENT)
  969. )
  970. ( arguments -> ^(METHOD_CALL $postfixedExpression arguments)
  971. )?
  972. | innerNewExpression -> ^(DOT $postfixedExpression innerNewExpression)
  973. )
  974. | LBRACK expression RBRACK -> ^(ARRAY_ELEMENT_ACCESS $postfixedExpression expression)
  975. )*
  976. // At the end there may follow a post increment/decrement.
  977. ( INC -> ^(POST_INC[$INC, "POST_INC"] $postfixedExpression)
  978. | DEC -> ^(POST_DEC[$DEC, "POST_DEC"] $postfixedExpression)
  979. )?
  980. ;
  981.  
  982. primaryExpression
  983. : parenthesizedExpression
  984. | literal
  985. | newExpression
  986. | qualifiedIdentExpression
  987. | genericTypeArgumentListSimplified
  988. ( SUPER
  989. ( arguments -> ^(SUPER_CONSTRUCTOR_CALL[$SUPER, "SUPER_CONSTRUCTOR_CALL"] genericTypeArgumentListSimplified arguments)
  990. | DOT IDENT arguments -> ^(METHOD_CALL ^(DOT SUPER IDENT) genericTypeArgumentListSimplified arguments)
  991. )
  992. | IDENT arguments -> ^(METHOD_CALL IDENT genericTypeArgumentListSimplified arguments)
  993. | THIS arguments -> ^(THIS_CONSTRUCTOR_CALL[$THIS, "THIS_CONSTRUCTOR_CALL"] genericTypeArgumentListSimplified arguments)
  994. )
  995. | ( THIS -> THIS
  996. )
  997. ( arguments -> ^(THIS_CONSTRUCTOR_CALL[$THIS, "THIS_CONSTRUCTOR_CALL"] arguments)
  998. )?
  999. | SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$SUPER, "SUPER_CONSTRUCTOR_CALL"] arguments)
  1000. | ( SUPER DOT IDENT
  1001. )
  1002. ( arguments -> ^(METHOD_CALL ^(DOT SUPER IDENT) arguments)
  1003. | -> ^(DOT SUPER IDENT)
  1004. )
  1005. | ( primitiveType -> primitiveType
  1006. )
  1007. ( arrayDeclarator -> ^(arrayDeclarator $primaryExpression)
  1008. )*
  1009. DOT CLASS -> ^(DOT $primaryExpression CLASS)
  1010. | VOID DOT CLASS -> ^(DOT VOID CLASS)
  1011. ;
  1012.  
  1013. qualifiedIdentExpression
  1014. // The qualified identifier itself is the starting point for this rule.
  1015. : ( qualifiedIdentifier -> qualifiedIdentifier
  1016. )
  1017. // And now comes the stuff that may follow the qualified identifier.
  1018. ( ( arrayDeclarator -> ^(arrayDeclarator $qualifiedIdentExpression)
  1019. )+
  1020. ( DOT CLASS -> ^(DOT $qualifiedIdentExpression CLASS)
  1021. )
  1022. | arguments -> ^(METHOD_CALL qualifiedIdentifier arguments)
  1023. | outerDot=DOT
  1024. ( CLASS -> ^(DOT qualifiedIdentifier CLASS)
  1025. | genericTypeArgumentListSimplified
  1026. ( Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] qualifiedIdentifier genericTypeArgumentListSimplified arguments)
  1027. | SUPER innerDot=DOT IDENT arguments -> ^(METHOD_CALL ^($innerDot ^($outerDot qualifiedIdentifier SUPER) IDENT) genericTypeArgumentListSimplified arguments)
  1028. | IDENT arguments -> ^(METHOD_CALL ^(DOT qualifiedIdentifier IDENT) genericTypeArgumentListSimplified arguments)
  1029. )
  1030. | THIS -> ^(DOT qualifiedIdentifier THIS)
  1031. | Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] qualifiedIdentifier arguments)
  1032. | innerNewExpression -> ^(DOT qualifiedIdentifier innerNewExpression)
  1033. )
  1034. )?
  1035. ;
  1036.  
  1037. newExpression
  1038. : NEW
  1039. ( primitiveType newArrayConstruction // new static array of primitive type elements
  1040. -> ^(STATIC_ARRAY_CREATOR[$NEW, "STATIC_ARRAY_CREATOR"] primitiveType newArrayConstruction)
  1041. | genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified
  1042. ( newArrayConstruction // new static array of object type reference elements
  1043. -> ^(STATIC_ARRAY_CREATOR[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified newArrayConstruction)
  1044. | arguments classBody? // new object type via constructor invocation
  1045. -> ^(CLASS_CONSTRUCTOR_CALL[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified arguments classBody?)
  1046. )
  1047. )
  1048. ;
  1049.  
  1050. innerNewExpression // something like 'InnerType innerType = outer.new InnerType();'
  1051. : NEW genericTypeArgumentListSimplified? IDENT arguments classBody?
  1052. -> ^(CLASS_CONSTRUCTOR_CALL[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? IDENT arguments classBody?)
  1053. ;
  1054.  
  1055. newArrayConstruction
  1056. : arrayDeclaratorList arrayInitializer
  1057. | LBRACK! expression RBRACK! (LBRACK! expression RBRACK!)* arrayDeclaratorList?
  1058. ;
  1059.  
  1060. arguments
  1061. : LPAREN expressionList? RPAREN
  1062. -> ^(ARGUMENT_LIST[$LPAREN, "ARGUMENT_LIST"] expressionList?)
  1063. ;
  1064.  
  1065. literal
  1066. : HEX_LITERAL
  1067. | OCTAL_LITERAL
  1068. | DECIMAL_LITERAL
  1069. | FLOATING_POINT_LITERAL
  1070. | CHARACTER_LITERAL
  1071. | STRING_LITERAL
  1072. | TRUE
  1073. | FALSE
  1074. | NULL
  1075. ;
  1076.  
  1077. // LEXER
  1078.  
  1079. HEX_LITERAL : '0' ('x'|'X') HEX_DIGIT+ INTEGER_TYPE_SUFFIX? ;
  1080.  
  1081. DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) INTEGER_TYPE_SUFFIX? ;
  1082.  
  1083. OCTAL_LITERAL : '0' ('0'..'7')+ INTEGER_TYPE_SUFFIX? ;
  1084.  
  1085. fragment
  1086. HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
  1087.  
  1088. fragment
  1089. INTEGER_TYPE_SUFFIX : ('l'|'L') ;
  1090.  
  1091. FLOATING_POINT_LITERAL
  1092. : ('0'..'9')+
  1093. (
  1094. DOT ('0'..'9')* EXPONENT? FLOAT_TYPE_SUFFIX?
  1095. | EXPONENT FLOAT_TYPE_SUFFIX?
  1096. | FLOAT_TYPE_SUFFIX
  1097. )
  1098. | DOT ('0'..'9')+ EXPONENT? FLOAT_TYPE_SUFFIX?
  1099. ;
  1100.  
  1101. fragment
  1102. EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
  1103.  
  1104. fragment
  1105. FLOAT_TYPE_SUFFIX : ('f'|'F'|'d'|'D') ;
  1106.  
  1107. CHARACTER_LITERAL
  1108. : '\'' ( ESCAPE_SEQUENCE | ~('\''|'\\') ) '\''
  1109. ;
  1110.  
  1111. STRING_LITERAL
  1112. : '"' ( ESCAPE_SEQUENCE | ~('\\'|'"') )* '"'
  1113. ;
  1114.  
  1115. fragment
  1116. ESCAPE_SEQUENCE
  1117. : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
  1118. | UNICODE_ESCAPE
  1119. | OCTAL_ESCAPE
  1120. ;
  1121.  
  1122. fragment
  1123. OCTAL_ESCAPE
  1124. : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
  1125. | '\\' ('0'..'7') ('0'..'7')
  1126. | '\\' ('0'..'7')
  1127. ;
  1128.  
  1129. fragment
  1130. UNICODE_ESCAPE
  1131. : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
  1132. ;
  1133.  
  1134. IDENT
  1135. : JAVA_ID_START (JAVA_ID_PART)*
  1136. ;
  1137.  
  1138. fragment
  1139. JAVA_ID_START
  1140. : '\u0024'
  1141. | '\u0041'..'\u005a'
  1142. | '\u005f'
  1143. | '\u0061'..'\u007a'
  1144. | '\u00c0'..'\u00d6'
  1145. | '\u00d8'..'\u00f6'
  1146. | '\u00f8'..'\u00ff'
  1147. | '\u0100'..'\u1fff'
  1148. | '\u3040'..'\u318f'
  1149. | '\u3300'..'\u337f'
  1150. | '\u3400'..'\u3d2d'
  1151. | '\u4e00'..'\u9fff'
  1152. | '\uf900'..'\ufaff'
  1153. ;
  1154.  
  1155. fragment
  1156. JAVA_ID_PART
  1157. : JAVA_ID_START
  1158. | '\u0030'..'\u0039'
  1159. ;
  1160.  
  1161. WS : (' '|'\r'|'\t'|'\u000C'|'\n')
  1162. {
  1163. if (!preserveWhitespacesAndComments) {
  1164. skip();
  1165. } else {
  1166. $channel = HIDDEN;
  1167. }
  1168. }
  1169. ;
  1170.  
  1171. COMMENT
  1172. : '/*' ( options {greedy=false;} : . )* '*/'
  1173. {
  1174. if (!preserveWhitespacesAndComments) {
  1175. skip();
  1176. } else {
  1177. $channel = HIDDEN;
  1178. }
  1179. }
  1180. ;
  1181.  
  1182. LINE_COMMENT
  1183. : '//' ~('\n'|'\r')* '\r'? '\n'
  1184. {
  1185. if (!preserveWhitespacesAndComments) {
  1186. skip();
  1187. } else {
  1188. $channel = HIDDEN;
  1189. }
  1190. }
  1191. ;
  1192.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty