From 15f3c91db6ab020e4ac711256a6db6ff0aa587b8 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 8 Dec 2022 16:54:57 +0800 Subject: [PATCH] build: update grammer file for Java 17 #29 --- languages/g4/JavaLexer.g4 | 32 ++++- languages/g4/JavaParser.g4 | 234 +++++++++++++++++++++++++++++-------- 2 files changed, 214 insertions(+), 52 deletions(-) diff --git a/languages/g4/JavaLexer.g4 b/languages/g4/JavaLexer.g4 index 6919d231..2cdc0e1a 100644 --- a/languages/g4/JavaLexer.g4 +++ b/languages/g4/JavaLexer.g4 @@ -2,6 +2,8 @@ [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 @@ -82,13 +84,39 @@ VOID: 'void'; VOLATILE: 'volatile'; WHILE: 'while'; +// Module related keywords +MODULE: 'module'; +OPEN: 'open'; +REQUIRES: 'requires'; +EXPORTS: 'exports'; +OPENS: 'opens'; +TO: 'to'; +USES: 'uses'; +PROVIDES: 'provides'; +WITH: 'with'; +TRANSITIVE: 'transitive'; + +// Local Variable Type Inference +VAR: 'var'; // reserved type name + +// Switch Expressions +YIELD: 'yield'; + +// Records +RECORD: 'record'; + +// Sealed Classes +SEALED: 'sealed'; +PERMITS: 'permits'; +NON_SEALED: 'non-sealed'; + // Literals DECIMAL_LITERAL: ('0' | [1-9] (Digits? | '_'+ Digits)) [lL]?; HEX_LITERAL: '0' [xX] [0-9a-fA-F] ([0-9a-fA-F_]* [0-9a-fA-F])? [lL]?; OCT_LITERAL: '0' '_'* [0-7] ([0-7_]* [0-7])? [lL]?; BINARY_LITERAL: '0' [bB] [01] ([01_]* [01])? [lL]?; - + FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits) ExponentPart? [fFdD]? | Digits (ExponentPart [fFdD]? | [fFdD]) ; @@ -103,6 +131,8 @@ CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\''; STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"'; +TEXT_BLOCK: '"""' [ \t]* [\r\n] (. | EscapeSequence)*? '"""'; + NULL_LITERAL: 'null'; // Separators diff --git a/languages/g4/JavaParser.g4 b/languages/g4/JavaParser.g4 index 70dafa69..6a6d4f0a 100644 --- a/languages/g4/JavaParser.g4 +++ b/languages/g4/JavaParser.g4 @@ -2,6 +2,8 @@ [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 @@ -32,7 +34,8 @@ parser grammar JavaParser; options { tokenVocab=JavaLexer; } compilationUnit - : packageDeclaration? importDeclaration* typeDeclaration* EOF + : packageDeclaration? importDeclaration* typeDeclaration* + | moduleDeclaration EOF ; packageDeclaration @@ -45,7 +48,7 @@ importDeclaration typeDeclaration : classOrInterfaceModifier* - (classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration) + (classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration | recordDeclaration) | ';' ; @@ -66,6 +69,8 @@ classOrInterfaceModifier | ABSTRACT | FINAL // FINAL for class only -- does not apply to interfaces | STRICTFP + | SEALED // Java17 + | NON_SEALED // Java17 ; variableModifier @@ -74,9 +79,10 @@ variableModifier ; classDeclaration - : CLASS IDENTIFIER typeParameters? + : CLASS identifier typeParameters? (EXTENDS typeType)? (IMPLEMENTS typeList)? + (PERMITS typeList)? // Java17 classBody ; @@ -85,7 +91,7 @@ typeParameters ; typeParameter - : annotation* IDENTIFIER (EXTENDS annotation* typeBound)? + : annotation* identifier (EXTENDS annotation* typeBound)? ; typeBound @@ -93,7 +99,7 @@ typeBound ; enumDeclaration - : ENUM IDENTIFIER (IMPLEMENTS typeList)? '{' enumConstants? ','? enumBodyDeclarations? '}' + : ENUM identifier (IMPLEMENTS typeList)? '{' enumConstants? ','? enumBodyDeclarations? '}' ; enumConstants @@ -101,7 +107,7 @@ enumConstants ; enumConstant - : annotation* IDENTIFIER arguments? classBody? + : annotation* identifier arguments? classBody? ; enumBodyDeclarations @@ -109,7 +115,7 @@ enumBodyDeclarations ; interfaceDeclaration - : INTERFACE IDENTIFIER typeParameters? (EXTENDS typeList)? interfaceBody + : INTERFACE identifier typeParameters? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody ; classBody @@ -136,6 +142,7 @@ memberDeclaration | annotationTypeDeclaration | classDeclaration | enumDeclaration + | recordDeclaration //Java17 ; /* We use rule this even for void methods which cannot have [] after parameters. @@ -144,7 +151,7 @@ memberDeclaration for invalid return type after parsing. */ methodDeclaration - : typeTypeOrVoid IDENTIFIER formalParameters ('[' ']')* + : typeTypeOrVoid identifier formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody ; @@ -168,7 +175,7 @@ genericConstructorDeclaration ; constructorDeclaration - : IDENTIFIER formalParameters (THROWS qualifiedNameList)? constructorBody=block + : identifier formalParameters (THROWS qualifiedNameList)? constructorBody=block ; fieldDeclaration @@ -188,6 +195,7 @@ interfaceMemberDeclaration | annotationTypeDeclaration | classDeclaration | enumDeclaration + | recordDeclaration // Java17 ; constDeclaration @@ -195,14 +203,15 @@ constDeclaration ; constantDeclarator - : IDENTIFIER ('[' ']')* '=' variableInitializer + : identifier ('[' ']')* '=' variableInitializer ; -// see matching of [] comment in methodDeclaratorRest -// methodBody from Java8 +// Early versions of Java allows brackets after the method name, eg. +// public int[] return2DArray() [] { ... } +// is the same as +// public int[][] return2DArray() { ... } interfaceMethodDeclaration - : interfaceMethodModifier* (typeTypeOrVoid | typeParameters annotation* typeTypeOrVoid) - IDENTIFIER formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody + : interfaceMethodModifier* interfaceCommonBodyDeclaration ; // Java8 @@ -216,7 +225,11 @@ interfaceMethodModifier ; genericInterfaceMethodDeclaration - : typeParameters interfaceMethodDeclaration + : interfaceMethodModifier* typeParameters interfaceCommonBodyDeclaration + ; + +interfaceCommonBodyDeclaration + : annotation* typeTypeOrVoid identifier formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody ; variableDeclarators @@ -228,7 +241,7 @@ variableDeclarator ; variableDeclaratorId - : IDENTIFIER ('[' ']')* + : identifier ('[' ']')* ; variableInitializer @@ -241,7 +254,7 @@ arrayInitializer ; classOrInterfaceType - : IDENTIFIER typeArguments? ('.' IDENTIFIER typeArguments?)* + : identifier typeArguments? ('.' identifier typeArguments?)* ; typeArgument @@ -254,7 +267,14 @@ qualifiedNameList ; formalParameters - : '(' formalParameterList? ')' + : '(' ( receiverParameter? + | receiverParameter (',' formalParameterList)? + | formalParameterList? + ) ')' + ; + +receiverParameter + : typeType (identifier '.')* THIS ; formalParameterList @@ -270,8 +290,17 @@ lastFormalParameter : variableModifier* typeType annotation* '...' variableDeclaratorId ; +// local variable type inference +lambdaLVTIList + : lambdaLVTIParameter (',' lambdaLVTIParameter)* + ; + +lambdaLVTIParameter + : variableModifier* VAR identifier + ; + qualifiedName - : IDENTIFIER ('.' IDENTIFIER)* + : identifier ('.' identifier)* ; literal @@ -281,6 +310,7 @@ literal | STRING_LITERAL | BOOL_LITERAL | NULL_LITERAL + | TEXT_BLOCK // Java17 ; integerLiteral @@ -297,7 +327,7 @@ floatLiteral // ANNOTATIONS altAnnotationQualifiedName - : (IDENTIFIER DOT)* '@' IDENTIFIER + : (identifier DOT)* '@' identifier ; annotation @@ -309,7 +339,7 @@ elementValuePairs ; elementValuePair - : IDENTIFIER '=' elementValue + : identifier '=' elementValue ; elementValue @@ -323,7 +353,7 @@ elementValueArrayInitializer ; annotationTypeDeclaration - : '@' INTERFACE IDENTIFIER annotationTypeBody + : '@' INTERFACE identifier annotationTypeBody ; annotationTypeBody @@ -341,6 +371,7 @@ annotationTypeElementRest | interfaceDeclaration ';'? | enumDeclaration ';'? | annotationTypeDeclaration ';'? + | recordDeclaration ';'? // Java17 ; annotationMethodOrConstantRest @@ -349,7 +380,7 @@ annotationMethodOrConstantRest ; annotationMethodRest - : IDENTIFIER '(' ')' defaultValue? + : identifier '(' ')' defaultValue? ; annotationConstantRest @@ -360,6 +391,53 @@ 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 typeParameters? recordHeader + (IMPLEMENTS typeList)? + recordBody + ; + +recordHeader + : '(' recordComponentList? ')' + ; + +recordComponentList + : recordComponent (',' recordComponent)* + ; + +recordComponent + : typeType identifier + ; + +recordBody + : '{' classBodyDeclaration* '}' + ; + // STATEMENTS / BLOCKS block @@ -373,12 +451,31 @@ blockStatement ; localVariableDeclaration - : variableModifier* typeType variableDeclarators + : variableModifier* (typeType variableDeclarators | VAR identifier '=' expression) + ; + +identifier + : IDENTIFIER + | MODULE + | OPEN + | REQUIRES + | EXPORTS + | OPENS + | TO + | USES + | PROVIDES + | WITH + | TRANSITIVE + | YIELD + | SEALED + | PERMITS + | RECORD + | VAR ; localTypeDeclaration : classOrInterfaceModifier* - (classDeclaration | interfaceDeclaration) + (classDeclaration | interfaceDeclaration | recordDeclaration) | ';' ; @@ -395,15 +492,17 @@ statement | SYNCHRONIZED parExpression block | RETURN expression? ';' | THROW expression ';' - | BREAK IDENTIFIER? ';' - | CONTINUE IDENTIFIER? ';' + | BREAK identifier? ';' + | CONTINUE identifier? ';' + | YIELD expression ';' // Java17 | SEMI | statementExpression=expression ';' - | identifierLabel=IDENTIFIER ':' statement + | switchExpression ';'? // Java17 + | identifierLabel=identifier ':' statement ; catchClause - : CATCH '(' variableModifier* catchType IDENTIFIER ')' block + : CATCH '(' variableModifier* catchType identifier ')' block ; catchType @@ -423,7 +522,8 @@ resources ; resource - : variableModifier* classOrInterfaceType variableDeclaratorId '=' expression + : variableModifier* ( classOrInterfaceType variableDeclaratorId | VAR identifier ) '=' expression + | identifier ; /** Matches cases then statements, both of which are mandatory. @@ -434,7 +534,7 @@ switchBlockStatementGroup ; switchLabel - : CASE (constantExpression=expression | enumConstantName=IDENTIFIER) ':' + : CASE (constantExpression=expression | enumConstantName=IDENTIFIER | typeType varName=identifier) ':' | DEFAULT ':' ; @@ -449,7 +549,7 @@ forInit ; enhancedForControl - : variableModifier* typeType variableDeclaratorId ':' expression + : variableModifier* (typeType | VAR) variableDeclaratorId ':' expression ; // EXPRESSIONS @@ -463,7 +563,7 @@ expressionList ; methodCall - : IDENTIFIER '(' expressionList? ')' + : identifier '(' expressionList? ')' | THIS '(' expressionList? ')' | SUPER '(' expressionList? ')' ; @@ -471,17 +571,18 @@ methodCall expression : primary | expression bop='.' - ( IDENTIFIER - | methodCall - | THIS - | NEW nonWildcardTypeArguments? innerCreator - | SUPER superSuffix - | explicitGenericInvocation + ( + identifier + | methodCall + | THIS + | NEW nonWildcardTypeArguments? innerCreator + | SUPER superSuffix + | explicitGenericInvocation ) | expression '[' expression ']' | methodCall | NEW creator - | '(' annotation* typeType ')' expression + | '(' annotation* typeType ('&' typeType)* ')' expression | expression postfix=('++' | '--') | prefix=('+'|'-'|'++'|'--') expression | prefix=('~'|'!') expression @@ -489,7 +590,7 @@ expression | expression bop=('+'|'-') expression | expression ('<' '<' | '>' '>' '>' | '>' '>') expression | expression bop=('<=' | '>=' | '>' | '<') expression - | expression bop=INSTANCEOF typeType + | expression bop=INSTANCEOF (typeType | pattern) | expression bop=('==' | '!=') expression | expression bop='&' expression | expression bop='^' expression @@ -501,13 +602,19 @@ expression bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=') expression | lambdaExpression // Java8 + | switchExpression // Java17 // Java 8 methodReference - | expression '::' typeArguments? IDENTIFIER - | typeType '::' (typeArguments? IDENTIFIER | NEW) + | expression '::' typeArguments? identifier + | typeType '::' (typeArguments? identifier | NEW) | classType '::' typeArguments? NEW ; +// Java17 +pattern + : variableModifier* typeType annotation* identifier + ; + // Java8 lambdaExpression : lambdaParameters '->' lambdaBody @@ -515,9 +622,10 @@ lambdaExpression // Java8 lambdaParameters - : IDENTIFIER + : identifier | '(' formalParameterList? ')' - | '(' IDENTIFIER (',' IDENTIFIER)* ')' + | '(' identifier (',' identifier)* ')' + | '(' lambdaLVTIList? ')' ; // Java8 @@ -531,13 +639,37 @@ primary | THIS | SUPER | literal - | IDENTIFIER + | identifier | typeTypeOrVoid '.' CLASS | nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments) ; +// Java17 +switchExpression + : SWITCH parExpression '{' switchLabeledRule* '}' + ; + +// Java17 +switchLabeledRule + : CASE (expressionList | NULL_LITERAL | guardedPattern) (ARROW | COLON) switchRuleOutcome + | DEFAULT (ARROW | COLON) switchRuleOutcome + ; + +// Java17 +guardedPattern + : '(' guardedPattern ')' + | variableModifier* typeType annotation* identifier ('&&' expression)* + | guardedPattern '&&' expression + ; + +// Java17 +switchRuleOutcome + : block + | blockStatement* + ; + classType - : (classOrInterfaceType '.')? annotation* IDENTIFIER typeArguments? + : (classOrInterfaceType '.')? annotation* identifier typeArguments? ; creator @@ -546,12 +678,12 @@ creator ; createdName - : IDENTIFIER typeArgumentsOrDiamond? ('.' IDENTIFIER typeArgumentsOrDiamond?)* + : identifier typeArgumentsOrDiamond? ('.' identifier typeArgumentsOrDiamond?)* | primitiveType ; innerCreator - : IDENTIFIER nonWildcardTypeArgumentsOrDiamond? classCreatorRest + : identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest ; arrayCreatorRest @@ -605,12 +737,12 @@ typeArguments superSuffix : arguments - | '.' IDENTIFIER arguments? + | '.' typeArguments? identifier arguments? ; explicitGenericInvocationSuffix : SUPER superSuffix - | IDENTIFIER arguments + | identifier arguments ; arguments