mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 11:20:44 +07:00
Java: BasicDeclarationParser cleanup
GitOrigin-RevId: af9d7d4e473eaa6ba2e9627d135847b6dc260d79
This commit is contained in:
committed by
intellij-monorepo-bot
parent
11d74fb190
commit
d716a552d4
+35
-41
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.lang.java.parser;
|
||||
|
||||
import com.intellij.core.JavaPsiBundle;
|
||||
@@ -6,7 +6,6 @@ import com.intellij.lang.PsiBuilder;
|
||||
import com.intellij.lang.PsiBuilderUtil;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.PsiKeyword;
|
||||
import com.intellij.psi.impl.source.AbstractBasicJavaElementTypeFactory;
|
||||
@@ -40,7 +39,8 @@ public class BasicDeclarationParser {
|
||||
JavaTokenType.RPARENTH, JavaTokenType.LBRACE, JavaTokenType.ARROW);
|
||||
private static final TokenSet METHOD_PARAM_LIST_STOPPERS = TokenSet.create(
|
||||
JavaTokenType.RPARENTH, JavaTokenType.LBRACE, JavaTokenType.ARROW, JavaTokenType.SEMICOLON);
|
||||
private final TokenSet TYPE_START;
|
||||
private static final TokenSet TYPE_START = TokenSet.orSet(
|
||||
BASIC_PRIMITIVE_TYPE_BIT_SET, TokenSet.create(JavaTokenType.IDENTIFIER, JavaTokenType.AT, JavaTokenType.VAR_KEYWORD));
|
||||
private final TokenSet RESOURCE_EXPRESSIONS;
|
||||
|
||||
private static final String WHITESPACES = "\n\r \t";
|
||||
@@ -48,20 +48,16 @@ public class BasicDeclarationParser {
|
||||
|
||||
private final BasicJavaParser myParser;
|
||||
private final AbstractBasicJavaElementTypeFactory.JavaElementTypeContainer myJavaElementTypeContainer;
|
||||
private final WhiteSpaceAndCommentSetHolder myWhiteSpaceAndCommentSetHolder = WhiteSpaceAndCommentSetHolder.INSTANCE;
|
||||
|
||||
public BasicDeclarationParser(final @NotNull BasicJavaParser javaParser) {
|
||||
public BasicDeclarationParser(@NotNull BasicJavaParser javaParser) {
|
||||
myParser = javaParser;
|
||||
myJavaElementTypeContainer = javaParser.getJavaElementTypeFactory().getContainer();
|
||||
TYPE_START = TokenSet.orSet(
|
||||
BASIC_PRIMITIVE_TYPE_BIT_SET,
|
||||
TokenSet.create(JavaTokenType.IDENTIFIER, JavaTokenType.AT, JavaTokenType.VAR_KEYWORD));
|
||||
RESOURCE_EXPRESSIONS = TokenSet.create(
|
||||
myJavaElementTypeContainer.REFERENCE_EXPRESSION, myJavaElementTypeContainer.THIS_EXPRESSION,
|
||||
myJavaElementTypeContainer.METHOD_CALL_EXPRESSION, myJavaElementTypeContainer.NEW_EXPRESSION);
|
||||
}
|
||||
|
||||
public void parseClassBodyWithBraces(final PsiBuilder builder, final boolean isAnnotation, final boolean isEnum) {
|
||||
public void parseClassBodyWithBraces(PsiBuilder builder, boolean isAnnotation, boolean isEnum) {
|
||||
assert builder.getTokenType() == JavaTokenType.LBRACE : builder.getTokenType();
|
||||
builder.advanceLexer();
|
||||
|
||||
@@ -140,11 +136,11 @@ public class BasicDeclarationParser {
|
||||
parseClassBodyWithBraces(builder, isAnnotation, isEnum);
|
||||
}
|
||||
|
||||
done(declaration, myJavaElementTypeContainer.CLASS, myWhiteSpaceAndCommentSetHolder);
|
||||
done(declaration, myJavaElementTypeContainer.CLASS, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return declaration;
|
||||
}
|
||||
|
||||
private void parseEnumConstants(final PsiBuilder builder) {
|
||||
private void parseEnumConstants(PsiBuilder builder) {
|
||||
boolean first = true;
|
||||
while (builder.getTokenType() != null) {
|
||||
if (expect(builder, JavaTokenType.SEMICOLON)) {
|
||||
@@ -183,7 +179,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable PsiBuilder.Marker parseEnumConstant(final PsiBuilder builder) {
|
||||
public @Nullable PsiBuilder.Marker parseEnumConstant(PsiBuilder builder) {
|
||||
final PsiBuilder.Marker constant = builder.mark();
|
||||
|
||||
parseModifierList(builder);
|
||||
@@ -199,10 +195,10 @@ public class BasicDeclarationParser {
|
||||
if (builder.getTokenType() == JavaTokenType.LBRACE) {
|
||||
final PsiBuilder.Marker constantInit = builder.mark();
|
||||
parseClassBodyWithBraces(builder, false, false);
|
||||
done(constantInit, myJavaElementTypeContainer.ENUM_CONSTANT_INITIALIZER, myWhiteSpaceAndCommentSetHolder);
|
||||
done(constantInit, myJavaElementTypeContainer.ENUM_CONSTANT_INITIALIZER, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
}
|
||||
|
||||
done(constant, myJavaElementTypeContainer.ENUM_CONSTANT, myWhiteSpaceAndCommentSetHolder);
|
||||
done(constant, myJavaElementTypeContainer.ENUM_CONSTANT, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return constant;
|
||||
}
|
||||
else {
|
||||
@@ -211,7 +207,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
public void parseClassBodyDeclarations(final PsiBuilder builder, final boolean isAnnotation) {
|
||||
public void parseClassBodyDeclarations(PsiBuilder builder, boolean isAnnotation) {
|
||||
final BaseContext context = isAnnotation ? BaseContext.ANNOTATION_INTERFACE : BaseContext.CLASS;
|
||||
|
||||
PsiBuilder.Marker invalidElements = null;
|
||||
@@ -253,7 +249,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable PsiBuilder.Marker parse(final @NotNull PsiBuilder builder, final BaseContext context) {
|
||||
public @Nullable PsiBuilder.Marker parse(@NotNull PsiBuilder builder, BaseContext context) {
|
||||
IElementType tokenType = builder.getTokenType();
|
||||
if (tokenType == null) return null;
|
||||
|
||||
@@ -316,7 +312,7 @@ public class BasicDeclarationParser {
|
||||
error.errorBefore(JavaPsiBundle.message("unexpected.token"), codeBlock);
|
||||
}
|
||||
|
||||
done(declaration, myJavaElementTypeContainer.CLASS_INITIALIZER, myWhiteSpaceAndCommentSetHolder);
|
||||
done(declaration, myJavaElementTypeContainer.CLASS_INITIALIZER, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return declaration;
|
||||
}
|
||||
|
||||
@@ -414,7 +410,7 @@ public class BasicDeclarationParser {
|
||||
return parseFieldOrLocalVariable(builder, declaration, declarationStart, context);
|
||||
}
|
||||
|
||||
boolean isRecordToken(PsiBuilder builder, IElementType tokenType) {
|
||||
static boolean isRecordToken(PsiBuilder builder, IElementType tokenType) {
|
||||
if (tokenType == JavaTokenType.IDENTIFIER && PsiKeyword.RECORD.equals(builder.getTokenText())) {
|
||||
IElementType nextToken = builder.lookAhead(1);
|
||||
if (nextToken == JavaTokenType.IDENTIFIER ||
|
||||
@@ -422,8 +418,7 @@ public class BasicDeclarationParser {
|
||||
// but we assume it to be a malformed record, rather than a malformed type.
|
||||
BASIC_MODIFIER_BIT_SET.contains(nextToken) || BASIC_CLASS_KEYWORD_BIT_SET.contains(nextToken) || TYPE_START.contains(nextToken) ||
|
||||
nextToken == JavaTokenType.AT || nextToken == JavaTokenType.LBRACE || nextToken == JavaTokenType.RBRACE) {
|
||||
LanguageLevel level = getLanguageLevel(builder);
|
||||
return JavaFeature.RECORDS.isSufficient(level);
|
||||
return JavaFeature.RECORDS.isSufficient(getLanguageLevel(builder));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -456,7 +451,7 @@ public class BasicDeclarationParser {
|
||||
return isNonSealed;
|
||||
}
|
||||
|
||||
public @NotNull Pair<PsiBuilder.Marker, Boolean> parseModifierList(final PsiBuilder builder) {
|
||||
public @NotNull Pair<PsiBuilder.Marker, Boolean> parseModifierList(PsiBuilder builder) {
|
||||
return parseModifierList(builder, BASIC_MODIFIER_BIT_SET);
|
||||
}
|
||||
|
||||
@@ -497,7 +492,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
done(modList, myJavaElementTypeContainer.MODIFIER_LIST, myWhiteSpaceAndCommentSetHolder);
|
||||
done(modList, myJavaElementTypeContainer.MODIFIER_LIST, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return Pair.create(modList, isEmpty);
|
||||
}
|
||||
|
||||
@@ -546,7 +541,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
|
||||
done(declaration, anno ? myJavaElementTypeContainer.ANNOTATION_METHOD : myJavaElementTypeContainer.METHOD,
|
||||
myWhiteSpaceAndCommentSetHolder);
|
||||
WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return declaration;
|
||||
}
|
||||
|
||||
@@ -685,7 +680,7 @@ public class BasicDeclarationParser {
|
||||
invalidElements.error(errorMessage);
|
||||
}
|
||||
|
||||
done(elementList, type.getNodeType(myJavaElementTypeContainer), myWhiteSpaceAndCommentSetHolder);
|
||||
done(elementList, type.getNodeType(myJavaElementTypeContainer), WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
}
|
||||
|
||||
public @Nullable PsiBuilder.Marker parseParameter(PsiBuilder builder, boolean ellipsis, boolean disjunctiveType, boolean varType) {
|
||||
@@ -731,10 +726,9 @@ public class BasicDeclarationParser {
|
||||
|
||||
Pair<PsiBuilder.Marker, Boolean> modListInfo = parseModifierList(builder);
|
||||
|
||||
BasicReferenceParser.TypeInfo typeInfo;
|
||||
if (typed) {
|
||||
int flags = BasicReferenceParser.EAT_LAST_DOT | BasicReferenceParser.WILDCARD | typeFlags;
|
||||
typeInfo = myParser.getReferenceParser().parseTypeInfo(builder, flags);
|
||||
BasicReferenceParser.TypeInfo typeInfo = myParser.getReferenceParser().parseTypeInfo(builder, flags);
|
||||
|
||||
if (typeInfo == null) {
|
||||
if (Boolean.TRUE.equals(modListInfo.second)) {
|
||||
@@ -756,7 +750,7 @@ public class BasicDeclarationParser {
|
||||
PsiBuilder.Marker expr = myParser.getExpressionParser().parse(builder);
|
||||
if (expr != null && exprType(expr) == myJavaElementTypeContainer.THIS_EXPRESSION) {
|
||||
mark.drop();
|
||||
done(param, myJavaElementTypeContainer.RECEIVER_PARAMETER, myWhiteSpaceAndCommentSetHolder);
|
||||
done(param, myJavaElementTypeContainer.RECEIVER_PARAMETER, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return param;
|
||||
}
|
||||
|
||||
@@ -767,7 +761,7 @@ public class BasicDeclarationParser {
|
||||
if (expect(builder, JavaTokenType.IDENTIFIER)) {
|
||||
if (type == myJavaElementTypeContainer.PARAMETER || type == myJavaElementTypeContainer.RECORD_COMPONENT) {
|
||||
eatBrackets(builder, null);
|
||||
done(param, type, myWhiteSpaceAndCommentSetHolder);
|
||||
done(param, type, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -783,7 +777,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
done(param, myJavaElementTypeContainer.RESOURCE_VARIABLE, myWhiteSpaceAndCommentSetHolder);
|
||||
done(param, myJavaElementTypeContainer.RESOURCE_VARIABLE, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return param;
|
||||
}
|
||||
|
||||
@@ -830,7 +824,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
|
||||
if (builder.getTokenType() != JavaTokenType.COMMA) break;
|
||||
done(variable, varType, myWhiteSpaceAndCommentSetHolder);
|
||||
done(variable, varType, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
builder.advanceLexer();
|
||||
|
||||
if (builder.getTokenType() != JavaTokenType.IDENTIFIER) {
|
||||
@@ -869,7 +863,7 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
|
||||
if (openMarker) {
|
||||
done(variable, varType, myWhiteSpaceAndCommentSetHolder);
|
||||
done(variable, varType, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
}
|
||||
|
||||
return declaration;
|
||||
@@ -914,7 +908,7 @@ public class BasicDeclarationParser {
|
||||
return paired;
|
||||
}
|
||||
|
||||
public @Nullable PsiBuilder.Marker parseAnnotations(final PsiBuilder builder) {
|
||||
public @Nullable PsiBuilder.Marker parseAnnotations(PsiBuilder builder) {
|
||||
PsiBuilder.Marker firstAnno = null;
|
||||
|
||||
while (builder.getTokenType() == JavaTokenType.AT) {
|
||||
@@ -925,7 +919,7 @@ public class BasicDeclarationParser {
|
||||
return firstAnno;
|
||||
}
|
||||
|
||||
public @NotNull PsiBuilder.Marker parseAnnotation(final PsiBuilder builder) {
|
||||
public @NotNull PsiBuilder.Marker parseAnnotation(PsiBuilder builder) {
|
||||
assert builder.getTokenType() == JavaTokenType.AT : builder.getTokenType();
|
||||
final PsiBuilder.Marker anno = builder.mark();
|
||||
builder.advanceLexer();
|
||||
@@ -940,21 +934,21 @@ public class BasicDeclarationParser {
|
||||
|
||||
parseAnnotationParameterList(builder);
|
||||
|
||||
done(anno, myJavaElementTypeContainer.ANNOTATION, myWhiteSpaceAndCommentSetHolder);
|
||||
done(anno, myJavaElementTypeContainer.ANNOTATION, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return anno;
|
||||
}
|
||||
|
||||
private void parseAnnotationParameterList(final PsiBuilder builder) {
|
||||
private void parseAnnotationParameterList(PsiBuilder builder) {
|
||||
PsiBuilder.Marker list = builder.mark();
|
||||
|
||||
if (!expect(builder, JavaTokenType.LPARENTH) || expect(builder, JavaTokenType.RPARENTH)) {
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, myWhiteSpaceAndCommentSetHolder);
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (builder.getTokenType() == null) {
|
||||
error(builder, JavaPsiBundle.message("expected.parameter.or.rparen"));
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, myWhiteSpaceAndCommentSetHolder);
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return;
|
||||
}
|
||||
PsiBuilder.Marker elementMarker = parseAnnotationElement(builder);
|
||||
@@ -986,10 +980,10 @@ public class BasicDeclarationParser {
|
||||
}
|
||||
}
|
||||
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, myWhiteSpaceAndCommentSetHolder);
|
||||
done(list, myJavaElementTypeContainer.ANNOTATION_PARAMETER_LIST, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
}
|
||||
|
||||
private PsiBuilder.Marker parseAnnotationElement(final PsiBuilder builder) {
|
||||
private PsiBuilder.Marker parseAnnotationElement(PsiBuilder builder) {
|
||||
PsiBuilder.Marker pair = builder.mark();
|
||||
|
||||
PsiBuilder.Marker valueMarker = parseAnnotationValue(builder);
|
||||
@@ -998,7 +992,7 @@ public class BasicDeclarationParser {
|
||||
return null;
|
||||
}
|
||||
if (builder.getTokenType() != JavaTokenType.EQ) {
|
||||
done(pair, myJavaElementTypeContainer.NAME_VALUE_PAIR, myWhiteSpaceAndCommentSetHolder);
|
||||
done(pair, myJavaElementTypeContainer.NAME_VALUE_PAIR, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return pair;
|
||||
}
|
||||
|
||||
@@ -1010,7 +1004,7 @@ public class BasicDeclarationParser {
|
||||
valueMarker = parseAnnotationValue(builder);
|
||||
if (valueMarker == null) error(builder, JavaPsiBundle.message("expected.value"));
|
||||
|
||||
done(pair, myJavaElementTypeContainer.NAME_VALUE_PAIR, myWhiteSpaceAndCommentSetHolder);
|
||||
done(pair, myJavaElementTypeContainer.NAME_VALUE_PAIR, WhiteSpaceAndCommentSetHolder.INSTANCE);
|
||||
return pair;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.lang.java.parser;
|
||||
|
||||
import com.intellij.AbstractBundle;
|
||||
@@ -104,7 +104,7 @@ public class BasicFileParser {
|
||||
|
||||
private boolean stopImportListParsing(PsiBuilder b) {
|
||||
IElementType type = b.getTokenType();
|
||||
if (IMPORT_LIST_STOPPER_SET.contains(type) || myParser.getDeclarationParser().isRecordToken(b, type)) return true;
|
||||
if (IMPORT_LIST_STOPPER_SET.contains(type) || BasicDeclarationParser.isRecordToken(b, type)) return true;
|
||||
if (type == JavaTokenType.IDENTIFIER) {
|
||||
String text = b.getTokenText();
|
||||
if (PsiKeyword.OPEN.equals(text) || PsiKeyword.MODULE.equals(text)) return true;
|
||||
|
||||
Reference in New Issue
Block a user