Parsing in type code fragment fixed and cleaned

This commit is contained in:
Roman Shevchenko
2011-03-21 19:24:05 +01:00
parent ddf26ebc62
commit 7583439e95
7 changed files with 120 additions and 72 deletions
@@ -25,10 +25,10 @@ import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.intellij.BitUtil.isSet;
import static com.intellij.lang.PsiBuilderUtil.expect;
import static com.intellij.lang.PsiBuilderUtil.nextTokenType;
import static com.intellij.lang.java.parser.JavaParserUtil.*;
import static com.intellij.lang.java.parser.JavaParserUtil.emptyElement;
public class ReferenceParser {
@@ -147,10 +147,6 @@ public class ReferenceParser {
return typeInfo;
}
private static boolean isSet(final int mask, final int flag) {
return (mask & flag) != 0;
}
@NotNull
private static PsiBuilder.Marker parseWildcardType(final PsiBuilder builder) {
final PsiBuilder.Marker type = builder.mark();
@@ -559,16 +559,24 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
@NotNull
public PsiTypeCodeFragment createTypeCodeFragment(@NotNull final String text, final PsiElement context, final boolean isPhysical) {
return createTypeCodeFragment(text, context, false, isPhysical, false);
return createTypeCodeFragment(text, context, isPhysical, 0);
}
@NotNull
public PsiTypeCodeFragment createTypeCodeFragment(@NotNull final String text, final PsiElement context, final boolean isPhysical, final int flags) {
final PsiTypeCodeFragmentImpl result = new PsiTypeCodeFragmentImpl(myManager.getProject(), isPhysical, "fragment.java", text, flags);
result.setContext(context);
return result;
}
@NotNull
public PsiTypeCodeFragment createTypeCodeFragment(@NotNull final String text,
final PsiElement context,
final boolean isVoidValid,
final boolean isPhysical) {
return createTypeCodeFragment(text, context, true, isPhysical, false);
int flags = 0;
if (isVoidValid) flags |= ALLOW_VOID;
return createTypeCodeFragment(text, context, isPhysical, flags);
}
@NotNull
@@ -577,12 +585,10 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
final boolean isVoidValid,
final boolean isPhysical,
final boolean allowEllipsis) {
final PsiTypeCodeFragmentImpl result = new PsiTypeCodeFragmentImpl(myManager.getProject(), isPhysical, allowEllipsis, "fragment.java", text);
result.setContext(context);
if (isVoidValid) {
result.putUserData(PsiUtil.VALID_VOID_TYPE_IN_CODE_FRAGMENT, Boolean.TRUE);
}
return result;
int flags = 0;
if (isVoidValid) flags |= ALLOW_VOID;
if (allowEllipsis) flags |= ALLOW_ELLIPSIS;
return createTypeCodeFragment(text, context, isPhysical, flags);
}
@NotNull
@@ -15,61 +15,70 @@
*/
package com.intellij.psi.impl.source;
import com.intellij.BitUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import static com.intellij.BitUtil.isSet;
/**
* @author dsl
*/
public class PsiTypeCodeFragmentImpl extends PsiCodeFragmentImpl implements PsiTypeCodeFragment {
private final boolean myAllowEllipsis;
private final boolean myAllowDisjunction;
public PsiTypeCodeFragmentImpl(Project manager,
boolean isPhysical,
boolean allowEllipsis,
@NonNls String name,
CharSequence text) {
super(manager, JavaElementType.TYPE_TEXT, isPhysical, name, text);
myAllowEllipsis = allowEllipsis;
public PsiTypeCodeFragmentImpl(final Project project,
final boolean isPhysical,
final @NonNls String name,
final CharSequence text,
final int flags) {
super(project, JavaElementType.TYPE_TEXT, isPhysical, name, text);
myAllowEllipsis = BitUtil.isSet(flags, PsiElementFactory.ALLOW_ELLIPSIS);
myAllowDisjunction = BitUtil.isSet(flags, PsiElementFactory.ALLOW_DISJUNCTION);
if (isSet(flags, PsiElementFactory.ALLOW_VOID)) {
putUserData(PsiUtil.VALID_VOID_TYPE_IN_CODE_FRAGMENT, Boolean.TRUE);
}
}
@NotNull
public PsiType getType()
throws TypeSyntaxException, NoTypeException {
class SyntaxError extends RuntimeException {}
public PsiType getType() throws TypeSyntaxException, NoTypeException {
class MyTypeSyntaxException extends RuntimeException {
MyTypeSyntaxException(final String message) { super(message); }
}
try {
accept(new PsiRecursiveElementWalkingVisitor() {
@Override public void visitErrorElement(PsiErrorElement element) {
throw new SyntaxError();
@Override
public void visitErrorElement(PsiErrorElement element) {
throw new MyTypeSyntaxException(element.getErrorDescription());
}
});
}
catch(SyntaxError e) {
throw new TypeSyntaxException();
catch (MyTypeSyntaxException e) {
throw new TypeSyntaxException(e.getMessage());
}
PsiElement child = getFirstChild();
while (child != null && !(child instanceof PsiTypeElement)) {
child = child.getNextSibling();
}
PsiTypeElement typeElement = (PsiTypeElement)child;
final PsiTypeElement typeElement = PsiTreeUtil.getChildOfType(this, PsiTypeElement.class);
if (typeElement == null) {
throw new NoTypeException();
throw new NoTypeException("No type found in '" + getText() + "'");
}
PsiType type = typeElement.getType();
PsiElement sibling = typeElement.getNextSibling();
while (sibling instanceof PsiWhiteSpace) {
sibling = sibling.getNextSibling();
final PsiType type = typeElement.getType();
if (type instanceof PsiEllipsisType && !myAllowEllipsis) {
throw new TypeSyntaxException("Ellipsis not allowed: " + type);
}
if (sibling instanceof PsiJavaToken && "...".equals(sibling.getText())) {
if (myAllowEllipsis) return new PsiEllipsisType(type);
else throw new TypeSyntaxException();
} else {
return type;
else if (type instanceof PsiDisjunctionType && !myAllowDisjunction) {
throw new TypeSyntaxException("Disjunction not allowed: " + type);
}
return type;
}
public boolean isVoidValid() {
@@ -38,8 +38,6 @@ import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Constructor;
import static com.intellij.lang.PsiBuilderUtil.expect;
public interface JavaElementType {
class JavaCompositeElementType extends IJavaElementType implements ICompositeElementType {
private final Constructor<? extends ASTNode> myConstructor;
@@ -238,8 +236,8 @@ public interface JavaElementType {
return JavaParserUtil.parseFragment(chameleon,
new JavaParserUtil.ParserWrapper() {
public void parse(final PsiBuilder builder) {
ReferenceParser.parseType(builder, ReferenceParser.EAT_LAST_DOT | ReferenceParser.WILDCARD);
expect(builder, JavaTokenType.ELLIPSIS); // todo[r.sh] parse ellipsis and fix PsiTypeCodeFragmentImpl.getType()
ReferenceParser.parseType(builder, ReferenceParser.EAT_LAST_DOT | ReferenceParser.ELLIPSIS |
ReferenceParser.WILDCARD | ReferenceParser.DISJUNCTIONS);
}
});
}