Search in sources :

Example 1 with BreakStatement

use of dyvilx.tools.compiler.ast.statement.control.BreakStatement in project Dyvil by Dyvil.

the class ExpressionParser method parseValue.

private boolean parseValue(IParserManager pm, IToken token, int type) {
    switch(type) {
        case Tokens.STRING:
        case Tokens.VERBATIM_STRING:
            this.value = new StringValue(token.raw(), token.stringValue());
            this.mode = ACCESS;
            return true;
        case Tokens.STRING_START:
            {
                final StringInterpolationExpr stringInterpolation = new StringInterpolationExpr(token);
                this.value = stringInterpolation;
                this.mode = ACCESS;
                pm.pushParser(new StingInterpolationParser(stringInterpolation), true);
                return true;
            }
        case Tokens.SINGLE_QUOTED_STRING:
            this.value = new CharValue(token.raw(), token.stringValue());
            this.mode = ACCESS;
            return true;
        case Tokens.VERBATIM_CHAR:
            this.value = new CharValue(token.raw(), token.stringValue(), true);
            this.mode = ACCESS;
            return true;
        case Tokens.INT:
            this.value = new IntValue(token.raw(), token.intValue());
            this.mode = ACCESS;
            return true;
        case Tokens.LONG:
            this.value = new LongValue(token.raw(), token.longValue());
            this.mode = ACCESS;
            return true;
        case Tokens.FLOAT:
            this.value = new FloatValue(token.raw(), token.floatValue());
            this.mode = ACCESS;
            return true;
        case Tokens.DOUBLE:
            this.value = new DoubleValue(token.raw(), token.doubleValue());
            this.mode = ACCESS;
            return true;
        case DyvilSymbols.UNDERSCORE:
            // _ ...
            this.value = new WildcardValue(token.raw());
            this.mode = ACCESS;
            return true;
        case BaseSymbols.OPEN_PARENTHESIS:
            {
                // ( ...
                final IToken next = token.next();
                if (next.type() != BaseSymbols.CLOSE_PARENTHESIS) {
                    pm.pushParser(new LambdaOrTupleParser(this, this.hasFlag(IGNORE_LAMBDA)), true);
                    this.mode = ACCESS;
                    return true;
                }
                if (!this.hasFlag(IGNORE_LAMBDA)) {
                    final IToken next2 = next.next();
                    final int next2Type = next2.type();
                    if (next2Type == DyvilSymbols.ARROW_RIGHT || next2Type == DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                        // () =>
                        // () ->
                        pm.skip();
                        pm.pushParser(new LambdaOrTupleParser(this, LambdaOrTupleParser.TYPE_ARROW));
                        this.mode = END;
                        return true;
                    }
                }
                // ()
                this.value = new VoidValue(token.to(token.next()));
                pm.skip();
                this.mode = ACCESS;
                return true;
            }
        case BaseSymbols.OPEN_SQUARE_BRACKET:
            // [ ...
            this.mode = ACCESS;
            pm.pushParser(new ArrayLiteralParser(this), true);
            return true;
        case BaseSymbols.OPEN_CURLY_BRACKET:
            // { ...
            this.mode = ACCESS;
            pm.pushParser(new StatementListParser(this), true);
            return true;
        case DyvilSymbols.AT:
            // @ ...
            Annotation a = new CodeAnnotation(token.raw());
            pm.pushParser(new AnnotationParser(a));
            this.value = new AnnotationExpr(a);
            this.mode = END;
            return true;
        case DyvilSymbols.ARROW_RIGHT:
        case DyvilSymbols.DOUBLE_ARROW_RIGHT:
            {
                if (this.hasFlag(IGNORE_LAMBDA)) {
                    pm.popParser(true);
                    return true;
                }
                // => ...
                // -> ...
                pm.pushParser(new LambdaOrTupleParser(this, LambdaOrTupleParser.TYPE_ARROW), true);
                return true;
            }
        case DyvilKeywords.NULL:
            this.value = new NullValue(token.raw());
            this.mode = ACCESS;
            return true;
        case DyvilKeywords.TRUE:
            this.value = new BooleanValue(token.raw(), true);
            this.mode = ACCESS;
            return true;
        case DyvilKeywords.FALSE:
            this.value = new BooleanValue(token.raw(), false);
            this.mode = ACCESS;
            return true;
        case DyvilKeywords.INIT:
            // init ...
            this.mode = ACCESS;
            pm.pushParser(new ThisSuperParser(this), true);
            return true;
        case DyvilKeywords.THIS:
            // this ...
            this.mode = ACCESS;
            pm.pushParser(new ThisSuperParser(this), true);
            return true;
        case DyvilKeywords.SUPER:
            // super ...
            this.mode = ACCESS;
            pm.pushParser(new ThisSuperParser(this), true);
            return true;
        case DyvilKeywords.CLASS:
            // class ...
            this.mode = ACCESS;
            pm.pushParser(new TypeClassParser(this, token, true));
            return true;
        case DyvilKeywords.TYPE:
            // type ...
            this.mode = ACCESS;
            pm.pushParser(new TypeClassParser(this, token, false));
            return true;
        case DyvilKeywords.NEW:
            // new ...
            this.mode = ACCESS;
            final int flags = this.hasFlag(IGNORE_CLOSURE) ? ConstructorCallParser.IGNORE_ANON_CLASS : 0;
            pm.pushParser(new ConstructorCallParser(this).withFlags(flags), true);
            return true;
        case DyvilKeywords.RETURN:
            {
                // return ...
                ReturnStatement returnStatement = new ReturnStatement(token.raw());
                this.value = returnStatement;
                pm.pushParser(new ExpressionParser(returnStatement));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.IF:
            {
                // if ...
                pm.pushParser(new IfStatementParser(this), true);
                this.mode = END;
                return true;
            }
        case DyvilKeywords.ELSE:
            {
                if (!(this.parent instanceof IfStatementParser) && !(this.parent instanceof ExpressionParser)) {
                    pm.report(token, "expression.else");
                    return true;
                }
                this.end(pm, true);
                return true;
            }
        case DyvilKeywords.MATCH:
            {
                // match ...
                final MatchExpr matchExpr = new MatchExpr(token.raw());
                this.value = matchExpr;
                pm.pushParser(new MatchExpressionParser(matchExpr));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.WHILE:
            {
                if (// repeat parent
                this.parent instanceof RepeatStatementParser || // repeat grandparent
                this.parent instanceof ExpressionParser && this.parent.getParent() instanceof RepeatStatementParser) {
                    this.end(pm, true);
                    return true;
                }
                final WhileStatement whileStatement = new WhileStatement(token.raw());
                this.value = whileStatement;
                pm.pushParser(new WhileStatementParser(whileStatement));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.REPEAT:
            {
                // repeat ...
                final RepeatStatement repeatStatement = new RepeatStatement(token.raw());
                this.value = repeatStatement;
                pm.pushParser(new RepeatStatementParser(repeatStatement));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.FOR:
            {
                pm.pushParser(new ForStatementParser(this.valueConsumer, token.raw()));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.BREAK:
            {
                final BreakStatement breakStatement = new BreakStatement(token);
                this.value = breakStatement;
                final IToken next = token.next();
                if (Tokens.isIdentifier(next.type())) {
                    breakStatement.setName(next.nameValue());
                    pm.skip();
                }
                this.mode = END;
                return true;
            }
        case DyvilKeywords.CONTINUE:
            {
                final ContinueStatement continueStatement = new ContinueStatement(token);
                this.value = continueStatement;
                final IToken next = token.next();
                if (Tokens.isIdentifier(next.type())) {
                    continueStatement.setName(next.nameValue());
                    pm.skip();
                }
                this.mode = END;
                return true;
            }
        case DyvilKeywords.GOTO:
            {
                GoToStatement statement = new GoToStatement(token);
                this.value = statement;
                final IToken next = token.next();
                if (Tokens.isIdentifier(next.type())) {
                    statement.setName(next.nameValue());
                    pm.skip();
                }
                this.mode = END;
                return true;
            }
        case DyvilKeywords.TRY:
            {
                // try ...
                final TryStatement tryStatement = new TryStatement(token.raw());
                this.value = tryStatement;
                pm.pushParser(new TryStatementParser(tryStatement));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.CATCH:
            {
                if (!(this.parent instanceof TryStatementParser) && !(this.parent instanceof ExpressionParser)) {
                    pm.report(token, "expression.catch");
                    return true;
                }
                this.end(pm, true);
                return true;
            }
        case DyvilKeywords.FINALLY:
            {
                if (!(this.parent instanceof TryStatementParser) && !(this.parent instanceof ExpressionParser)) {
                    pm.report(token, "expression.finally");
                    return true;
                }
                this.end(pm, true);
                return true;
            }
        case DyvilKeywords.THROW:
            {
                final ThrowStatement throwStatement = new ThrowStatement(token.raw());
                this.value = throwStatement;
                pm.pushParser(new ExpressionParser(throwStatement));
                this.mode = END;
                return true;
            }
        case DyvilKeywords.SYNCHRONIZED:
            {
                final SyncStatement syncStatement = new SyncStatement(token.raw());
                this.value = syncStatement;
                pm.pushParser(new SyncStatementParser(syncStatement));
                this.mode = END;
                return true;
            }
    }
    return false;
}
Also used : CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) AnnotationParser(dyvilx.tools.compiler.parser.annotation.AnnotationParser) WhileStatement(dyvilx.tools.compiler.ast.statement.loop.WhileStatement) IToken(dyvilx.tools.parsing.token.IToken) TryStatement(dyvilx.tools.compiler.ast.statement.exception.TryStatement) ReturnStatement(dyvilx.tools.compiler.ast.statement.ReturnStatement) ThrowStatement(dyvilx.tools.compiler.ast.statement.exception.ThrowStatement) ContinueStatement(dyvilx.tools.compiler.ast.statement.control.ContinueStatement) GoToStatement(dyvilx.tools.compiler.ast.statement.control.GoToStatement) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) BreakStatement(dyvilx.tools.compiler.ast.statement.control.BreakStatement) SyncStatement(dyvilx.tools.compiler.ast.statement.SyncStatement) RepeatStatement(dyvilx.tools.compiler.ast.statement.loop.RepeatStatement)

Aggregations

Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 CodeAnnotation (dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation)1 ReturnStatement (dyvilx.tools.compiler.ast.statement.ReturnStatement)1 SyncStatement (dyvilx.tools.compiler.ast.statement.SyncStatement)1 BreakStatement (dyvilx.tools.compiler.ast.statement.control.BreakStatement)1 ContinueStatement (dyvilx.tools.compiler.ast.statement.control.ContinueStatement)1 GoToStatement (dyvilx.tools.compiler.ast.statement.control.GoToStatement)1 ThrowStatement (dyvilx.tools.compiler.ast.statement.exception.ThrowStatement)1 TryStatement (dyvilx.tools.compiler.ast.statement.exception.TryStatement)1 RepeatStatement (dyvilx.tools.compiler.ast.statement.loop.RepeatStatement)1 WhileStatement (dyvilx.tools.compiler.ast.statement.loop.WhileStatement)1 AnnotationParser (dyvilx.tools.compiler.parser.annotation.AnnotationParser)1 IToken (dyvilx.tools.parsing.token.IToken)1