Java9.g4 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * [The "BSD license"]
  3. * Copyright (c) 2014 Terence Parr
  4. * Copyright (c) 2014 Sam Harwell
  5. * Copyright (c) 2017 Chan Chung Kwong
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * A Java 9 grammar for ANTLR 4 derived from the Java Language Specification
  33. * chapter 19.
  34. *
  35. * NOTE: This grammar results in a generated parser that is much slower
  36. * than the Java 7 grammar in the grammars-v4/java directory. This
  37. * one is, however, extremely close to the spec.
  38. *
  39. * You can test with
  40. *
  41. * $ antlr4 Java9.g4
  42. * $ javac *.java
  43. * $ grun Java9 compilationUnit *.java
  44. *
  45. * Or,
  46. ~/antlr/code/grammars-v4/java9 $ java Test .
  47. /Users/parrt/antlr/code/grammars-v4/java9/./Java9BaseListener.java
  48. /Users/parrt/antlr/code/grammars-v4/java9/./Java9Lexer.java
  49. /Users/parrt/antlr/code/grammars-v4/java9/./Java9Listener.java
  50. /Users/parrt/antlr/code/grammars-v4/java9/./Java9Parser.java
  51. /Users/parrt/antlr/code/grammars-v4/java9/./Test.java
  52. Total lexer+parser time 30844ms.
  53. ~/antlr/code/grammars-v4/java9 $ java Test examples/module-info.java
  54. /home/kwong/projects/grammars-v4/java9/examples/module-info.java
  55. Total lexer+parser time 914ms.
  56. ~/antlr/code/grammars-v4/java9 $ java Test examples/TryWithResourceDemo.java
  57. /home/kwong/projects/grammars-v4/java9/examples/TryWithResourceDemo.java
  58. Total lexer+parser time 3634ms.
  59. ~/antlr/code/grammars-v4/java9 $ java Test examples/helloworld.java
  60. /home/kwong/projects/grammars-v4/java9/examples/helloworld.java
  61. Total lexer+parser time 2497ms.
  62. */
  63. grammar Java9;
  64. /*
  65. * Productions from §3 (Lexical Structure)
  66. */
  67. literal
  68. : IntegerLiteral
  69. | FloatingPointLiteral
  70. | BooleanLiteral
  71. | CharacterLiteral
  72. | StringLiteral
  73. | NullLiteral
  74. ;
  75. /*
  76. * Productions from §4 (Types, Values, and Variables)
  77. */
  78. primitiveType
  79. : annotation* numericType
  80. | annotation* 'boolean'
  81. ;
  82. numericType
  83. : integralType
  84. | floatingPointType
  85. ;
  86. integralType
  87. : 'byte'
  88. | 'short'
  89. | 'int'
  90. | 'long'
  91. | 'char'
  92. ;
  93. floatingPointType
  94. : 'float'
  95. | 'double'
  96. ;
  97. referenceType
  98. : classOrInterfaceType
  99. | typeVariable
  100. | arrayType
  101. ;
  102. /*classOrInterfaceType
  103. : classType
  104. | interfaceType
  105. ;
  106. */
  107. classOrInterfaceType
  108. : ( classType_lfno_classOrInterfaceType
  109. | interfaceType_lfno_classOrInterfaceType
  110. )
  111. ( classType_lf_classOrInterfaceType
  112. | interfaceType_lf_classOrInterfaceType
  113. )*
  114. ;
  115. classType
  116. : annotation* identifier typeArguments?
  117. | classOrInterfaceType '.' annotation* identifier typeArguments?
  118. ;
  119. classType_lf_classOrInterfaceType
  120. : '.' annotation* identifier typeArguments?
  121. ;
  122. classType_lfno_classOrInterfaceType
  123. : annotation* identifier typeArguments?
  124. ;
  125. interfaceType
  126. : classType
  127. ;
  128. interfaceType_lf_classOrInterfaceType
  129. : classType_lf_classOrInterfaceType
  130. ;
  131. interfaceType_lfno_classOrInterfaceType
  132. : classType_lfno_classOrInterfaceType
  133. ;
  134. typeVariable
  135. : annotation* identifier
  136. ;
  137. arrayType
  138. : primitiveType dims
  139. | classOrInterfaceType dims
  140. | typeVariable dims
  141. ;
  142. dims
  143. : annotation* '[' ']' (annotation* '[' ']')*
  144. ;
  145. typeParameter
  146. : typeParameterModifier* identifier typeBound?
  147. ;
  148. typeParameterModifier
  149. : annotation
  150. ;
  151. typeBound
  152. : 'extends' typeVariable
  153. | 'extends' classOrInterfaceType additionalBound*
  154. ;
  155. additionalBound
  156. : '&' interfaceType
  157. ;
  158. typeArguments
  159. : '<' typeArgumentList '>'
  160. ;
  161. typeArgumentList
  162. : typeArgument (',' typeArgument)*
  163. ;
  164. typeArgument
  165. : referenceType
  166. | wildcard
  167. ;
  168. wildcard
  169. : annotation* '?' wildcardBounds?
  170. ;
  171. wildcardBounds
  172. : 'extends' referenceType
  173. | 'super' referenceType
  174. ;
  175. /*
  176. * Productions from §6 (Names)
  177. */
  178. moduleName
  179. : identifier
  180. | moduleName '.' identifier
  181. ;
  182. packageName
  183. : identifier
  184. | packageName '.' identifier
  185. ;
  186. typeName
  187. : identifier
  188. | packageOrTypeName '.' identifier
  189. ;
  190. packageOrTypeName
  191. : identifier
  192. | packageOrTypeName '.' identifier
  193. ;
  194. expressionName
  195. : identifier
  196. | ambiguousName '.' identifier
  197. ;
  198. methodName
  199. : identifier
  200. ;
  201. ambiguousName
  202. : identifier
  203. | ambiguousName '.' identifier
  204. ;
  205. /*
  206. * Productions from §7 (Packages)
  207. */
  208. compilationUnit
  209. : ordinaryCompilation
  210. | modularCompilation
  211. ;
  212. ordinaryCompilation
  213. : packageDeclaration? importDeclaration* typeDeclaration* EOF
  214. ;
  215. modularCompilation
  216. : importDeclaration* moduleDeclaration
  217. ;
  218. packageDeclaration
  219. : packageModifier* 'package' packageName ';'
  220. ;
  221. packageModifier
  222. : annotation
  223. ;
  224. importDeclaration
  225. : singleTypeImportDeclaration
  226. | typeImportOnDemandDeclaration
  227. | singleStaticImportDeclaration
  228. | staticImportOnDemandDeclaration
  229. ;
  230. singleTypeImportDeclaration
  231. : 'import' typeName ';'
  232. ;
  233. typeImportOnDemandDeclaration
  234. : 'import' packageOrTypeName '.' '*' ';'
  235. ;
  236. singleStaticImportDeclaration
  237. : 'import' 'static' typeName '.' identifier ';'
  238. ;
  239. staticImportOnDemandDeclaration
  240. : 'import' 'static' typeName '.' '*' ';'
  241. ;
  242. typeDeclaration
  243. : classDeclaration
  244. | interfaceDeclaration
  245. | ';'
  246. ;
  247. moduleDeclaration
  248. : annotation* 'open'? 'module' moduleName '{' moduleDirective* '}'
  249. ;
  250. moduleDirective
  251. : 'requires' requiresModifier* moduleName ';'
  252. | 'exports' packageName ('to' moduleName (',' moduleName)*)? ';'
  253. | 'opens' packageName ('to' moduleName (',' moduleName)*)? ';'
  254. | 'uses' typeName ';'
  255. | 'provides' typeName 'with' typeName (',' typeName)* ';'
  256. ;
  257. requiresModifier
  258. : 'transitive'
  259. | 'static'
  260. ;
  261. /*
  262. * Productions from §8 (Classes)
  263. */
  264. classDeclaration
  265. : normalClassDeclaration
  266. | enumDeclaration
  267. ;
  268. normalClassDeclaration
  269. : classModifier* 'class' identifier typeParameters? superclass? superinterfaces? classBody
  270. ;
  271. classModifier
  272. : annotation
  273. | 'public'
  274. | 'protected'
  275. | 'private'
  276. | 'abstract'
  277. | 'static'
  278. | 'final'
  279. | 'strictfp'
  280. ;
  281. typeParameters
  282. : '<' typeParameterList '>'
  283. ;
  284. typeParameterList
  285. : typeParameter (',' typeParameter)*
  286. ;
  287. superclass
  288. : 'extends' classType
  289. ;
  290. superinterfaces
  291. : 'implements' interfaceTypeList
  292. ;
  293. interfaceTypeList
  294. : interfaceType (',' interfaceType)*
  295. ;
  296. classBody
  297. : '{' classBodyDeclaration* '}'
  298. ;
  299. classBodyDeclaration
  300. : classMemberDeclaration
  301. | instanceInitializer
  302. | staticInitializer
  303. | constructorDeclaration
  304. ;
  305. classMemberDeclaration
  306. : fieldDeclaration
  307. | methodDeclaration
  308. | classDeclaration
  309. | interfaceDeclaration
  310. | ';'
  311. ;
  312. fieldDeclaration
  313. : fieldModifier* unannType variableDeclaratorList ';'
  314. ;
  315. fieldModifier
  316. : annotation
  317. | 'public'
  318. | 'protected'
  319. | 'private'
  320. | 'static'
  321. | 'final'
  322. | 'transient'
  323. | 'volatile'
  324. ;
  325. variableDeclaratorList
  326. : variableDeclarator (',' variableDeclarator)*
  327. ;
  328. variableDeclarator
  329. : variableDeclaratorId ('=' variableInitializer)?
  330. ;
  331. variableDeclaratorId
  332. : identifier dims?
  333. ;
  334. variableInitializer
  335. : expression
  336. | arrayInitializer
  337. ;
  338. unannType
  339. : unannPrimitiveType
  340. | unannReferenceType
  341. ;
  342. unannPrimitiveType
  343. : numericType
  344. | 'boolean'
  345. ;
  346. unannReferenceType
  347. : unannClassOrInterfaceType
  348. | unannTypeVariable
  349. | unannArrayType
  350. ;
  351. /*unannClassOrInterfaceType
  352. : unannClassType
  353. | unannInterfaceType
  354. ;
  355. */
  356. unannClassOrInterfaceType
  357. : ( unannClassType_lfno_unannClassOrInterfaceType
  358. | unannInterfaceType_lfno_unannClassOrInterfaceType
  359. )
  360. ( unannClassType_lf_unannClassOrInterfaceType
  361. | unannInterfaceType_lf_unannClassOrInterfaceType
  362. )*
  363. ;
  364. unannClassType
  365. : identifier typeArguments?
  366. | unannClassOrInterfaceType '.' annotation* identifier typeArguments?
  367. ;
  368. unannClassType_lf_unannClassOrInterfaceType
  369. : '.' annotation* identifier typeArguments?
  370. ;
  371. unannClassType_lfno_unannClassOrInterfaceType
  372. : identifier typeArguments?
  373. ;
  374. unannInterfaceType
  375. : unannClassType
  376. ;
  377. unannInterfaceType_lf_unannClassOrInterfaceType
  378. : unannClassType_lf_unannClassOrInterfaceType
  379. ;
  380. unannInterfaceType_lfno_unannClassOrInterfaceType
  381. : unannClassType_lfno_unannClassOrInterfaceType
  382. ;
  383. unannTypeVariable
  384. : identifier
  385. ;
  386. unannArrayType
  387. : unannPrimitiveType dims
  388. | unannClassOrInterfaceType dims
  389. | unannTypeVariable dims
  390. ;
  391. methodDeclaration
  392. : methodModifier* methodHeader methodBody
  393. ;
  394. methodModifier
  395. : annotation
  396. | 'public'
  397. | 'protected'
  398. | 'private'
  399. | 'abstract'
  400. | 'static'
  401. | 'final'
  402. | 'synchronized'
  403. | 'native'
  404. | 'strictfp'
  405. ;
  406. methodHeader
  407. : result methodDeclarator throws_?
  408. | typeParameters annotation* result methodDeclarator throws_?
  409. ;
  410. result
  411. : unannType
  412. | 'void'
  413. ;
  414. methodDeclarator
  415. : identifier '(' formalParameterList? ')' dims?
  416. ;
  417. formalParameterList
  418. : formalParameters ',' lastFormalParameter
  419. | lastFormalParameter
  420. | receiverParameter
  421. ;
  422. formalParameters
  423. : formalParameter (',' formalParameter)*
  424. | receiverParameter (',' formalParameter)*
  425. ;
  426. formalParameter
  427. : variableModifier* unannType variableDeclaratorId
  428. ;
  429. variableModifier
  430. : annotation
  431. | 'final'
  432. ;
  433. lastFormalParameter
  434. : variableModifier* unannType annotation* '...' variableDeclaratorId
  435. | formalParameter
  436. ;
  437. receiverParameter
  438. : annotation* unannType (identifier '.')? 'this'
  439. ;
  440. throws_
  441. : 'throws' exceptionTypeList
  442. ;
  443. exceptionTypeList
  444. : exceptionType (',' exceptionType)*
  445. ;
  446. exceptionType
  447. : classType
  448. | typeVariable
  449. ;
  450. methodBody
  451. : block
  452. | ';'
  453. ;
  454. instanceInitializer
  455. : block
  456. ;
  457. staticInitializer
  458. : 'static' block
  459. ;
  460. constructorDeclaration
  461. : constructorModifier* constructorDeclarator throws_? constructorBody
  462. ;
  463. constructorModifier
  464. : annotation
  465. | 'public'
  466. | 'protected'
  467. | 'private'
  468. ;
  469. constructorDeclarator
  470. : typeParameters? simpleTypeName '(' formalParameterList? ')'
  471. ;
  472. simpleTypeName
  473. : identifier
  474. ;
  475. constructorBody
  476. : '{' explicitConstructorInvocation? blockStatements? '}'
  477. ;
  478. explicitConstructorInvocation
  479. : typeArguments? 'this' '(' argumentList? ')' ';'
  480. | typeArguments? 'super' '(' argumentList? ')' ';'
  481. | expressionName '.' typeArguments? 'super' '(' argumentList? ')' ';'
  482. | primary '.' typeArguments? 'super' '(' argumentList? ')' ';'
  483. ;
  484. enumDeclaration
  485. : classModifier* 'enum' identifier superinterfaces? enumBody
  486. ;
  487. enumBody
  488. : '{' enumConstantList? ','? enumBodyDeclarations? '}'
  489. ;
  490. enumConstantList
  491. : enumConstant (',' enumConstant)*
  492. ;
  493. enumConstant
  494. : enumConstantModifier* identifier ('(' argumentList? ')')? classBody?
  495. ;
  496. enumConstantModifier
  497. : annotation
  498. ;
  499. enumBodyDeclarations
  500. : ';' classBodyDeclaration*
  501. ;
  502. /*
  503. * Productions from §9 (Interfaces)
  504. */
  505. interfaceDeclaration
  506. : normalInterfaceDeclaration
  507. | annotationTypeDeclaration
  508. ;
  509. normalInterfaceDeclaration
  510. : interfaceModifier* 'interface' identifier typeParameters? extendsInterfaces? interfaceBody
  511. ;
  512. interfaceModifier
  513. : annotation
  514. | 'public'
  515. | 'protected'
  516. | 'private'
  517. | 'abstract'
  518. | 'static'
  519. | 'strictfp'
  520. ;
  521. extendsInterfaces
  522. : 'extends' interfaceTypeList
  523. ;
  524. interfaceBody
  525. : '{' interfaceMemberDeclaration* '}'
  526. ;
  527. interfaceMemberDeclaration
  528. : constantDeclaration
  529. | interfaceMethodDeclaration
  530. | classDeclaration
  531. | interfaceDeclaration
  532. | ';'
  533. ;
  534. constantDeclaration
  535. : constantModifier* unannType variableDeclaratorList ';'
  536. ;
  537. constantModifier
  538. : annotation
  539. | 'public'
  540. | 'static'
  541. | 'final'
  542. ;
  543. interfaceMethodDeclaration
  544. : interfaceMethodModifier* methodHeader methodBody
  545. ;
  546. interfaceMethodModifier
  547. : annotation
  548. | 'public'
  549. | 'private'//Introduced in Java 9
  550. | 'abstract'
  551. | 'default'
  552. | 'static'
  553. | 'strictfp'
  554. ;
  555. annotationTypeDeclaration
  556. : interfaceModifier* '@' 'interface' identifier annotationTypeBody
  557. ;
  558. annotationTypeBody
  559. : '{' annotationTypeMemberDeclaration* '}'
  560. ;
  561. annotationTypeMemberDeclaration
  562. : annotationTypeElementDeclaration
  563. | constantDeclaration
  564. | classDeclaration
  565. | interfaceDeclaration
  566. | ';'
  567. ;
  568. annotationTypeElementDeclaration
  569. : annotationTypeElementModifier* unannType identifier '(' ')' dims? defaultValue? ';'
  570. ;
  571. annotationTypeElementModifier
  572. : annotation
  573. | 'public'
  574. | 'abstract'
  575. ;
  576. defaultValue
  577. : 'default' elementValue
  578. ;
  579. annotation
  580. : normalAnnotation
  581. | markerAnnotation
  582. | singleElementAnnotation
  583. ;
  584. normalAnnotation
  585. : '@' typeName '(' elementValuePairList? ')'
  586. ;
  587. elementValuePairList
  588. : elementValuePair (',' elementValuePair)*
  589. ;
  590. elementValuePair
  591. : identifier '=' elementValue
  592. ;
  593. elementValue
  594. : conditionalExpression
  595. | elementValueArrayInitializer
  596. | annotation
  597. ;
  598. elementValueArrayInitializer
  599. : '{' elementValueList? ','? '}'
  600. ;
  601. elementValueList
  602. : elementValue (',' elementValue)*
  603. ;
  604. markerAnnotation
  605. : '@' typeName
  606. ;
  607. singleElementAnnotation
  608. : '@' typeName '(' elementValue ')'
  609. ;
  610. /*
  611. * Productions from §10 (Arrays)
  612. */
  613. arrayInitializer
  614. : '{' variableInitializerList? ','? '}'
  615. ;
  616. variableInitializerList
  617. : variableInitializer (',' variableInitializer)*
  618. ;
  619. /*
  620. * Productions from §14 (Blocks and Statements)
  621. */
  622. block
  623. : '{' blockStatements? '}'
  624. ;
  625. blockStatements
  626. : blockStatement+
  627. ;
  628. blockStatement
  629. : localVariableDeclarationStatement
  630. | classDeclaration
  631. | statement
  632. ;
  633. localVariableDeclarationStatement
  634. : localVariableDeclaration ';'
  635. ;
  636. localVariableDeclaration
  637. : variableModifier* unannType variableDeclaratorList
  638. ;
  639. statement
  640. : statementWithoutTrailingSubstatement
  641. | labeledStatement
  642. | ifThenStatement
  643. | ifThenElseStatement
  644. | whileStatement
  645. | forStatement
  646. ;
  647. statementNoShortIf
  648. : statementWithoutTrailingSubstatement
  649. | labeledStatementNoShortIf
  650. | ifThenElseStatementNoShortIf
  651. | whileStatementNoShortIf
  652. | forStatementNoShortIf
  653. ;
  654. statementWithoutTrailingSubstatement
  655. : block
  656. | emptyStatement
  657. | expressionStatement
  658. | assertStatement
  659. | switchStatement
  660. | doStatement
  661. | breakStatement
  662. | continueStatement
  663. | returnStatement
  664. | synchronizedStatement
  665. | throwStatement
  666. | tryStatement
  667. ;
  668. emptyStatement
  669. : ';'
  670. ;
  671. labeledStatement
  672. : identifier ':' statement
  673. ;
  674. labeledStatementNoShortIf
  675. : identifier ':' statementNoShortIf
  676. ;
  677. expressionStatement
  678. : statementExpression ';'
  679. ;
  680. statementExpression
  681. : assignment
  682. | preIncrementExpression
  683. | preDecrementExpression
  684. | postIncrementExpression
  685. | postDecrementExpression
  686. | methodInvocation
  687. | classInstanceCreationExpression
  688. ;
  689. ifThenStatement
  690. : 'if' '(' expression ')' statement
  691. ;
  692. ifThenElseStatement
  693. : 'if' '(' expression ')' statementNoShortIf 'else' statement
  694. ;
  695. ifThenElseStatementNoShortIf
  696. : 'if' '(' expression ')' statementNoShortIf 'else' statementNoShortIf
  697. ;
  698. assertStatement
  699. : 'assert' expression ';'
  700. | 'assert' expression ':' expression ';'
  701. ;
  702. switchStatement
  703. : 'switch' '(' expression ')' switchBlock
  704. ;
  705. switchBlock
  706. : '{' switchBlockStatementGroup* switchLabel* '}'
  707. ;
  708. switchBlockStatementGroup
  709. : switchLabels blockStatements
  710. ;
  711. switchLabels
  712. : switchLabel+
  713. ;
  714. switchLabel
  715. : 'case' constantExpression ':'
  716. | 'case' enumConstantName ':'
  717. | 'default' ':'
  718. ;
  719. enumConstantName
  720. : identifier
  721. ;
  722. whileStatement
  723. : 'while' '(' expression ')' statement
  724. ;
  725. whileStatementNoShortIf
  726. : 'while' '(' expression ')' statementNoShortIf
  727. ;
  728. doStatement
  729. : 'do' statement 'while' '(' expression ')' ';'
  730. ;
  731. forStatement
  732. : basicForStatement
  733. | enhancedForStatement
  734. ;
  735. forStatementNoShortIf
  736. : basicForStatementNoShortIf
  737. | enhancedForStatementNoShortIf
  738. ;
  739. basicForStatement
  740. : 'for' '(' forInit? ';' expression? ';' forUpdate? ')' statement
  741. ;
  742. basicForStatementNoShortIf
  743. : 'for' '(' forInit? ';' expression? ';' forUpdate? ')' statementNoShortIf
  744. ;
  745. forInit
  746. : statementExpressionList
  747. | localVariableDeclaration
  748. ;
  749. forUpdate
  750. : statementExpressionList
  751. ;
  752. statementExpressionList
  753. : statementExpression (',' statementExpression)*
  754. ;
  755. enhancedForStatement
  756. : 'for' '(' variableModifier* unannType variableDeclaratorId ':' expression ')' statement
  757. ;
  758. enhancedForStatementNoShortIf
  759. : 'for' '(' variableModifier* unannType variableDeclaratorId ':' expression ')' statementNoShortIf
  760. ;
  761. breakStatement
  762. : 'break' identifier? ';'
  763. ;
  764. continueStatement
  765. : 'continue' identifier? ';'
  766. ;
  767. returnStatement
  768. : 'return' expression? ';'
  769. ;
  770. throwStatement
  771. : 'throw' expression ';'
  772. ;
  773. synchronizedStatement
  774. : 'synchronized' '(' expression ')' block
  775. ;
  776. tryStatement
  777. : 'try' block catches
  778. | 'try' block catches? finally_
  779. | tryWithResourcesStatement
  780. ;
  781. catches
  782. : catchClause+
  783. ;
  784. catchClause
  785. : 'catch' '(' catchFormalParameter ')' block
  786. ;
  787. catchFormalParameter
  788. : variableModifier* catchType variableDeclaratorId
  789. ;
  790. catchType
  791. : unannClassType ('|' classType)*
  792. ;
  793. finally_
  794. : 'finally' block
  795. ;
  796. tryWithResourcesStatement
  797. : 'try' resourceSpecification block catches? finally_?
  798. ;
  799. resourceSpecification
  800. : '(' resourceList ';'? ')'
  801. ;
  802. resourceList
  803. : resource (';' resource)*
  804. ;
  805. resource
  806. : variableModifier* unannType variableDeclaratorId '=' expression
  807. | variableAccess//Introduced in Java 9
  808. ;
  809. variableAccess
  810. : expressionName
  811. | fieldAccess
  812. ;
  813. /*
  814. * Productions from §15 (Expressions)
  815. */
  816. /*primary
  817. : primaryNoNewArray
  818. | arrayCreationExpression
  819. ;
  820. */
  821. primary
  822. : ( primaryNoNewArray_lfno_primary
  823. | arrayCreationExpression
  824. )
  825. ( primaryNoNewArray_lf_primary
  826. )*
  827. ;
  828. primaryNoNewArray
  829. : literal
  830. | classLiteral
  831. | 'this'
  832. | typeName '.' 'this'
  833. | '(' expression ')'
  834. | classInstanceCreationExpression
  835. | fieldAccess
  836. | arrayAccess
  837. | methodInvocation
  838. | methodReference
  839. ;
  840. primaryNoNewArray_lf_arrayAccess
  841. :
  842. ;
  843. primaryNoNewArray_lfno_arrayAccess
  844. : literal
  845. | typeName ('[' ']')* '.' 'class'
  846. | 'void' '.' 'class'
  847. | 'this'
  848. | typeName '.' 'this'
  849. | '(' expression ')'
  850. | classInstanceCreationExpression
  851. | fieldAccess
  852. | methodInvocation
  853. | methodReference
  854. ;
  855. primaryNoNewArray_lf_primary
  856. : classInstanceCreationExpression_lf_primary
  857. | fieldAccess_lf_primary
  858. | arrayAccess_lf_primary
  859. | methodInvocation_lf_primary
  860. | methodReference_lf_primary
  861. ;
  862. primaryNoNewArray_lf_primary_lf_arrayAccess_lf_primary
  863. :
  864. ;
  865. primaryNoNewArray_lf_primary_lfno_arrayAccess_lf_primary
  866. : classInstanceCreationExpression_lf_primary
  867. | fieldAccess_lf_primary
  868. | methodInvocation_lf_primary
  869. | methodReference_lf_primary
  870. ;
  871. primaryNoNewArray_lfno_primary
  872. : literal
  873. | typeName ('[' ']')* '.' 'class'
  874. | unannPrimitiveType ('[' ']')* '.' 'class'
  875. | 'void' '.' 'class'
  876. | 'this'
  877. | typeName '.' 'this'
  878. | '(' expression ')'
  879. | classInstanceCreationExpression_lfno_primary
  880. | fieldAccess_lfno_primary
  881. | arrayAccess_lfno_primary
  882. | methodInvocation_lfno_primary
  883. | methodReference_lfno_primary
  884. ;
  885. primaryNoNewArray_lfno_primary_lf_arrayAccess_lfno_primary
  886. :
  887. ;
  888. primaryNoNewArray_lfno_primary_lfno_arrayAccess_lfno_primary
  889. : literal
  890. | typeName ('[' ']')* '.' 'class'
  891. | unannPrimitiveType ('[' ']')* '.' 'class'
  892. | 'void' '.' 'class'
  893. | 'this'
  894. | typeName '.' 'this'
  895. | '(' expression ')'
  896. | classInstanceCreationExpression_lfno_primary
  897. | fieldAccess_lfno_primary
  898. | methodInvocation_lfno_primary
  899. | methodReference_lfno_primary
  900. ;
  901. classLiteral
  902. : (typeName|numericType|'boolean') ('[' ']')* '.' 'class'
  903. | 'void' '.' 'class'
  904. ;
  905. classInstanceCreationExpression
  906. : 'new' typeArguments? annotation* identifier ('.' annotation* identifier)* typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  907. | expressionName '.' 'new' typeArguments? annotation* identifier typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  908. | primary '.' 'new' typeArguments? annotation* identifier typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  909. ;
  910. classInstanceCreationExpression_lf_primary
  911. : '.' 'new' typeArguments? annotation* identifier typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  912. ;
  913. classInstanceCreationExpression_lfno_primary
  914. : 'new' typeArguments? annotation* identifier ('.' annotation* identifier)* typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  915. | expressionName '.' 'new' typeArguments? annotation* identifier typeArgumentsOrDiamond? '(' argumentList? ')' classBody?
  916. ;
  917. typeArgumentsOrDiamond
  918. : typeArguments
  919. | '<' '>'
  920. ;
  921. fieldAccess
  922. : primary '.' identifier
  923. | 'super' '.' identifier
  924. | typeName '.' 'super' '.' identifier
  925. ;
  926. fieldAccess_lf_primary
  927. : '.' identifier
  928. ;
  929. fieldAccess_lfno_primary
  930. : 'super' '.' identifier
  931. | typeName '.' 'super' '.' identifier
  932. ;
  933. /*arrayAccess
  934. : expressionName '[' expression ']'
  935. | primaryNoNewArray '[' expression ']'
  936. ;
  937. */
  938. arrayAccess
  939. : ( expressionName '[' expression ']'
  940. | primaryNoNewArray_lfno_arrayAccess '[' expression ']'
  941. )
  942. ( primaryNoNewArray_lf_arrayAccess '[' expression ']'
  943. )*
  944. ;
  945. arrayAccess_lf_primary
  946. : ( primaryNoNewArray_lf_primary_lfno_arrayAccess_lf_primary '[' expression ']'
  947. )
  948. ( primaryNoNewArray_lf_primary_lf_arrayAccess_lf_primary '[' expression ']'
  949. )*
  950. ;
  951. arrayAccess_lfno_primary
  952. : ( expressionName '[' expression ']'
  953. | primaryNoNewArray_lfno_primary_lfno_arrayAccess_lfno_primary '[' expression ']'
  954. )
  955. ( primaryNoNewArray_lfno_primary_lf_arrayAccess_lfno_primary '[' expression ']'
  956. )*
  957. ;
  958. methodInvocation
  959. : methodName '(' argumentList? ')'
  960. | typeName '.' typeArguments? identifier '(' argumentList? ')'
  961. | expressionName '.' typeArguments? identifier '(' argumentList? ')'
  962. | primary '.' typeArguments? identifier '(' argumentList? ')'
  963. | 'super' '.' typeArguments? identifier '(' argumentList? ')'
  964. | typeName '.' 'super' '.' typeArguments? identifier '(' argumentList? ')'
  965. ;
  966. methodInvocation_lf_primary
  967. : '.' typeArguments? identifier '(' argumentList? ')'
  968. ;
  969. methodInvocation_lfno_primary
  970. : methodName '(' argumentList? ')'
  971. | typeName '.' typeArguments? identifier '(' argumentList? ')'
  972. | expressionName '.' typeArguments? identifier '(' argumentList? ')'
  973. | 'super' '.' typeArguments? identifier '(' argumentList? ')'
  974. | typeName '.' 'super' '.' typeArguments? identifier '(' argumentList? ')'
  975. ;
  976. argumentList
  977. : expression (',' expression)*
  978. ;
  979. methodReference
  980. : expressionName '::' typeArguments? identifier
  981. | referenceType '::' typeArguments? identifier
  982. | primary '::' typeArguments? identifier
  983. | 'super' '::' typeArguments? identifier
  984. | typeName '.' 'super' '::' typeArguments? identifier
  985. | classType '::' typeArguments? 'new'
  986. | arrayType '::' 'new'
  987. ;
  988. methodReference_lf_primary
  989. : '::' typeArguments? identifier
  990. ;
  991. methodReference_lfno_primary
  992. : expressionName '::' typeArguments? identifier
  993. | referenceType '::' typeArguments? identifier
  994. | 'super' '::' typeArguments? identifier
  995. | typeName '.' 'super' '::' typeArguments? identifier
  996. | classType '::' typeArguments? 'new'
  997. | arrayType '::' 'new'
  998. ;
  999. arrayCreationExpression
  1000. : 'new' primitiveType dimExprs dims?
  1001. | 'new' classOrInterfaceType dimExprs dims?
  1002. | 'new' primitiveType dims arrayInitializer
  1003. | 'new' classOrInterfaceType dims arrayInitializer
  1004. ;
  1005. dimExprs
  1006. : dimExpr+
  1007. ;
  1008. dimExpr
  1009. : annotation* '[' expression ']'
  1010. ;
  1011. constantExpression
  1012. : expression
  1013. ;
  1014. expression
  1015. : lambdaExpression
  1016. | assignmentExpression
  1017. ;
  1018. lambdaExpression
  1019. : lambdaParameters '->' lambdaBody
  1020. ;
  1021. lambdaParameters
  1022. : identifier
  1023. | '(' formalParameterList? ')'
  1024. | '(' inferredFormalParameterList ')'
  1025. ;
  1026. inferredFormalParameterList
  1027. : identifier (',' identifier)*
  1028. ;
  1029. lambdaBody
  1030. : expression
  1031. | block
  1032. ;
  1033. assignmentExpression
  1034. : conditionalExpression
  1035. | assignment
  1036. ;
  1037. assignment
  1038. : leftHandSide assignmentOperator expression
  1039. ;
  1040. leftHandSide
  1041. : expressionName
  1042. | fieldAccess
  1043. | arrayAccess
  1044. ;
  1045. assignmentOperator
  1046. : '='
  1047. | '*='
  1048. | '/='
  1049. | '%='
  1050. | '+='
  1051. | '-='
  1052. | '<<='
  1053. | '>>='
  1054. | '>>>='
  1055. | '&='
  1056. | '^='
  1057. | '|='
  1058. ;
  1059. conditionalExpression
  1060. : conditionalOrExpression
  1061. | conditionalOrExpression '?' expression ':' (conditionalExpression|lambdaExpression)
  1062. ;
  1063. conditionalOrExpression
  1064. : conditionalAndExpression
  1065. | conditionalOrExpression '||' conditionalAndExpression
  1066. ;
  1067. conditionalAndExpression
  1068. : inclusiveOrExpression
  1069. | conditionalAndExpression '&&' inclusiveOrExpression
  1070. ;
  1071. inclusiveOrExpression
  1072. : exclusiveOrExpression
  1073. | inclusiveOrExpression '|' exclusiveOrExpression
  1074. ;
  1075. exclusiveOrExpression
  1076. : andExpression
  1077. | exclusiveOrExpression '^' andExpression
  1078. ;
  1079. andExpression
  1080. : equalityExpression
  1081. | andExpression '&' equalityExpression
  1082. ;
  1083. equalityExpression
  1084. : relationalExpression
  1085. | equalityExpression '==' relationalExpression
  1086. | equalityExpression '!=' relationalExpression
  1087. ;
  1088. relationalExpression
  1089. : shiftExpression
  1090. | relationalExpression '<' shiftExpression
  1091. | relationalExpression '>' shiftExpression
  1092. | relationalExpression '<=' shiftExpression
  1093. | relationalExpression '>=' shiftExpression
  1094. | relationalExpression 'instanceof' referenceType
  1095. ;
  1096. shiftExpression
  1097. : additiveExpression
  1098. | shiftExpression '<' '<' additiveExpression
  1099. | shiftExpression '>' '>' additiveExpression
  1100. | shiftExpression '>' '>' '>' additiveExpression
  1101. ;
  1102. additiveExpression
  1103. : multiplicativeExpression
  1104. | additiveExpression '+' multiplicativeExpression
  1105. | additiveExpression '-' multiplicativeExpression
  1106. ;
  1107. multiplicativeExpression
  1108. : unaryExpression
  1109. | multiplicativeExpression '*' unaryExpression
  1110. | multiplicativeExpression '/' unaryExpression
  1111. | multiplicativeExpression '%' unaryExpression
  1112. ;
  1113. unaryExpression
  1114. : preIncrementExpression
  1115. | preDecrementExpression
  1116. | '+' unaryExpression
  1117. | '-' unaryExpression
  1118. | unaryExpressionNotPlusMinus
  1119. ;
  1120. preIncrementExpression
  1121. : '++' unaryExpression
  1122. ;
  1123. preDecrementExpression
  1124. : '--' unaryExpression
  1125. ;
  1126. unaryExpressionNotPlusMinus
  1127. : postfixExpression
  1128. | '~' unaryExpression
  1129. | '!' unaryExpression
  1130. | castExpression
  1131. ;
  1132. /*postfixExpression
  1133. : primary
  1134. | expressionName
  1135. | postIncrementExpression
  1136. | postDecrementExpression
  1137. ;
  1138. */
  1139. postfixExpression
  1140. : ( primary
  1141. | expressionName
  1142. )
  1143. ( postIncrementExpression_lf_postfixExpression
  1144. | postDecrementExpression_lf_postfixExpression
  1145. )*
  1146. ;
  1147. postIncrementExpression
  1148. : postfixExpression '++'
  1149. ;
  1150. postIncrementExpression_lf_postfixExpression
  1151. : '++'
  1152. ;
  1153. postDecrementExpression
  1154. : postfixExpression '--'
  1155. ;
  1156. postDecrementExpression_lf_postfixExpression
  1157. : '--'
  1158. ;
  1159. castExpression
  1160. : '(' primitiveType ')' unaryExpression
  1161. | '(' referenceType additionalBound* ')' unaryExpressionNotPlusMinus
  1162. | '(' referenceType additionalBound* ')' lambdaExpression
  1163. ;
  1164. // LEXER
  1165. identifier : Identifier | 'to' | 'module' | 'open' | 'with' | 'provides' | 'uses' | 'opens' | 'requires' | 'exports';
  1166. // §3.9 Keywords
  1167. ABSTRACT : 'abstract';
  1168. ASSERT : 'assert';
  1169. BOOLEAN : 'boolean';
  1170. BREAK : 'break';
  1171. BYTE : 'byte';
  1172. CASE : 'case';
  1173. CATCH : 'catch';
  1174. CHAR : 'char';
  1175. CLASS : 'class';
  1176. CONST : 'const';
  1177. CONTINUE : 'continue';
  1178. DEFAULT : 'default';
  1179. DO : 'do';
  1180. DOUBLE : 'double';
  1181. ELSE : 'else';
  1182. ENUM : 'enum';
  1183. EXTENDS : 'extends';
  1184. FINAL : 'final';
  1185. FINALLY : 'finally';
  1186. FLOAT : 'float';
  1187. FOR : 'for';
  1188. IF : 'if';
  1189. GOTO : 'goto';
  1190. IMPLEMENTS : 'implements';
  1191. IMPORT : 'import';
  1192. INSTANCEOF : 'instanceof';
  1193. INT : 'int';
  1194. INTERFACE : 'interface';
  1195. LONG : 'long';
  1196. NATIVE : 'native';
  1197. NEW : 'new';
  1198. PACKAGE : 'package';
  1199. PRIVATE : 'private';
  1200. PROTECTED : 'protected';
  1201. PUBLIC : 'public';
  1202. RETURN : 'return';
  1203. SHORT : 'short';
  1204. STATIC : 'static';
  1205. STRICTFP : 'strictfp';
  1206. SUPER : 'super';
  1207. SWITCH : 'switch';
  1208. SYNCHRONIZED : 'synchronized';
  1209. THIS : 'this';
  1210. THROW : 'throw';
  1211. THROWS : 'throws';
  1212. TRANSIENT : 'transient';
  1213. TRY : 'try';
  1214. VOID : 'void';
  1215. VOLATILE : 'volatile';
  1216. WHILE : 'while';
  1217. UNDER_SCORE : '_';//Introduced in Java 9
  1218. // §3.10.1 Integer Literals
  1219. IntegerLiteral
  1220. : DecimalIntegerLiteral
  1221. | HexIntegerLiteral
  1222. | OctalIntegerLiteral
  1223. | BinaryIntegerLiteral
  1224. ;
  1225. fragment
  1226. DecimalIntegerLiteral
  1227. : DecimalNumeral IntegerTypeSuffix?
  1228. ;
  1229. fragment
  1230. HexIntegerLiteral
  1231. : HexNumeral IntegerTypeSuffix?
  1232. ;
  1233. fragment
  1234. OctalIntegerLiteral
  1235. : OctalNumeral IntegerTypeSuffix?
  1236. ;
  1237. fragment
  1238. BinaryIntegerLiteral
  1239. : BinaryNumeral IntegerTypeSuffix?
  1240. ;
  1241. fragment
  1242. IntegerTypeSuffix
  1243. : [lL]
  1244. ;
  1245. fragment
  1246. DecimalNumeral
  1247. : '0'
  1248. | NonZeroDigit (Digits? | Underscores Digits)
  1249. ;
  1250. fragment
  1251. Digits
  1252. : Digit (DigitsAndUnderscores? Digit)?
  1253. ;
  1254. fragment
  1255. Digit
  1256. : '0'
  1257. | NonZeroDigit
  1258. ;
  1259. fragment
  1260. NonZeroDigit
  1261. : [1-9]
  1262. ;
  1263. fragment
  1264. DigitsAndUnderscores
  1265. : DigitOrUnderscore+
  1266. ;
  1267. fragment
  1268. DigitOrUnderscore
  1269. : Digit
  1270. | '_'
  1271. ;
  1272. fragment
  1273. Underscores
  1274. : '_'+
  1275. ;
  1276. fragment
  1277. HexNumeral
  1278. : '0' [xX] HexDigits
  1279. ;
  1280. fragment
  1281. HexDigits
  1282. : HexDigit (HexDigitsAndUnderscores? HexDigit)?
  1283. ;
  1284. fragment
  1285. HexDigit
  1286. : [0-9a-fA-F]
  1287. ;
  1288. fragment
  1289. HexDigitsAndUnderscores
  1290. : HexDigitOrUnderscore+
  1291. ;
  1292. fragment
  1293. HexDigitOrUnderscore
  1294. : HexDigit
  1295. | '_'
  1296. ;
  1297. fragment
  1298. OctalNumeral
  1299. : '0' Underscores? OctalDigits
  1300. ;
  1301. fragment
  1302. OctalDigits
  1303. : OctalDigit (OctalDigitsAndUnderscores? OctalDigit)?
  1304. ;
  1305. fragment
  1306. OctalDigit
  1307. : [0-7]
  1308. ;
  1309. fragment
  1310. OctalDigitsAndUnderscores
  1311. : OctalDigitOrUnderscore+
  1312. ;
  1313. fragment
  1314. OctalDigitOrUnderscore
  1315. : OctalDigit
  1316. | '_'
  1317. ;
  1318. fragment
  1319. BinaryNumeral
  1320. : '0' [bB] BinaryDigits
  1321. ;
  1322. fragment
  1323. BinaryDigits
  1324. : BinaryDigit (BinaryDigitsAndUnderscores? BinaryDigit)?
  1325. ;
  1326. fragment
  1327. BinaryDigit
  1328. : [01]
  1329. ;
  1330. fragment
  1331. BinaryDigitsAndUnderscores
  1332. : BinaryDigitOrUnderscore+
  1333. ;
  1334. fragment
  1335. BinaryDigitOrUnderscore
  1336. : BinaryDigit
  1337. | '_'
  1338. ;
  1339. // §3.10.2 Floating-Point Literals
  1340. FloatingPointLiteral
  1341. : DecimalFloatingPointLiteral
  1342. | HexadecimalFloatingPointLiteral
  1343. ;
  1344. fragment
  1345. DecimalFloatingPointLiteral
  1346. : Digits '.' Digits? ExponentPart? FloatTypeSuffix?
  1347. | '.' Digits ExponentPart? FloatTypeSuffix?
  1348. | Digits ExponentPart FloatTypeSuffix?
  1349. | Digits FloatTypeSuffix
  1350. ;
  1351. fragment
  1352. ExponentPart
  1353. : ExponentIndicator SignedInteger
  1354. ;
  1355. fragment
  1356. ExponentIndicator
  1357. : [eE]
  1358. ;
  1359. fragment
  1360. SignedInteger
  1361. : Sign? Digits
  1362. ;
  1363. fragment
  1364. Sign
  1365. : [+-]
  1366. ;
  1367. fragment
  1368. FloatTypeSuffix
  1369. : [fFdD]
  1370. ;
  1371. fragment
  1372. HexadecimalFloatingPointLiteral
  1373. : HexSignificand BinaryExponent FloatTypeSuffix?
  1374. ;
  1375. fragment
  1376. HexSignificand
  1377. : HexNumeral '.'?
  1378. | '0' [xX] HexDigits? '.' HexDigits
  1379. ;
  1380. fragment
  1381. BinaryExponent
  1382. : BinaryExponentIndicator SignedInteger
  1383. ;
  1384. fragment
  1385. BinaryExponentIndicator
  1386. : [pP]
  1387. ;
  1388. // §3.10.3 Boolean Literals
  1389. BooleanLiteral
  1390. : 'true'
  1391. | 'false'
  1392. ;
  1393. // §3.10.4 Character Literals
  1394. CharacterLiteral
  1395. : '\'' SingleCharacter '\''
  1396. | '\'' EscapeSequence '\''
  1397. ;
  1398. fragment
  1399. SingleCharacter
  1400. : ~['\\\r\n]
  1401. ;
  1402. // §3.10.5 String Literals
  1403. StringLiteral
  1404. : '"' StringCharacters? '"'
  1405. ;
  1406. fragment
  1407. StringCharacters
  1408. : StringCharacter+
  1409. ;
  1410. fragment
  1411. StringCharacter
  1412. : ~["\\\r\n]
  1413. | EscapeSequence
  1414. ;
  1415. // §3.10.6 Escape Sequences for Character and String Literals
  1416. fragment
  1417. EscapeSequence
  1418. : '\\' [btnfr"'\\]
  1419. | OctalEscape
  1420. | UnicodeEscape // This is not in the spec but prevents having to preprocess the input
  1421. ;
  1422. fragment
  1423. OctalEscape
  1424. : '\\' OctalDigit
  1425. | '\\' OctalDigit OctalDigit
  1426. | '\\' ZeroToThree OctalDigit OctalDigit
  1427. ;
  1428. fragment
  1429. ZeroToThree
  1430. : [0-3]
  1431. ;
  1432. // This is not in the spec but prevents having to preprocess the input
  1433. fragment
  1434. UnicodeEscape
  1435. : '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit
  1436. ;
  1437. // §3.10.7 The Null Literal
  1438. NullLiteral
  1439. : 'null'
  1440. ;
  1441. // §3.11 Separators
  1442. LPAREN : '(';
  1443. RPAREN : ')';
  1444. LBRACE : '{';
  1445. RBRACE : '}';
  1446. LBRACK : '[';
  1447. RBRACK : ']';
  1448. SEMI : ';';
  1449. COMMA : ',';
  1450. DOT : '.';
  1451. ELLIPSIS : '...';
  1452. AT : '@';
  1453. COLONCOLON : '::';
  1454. // §3.12 Operators
  1455. ASSIGN : '=';
  1456. GT : '>';
  1457. LT : '<';
  1458. BANG : '!';
  1459. TILDE : '~';
  1460. QUESTION : '?';
  1461. COLON : ':';
  1462. ARROW : '->';
  1463. EQUAL : '==';
  1464. LE : '<=';
  1465. GE : '>=';
  1466. NOTEQUAL : '!=';
  1467. AND : '&&';
  1468. OR : '||';
  1469. INC : '++';
  1470. DEC : '--';
  1471. ADD : '+';
  1472. SUB : '-';
  1473. MUL : '*';
  1474. DIV : '/';
  1475. BITAND : '&';
  1476. BITOR : '|';
  1477. CARET : '^';
  1478. MOD : '%';
  1479. //LSHIFT : '<<';
  1480. //RSHIFT : '>>';
  1481. //URSHIFT : '>>>';
  1482. ADD_ASSIGN : '+=';
  1483. SUB_ASSIGN : '-=';
  1484. MUL_ASSIGN : '*=';
  1485. DIV_ASSIGN : '/=';
  1486. AND_ASSIGN : '&=';
  1487. OR_ASSIGN : '|=';
  1488. XOR_ASSIGN : '^=';
  1489. MOD_ASSIGN : '%=';
  1490. LSHIFT_ASSIGN : '<<=';
  1491. RSHIFT_ASSIGN : '>>=';
  1492. URSHIFT_ASSIGN : '>>>=';
  1493. // §3.8 Identifiers (must appear after all keywords in the grammar)
  1494. Identifier
  1495. : JavaLetter JavaLetterOrDigit*
  1496. ;
  1497. fragment
  1498. JavaLetter
  1499. : [a-zA-Z$_] // these are the "java letters" below 0x7F
  1500. | // covers all characters above 0x7F which are not a surrogate
  1501. ~[\u0000-\u007F\uD800-\uDBFF]
  1502. {Character.isJavaIdentifierStart(_input.LA(-1))}?
  1503. | // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
  1504. [\uD800-\uDBFF] [\uDC00-\uDFFF]
  1505. {Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
  1506. ;
  1507. fragment
  1508. JavaLetterOrDigit
  1509. : [a-zA-Z0-9$_] // these are the "java letters or digits" below 0x7F
  1510. | // covers all characters above 0x7F which are not a surrogate
  1511. ~[\u0000-\u007F\uD800-\uDBFF]
  1512. {Character.isJavaIdentifierPart(_input.LA(-1))}?
  1513. | // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
  1514. [\uD800-\uDBFF] [\uDC00-\uDFFF]
  1515. {Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
  1516. ;
  1517. //
  1518. // Whitespace and comments
  1519. //
  1520. WS : [ \t\r\n\u000C]+ -> skip
  1521. ;
  1522. COMMENT
  1523. : '/*' .*? '*/' -> channel(HIDDEN)
  1524. ;
  1525. LINE_COMMENT
  1526. : '//' ~[\r\n]* -> channel(HIDDEN)
  1527. ;