Search in sources :

Example 6 with AnnotationParser

use of dyvilx.tools.compiler.parser.annotation.AnnotationParser in project Dyvil by Dyvil.

the class TypeParameterParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case ANNOTATIONS:
            switch(type) {
                case DyvilSymbols.AT:
                    final Annotation annotation = new CodeAnnotation(token.raw());
                    this.attributes.add(annotation);
                    pm.pushParser(new AnnotationParser(annotation));
                    return;
                case DyvilKeywords.TYPE:
                    // type IDENTIFIER
                    // type +IDENTIFIER
                    // type -IDENTIFIER
                    this.mode = VARIANCE;
                    return;
                case BaseSymbols.SEMICOLON:
                    if (token.isInferred()) {
                        return;
                    }
            }
            if (TypeParser.isGenericEnd(token, type)) {
                pm.popParser(true);
                return;
            }
        // Fallthrough
        case VARIANCE:
            {
                if (!Tokens.isIdentifier(type)) {
                    pm.report(token, "type_parameter.identifier");
                    return;
                }
                final Name name = token.nameValue();
                if (Tokens.isIdentifier(token.next().type())) {
                    if (name == Names.plus) {
                        this.mode = NAME;
                        this.variance = Variance.COVARIANT;
                        return;
                    }
                    if (name == Names.minus) {
                        this.mode = NAME;
                        this.variance = Variance.CONTRAVARIANT;
                        return;
                    }
                }
                this.createTypeParameter(token, this.variance);
                return;
            }
        case NAME:
            if (Tokens.isIdentifier(type)) {
                this.createTypeParameter(token, this.variance);
                return;
            }
            pm.report(token, "type_parameter.identifier");
            return;
        case TYPE_BOUNDS:
            switch(type) {
                case DyvilKeywords.EXTENDS:
                case BaseSymbols.COLON:
                    // type T: Super
                    // type T extends Super
                    pm.pushParser(this.newTypeParser());
                    this.setBoundMode(UPPER_BOUND);
                    return;
                case DyvilKeywords.SUPER:
                    pm.pushParser(this.newTypeParser());
                    this.setBoundMode(LOWER_BOUND);
                    return;
            }
            if (BaseSymbols.isTerminator(type) || TypeParser.isGenericEnd(token, type)) {
                if (this.typeParameter != null) {
                    this.typeParameterized.getTypeParameters().add(this.typeParameter);
                }
                pm.popParser(true);
                return;
            }
            if (this.typeParameter != null) {
                this.typeParameterized.getTypeParameters().add(this.typeParameter);
            }
            pm.popParser(true);
            pm.report(token, "type_parameter.bound.invalid");
    }
}
Also used : CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) AnnotationParser(dyvilx.tools.compiler.parser.annotation.AnnotationParser) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) Name(dyvil.lang.Name)

Aggregations

Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)6 CodeAnnotation (dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation)6 AnnotationParser (dyvilx.tools.compiler.parser.annotation.AnnotationParser)6 Name (dyvil.lang.Name)4 IToken (dyvilx.tools.parsing.token.IToken)3 Modifier (dyvilx.tools.compiler.ast.attribute.modifiers.Modifier)2 TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)2 Modifiers (dyvil.reflect.Modifiers)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 ITypeConsumer (dyvilx.tools.compiler.ast.consumer.ITypeConsumer)1 IProperty (dyvilx.tools.compiler.ast.field.IProperty)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1 IParametric (dyvilx.tools.compiler.ast.parameter.IParametric)1 ReturnStatement (dyvilx.tools.compiler.ast.statement.ReturnStatement)1 SyncStatement (dyvilx.tools.compiler.ast.statement.SyncStatement)1 BreakStatement (dyvilx.tools.compiler.ast.statement.control.BreakStatement)1 ContinueStatement (dyvilx.tools.compiler.ast.statement.control.ContinueStatement)1 GoToStatement (dyvilx.tools.compiler.ast.statement.control.GoToStatement)1 ThrowStatement (dyvilx.tools.compiler.ast.statement.exception.ThrowStatement)1