/*
 [The "BSD licence"]
 Copyright (c) 2013 Terence Parr, Sam Harwell
 Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8)
 Copyright (c) 2021 Michał Lorek (upgrade to Java 11)
 Copyright (c) 2022 Michał Lorek (upgrade to Java 17)
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

parser grammar Java17Parser;

options { tokenVocab=Java17Lexer; }

sourceFile
    : packageDeclaration? importDeclaration* classOrInterface*      # srcfile
    | moduleDeclaration EOF                                         # moduledecl
    ;

packageDeclaration
    : annotation* PACKAGE qualifiedName ';'
    ;

importDeclaration
    : IMPORT STATIC? qualifiedName ('.' '*')? ';'
    ;

classOrInterface
    : classOrInterfaceModifier*
      (classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration | recordDeclaration)       # classorinterfacedecl
    | ';'                                                          # noclassorinterface
    ;

modifier
    : classOrInterfaceModifier
    | NATIVE                        
    | SYNCHRONIZED                  
    | TRANSIENT                     
    | VOLATILE                      
    ;

classOrInterfaceModifier
    : annotation
    | PUBLIC
    | PROTECTED
    | PRIVATE
    | STATIC
    | ABSTRACT
    | FINAL         // FINAL for class only -- does not apply to interfaces
    | STRICTFP
    | SEALED        // Java17  
    | NON_SEALED    // Java17
    ;

variableModifier
    : FINAL                         # finalvarmod
    | annotation                    # annotationvarmod
    ;

classDeclaration
    : CLASS identifier genericDeclarationList?
      (EXTENDS typeType)?
      (IMPLEMENTS typeList)?
      (PERMITS typeList)? // Java17
      classBody
    ;

genericDeclarationList
    : '<' genericTypeVar (',' genericTypeVar)* '>'
    ;

genericTypeVar
    : annotation* identifier ((EXTENDS | IMPLEMENTS) annotation* typeBound)?
    ;

typeBound
    : typeType ('&' typeType)*
    ;

enumDeclaration
    : ENUM identifier (IMPLEMENTS typeList)? '{' enumConstants? ','? enumBodyDeclarations? '}'
    ;

enumConstants
    : enumConstant (',' enumConstant)*
    ;

enumConstant
    : annotation* identifier arguments? classBody?
    ;

enumBodyDeclarations
    : ';' classBodyDeclaration*
    ;

interfaceDeclaration
    : INTERFACE identifier genericDeclarationList? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody
    ;

classBody
    : '{' classBodyDeclaration* '}'
    ;

interfaceBody
    : '{' interfaceBodyDeclaration* '}'
    ;

classBodyDeclaration
    : ';'                           # emptyclassbody
    | STATIC? block                 # classblock
    | modifier* memberDeclaration   # memberdecl
    ;

memberDeclaration
    : classOrInterface              # memberclassorinterface
    | fieldDeclaration              # memberfield
    | method                        # membermethod
    | constructor                   # memberconstructor
    ;

method
    : methodDeclaration             # methoddecl
    | genericMethodDeclaration      # genericmethod
    ;

/* We use rule this even for void methods which cannot have [] after parameters.
   This simplifies grammar and we can consider void to be a type, which
   renders the [] matching as a context-sensitive issue or a semantic check
   for invalid return type after parsing.
 */
methodDeclaration
    : methodHeader
      (THROWS exceptionList)?
      methodBody
    ;

methodHeader
    : refType? identifier formalParameters ('[' ']')*
    ;

methodBody
    : block     # methodblock
    | ';'       # emptymethod
    ;

refType
    : typeType  # refType2
    | VOID      # refvoid
    ;

genericMethodDeclaration
    : genericDeclarationList methodDeclaration
    ;

constructor
    : genericConstructorDeclaration     # genericconstructor
    | constructorDeclaration            # constructordecl
    ;

genericConstructorDeclaration
    : genericDeclarationList constructorDeclaration
    ;

constructorDeclaration
    : identifier formalParameters (THROWS exceptionList)? constructorBody=block
    ;

fieldDeclaration
    : typeType? variableDeclarators ';'
    ;

interfaceBodyDeclaration
    : modifier* interfaceMemberDeclaration      # interfacemember
    | ';'                                       # emptyinterface
    ;

interfaceMemberDeclaration
    : constDeclaration                          # interfaceconst
    | interfaceMethodDeclaration                # interfacemethod
    | genericInterfaceMethodDeclaration         # genericinterfacemethod
    | classOrInterface                          # subclassorinterface
    ;

constDeclaration
    : typeType? constantDeclarator (',' constantDeclarator)* ';'
    ;

constantDeclarator
    : identifier ('[' ']')* '=' variableInitializer
    ;

// Early versions of Java allows brackets after the method name, eg.
// public int[] return2DArray() [] { ... }
// is the same as
// public int[][] return2DArray() { ... }
interfaceMethodDeclaration
    : interfaceMethodModifier* interfaceCommonBodyDeclaration
    ;

// Java8
interfaceMethodModifier
    : annotation
    | PUBLIC
    | ABSTRACT
    | DEFAULT
    | STATIC
    | STRICTFP
    ;

genericInterfaceMethodDeclaration
    : interfaceMethodModifier* genericDeclarationList interfaceCommonBodyDeclaration
    ;

interfaceCommonBodyDeclaration
    : annotation* refType? identifier formalParameters ('[' ']')* (THROWS exceptionList)? methodBody
    ;

variableDeclarators
    : variableDeclarator (',' variableDeclarator)*
    ;

variableDeclarator
    : variableDeclaratorId ('=' variableInitializer)?
    ;

variableDeclaratorId
    : identifier ('[' ']')*
    ;

variableInitializer
    : arrayInitializer
    | expression
    ;

arrayInitializer
    : '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
    ;

classOrInterfaceType
    : (identifier typeArguments? '.')* typeIdentifier typeArguments?
    ;

typeArgument
    : typeType
    | wildcardType
    ;

wildcardType
    : annotation* '?' (extendsWildcardType | superWildcardType)?
    ;

extendsWildcardType
    : EXTENDS typeType
    ;

superWildcardType
    : SUPER typeType
    ;

qualifiedNameList
    : qualifiedName (',' qualifiedName)*
    ;

exceptionList
    : qualifiedNameList
    ;

formalParameters
    : '(' ( receiverParameter?
          | receiverParameter (',' formalParameterList)?
          | formalParameterList?
          ) ')'
    ;

receiverParameter
    : typeType? (identifier '.')* THIS
    ;

formalParameterList
    : formalParameter (',' formalParameter)* (',' lastFormalParameter)?
    | lastFormalParameter
    ;

formalParameter
    : variableModifier* typeType? variableDeclaratorId
    | pattern // Pattern matching for Methods
    ;

lastFormalParameter
    : variableModifier* typeType? annotation* '...' variableDeclaratorId
    ;

// local variable type inference
lambdaLVTIList
    : lambdaLVTIParameter (',' lambdaLVTIParameter)*
    ;

lambdaLVTIParameter
    : variableModifier* VAR identifier
    ;

qualifiedName
    : identifier ('.' identifier)*
    ;

literal
    : integerLiteral  # intLiteral
    | floatLiteral  # fltLiteral
    | CHAR_LITERAL  # charLiteral
    | STRING_LITERAL  # stringLiteral
    | BOOL_LITERAL  # boolLiteral
    | NULL_LITERAL  # nullLiteral
    | TEXT_BLOCK  # textBlock // Java17
    ;

integerLiteral
    : DECIMAL_LITERAL
    | HEX_LITERAL
    | OCT_LITERAL
    | BINARY_LITERAL
    ;

floatLiteral
    : FLOAT_LITERAL
    | HEX_FLOAT_LITERAL
    ;

// ANNOTATIONS
altAnnotationQualifiedName
    : (identifier DOT)* '@' identifier
    ;

annotation
    : ('@' qualifiedName | altAnnotationQualifiedName) ('(' ( elementValuePairs | elementValue )? ')')?
    ;

elementValuePairs
    : elementValuePair (',' elementValuePair)*
    ;

elementValuePair
    : identifier '=' elementValue
    ;

elementValue
    : expression
    | annotation
    | elementValueArrayInitializer
    ;

elementValueArrayInitializer
    : '{' (elementValue (',' elementValue)*)? (',')? '}'
    ;

annotationTypeDeclaration
    : '@' INTERFACE identifier annotationTypeBody
    ;

annotationTypeBody
    : '{' (annotationTypeElementDeclaration)* '}'
    ;

annotationTypeElementDeclaration
    : modifier* annotationTypeElementRest
    | ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler
    ;

annotationTypeElementRest
    : typeType annotationMethodOrConstantRest ';'
    | classOrInterface ';'?
    ;

annotationMethodOrConstantRest
    : annotationMethodRest
    | annotationConstantRest
    ;

annotationMethodRest
    : identifier '(' ')' defaultValue?
    ;

annotationConstantRest
    : variableDeclarators
    ;

defaultValue
    : DEFAULT elementValue
    ;

// MODULES - Java9

moduleDeclaration
    : OPEN? MODULE qualifiedName moduleBody
    ;

moduleBody
    : '{' moduleDirective* '}'
    ;

moduleDirective
	: REQUIRES requiresModifier* qualifiedName ';'
	| EXPORTS qualifiedName (TO qualifiedName)? ';'
	| OPENS qualifiedName (TO qualifiedName)? ';'
	| USES qualifiedName ';'
	| PROVIDES qualifiedName WITH qualifiedName ';'
	;

requiresModifier
	: TRANSITIVE
	| STATIC
	;

// RECORDS - Java 17

recordDeclaration
    : RECORD identifier genericDeclarationList? recordHeader
      (IMPLEMENTS typeList)?
      recordBody
    ;

recordHeader
    : '(' recordComponentList? ')'
    ;

recordComponentList
    : recordComponent (',' recordComponent)*
    ;

recordComponent
    : typeType? identifier
    ;

recordBody
    : '{' classBodyDeclaration* '}'
    ;

// STATEMENTS / BLOCKS

block
    : '{' blockStatement* '}'
    ;

blockStatement
    : localVariableDeclaration ';'
    | localTypeDeclaration
    | statement
    ;

localVariableDeclaration
    : variableModifier* (VAR | typeType) variableDeclarators
    ;

identifier
    : IDENTIFIER
    | MODULE
    | OPEN
    | REQUIRES
    | EXPORTS
    | OPENS
    | TO
    | USES
    | PROVIDES
    | WITH
    | TRANSITIVE
    | YIELD
    | SEALED
    | PERMITS
    | RECORD
    | VAR
    ;

typeIdentifier  // Identifiers that are not restricted for type declarations
    : IDENTIFIER
    | MODULE
    | OPEN
    | REQUIRES
    | EXPORTS
    | OPENS
    | TO
    | USES
    | PROVIDES
    | WITH
    | TRANSITIVE
    | SEALED
    | PERMITS
    | RECORD
    ;

localTypeDeclaration
    : classOrInterfaceModifier*
      (classDeclaration | interfaceDeclaration | recordDeclaration)
    | ';'
    ;

statement
    : blockLabel=block  #blockstmt
    | ASSERT expression (':' expression)? ';'  #assertstmt
    | IF parExpression statement (ELSE statement)?  #conditionalstmt
    | FOR '(' forControl ')' statement  #forloop
    | WHILE parExpression statement  #whileloop
    | DO statement WHILE parExpression ';'  #dowhileloop
    | TRY block (catchClause+ finallyBlock? | finallyBlock)  #trycatchblock
    | TRY resourceSpecification block catchClause* finallyBlock?  #trycatchresource
    | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}'  #switchstmt
    | SYNCHRONIZED parExpression block  #synchronizedstmt
    | RETURN expression? ';'  #returnstmt
    | THROW expression ';'  #throwstmt
    | BREAK identifier? ';'  #breakstmt
    | CONTINUE identifier? ';'  #continuestmt
    | YIELD expression ';'  #yieldstmt // Java17
    | SEMI  #semistmt
    | statementExpression=expression ';'  #stmtexpression
    | identifierLabel=identifier ':' statement  #labeledstmt
    ;

catchClause
    : CATCH '(' variableModifier* catchType? identifier ')' block
    ;

catchType
    : qualifiedName ('|' qualifiedName)*
    ;

finallyBlock
    : FINALLY block
    ;

resourceSpecification
    : '(' resources ';'? ')'
    ;

resources
    : resource (';' resource)*
    ;

resource
    : variableModifier* ( classOrInterfaceType? variableDeclaratorId | VAR identifier ) '=' expression
    | identifier
    ;

/** Matches cases then statements, both of which are mandatory.
 *  To handle empty cases at the end, we add switchLabel* to statement.
 */
switchBlockStatementGroup
    : switchLabel+ blockStatement+
    ;

switchLabel
    : CASE constantExpression=expression ':'  #switchLabelConst
    | CASE enumConstantName=IDENTIFIER ':'  #switchLabelEnum
    | CASE pattern ':'  #switchLabelPattern
    | DEFAULT ':'  #switchLabelDefault
    ;

forControl
    : enhancedForControl
    | forInit? ';' expression? ';' forUpdate=expressionList?
    ;

forInit
    : localVariableDeclaration
    | expressionList
    ;

enhancedForControl
    : variableModifier* (typeType? | VAR) variableDeclaratorId ':' expression
    ;

// EXPRESSIONS

parExpression
    : '(' expression ')'
    ;

expressionList
    : expression (',' expression)*
    ;

methodCall
    : identifier '(' expressionList? ')'
    | THIS '(' expressionList? ')'
    | SUPER '(' expressionList? ')'
    ;

expression
    : primary  #primaryExpression2
    | expression bop='.'
      (
         identifier
       | methodCall
       | THIS
       | NEW nonWildcardTypeArguments? innerCreator
       | SUPER superSuffix
       | explicitGenericInvocation
      )  # dottedexpression
    | expression '[' expression ']' #arrayaccessexpression
    | methodCall  #methodcallexpression 
    | NEW creator  #newinstanceexpression
    | '(' annotation* typeType ('&' typeType)* ')' expression  #castexpression
    | expression postfix=('++' | '--')  #postfixexpression
    | prefix=('+'|'-'|'++'|'--'|'~'|'!') expression  #prefixexpression
    | expression bop=('*'|'/'|'%') expression  #mathmuldivmodexpression
    | expression bop=('+'|'-') expression  #mathaddsubexpression
    | expression ('<' '<' | '>' '>' '>' | '>' '>') expression  #shiftexpression
    | expression bop=('<=' | '>=' | '>' | '<') expression  #relationalexpression
    | expression bop=INSTANCEOF (typeType | pattern)  #instanceofexpression
    | expression bop=('==' | '!=') expression  #equalityexpression
    | expression bop='&' expression  #bitwiseandexpression
    | expression bop='^' expression  #bitwisexorexpression
    | expression bop='|' expression  #bitwiseorexpression
    | expression bop='&&' expression  #andexpression
    | expression bop='||' expression  #orexpression
    | <assoc=right> expression bop='?' expression ':' expression  #conditionalassignexpression
    | <assoc=right> expression
      bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=')
      expression  #assignexpression
    | lambdaExpression  #lambdaExpression2 // Java8
    | switchExpression  #switchExpression2 // Java17

    // Java 8 methodReference
    | expression '::' typeArguments? identifier  #methodreferenceexpression
    | typeType '::' (typeArguments? identifier | NEW)  #methodorcreatorreferenceexpression
    | classType '::' typeArguments? NEW  #creatorreferenceexpression
    ;

// Java17
pattern
    : primaryPattern  #pPattern
    | guardedPattern  #gPattern
    ;

primaryPattern
    : typePattern  #tPattern
    | recordPattern  #rPattern
    | '(' pattern ')'  #enclosedPattern
    | literal (',' literal)* #lPattern
    ;

recordPattern
    : type=typeType? ctor=typeType recordStructurePattern ('@' identifier)?
    //| recordStructurePattern identifier?
    ;

typePattern
    : variableModifier* typeType identifier
    | variableModifier* identifier
    ;

recordStructurePattern
    : '(' recordComponentPatternList? ')'
    ;

recordComponentPatternElement
    : pattern
    | identifier
    ;

recordComponentPatternList
    : pattern (',' pattern)*
    ;

// Java8
lambdaExpression
    : lambdaParameters '->' lambdaBody
    ;

// Java8
lambdaParameters
    : identifier
    | '(' formalParameterList? ')'
    | '(' identifier (',' identifier)* ')'
    | '(' lambdaLVTIList? ')'
    ;

// Java8
lambdaBody
    : expression
    | block
    ;

primary
    : '(' expression ')'  # primaryExpression
    | THIS  # primaryThis
    | SUPER  # primarySuper
    | literal  # primaryLiteral
    | identifier  # primaryIdentifier
    | refType '.' CLASS  # primaryClassref
    | nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments)  # primaryInvocation
    ;

// Java17
switchExpression
    : SWITCH parExpression '{' switchLabeledRule* '}'
    ;

// Java17
switchLabeledRule
    : switchLabelCase switchRuleOutcome
    ;

switchLabelCase
    : CASE NULL_LITERAL (ARROW | COLON) #labeledRuleNull
    | CASE pattern (ARROW | COLON) #labeledRulePattern
    | DEFAULT (ARROW | COLON) #labeledRuleDefault
    | CASE expressionList (ARROW | COLON) #labeledRuleExprList
    ;

// Java20
guardedPattern
    : primaryPattern WITH expression
    ;

// Java17
switchRuleOutcome
    : block
    | expression ';'
    ;

classType
    : (classOrInterfaceType '.')? annotation* identifier typeArguments?
    ;

creator
    : nonWildcardTypeArguments createdName classCreatorRest
    | createdName (arrayCreatorRest | classCreatorRest)
    ;

createdName
    : identifier typeArgumentsOrDiamond?
    | primitiveType
    ;

innerCreator
    : identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest
    ;

arrayCreatorRest
    : '[' (']' ('[' ']')* arrayInitializer | expression ']' ('[' expression ']')* ('[' ']')*)
    ;

classCreatorRest
    : arguments classBody?
    ;

explicitGenericInvocation
    : nonWildcardTypeArguments explicitGenericInvocationSuffix
    ;

typeArgumentsOrDiamond
    : '<' '>'
    | typeArguments
    ;

nonWildcardTypeArgumentsOrDiamond
    : '<' '>'
    | nonWildcardTypeArguments
    ;

nonWildcardTypeArguments
    : '<' typeList '>'
    ;

typeList
    : typeType (',' typeType)*
    ;

typeType
    : annotation* (classOrInterfaceType | primitiveType) (annotation* '[' ']')*
    ;

primitiveType
    : BOOLEAN
    | CHAR
    | BYTE
    | SHORT
    | INT
    | LONG
    | FLOAT
    | DOUBLE
    ;

typeArguments
    : '<' typeArgument (',' typeArgument)* '>'
    ;

superSuffix
    : arguments
    | '.' typeArguments? identifier arguments?
    ;

explicitGenericInvocationSuffix
    : SUPER superSuffix
    | identifier arguments
    ;

arguments
    : '(' expressionList? ')'
    ;