Search in sources :

Example 1 with PackageDeclaration

use of dyvilx.tools.compiler.ast.header.PackageDeclaration 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 2 with PackageDeclaration

use of dyvilx.tools.compiler.ast.header.PackageDeclaration in project Dyvil by Dyvil.

the class Template method resolveTypes.

@Override
public void resolveTypes() {
    this.makeGenerateSpecMethod();
    this.makeMainMethod();
    // automatically infer package declaration
    this.packageDeclaration = new PackageDeclaration(null, this.getPackage().getFullName());
    this.templateClass.setSuperType(LazyTypes.Template);
    this.templateClass.setSuperConstructorArguments(new ArgumentList(new StringValue(this.getTemplateName())));
    super.resolveTypes();
}
Also used : ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue) PackageDeclaration(dyvilx.tools.compiler.ast.header.PackageDeclaration)

Aggregations

PackageDeclaration (dyvilx.tools.compiler.ast.header.PackageDeclaration)2 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 StringValue (dyvilx.tools.compiler.ast.expression.constant.StringValue)1 HeaderDeclaration (dyvilx.tools.compiler.ast.header.HeaderDeclaration)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 TypeAlias (dyvilx.tools.compiler.ast.type.alias.TypeAlias)1 ClassDeclarationParser (dyvilx.tools.compiler.parser.classes.ClassDeclarationParser)1 IToken (dyvilx.tools.parsing.token.IToken)1