Search in sources :

Example 1 with TypeAlias

use of dyvilx.tools.compiler.ast.type.alias.TypeAlias 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 2 with TypeAlias

use of dyvilx.tools.compiler.ast.type.alias.TypeAlias 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 3 with TypeAlias

use of dyvilx.tools.compiler.ast.type.alias.TypeAlias in project Dyvil by Dyvil.

the class HeaderContext method read.

@Override
public void read(DataInput in) throws IOException {
    this.headerDeclaration = new HeaderDeclaration(this);
    this.headerDeclaration.read(in);
    this.name = this.headerDeclaration.getName();
    // Import Declarations
    int imports = in.readShort();
    for (int i = 0; i < imports; i++) {
        ImportDeclaration id = new ImportDeclaration(null);
        id.read(in);
        this.addImport(id);
    }
    int operators = in.readShort();
    for (int i = 0; i < operators; i++) {
        Operator op = Operator.read(in);
        this.addOperator(op);
    }
    int typeAliases = in.readShort();
    for (int i = 0; i < typeAliases; i++) {
        TypeAlias ta = new TypeAlias();
        ta.read(in);
        this.addTypeAlias(ta);
    }
}
Also used : IOperator(dyvilx.tools.compiler.ast.expression.operator.IOperator) Operator(dyvilx.tools.compiler.ast.expression.operator.Operator) ImportDeclaration(dyvilx.tools.compiler.ast.imports.ImportDeclaration) TypeAlias(dyvilx.tools.compiler.ast.type.alias.TypeAlias) ITypeAlias(dyvilx.tools.compiler.ast.type.alias.ITypeAlias)

Aggregations

TypeAlias (dyvilx.tools.compiler.ast.type.alias.TypeAlias)3 ITypeAlias (dyvilx.tools.compiler.ast.type.alias.ITypeAlias)2 Name (dyvil.lang.Name)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 IOperator (dyvilx.tools.compiler.ast.expression.operator.IOperator)1 Operator (dyvilx.tools.compiler.ast.expression.operator.Operator)1 HeaderDeclaration (dyvilx.tools.compiler.ast.header.HeaderDeclaration)1 PackageDeclaration (dyvilx.tools.compiler.ast.header.PackageDeclaration)1 ImportDeclaration (dyvilx.tools.compiler.ast.imports.ImportDeclaration)1 ClassDeclarationParser (dyvilx.tools.compiler.parser.classes.ClassDeclarationParser)1 TypeParameterListParser (dyvilx.tools.compiler.parser.type.TypeParameterListParser)1 TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)1 IToken (dyvilx.tools.parsing.token.IToken)1