[java] fixes degenerate enums parsing (IDEA-179650)

This commit is contained in:
Roman Shevchenko
2017-09-28 16:13:37 +02:00
parent f7367a4a64
commit f502283de7
6 changed files with 63 additions and 58 deletions
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
package com.intellij.lang.java.parser;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
@@ -133,6 +121,7 @@ public class DeclarationParser {
}
private void parseEnumConstants(final PsiBuilder builder) {
boolean first = true;
while (builder.getTokenType() != null) {
if (expect(builder, JavaTokenType.SEMICOLON)) {
return;
@@ -143,16 +132,21 @@ public class DeclarationParser {
return;
}
final PsiBuilder.Marker enumConstant = parseEnumConstant(builder);
if (enumConstant == null) {
error(builder, JavaErrorMessages.message("expected.identifier"));
PsiBuilder.Marker enumConstant = parseEnumConstant(builder);
if (enumConstant == null && builder.getTokenType() == JavaTokenType.COMMA && first) {
IElementType next = builder.lookAhead(1);
if (next != JavaTokenType.SEMICOLON && next != JavaTokenType.RBRACE) {
error(builder, JavaErrorMessages.message("expected.identifier"));
}
}
if (!expect(builder, JavaTokenType.COMMA)) {
if (builder.getTokenType() != null && builder.getTokenType() != JavaTokenType.SEMICOLON) {
error(builder, JavaErrorMessages.message("expected.comma.or.semicolon"));
return;
}
first = false;
if (!expect(builder, JavaTokenType.COMMA) &&
builder.getTokenType() != null &&
builder.getTokenType() != JavaTokenType.SEMICOLON) {
error(builder, JavaErrorMessages.message("expected.comma.or.semicolon"));
return;
}
}
}
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
package com.intellij.lang.java.parser;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
@@ -654,11 +642,8 @@ public class ExpressionParser {
first = false;
final IElementType tokenType = builder.getTokenType();
if (tokenType == JavaTokenType.COMMA) {
builder.advanceLexer();
}
else if (tokenType != JavaTokenType.RBRACE) {
IElementType tokenType = builder.getTokenType();
if (!expect(builder, JavaTokenType.COMMA) && tokenType != JavaTokenType.RBRACE) {
error(builder, JavaErrorMessages.message("expected.comma"));
}
}
@@ -0,0 +1,8 @@
PsiJavaFile:EnumBody7.java
PsiJavaToken:LBRACE('{')
PsiWhiteSpace(' ')
PsiJavaToken:COMMA(',')
PsiWhiteSpace(' ')
PsiJavaToken:SEMICOLON(';')
PsiWhiteSpace(' ')
PsiJavaToken:RBRACE('}')
@@ -0,0 +1,12 @@
PsiJavaFile:EnumBody8.java
PsiJavaToken:LBRACE('{')
PsiWhiteSpace(' ')
PsiEnumConstant:A
PsiModifierList:
<empty list>
PsiIdentifier:A('A')
PsiExpressionList
<empty list>
PsiJavaToken:COMMA(',')
PsiWhiteSpace(' ')
PsiJavaToken:RBRACE('}')
@@ -0,0 +1,15 @@
PsiJavaFile:EnumBody9.java
PsiJavaToken:LBRACE('{')
PsiErrorElement:Identifier expected
<empty list>
PsiWhiteSpace(' ')
PsiJavaToken:COMMA(',')
PsiWhiteSpace(' ')
PsiEnumConstant:B
PsiModifierList:
<empty list>
PsiIdentifier:B('B')
PsiExpressionList
<empty list>
PsiWhiteSpace(' ')
PsiJavaToken:RBRACE('}')
@@ -1,18 +1,6 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
package com.intellij.java.parser.partial;
import com.intellij.java.parser.JavaParsingTestCase;
@@ -48,6 +36,9 @@ public class DeclarationParserTest extends JavaParsingTestCase {
public void testEnumBody4() { doParserTest("{ RED(0), GREEN(1), BLUE(2); }", false, true); }
public void testEnumBody5() { doParserTest("{ @ANNOTATION A(10) }", false, true); }
public void testEnumBody6() { doParserTest("{ RED, GREEN, BLUE\n OurEnum() {} }", false, true); }
public void testEnumBody7() { doParserTest("{ , ; }", false, true); }
public void testEnumBody8() { doParserTest("{ A, }", false, true); }
public void testEnumBody9() { doParserTest("{ , B }", false, true); }
public void testEnumWithInitializedConstants() { doParserTest("{ A(10) { },\n B { void method() {} } }", false, true); }
public void testEnumWithoutConstants() { doParserTest("{ private A }", false, true); }