Replace parser conditionals with language level highlighting

This commit is contained in:
Roman Shevchenko
2012-05-11 21:09:41 +04:00
parent 97eb59356d
commit 7115bb9787
14 changed files with 249 additions and 189 deletions
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2012 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.
*/
import java.io.*;
import java.util.*;
<error descr="Static imports are not supported at this language level">import static java.lang.Math.*;</error>
@interface <error descr="Annotations are not supported at this language level">Anno</error> { }
<error descr="Annotations are not supported at this language level">@Anno</error>
class UnsupportedFeatures {
void m(<error descr="Variable arity methods are not supported at this language level">String... args</error>) throws Exception {
<error descr="For-each loops are not supported at this language level">for (String s : args) { System.out.println(s); }</error>
List<error descr="Generics are not supported at this language level"><String></error> list =
new ArrayList<error descr="Diamond types are not supported at this language level"><></error>();
try { Reader r = new FileReader("/dev/null"); }
catch (<error descr="Multi-catches are not supported at this language level">FileNotFoundException | IOException e</error>) { e.printStackTrace(); }
try <error descr="Try-with-resources are not supported at this language level">(Reader r = new FileReader("/dev/null"))</error> { }
}
}
@@ -1,24 +1,21 @@
PsiJavaFile:New15.java
PsiMethodCallExpression:new C<?>.B()
PsiReferenceExpression:new C<?>.B
PsiNewExpression:new C<?>
PsiKeyword:new('new')
PsiNewExpression:new C<?>.B()
PsiKeyword:new('new')
PsiReferenceParameterList
<empty list>
PsiWhiteSpace(' ')
PsiJavaCodeReferenceElement:C<?>.B
PsiJavaCodeReferenceElement:C<?>
PsiIdentifier:C('C')
PsiReferenceParameterList
<empty list>
PsiWhiteSpace(' ')
PsiJavaCodeReferenceElement:C<?>
PsiIdentifier:C('C')
PsiReferenceParameterList
PsiJavaToken:LT('<')
PsiTypeElement:?
PsiJavaToken:QUEST('?')
PsiJavaToken:GT('>')
PsiErrorElement:'(' or '[' expected
<empty list>
PsiJavaToken:LT('<')
PsiTypeElement:?
PsiJavaToken:QUEST('?')
PsiJavaToken:GT('>')
PsiJavaToken:DOT('.')
PsiIdentifier:B('B')
PsiReferenceParameterList
<empty list>
PsiIdentifier:B('B')
PsiExpressionList
PsiJavaToken:LPARENTH('(')
PsiJavaToken:RPARENTH(')')
@@ -40,6 +40,7 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.xml.XmlAttribute;
@@ -161,7 +162,7 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testQualifiedNew() throws Exception { doTest(false, false); }
public void testEnclosingInstance() throws Exception { doTest(false, false); }
public void testStaticViaInstance() throws Exception { doTest(true, false); } // static via instabnce
public void testStaticViaInstance() throws Exception { doTest(true, false); } // static via instance
public void testQualifiedThisSuper() throws Exception { doTest(true, false); } //illegal qualified this or super
public void testAmbiguousMethodCall() throws Exception { doTest(false, false); }
@@ -230,19 +231,13 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
}
@Override
public void setSelected(boolean selected) {
}
public void setSelected(boolean selected) { }
@Override
public void readExternal(Element element) {
}
public void readExternal(Element element) { }
@Override
public void writeExternal(Element element) {
}
public void writeExternal(Element element) { }
};
point.registerExtension(extension);
@@ -265,9 +260,13 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
PsiFile txt = myFile.getParent().createFile("x.txt");
PsiDirectory directory = myFile.getParent();
assertNotNull(myFile.toString(), directory);
PsiFile txt = directory.createFile("x.txt");
VirtualFile vFile = txt.getVirtualFile();
assertNotNull(txt.toString(), vFile);
try {
VfsUtil.saveText(txt.getVirtualFile(), "XXX");
VfsUtil.saveText(vFile, "XXX");
}
catch (IOException e) {
throw new RuntimeException(e);
@@ -356,11 +355,7 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
doHighlighting();
}
public void testClassicRethrow() throws Exception {
doTest(false, false);
}
public void testRegexp() throws Exception {
doTest(false, false);
}
public void testClassicRethrow() throws Exception { doTest(false, false); }
public void testRegexp() throws Exception { doTest(false, false); }
public void testUnsupportedFeatures() throws Exception { doTest(false, false); }
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -15,11 +15,7 @@
*/
package com.intellij.lang.java.parser;
import com.intellij.pom.java.LanguageLevel;
public class ExpressionParsingTest extends JavaParsingTestCase {
public ExpressionParsingTest() {
super("parser-full/expressionParsing");
}
@@ -75,11 +71,7 @@ public class ExpressionParsingTest extends JavaParsingTestCase {
public void testNew12() { doTest(true); }
public void testNew13() { doTest(true); }
public void testNew14() { doTest(true); }
public void testNew15() {
withLevel(LanguageLevel.JDK_1_7, new Runnable() { @Override public void run() {
doTest(true);
}});
}
public void testNew15() { doTest(true); }
public void testExprList0() { doTest(true); }
public void testExprList1() { doTest(true); }
@@ -18,10 +18,8 @@ package com.intellij.lang.java.parser.partial;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.java.parser.JavaParsers;
import com.intellij.lang.java.parser.JavaParsingTestCase;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.annotations.NonNls;
public class ExpressionParserTest extends JavaParsingTestCase {
public ExpressionParserTest() {
super("parser-partial/expressions");
@@ -77,11 +75,7 @@ public class ExpressionParserTest extends JavaParsingTestCase {
public void testNew13() { doParserTest("new int[1][][2]"); }
public void testNew14() { doParserTest("Q.new A()"); }
public void testNew15() { doParserTest("new C<?>.B()"); }
public void testNew16() {
withLevel(LanguageLevel.JDK_1_7,
new Runnable() { @Override
public void run() { doParserTest("new C<>()"); } });
}
public void testNew16() { doParserTest("new C<>()"); }
public void testExprList0() { doParserTest("f(1,2)"); }
public void testExprList1() { doParserTest("f("); }
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -18,8 +18,6 @@ package com.intellij.lang.java.parser.partial;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.java.parser.JavaParsers;
import com.intellij.lang.java.parser.JavaParsingTestCase;
import com.intellij.pom.java.LanguageLevel;
public class StatementParserTest extends JavaParsingTestCase {
public StatementParserTest() {
@@ -119,10 +117,10 @@ public class StatementParserTest extends JavaParsingTestCase {
public void testTryNormal0() { doParserTest("try{}catch(E e){}"); }
public void testTryNormal1() { doParserTest("try{}catch(final E e){}finally{}"); }
public void testTryNormal2() { doParserTest("try{}finally{}"); }
public void testTryNormal3() { doParserTestJDK7("try{}catch(A|B e){}"); }
public void testTryNormal4() { doParserTestJDK7("try(R r = 0){}"); }
public void testTryNormal5() { doParserTestJDK7("try(R1 r1 = 1; R2 r2 = 2){}"); }
public void testTryNormal6() { doParserTestJDK7("try(R r = 0;){}"); }
public void testTryNormal3() { doParserTest("try{}catch(A|B e){}"); }
public void testTryNormal4() { doParserTest("try(R r = 0){}"); }
public void testTryNormal5() { doParserTest("try(R1 r1 = 1; R2 r2 = 2){}"); }
public void testTryNormal6() { doParserTest("try(R r = 0;){}"); }
public void testTryIncomplete0() { doParserTest("try"); }
public void testTryIncomplete1() { doParserTest("try{}"); }
public void testTryIncomplete2() { doParserTest("try{}catch"); }
@@ -131,16 +129,16 @@ public class StatementParserTest extends JavaParsingTestCase {
public void testTryIncomplete5() { doParserTest("try{}catch(E e"); }
public void testTryIncomplete6() { doParserTest("try{}catch(E e)"); }
public void testTryIncomplete7() { doParserTest("try{}finally"); }
public void testTryIncomplete8() { doParserTestJDK7("try{}catch(A|)"); }
public void testTryIncomplete9() { doParserTestJDK7("try{}catch(A|B)"); }
public void testTryIncomplete10() { doParserTestJDK7("try({}"); }
public void testTryIncomplete11() { doParserTestJDK7("try(){}"); }
public void testTryIncomplete12() { doParserTestJDK7("try(;){}"); }
public void testTryIncomplete13() { doParserTestJDK7("try(final ){}"); }
public void testTryIncomplete14() { doParserTestJDK7("try(int){}"); }
public void testTryIncomplete15() { doParserTestJDK7("try(R r){}"); }
public void testTryIncomplete16() { doParserTestJDK7("try(R r =){}"); }
public void testTryIncomplete17() { doParserTestJDK7("try(R r = 0;;){}"); }
public void testTryIncomplete8() { doParserTest("try{}catch(A|)"); }
public void testTryIncomplete9() { doParserTest("try{}catch(A|B)"); }
public void testTryIncomplete10() { doParserTest("try({}"); }
public void testTryIncomplete11() { doParserTest("try(){}"); }
public void testTryIncomplete12() { doParserTest("try(;){}"); }
public void testTryIncomplete13() { doParserTest("try(final ){}"); }
public void testTryIncomplete14() { doParserTest("try(int){}"); }
public void testTryIncomplete15() { doParserTest("try(R r){}"); }
public void testTryIncomplete16() { doParserTest("try(R r =){}"); }
public void testTryIncomplete17() { doParserTest("try(R r = 0;;){}"); }
public void testWhileNormal() { doParserTest("while (true) foo();"); }
public void testWhileIncomplete0() { doParserTest("while"); }
@@ -151,33 +149,22 @@ public class StatementParserTest extends JavaParsingTestCase {
public void testWhileIncomplete5() { doParserTest("while() foo();"); }
private void doBlockParserTest(final String text) {
doParserTest(text, new MyTestParser1());
doParserTest(text, new MyBlockTestParser());
}
private void doParserTest(final String text) {
doParserTest(text, new MyTestParser2());
}
private void doParserTestJDK7(final String text) {
withLevel(LanguageLevel.JDK_1_7, new Runnable() {
@Override
public void run() {
doParserTest(text);
}
});
}
private static class MyTestParser2 implements TestParser {
@Override
public void parse(final PsiBuilder builder) {
JavaParsers.STATEMENT_PARSER.parseStatements(builder);
}
}
private static class MyTestParser1 implements TestParser {
private static class MyBlockTestParser implements TestParser {
@Override
public void parse(final PsiBuilder builder) {
JavaParsers.STATEMENT_PARSER.parseCodeBlockDeep(builder, true);
}
}
private void doParserTest(final String text) {
doParserTest(text, new MyStatementsTestParser());
}
private static class MyStatementsTestParser implements TestParser {
@Override
public void parse(final PsiBuilder builder) {
JavaParsers.STATEMENT_PARSER.parseStatements(builder);
}
}
}
@@ -1,8 +1,21 @@
/*
* Copyright 2000-2012 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.
*/
package com.intellij.lang.java.parser.statementParsing;
import com.intellij.lang.java.parser.JavaParsingTestCase;
import com.intellij.pom.java.LanguageLevel;
public class TryParsingTest extends JavaParsingTestCase {
public TryParsingTest() {
@@ -12,11 +25,7 @@ public class TryParsingTest extends JavaParsingTestCase {
public void testNormal1() { doTest(true); }
public void testNormal2() { doTest(true); }
public void testNormal3() { doTest(true); }
public void testNormal4() {
withLevel(LanguageLevel.JDK_1_7, new Runnable() { @Override public void run() {
doTest(true);
}});
}
public void testNormal4() { doTest(true); }
public void testIncomplete1() { doTest(true); }
public void testIncomplete2() { doTest(true); }
@@ -26,9 +35,5 @@ public class TryParsingTest extends JavaParsingTestCase {
public void testIncomplete6() { doTest(true); }
public void testIncomplete7() { doTest(true); }
public void testIncomplete8() { doTest(true); }
public void testIncomplete9() {
withLevel(LanguageLevel.JDK_1_7, new Runnable() { @Override public void run() {
doTest(true);
}});
}
public void testIncomplete9() { doTest(true); }
}