Search in sources :

Example 16 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class SourceFileParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case SEPARATOR:
            if (this.unit.classCount() > 0) {
                // any classes -> only allow classes from here
                this.mode = CLASS;
            } else if (this.unit.importCount() > 0 || this.unit.typeAliasCount() > 0 || this.unit.operatorCount() > 0 || this.unit.getHeaderDeclaration() != null) {
                // any imports, type aliases, operators or header declarations -> don't allow any package declarations
                this.mode = IMPORT;
            } else {
                // nothing defined yet -> allow a package declaration
                this.mode = PACKAGE;
            }
            if (!checkEnd(pm, type)) {
                pm.report(token, "header.separator");
                pm.reparse();
            }
            return;
        case PACKAGE:
            if (type == DyvilKeywords.PACKAGE) {
                PackageDeclaration pack = new PackageDeclaration(token.raw());
                this.unit.setPackageDeclaration(pack);
                pm.pushParser(new PackageParser(pack));
                this.mode = SEPARATOR;
                return;
            }
        // Fallthrough
        case IMPORT:
            switch(type) {
                case DyvilKeywords.IMPORT:
                    pm.pushParser(new ImportParser(this.importConsumer(token)));
                    this.mode = SEPARATOR;
                    return;
                case DyvilKeywords.USING:
                    pm.pushParser(new ImportParser(this.importConsumer(token), KindedImport.USING_DECLARATION));
                    this.mode = SEPARATOR;
                    return;
                case DyvilKeywords.OPERATOR:
                    pm.pushParser(new OperatorParser(this.unit, Operator.INFIX), true);
                    this.mode = SEPARATOR;
                    return;
                case DyvilKeywords.PREFIX:
                case DyvilKeywords.POSTFIX:
                case DyvilKeywords.INFIX:
                    if (// 
                    token.next().type() == DyvilKeywords.OPERATOR || token.next().type() == DyvilKeywords.POSTFIX && token.next().next().type() == DyvilKeywords.OPERATOR) {
                        pm.pushParser(new OperatorParser(this.unit), true);
                        this.mode = SEPARATOR;
                        return;
                    }
                    // parse as modifier (in 'case CLASS' via fallthrough)
                    break;
                case DyvilKeywords.TYPE:
                    pm.pushParser(new TypeAliasParser(this.unit, new TypeAlias()));
                    this.mode = SEPARATOR;
                    return;
                case DyvilKeywords.HEADER:
                    final IToken next = token.next();
                    if (!Tokens.isIdentifier(next.type())) {
                        this.attributes = new AttributeList();
                        pm.report(next, "header.declaration.identifier");
                        return;
                    }
                    pm.skip();
                    if (this.unit.getHeaderDeclaration() != null) {
                        this.attributes = new AttributeList();
                        pm.report(token, "header.declaration.duplicate");
                        this.mode = SEPARATOR;
                        return;
                    }
                    final HeaderDeclaration declaration = new HeaderDeclaration(this.unit, next.raw(), next.nameValue(), this.attributes);
                    this.unit.setHeaderDeclaration(declaration);
                    // reset
                    this.attributes = new AttributeList();
                    this.mode = SEPARATOR;
                    return;
            }
        // Fallthrough
        case CLASS:
            if (this.parseAttribute(pm, token)) {
                return;
            }
            final int classType;
            if ((classType = ModifierParser.parseClassTypeModifier(token, pm)) >= 0) {
                if ((this.flags & NO_CLASSES) != 0) {
                    pm.report(token, "header.class");
                }
                this.attributes.addFlag(classType);
                pm.pushParser(new ClassDeclarationParser(this.unit, this.attributes));
                // reset
                this.attributes = new AttributeList();
                this.mode = SEPARATOR;
                return;
            }
    }
    if (!checkEnd(pm, type)) {
        pm.report(Markers.syntaxError(token, "header.element.invalid", token.toString()));
    }
}
Also used : IToken(dyvilx.tools.parsing.token.IToken) ClassDeclarationParser(dyvilx.tools.compiler.parser.classes.ClassDeclarationParser) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) TypeAlias(dyvilx.tools.compiler.ast.type.alias.TypeAlias) HeaderDeclaration(dyvilx.tools.compiler.ast.header.HeaderDeclaration) PackageDeclaration(dyvilx.tools.compiler.ast.header.PackageDeclaration)

Example 17 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class ParameterListParser method reset.

private void reset() {
    this.attributes = new AttributeList();
    this.type = Types.UNKNOWN;
    this.parameter = null;
    this.flags &= ~VARARGS;
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList)

Example 18 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class LambdaOrTupleParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case OPEN_PARENTHESIS:
            // ( ...
            // ^
            /*
			 * The new version of this parser tries to find the matching closing parenthesis instead of try-parsing the
			 * expression. If it finds that parenthesis token and the next token is a lambda arrow, we can assume that
			 * the expression is a lambda expression. Thus, we directly push a Parameter List Parser that may also
			 * produce syntax errors.
			 */
            final IToken closeParen = BracketMatcher.findMatch(token);
            if (closeParen != null) {
                final IToken next = closeParen.next();
                final int nextType = next.type();
                if (nextType == DyvilSymbols.ARROW_RIGHT || nextType == DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                    // (     ... )          =>
                    // (     ... )          ->
                    // token     closeParen next
                    final LambdaExpr lambdaExpr = new LambdaExpr(next);
                    pm.pushParser(new ParameterListParser(lambdaExpr));
                    this.value = lambdaExpr;
                    this.mode = PARAMETERS_END;
                    return;
                }
            }
        // Fallthrough
        case TUPLE:
            // ( ... )
            final TupleLikeExpr tupleExpr = new TupleLikeExpr(token);
            pm.pushParser(new ArgumentListParser(tupleExpr));
            this.value = tupleExpr;
            this.mode = TUPLE_END;
            return;
        case TUPLE_END:
            this.value.expandPosition(token);
            this.consumer.setValue(this.value);
            pm.popParser();
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "tuple.close_paren");
            }
            return;
        case PARAMETERS_END:
            this.mode = TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "lambda.close_paren");
            }
            return;
        case SINGLE_PARAMETER:
            if (Tokens.isIdentifier(type)) {
                final LambdaExpr lambdaExpr = new LambdaExpr(token.next());
                final IParameter parameter = lambdaExpr.createParameter(token.raw(), token.nameValue(), Types.UNKNOWN, new AttributeList());
                lambdaExpr.getParameters().add(parameter);
                this.value = lambdaExpr;
                this.mode = TYPE_ARROW;
                return;
            }
        // Fallthrough
        case TYPE_ARROW:
            if (this.value == null) {
                this.value = new LambdaExpr(token.raw());
            }
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(returnTypeParser((LambdaExpr) this.value));
                this.mode = RETURN_ARROW;
                return;
            }
        // Fallthrough
        case RETURN_ARROW:
            pm.pushParser(new ExpressionParser(((LambdaExpr) this.value)));
            this.mode = END;
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.reparse();
                pm.report(token, "lambda.arrow");
            }
            return;
        case END:
            pm.popParser(true);
            this.consumer.setValue(this.value);
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) TupleLikeExpr(dyvilx.tools.compiler.ast.expression.TupleLikeExpr) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) IToken(dyvilx.tools.parsing.token.IToken) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr)

Example 19 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class Field method writeAnnotations.

private void writeAnnotations(FieldVisitor fieldVisitor, long flags) {
    ModifierUtil.writeModifiers(fieldVisitor, flags);
    final AttributeList annotations = this.getAttributes();
    if (annotations != null) {
        int count = annotations.size();
        for (int i = 0; i < count; i++) {
            annotations.get(i).write(fieldVisitor);
        }
    }
    IType.writeAnnotations(this.getType(), fieldVisitor, TypeReference.newTypeReference(TypeReference.FIELD), "");
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList)

Example 20 with AttributeList

use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.

the class CodeTypeParameter method computeReifiedKind.

@Override
protected void computeReifiedKind() {
    super.computeReifiedKind();
    final IType type;
    final Reified.Type reifiedKind = this.getReifiedKind();
    if (reifiedKind == Reified.Type.TYPE) {
        type = TypeOperator.LazyFields.TYPE;
    } else if (reifiedKind != null) {
        type = ClassOperator.LazyFields.CLASS;
    } else {
        return;
    }
    if (this.getReifiedKind() != null) {
        final AttributeList attributes = AttributeList.of(Modifiers.MANDATED | Modifiers.SYNTHETIC | Modifiers.FINAL);
        final Name name = Name.apply("reify_" + this.getName().qualified);
        final CodeParameter parameter = new CodeParameter(null, this.getPosition(), name, type, attributes);
        this.setReifyParameter(parameter);
    }
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) Reified(dyvil.annotation.Reified) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType) Name(dyvil.lang.Name)

Aggregations

AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)20 SourcePosition (dyvil.source.position.SourcePosition)7 IValue (dyvilx.tools.compiler.ast.expression.IValue)6 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)6 IType (dyvilx.tools.compiler.ast.type.IType)6 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)5 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)4 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)4 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)4 Name (dyvil.lang.Name)2 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)2 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)2 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)2 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)2 IToken (dyvilx.tools.parsing.token.IToken)2 Reified (dyvil.annotation.Reified)1 Modifiers (dyvil.reflect.Modifiers)1 Opcodes (dyvil.reflect.Opcodes)1 AnnotatableVisitor (dyvilx.tools.asm.AnnotatableVisitor)1 AnnotationVisitor (dyvilx.tools.asm.AnnotationVisitor)1