Search in sources :

Example 1 with EnumConstant

use of dyvilx.tools.compiler.ast.field.EnumConstant in project Dyvil by Dyvil.

the class ClassBody method addField.

public void addField(IField field) {
    field.setEnclosingClass(this.enclosingClass);
    final int index = this.fieldCount++;
    if (index >= this.fields.length) {
        IField[] temp = new IField[index * 2];
        System.arraycopy(this.fields, 0, temp, 0, index);
        this.fields = temp;
    }
    this.fields[index] = field;
    final IProperty property = field.getProperty();
    if (property != null) {
        this.addToCache(property);
    }
    if (!(field instanceof EnumConstant)) {
        return;
    }
    final EnumConstant enumConst = (EnumConstant) field;
    // set enum constant index
    for (int i = index - 1; i >= 0; i--) {
        final IField fieldI = this.fields[i];
        if (fieldI instanceof EnumConstant) {
            enumConst.setIndex(((EnumConstant) fieldI).getIndex() + 1);
            return;
        }
    }
    enumConst.setIndex(0);
}
Also used : IProperty(dyvilx.tools.compiler.ast.field.IProperty) EnumConstant(dyvilx.tools.compiler.ast.field.EnumConstant) IField(dyvilx.tools.compiler.ast.field.IField)

Example 2 with EnumConstant

use of dyvilx.tools.compiler.ast.field.EnumConstant in project Dyvil by Dyvil.

the class EnumConstantParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            if (type == DyvilKeywords.CASE) {
                this.mode = NAME;
            }
            return;
        case NAME:
            if (!Tokens.isIdentifier(type)) {
                pm.report(token, "field.identifier");
            }
            this.constant = new EnumConstant(token.raw(), token.nameValue(), this.attributes);
            this.mode = VALUE;
            return;
        case VALUE:
            if (type == BaseSymbols.EQUALS) {
                pm.pushParser(new ExpressionParser(this.constant));
                this.mode = END;
                return;
            }
        // Fallthrough
        case END:
            if (this.consumer.acceptEnums()) {
                // noinspection unchecked
                this.consumer.addDataMember(this.constant);
            } else {
                pm.report(this.constant.getPosition(), "field.enum.invalid");
            }
            pm.popParser(type != Tokens.EOF);
    }
}
Also used : EnumConstant(dyvilx.tools.compiler.ast.field.EnumConstant) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Aggregations

EnumConstant (dyvilx.tools.compiler.ast.field.EnumConstant)2 IField (dyvilx.tools.compiler.ast.field.IField)1 IProperty (dyvilx.tools.compiler.ast.field.IProperty)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1