Search in sources :

Example 1 with TypeParser

use of dyvilx.tools.compiler.parser.type.TypeParser in project Dyvil by Dyvil.

the class ThisSuperParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case THIS_SUPER:
            switch(type) {
                case DyvilKeywords.THIS:
                    this.mode = TYPE;
                    this.value = new ThisExpr(token.raw());
                    return;
                case DyvilKeywords.SUPER:
                    this.mode = TYPE;
                    this.value = new SuperExpr(token.raw());
                    return;
            }
            pm.popParser(true);
            return;
        case TYPE:
            if (ExpressionParser.isGenericCall(token, type)) {
                this.mode = TYPE_END;
                pm.splitJump(token, 1);
                pm.pushParser(new TypeParser(this.value, true));
                return;
            }
            this.valueConsumer.setValue(this.value);
            pm.popParser(true);
            return;
        case TYPE_END:
            pm.popParser();
            this.valueConsumer.setValue(this.value);
            if (!TypeParser.isGenericEnd(token, type)) {
                pm.reparse();
                pm.report(token, this.value.valueTag() == IValue.SUPER ? "super.close_angle" : "this.close_angle");
                return;
            }
            pm.splitJump(token, 1);
    }
}
Also used : TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) SuperExpr(dyvilx.tools.compiler.ast.expression.SuperExpr) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Example 2 with TypeParser

use of dyvilx.tools.compiler.parser.type.TypeParser in project Dyvil by Dyvil.

the class TypeClassParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case TYPE_CLASS:
            if (type == DyvilKeywords.CLASS) {
                this.value = new ClassOperator(token.raw());
                this.mode = TYPE;
                return;
            }
            if (type == DyvilKeywords.TYPE) {
                this.value = new TypeOperator(token.raw());
                this.mode = TYPE;
                return;
            }
            throw new Error();
        case TYPE:
            if (TypeParser.isGenericStart(token, type)) {
                this.mode = ANGLE_END;
                pm.splitJump(token, 1);
                pm.pushParser(new TypeParser(this.value, true));
                return;
            }
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                this.mode = PARENTHESES_END;
            } else {
                this.mode = END;
                pm.reparse();
            }
            pm.pushParser(new TypeParser(this.value));
            return;
        case PARENTHESES_END:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, this.isClassOperator() ? "classoperator.close_paren" : "typeoperator.close_paren");
                return;
            }
            this.valueConsumer.setValue(this.value);
            pm.popParser();
            return;
        case ANGLE_END:
            if (!TypeParser.isGenericEnd(token, type)) {
                pm.report(token, this.isClassOperator() ? "classoperator.close_angle" : "typeoperator.close_angle");
                return;
            }
            this.valueConsumer.setValue(this.value);
            pm.popParser();
            pm.splitJump(token, 1);
            return;
        case END:
            this.valueConsumer.setValue(this.value);
            pm.popParser(true);
    }
}
Also used : TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) TypeOperator(dyvilx.tools.compiler.ast.expression.TypeOperator) ClassOperator(dyvilx.tools.compiler.ast.expression.ClassOperator)

Example 3 with TypeParser

use of dyvilx.tools.compiler.parser.type.TypeParser in project Dyvil by Dyvil.

the class TypeAliasParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    int type = token.type();
    switch(this.mode) {
        case END:
            this.map.addTypeAlias(this.typeAlias);
            pm.popParser(true);
            return;
        case TYPE:
            this.mode = NAME;
            if (type != DyvilKeywords.TYPE) {
                pm.reparse();
                pm.report(token, "typealias.type");
            }
            return;
        case NAME:
            if (Tokens.isIdentifier(type)) {
                Name name = token.nameValue();
                this.typeAlias = new TypeAlias(name, token.raw());
                this.mode = TYPE_PARAMETERS;
                return;
            }
            pm.popParser();
            pm.report(token, "typealias.identifier");
            return;
        case TYPE_PARAMETERS:
            if (TypeParser.isGenericStart(token, type)) {
                this.mode = TYPE_PARAMETERS_END;
                pm.splitJump(token, 1);
                pm.pushParser(new TypeParameterListParser(this.typeAlias));
                return;
            }
        // Fallthrough
        case EQUAL:
            this.mode = END;
            pm.pushParser(new TypeParser(this.typeAlias));
            if (type != BaseSymbols.EQUALS) {
                pm.reparse();
                pm.report(token, "typealias.equals");
            }
            return;
        case TYPE_PARAMETERS_END:
            this.mode = EQUAL;
            if (TypeParser.isGenericEnd(token, type)) {
                pm.splitJump(token, 1);
                return;
            }
            pm.reparse();
            pm.report(token, "generic.close_angle");
    }
}
Also used : TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) ITypeAlias(dyvilx.tools.compiler.ast.type.alias.ITypeAlias) TypeAlias(dyvilx.tools.compiler.ast.type.alias.TypeAlias) TypeParameterListParser(dyvilx.tools.compiler.parser.type.TypeParameterListParser) Name(dyvil.lang.Name)

Example 4 with TypeParser

use of dyvilx.tools.compiler.parser.type.TypeParser in project Dyvil by Dyvil.

the class ParameterListParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            switch(type) {
                case BaseSymbols.SEMICOLON:
                    if (token.isInferred()) {
                        return;
                    }
                    break;
                case DyvilKeywords.LET:
                    this.attributes.addFlag(Modifiers.FINAL);
                // Fallthrough
                case DyvilKeywords.VAR:
                    this.mode = NAME;
                    return;
                case DyvilKeywords.THIS:
                    if (token.next().type() != BaseSymbols.COLON) {
                        pm.report(token, "parameter.identifier");
                        this.mode = SEPARATOR;
                        return;
                    }
                    // this : TYPE
                    this.mode = TYPE_ASCRIPTION;
                    // the colon
                    pm.skip();
                    pm.pushParser(new TypeParser(t -> this.setThisType(t, token, pm)));
                    return;
                case DyvilSymbols.AT:
                    final Annotation annotation = new CodeAnnotation(token.raw());
                    this.attributes.add(annotation);
                    pm.pushParser(new AnnotationParser(annotation));
                    return;
            }
            final Modifier modifier;
            if ((modifier = ModifierParser.parseModifier(token, pm)) != null) {
                this.attributes.add(modifier);
                return;
            }
            if (BaseSymbols.isCloseBracket(type)) {
                pm.popParser(true);
                return;
            }
        // Fallthrough
        case NAME:
            final Name name;
            if (Tokens.isIdentifier(type)) {
                name = token.nameValue();
            } else if (type == BaseSymbols.UNDERSCORE) {
                name = null;
            } else if (Tokens.isKeyword(type)) {
                name = Name.fromRaw(token.stringValue());
            } else {
                if (BaseSymbols.isCloseBracket(type)) {
                    pm.popParser(true);
                }
                if (type == Tokens.EOF) {
                    pm.popParser();
                }
                this.mode = SEPARATOR;
                pm.report(token, "parameter.identifier");
                return;
            }
            this.parameter = this.consumer.createParameter(token.raw(), name, this.type, this.attributes);
            this.mode = INTERNAL_NAME;
            return;
        case INTERNAL_NAME:
            this.mode = VARARGS_AFTER_NAME;
            // overwrite the internal name if necessary
            if (Tokens.isIdentifier(type)) {
                // IDENTIFIER IDENTIFIER : TYPE
                this.parameter.setName(token.nameValue());
                return;
            } else if (type == BaseSymbols.UNDERSCORE) {
                // IDENTIFIER _ : TYPE
                this.parameter.setName(null);
                return;
            }
        // Fallthrough
        case VARARGS_AFTER_NAME:
            if (type == DyvilSymbols.ELLIPSIS) {
                this.flags |= VARARGS;
                this.mode = TYPE_ASCRIPTION;
                return;
            }
        // Fallthrough
        case TYPE_ASCRIPTION:
        case VARARGS_AFTER_POST_TYPE:
            // continue with the remaining cases (DEFAULT_VALUE, PROPERTY, ...).
            if (this.mode == VARARGS_AFTER_POST_TYPE) {
                // case (2)
                if (type == DyvilSymbols.ELLIPSIS) {
                    this.setTypeVarargs();
                    this.mode = DEFAULT_VALUE;
                    return;
                }
            } else /* case (1) */
            if (type == BaseSymbols.COLON) {
                this.mode = VARARGS_AFTER_POST_TYPE;
                final TypeParser parser = new TypeParser(this);
                if (this.hasFlag(LAMBDA_ARROW_END)) {
                    parser.withFlags(TypeParser.IGNORE_LAMBDA);
                }
                pm.pushParser(parser);
                return;
            }
        // Fallthrough
        case DEFAULT_VALUE:
            if (type == BaseSymbols.EQUALS) {
                this.mode = PROPERTY;
                pm.pushParser(new ExpressionParser(this.parameter));
                return;
            }
        // Fallthrough
        case PROPERTY:
            if (type == BaseSymbols.OPEN_CURLY_BRACKET && this.hasFlag(ALLOW_PROPERTIES)) {
                final IProperty property = this.parameter.createProperty();
                pm.pushParser(new PropertyBodyParser(property), true);
                this.mode = SEPARATOR;
                return;
            }
        // Fallthrough
        case SEPARATOR:
            this.mode = DECLARATOR;
            if (this.parameter != null) {
                if (this.hasFlag(VARARGS)) {
                    this.parameter.setVarargs();
                }
                this.parameter.setType(this.type);
                this.consumer.getParameters().add(this.parameter);
            }
            this.reset();
            switch(type) {
                case DyvilSymbols.ARROW_RIGHT:
                case DyvilSymbols.DOUBLE_ARROW_RIGHT:
                    if (!this.hasFlag(LAMBDA_ARROW_END)) {
                        // produce a syntax error
                        break;
                    }
                // Fallthrough
                case BaseSymbols.CLOSE_PARENTHESIS:
                case BaseSymbols.CLOSE_CURLY_BRACKET:
                case BaseSymbols.CLOSE_SQUARE_BRACKET:
                    pm.reparse();
                // Fallthrough
                case Tokens.EOF:
                    pm.popParser();
                    return;
                case BaseSymbols.COMMA:
                case BaseSymbols.SEMICOLON:
                    return;
            }
            pm.report(token, "parameter.separator");
    }
}
Also used : Parser(dyvilx.tools.parsing.Parser) ITypeConsumer(dyvilx.tools.compiler.ast.consumer.ITypeConsumer) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) IParametric(dyvilx.tools.compiler.ast.parameter.IParametric) Tokens(dyvilx.tools.parsing.lexer.Tokens) IProperty(dyvilx.tools.compiler.ast.field.IProperty) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) IParserManager(dyvilx.tools.parsing.IParserManager) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) ModifierParser(dyvilx.tools.compiler.parser.annotation.ModifierParser) AnnotationParser(dyvilx.tools.compiler.parser.annotation.AnnotationParser) DyvilSymbols(dyvilx.tools.compiler.parser.DyvilSymbols) PropertyBodyParser(dyvilx.tools.compiler.parser.classes.PropertyBodyParser) IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) Name(dyvil.lang.Name) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType) Types(dyvilx.tools.compiler.ast.type.builtin.Types) Modifier(dyvilx.tools.compiler.ast.attribute.modifiers.Modifier) DyvilKeywords(dyvilx.tools.compiler.parser.DyvilKeywords) Modifiers(dyvil.reflect.Modifiers) BaseSymbols(dyvilx.tools.parsing.lexer.BaseSymbols) IToken(dyvilx.tools.parsing.token.IToken) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) AnnotationParser(dyvilx.tools.compiler.parser.annotation.AnnotationParser) TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) IProperty(dyvilx.tools.compiler.ast.field.IProperty) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) PropertyBodyParser(dyvilx.tools.compiler.parser.classes.PropertyBodyParser) Modifier(dyvilx.tools.compiler.ast.attribute.modifiers.Modifier) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) Name(dyvil.lang.Name)

Example 5 with TypeParser

use of dyvilx.tools.compiler.parser.type.TypeParser in project Dyvil by Dyvil.

the class AnnotationParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case NAME:
            this.annotation.setPosition(token.prev());
            pm.pushParser(new TypeParser(this.annotation), true);
            this.mode = PARAMETERS_START;
            return;
        case PARAMETERS_START:
            this.annotation.expandPosition(token.prev());
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                ArgumentListParser.parseArguments(pm, token.next(), this.annotation);
                this.mode = PARAMETERS_END;
                return;
            }
            pm.popParser(true);
            return;
        case PARAMETERS_END:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "annotation.parenthesis");
            }
            pm.popParser();
    }
}
Also used : TypeParser(dyvilx.tools.compiler.parser.type.TypeParser)

Aggregations

TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)13 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)5 Name (dyvil.lang.Name)4 TypeParameterListParser (dyvilx.tools.compiler.parser.type.TypeParameterListParser)3 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)2 CodeAnnotation (dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation)2 Modifier (dyvilx.tools.compiler.ast.attribute.modifiers.Modifier)2 AnnotationParser (dyvilx.tools.compiler.parser.annotation.AnnotationParser)2 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)2 TypeListParser (dyvilx.tools.compiler.parser.type.TypeListParser)2 IToken (dyvilx.tools.parsing.token.IToken)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 ClassOperator (dyvilx.tools.compiler.ast.expression.ClassOperator)1 SuperExpr (dyvilx.tools.compiler.ast.expression.SuperExpr)1 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)1 TypeOperator (dyvilx.tools.compiler.ast.expression.TypeOperator)1 ClassConstructorCall (dyvilx.tools.compiler.ast.expression.access.ClassConstructorCall)1