Skip to content

Commit

Permalink
[JEP-445] Grammar, parser, AST and code generation for unnamed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria authored and rgrunber committed Feb 2, 2024
1 parent c9c22d8 commit 96b986d
Show file tree
Hide file tree
Showing 62 changed files with 2,003 additions and 1,347 deletions.
15 changes: 11 additions & 4 deletions org.eclipse.jdt.core.compiler.batch/grammar/java.g
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,6 @@ InternalCompilationUnit ::= PackageDeclaration TypeDeclarations
/.$putCase consumeInternalCompilationUnitWithTypes(); $break ./
InternalCompilationUnit ::= ImportDeclarations ReduceImports
/.$putCase consumeInternalCompilationUnit(); $break ./
InternalCompilationUnit ::= TypeDeclarations
/.$putCase consumeInternalCompilationUnitWithTypes(); $break ./
InternalCompilationUnit ::= ImportDeclarations ReduceImports TypeDeclarations
/.$putCase consumeInternalCompilationUnitWithTypes(); $break ./
InternalCompilationUnit ::= $empty
/.$putCase consumeEmptyInternalCompilationUnit(); $break ./
/:$readableName CompilationUnit:/
Expand All @@ -453,6 +449,12 @@ ModuleDeclaration ::= ModuleHeader ModuleBody
/:$compliance 9:/
/.$putCase consumeModuleDeclaration(); $break ./

-- JEP 445: unnamed class, this may capture type declarations without unnamed class, this case is fixed/reduced upon completioon of parsing
InternalCompilationUnit ::= UnnamedClassBodyDeclarations
/.$putCase consumeInternalCompilationUnitWithPotentialUnnamedClass(); $break ./
InternalCompilationUnit ::= ImportDeclarations ReduceImports UnnamedClassBodyDeclarations
/.$putCase consumeInternalCompilationUnitWithPotentialUnnamedClass(); $break ./

-- to work around shift/reduce conflicts, we allow Modifiersopt in order to support annotations
-- in a module declaration, and then report errors if any modifiers other than annotations are
-- encountered
Expand Down Expand Up @@ -746,6 +748,11 @@ ClassBodyDeclarations ::= ClassBodyDeclarations ClassBodyDeclaration
ClassBodyDeclaration -> ClassMemberDeclaration
ClassBodyDeclaration -> StaticInitializer
ClassBodyDeclaration -> ConstructorDeclaration

UnnamedClassBodyDeclarations -> ClassMemberDeclaration
UnnamedClassBodyDeclarations ::= ClassMemberDeclaration UnnamedClassBodyDeclarations
/.$putCase consumeUnnamedClassBodyDeclarations(); $break ./
/:$readableName UnnamedClassBodyDeclarations:/
--1.1 feature
ClassBodyDeclaration ::= Diet NestedMethod CreateInitializer Block
/.$putCase consumeClassBodyDeclaration(); $break ./
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;

import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

/**
* Represents an unnamed class as defined in JEP 445
*/
public class UnnamedClass extends TypeDeclaration {

private static String NAME_TEMPLATE = "<unnamed_class${0}>"; //$NON-NLS-1$

public UnnamedClass(CompilationResult result) {
super(result);
this.modifiers = ClassFileConstants.AccDefault | ClassFileConstants.AccFinal;

Path p = Paths.get(new String(result.fileName));
String basename = p.getFileName().toString();
String classSuffix;
if (basename.endsWith(".java")) { //$NON-NLS-1$
classSuffix = basename.substring(0, basename.length() - 5);
} else {
classSuffix = basename;
}

String nameString = MessageFormat.format(NAME_TEMPLATE, classSuffix);
this.name = nameString.toCharArray();
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public interface ParserBasicInformation {

ERROR_SYMBOL = 140,
MAX_NAME_LENGTH = 53,
NUM_STATES = 1248,
NUM_STATES = 1262,

NT_OFFSET = 140,
SCOPE_UBOUND = 321,
SCOPE_SIZE = 322,
LA_STATE_OFFSET = 18335,
LA_STATE_OFFSET = 18370,
MAX_LA = 1,
NUM_RULES = 954,
NUM_RULES = 956,
NUM_TERMINALS = 140,
NUM_NON_TERMINALS = 439,
NUM_SYMBOLS = 579,
START_STATE = 1002,
EOFT_SYMBOL = 64,
EOLT_SYMBOL = 64,
ACCEPT_ACTION = 18334,
ERROR_ACTION = 18335;
NUM_NON_TERMINALS = 440,
NUM_SYMBOLS = 580,
START_STATE = 1508,
EOFT_SYMBOL = 38,
EOLT_SYMBOL = 38,
ACCEPT_ACTION = 18369,
ERROR_ACTION = 18370;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int getRestrictedKeyword(String text) {

// BEGIN_AUTOGENERATED_REGION
int TokenNameIdentifier = 22,
TokenNameabstract = 42,
TokenNameabstract = 44,
TokenNameassert = 82,
TokenNameboolean = 106,
TokenNamebreak = 83,
Expand All @@ -86,11 +86,11 @@ static int getRestrictedKeyword(String text) {
TokenNamedefault = 77,
TokenNamedo = 85,
TokenNamedouble = 110,
TokenNameelse = 124,
TokenNameelse = 123,
TokenNameenum = 75,
TokenNameextends = 93,
TokenNamefalse = 55,
TokenNamefinal = 43,
TokenNamefalse = 56,
TokenNamefinal = 45,
TokenNamefinally = 117,
TokenNamefloat = 111,
TokenNamefor = 86,
Expand All @@ -102,32 +102,32 @@ static int getRestrictedKeyword(String text) {
TokenNameint = 113,
TokenNameinterface = 74,
TokenNamelong = 114,
TokenNamenative = 44,
TokenNamenative = 46,
TokenNamenew = 40,
TokenNamenon_sealed = 45,
TokenNamenull = 56,
TokenNamenon_sealed = 47,
TokenNamenull = 57,
TokenNamepackage = 91,
TokenNameprivate = 46,
TokenNameprotected = 47,
TokenNamepublic = 48,
TokenNameprivate = 48,
TokenNameprotected = 49,
TokenNamepublic = 50,
TokenNamereturn = 88,
TokenNameshort = 115,
TokenNamestatic = 36,
TokenNamestrictfp = 49,
TokenNamesuper = 37,
TokenNamestatic = 39,
TokenNamestrictfp = 51,
TokenNamesuper = 36,
TokenNameswitch = 65,
TokenNamesynchronized = 39,
TokenNamethis = 38,
TokenNamesynchronized = 41,
TokenNamethis = 37,
TokenNamethrow = 79,
TokenNamethrows = 118,
TokenNametransient = 50,
TokenNametrue = 57,
TokenNametransient = 52,
TokenNametrue = 58,
TokenNametry = 89,
TokenNamevoid = 116,
TokenNamevolatile = 51,
TokenNamevolatile = 53,
TokenNamewhile = 80,
TokenNamemodule = 119,
TokenNameopen = 120,
TokenNameopen = 124,
TokenNamerequires = 125,
TokenNametransitive = 131,
TokenNameexports = 126,
Expand All @@ -136,15 +136,15 @@ static int getRestrictedKeyword(String text) {
TokenNameuses = 128,
TokenNameprovides = 129,
TokenNamewith = 137,
TokenNameIntegerLiteral = 58,
TokenNameLongLiteral = 59,
TokenNameFloatingPointLiteral = 60,
TokenNameDoubleLiteral = 61,
TokenNameCharacterLiteral = 62,
TokenNameStringLiteral = 53,
TokenNameTextBlock = 54,
TokenNameStringTemplate = 121,
TokenNameTextBlockTemplate = 122,
TokenNameIntegerLiteral = 59,
TokenNameLongLiteral = 60,
TokenNameFloatingPointLiteral = 61,
TokenNameDoubleLiteral = 62,
TokenNameCharacterLiteral = 63,
TokenNameStringLiteral = 54,
TokenNameTextBlock = 55,
TokenNameStringTemplate = 120,
TokenNameTextBlockTemplate = 121,
TokenNamePLUS_PLUS = 2,
TokenNameMINUS_MINUS = 3,
TokenNameEQUAL_EQUAL = 19,
Expand Down Expand Up @@ -181,7 +181,7 @@ static int getRestrictedKeyword(String text) {
TokenNameLESS = 11,
TokenNameLPAREN = 24,
TokenNameRPAREN = 25,
TokenNameLBRACE = 52,
TokenNameLBRACE = 42,
TokenNameRBRACE = 33,
TokenNameLBRACKET = 6,
TokenNameRBRACKET = 69,
Expand All @@ -192,10 +192,10 @@ static int getRestrictedKeyword(String text) {
TokenNameDOT = 1,
TokenNameEQUAL = 78,
TokenNameAT = 35,
TokenNameELLIPSIS = 123,
TokenNameELLIPSIS = 122,
TokenNameARROW = 105,
TokenNameCOLON_COLON = 7,
TokenNameBeginLambda = 63,
TokenNameBeginLambda = 64,
TokenNameBeginIntersectionCast = 70,
TokenNameBeginTypeArguments = 90,
TokenNameElidedSemicolonAndRightBrace = 72,
Expand All @@ -204,11 +204,11 @@ static int getRestrictedKeyword(String text) {
TokenNameBeginCaseExpr = 73,
TokenNameRestrictedIdentifierYield = 81,
TokenNameRestrictedIdentifierrecord = 76,
TokenNameRestrictedIdentifiersealed = 41,
TokenNameRestrictedIdentifiersealed = 43,
TokenNameRestrictedIdentifierpermits = 130,
TokenNameBeginCaseElement = 133,
TokenNameRestrictedIdentifierWhen = 134,
TokenNameUNDERSCORE = 34,
TokenNameEOF = 64,
TokenNameEOF = 38,
TokenNameERROR = 140;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ UnaryExpressionNotPlusMinus=Expression
UnaryExpressionNotPlusMinus_NotName=Expression
UnaryExpression_NotName=Expression
UnionType=UnionType
UnnamedClassBodyDeclarations=UnnamedClassBodyDeclarations
UnnamedPattern=UnnamedPattern
UnqualifiedClassBodyopt=ClassBody
UnqualifiedEnterAnonymousClassBody=EnterAnonymousClassBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public void testBuilderRegression() throws JavaModelException, Exception {
env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.8");
fullBuild();
expectingProblemsFor(projectPath,
"Problem : Syntax error on token \".\", , expected [ resource : </Bug562420Test/src/module-info.java> range : <28,29> category : <20> severity : <2>]\n" +
"Problem : Syntax error on token \".\", = expected [ resource : </Bug562420Test/src/module-info.java> range : <47,48> category : <20> severity : <2>]\n" +
"Problem : Syntax error on token \"module\", interface expected [ resource : </Bug562420Test/src/module-info.java> range : <0,6> category : <20> severity : <2>]");
"""
Problem : Syntax error on token ".", = expected [ resource : </Bug562420Test/src/module-info.java> range : <47,48> category : <20> severity : <2>]
Problem : Syntax error on token "{", ( expected [ resource : </Bug562420Test/src/module-info.java> range : <12,13> category : <20> severity : <2>]
Problem : Syntax error on token "}", delete this token [ resource : </Bug562420Test/src/module-info.java> range : <63,64> category : <20> severity : <2>]
Problem : Syntax error on token(s), misplaced construct(s) [ resource : </Bug562420Test/src/module-info.java> range : <0,33> category : <20> severity : <2>]""");
}
}
Loading

0 comments on commit 96b986d

Please sign in to comment.