Search in sources :

Example 1 with ForEachStatement

use of dyvilx.tools.compiler.ast.statement.loop.ForEachStatement in project Dyvil by Dyvil.

the class ForStatementParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case FOR:
            this.mode = FOR_START;
            if (type != DyvilKeywords.FOR) {
                pm.reparse();
                pm.report(token, "for.for_keyword");
            }
            return;
        case FOR_START:
            this.mode = VARIABLE;
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                this.parenthesis = true;
                return;
            }
        // Fallthrough
        case VARIABLE:
            if (type == BaseSymbols.SEMICOLON) {
                // for (; ...
                // => No variable declaration, parse Condition next
                pm.pushParser(new ExpressionParser(this));
                this.mode = CONDITION_END;
                return;
            }
            // for ( i ...
            // for ( var i ...
            // for ( let i ...
            pm.pushParser(new DataMemberParser<>(this), true);
            this.mode = VARIABLE_SEPARATOR;
            return;
        case VARIABLE_SEPARATOR:
            switch(type) {
                case DyvilSymbols.ARROW_LEFT:
                    this.mode = FOR_EACH_END;
                    final ExpressionParser parser = new ExpressionParser(this.variable);
                    if (!this.parenthesis) {
                        parser.addFlags(IGNORE_COLON | IGNORE_CLOSURE);
                    }
                    pm.pushParser(parser);
                    return;
                case BaseSymbols.EQUALS:
                    this.mode = VARIABLE_END;
                    pm.pushParser(new ExpressionParser(this.variable));
                    return;
            }
            this.mode = VARIABLE_END;
            pm.reparse();
            pm.report(token, "for.variable.separator");
            return;
        case VARIABLE_END:
            this.mode = CONDITION_END;
            if (token.next().type() != BaseSymbols.SEMICOLON) {
                pm.pushParser(new ExpressionParser(this));
                return;
            }
            if (type != BaseSymbols.SEMICOLON) {
                pm.reparse();
                pm.report(token, "for.variable.semicolon");
            }
            return;
        case CONDITION_END:
            this.mode = FOR_END;
            if (type != BaseSymbols.SEMICOLON) {
                pm.reparse();
                pm.report(token, "for.condition.semicolon");
                return;
            }
            if (token.next().type() != BaseSymbols.SEMICOLON) {
                final ExpressionParser parser = new ExpressionParser(this);
                if (!this.parenthesis) {
                    parser.addFlags(IGNORE_COLON | IGNORE_CLOSURE);
                }
                pm.pushParser(parser);
            }
            return;
        case FOR_END:
            this.forStatement = new ForStatement(this.position, this.variable, this.condition, this.update);
            this.parseEnd(pm, token, type);
            return;
        case FOR_EACH_END:
            this.forStatement = new ForEachStatement(this.position, this.variable);
            this.parseEnd(pm, token, type);
            return;
        case STATEMENT:
            if (BaseSymbols.isTerminator(type) && !token.isInferred()) {
                pm.popParser(true);
                this.field.setValue(this.forStatement);
                return;
            }
            pm.pushParser(new ExpressionParser(this), true);
            this.mode = END;
            return;
        case END:
            pm.popParser(true);
            this.field.setValue(this.forStatement);
    }
}
Also used : ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) IForStatement(dyvilx.tools.compiler.ast.statement.loop.IForStatement) ForStatement(dyvilx.tools.compiler.ast.statement.loop.ForStatement) ForEachStatement(dyvilx.tools.compiler.ast.statement.loop.ForEachStatement)

Aggregations

ForEachStatement (dyvilx.tools.compiler.ast.statement.loop.ForEachStatement)1 ForStatement (dyvilx.tools.compiler.ast.statement.loop.ForStatement)1 IForStatement (dyvilx.tools.compiler.ast.statement.loop.IForStatement)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1