Search in sources :

Example 6 with ExpressionParser

use of dyvilx.tools.compiler.parser.expression.ExpressionParser in project Dyvil by Dyvil.

the class PropertyBodyParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case OPEN_BRACE:
            if (type == BaseSymbols.OPEN_CURLY_BRACKET) {
                this.mode = TAG;
            }
            return;
        case TAG:
            switch(type) {
                case BaseSymbols.CLOSE_CURLY_BRACKET:
                    pm.popParser();
                    return;
                case BaseSymbols.SEMICOLON:
                    if (token.isInferred()) {
                        return;
                    }
                    break;
                case Tokens.LETTER_IDENTIFIER:
                    final Name name = token.nameValue();
                    if (name == Names.get) {
                        this.configureMethod(this.property.initGetter(), token);
                        this.mode = SEPARATOR;
                        this.target = GETTER;
                        return;
                    }
                    if (name == Names.set) {
                        this.configureMethod(this.property.initSetter(), token);
                        this.mode = SETTER_PARAMETER;
                        this.target = SETTER;
                        return;
                    }
                    pm.report(token, "property.tag.unknown");
                    return;
                case DyvilKeywords.INIT:
                    this.property.setInitializerPosition(token.raw());
                    this.mode = SEPARATOR;
                    this.target = INITIALIZER;
                    return;
                case DyvilSymbols.AT:
                    this.parseAnnotation(pm, token);
                    return;
            }
            if (this.parseModifier(pm, token)) {
                return;
            }
            pm.report(token, "property.tag");
            return;
        case SETTER_PARAMETER:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                this.mode = SETTER_PARAMETER_NAME;
                return;
            }
        // Fallthrough
        case SEPARATOR:
            switch(type) {
                case BaseSymbols.COLON:
                    pm.pushParser(new ExpressionParser(this));
                    return;
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    pm.pushParser(new StatementListParser(this), true);
                    return;
                case BaseSymbols.CLOSE_CURLY_BRACKET:
                    pm.popParser();
                    return;
                case BaseSymbols.SEMICOLON:
                    this.mode = TAG;
                    return;
            }
            pm.report(token, "property.separator");
            return;
        case SETTER_PARAMETER_NAME:
            this.mode = SETTER_PARAMETER_END;
            if (Tokens.isIdentifier(type)) {
                this.property.setSetterParameterName(token.nameValue());
            } else {
                pm.report(token, "property.setter.identifier");
            }
            return;
        case SETTER_PARAMETER_END:
            this.mode = SEPARATOR;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "property.setter.close_paren");
            }
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) Name(dyvil.lang.Name)

Example 7 with ExpressionParser

use of dyvilx.tools.compiler.parser.expression.ExpressionParser in project Dyvil by Dyvil.

the class WhileStatementParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    int type = token.type();
    switch(this.mode) {
        case WHILE:
            this.mode = CONDITION;
            if (type == DyvilKeywords.SYNCHRONIZED) {
                return;
            }
            pm.report(token, "while.while_keyword");
        // Fallthrough
        case CONDITION:
            pm.pushParser(new ExpressionParser(this).withFlags(IGNORE_STATEMENT), true);
            this.mode = SEPARATOR;
            return;
        case SEPARATOR:
            switch(type) {
                case Tokens.EOF:
                    pm.popParser();
                    return;
                case BaseSymbols.SEMICOLON:
                    pm.popParser(true);
                    return;
            }
            this.mode = END;
            pm.pushParser(new ExpressionParser(this), true);
            // pm.report(token, "while.separator");
            return;
        case BLOCK:
            if (BaseSymbols.isTerminator(type) && !token.isInferred()) {
                pm.popParser(true);
                return;
            }
            pm.pushParser(new ExpressionParser(this), true);
            this.mode = END;
            return;
        case END:
            pm.popParser(true);
    }
}
Also used : ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Example 8 with ExpressionParser

use of dyvilx.tools.compiler.parser.expression.ExpressionParser in project Dyvil by Dyvil.

the class ConstructorParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            switch(type) {
                case DyvilSymbols.AT:
                    this.parseAnnotation(pm, token);
                    return;
                case DyvilKeywords.INIT:
                    this.member = this.consumer.createConstructor(token.raw(), this.attributes);
                    this.mode = PARAMETERS;
                    return;
            }
            if (this.parseModifier(pm, token)) {
                return;
            }
        // Fallthrough
        case PARAMETERS:
            this.mode = PARAMETERS_END;
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ParameterListParser(this.member));
                return;
            }
            pm.reparse();
            pm.report(token, "constructor.parameters.open_paren");
            return;
        case PARAMETERS_END:
            this.mode = INITIALIZER;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "constructor.parameters.close_paren");
            }
            return;
        case INITIALIZER:
            if (type == BaseSymbols.COLON) {
                this.mode = INIT_TYPE;
                return;
            }
        // Fallthrough
        case EXCEPTIONS:
            if (type == DyvilKeywords.THROWS) {
                pm.pushParser(new TypeListParser(this.member.getExceptions()));
                this.mode = BODY;
                return;
            }
        // Fallthrough
        case BODY:
            switch(type) {
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    pm.pushParser(new StatementListParser(this.member), true);
                    this.mode = END;
                    return;
                case BaseSymbols.EQUALS:
                    pm.pushParser(new ExpressionParser(this.member));
                    this.mode = END;
                    return;
            }
        // Fallthrough
        case END:
            this.consumer.addConstructor(this.member);
            pm.popParser(type != Tokens.EOF);
            return;
        case INIT_TYPE:
            boolean isSuper = false;
            switch(type) {
                case DyvilKeywords.SUPER:
                    isSuper = true;
                // Fallthrough
                case DyvilKeywords.THIS:
                    final InitializerCall init = new InitializerCall(token.raw(), isSuper);
                    this.member.setInitializer(init);
                    this.mode = INIT_ARGUMENTS;
                    return;
            }
            pm.report(token, "initializer.call.type");
            return;
        case INIT_ARGUMENTS:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ArgumentListParser(this.member.getInitializer()));
                this.mode = INIT_END;
                return;
            }
            pm.report(token, "initializer.call.open_paren");
            this.mode = EXCEPTIONS;
            return;
        case INIT_END:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "initializer.call.close_paren");
                return;
            }
            this.mode = EXCEPTIONS;
            return;
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) TypeListParser(dyvilx.tools.compiler.parser.type.TypeListParser) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) ArgumentListParser(dyvilx.tools.compiler.parser.expression.ArgumentListParser) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall)

Example 9 with ExpressionParser

use of dyvilx.tools.compiler.parser.expression.ExpressionParser in project Dyvil by Dyvil.

the class CaseParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case CASE:
            if (type == DyvilKeywords.CASE) {
                this.matchCase = new MatchCase();
                this.mode = CONDITION;
                pm.pushParser(new PatternParser(this.matchCase));
                return;
            }
            if (BaseSymbols.isTerminator(type)) {
                pm.popParser(true);
                return;
            }
            pm.report(token, "match.case");
            return;
        case CONDITION:
            if (type == DyvilKeywords.IF) {
                this.mode = ACTION;
                pm.pushParser(new ExpressionParser(this).withFlags(IGNORE_COLON | IGNORE_CLOSURE | IGNORE_LAMBDA));
                return;
            }
        // Fallthrough
        case ACTION:
            this.mode = END;
            if (type == BaseSymbols.OPEN_CURLY_BRACKET) {
                pm.pushParser(new StatementListParser(this), true);
                return;
            }
            pm.pushParser(new ExpressionParser(this));
            if (type != BaseSymbols.COLON && type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.reparse();
                pm.report(token, "match.case.action");
            }
            return;
        case END:
            pm.popParser(true);
            this.caseConsumer.addCase(this.matchCase);
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) MatchCase(dyvilx.tools.compiler.ast.expression.MatchCase) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Example 10 with ExpressionParser

use of dyvilx.tools.compiler.parser.expression.ExpressionParser in project Dyvil by Dyvil.

the class StatementListParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    if (type == BaseSymbols.CLOSE_CURLY_BRACKET) {
        this.end(pm);
        return;
    }
    if (type == Tokens.EOF) {
        this.end(pm);
        pm.report(token, "statement_list.close_brace");
        return;
    }
    switch(this.mode) {
        case OPEN_BRACKET:
            {
                final IToken next = token.next();
                final IToken lambdaArrow = this.findLambdaArrow(next);
                if (lambdaArrow != null) {
                    this.lambdaExpr = new LambdaExpr(lambdaArrow.raw());
                    this.lambdaExpr.setValue(this.statementList = new StatementList(token));
                    if (next == lambdaArrow) {
                        // { ->
                        // { =>
                        this.mode = LAMBDA_TYPE_ARROW;
                        return;
                    }
                    if (next.type() == BaseSymbols.OPEN_PARENTHESIS) {
                        // { ( ... ) =>
                        // { ( ... ) ->
                        pm.skip();
                        pm.pushParser(new ParameterListParser(this.lambdaExpr));
                        this.mode = LAMBDA_PARAMETERS_END;
                        return;
                    }
                    // { ... ->
                    // { ... =>
                    pm.pushParser(new ParameterListParser(this.lambdaExpr).withFlags(LAMBDA_ARROW_END));
                    this.mode = LAMBDA_TYPE_ARROW;
                    return;
                }
                // { ...
                this.statementList = this.closure ? new Closure(token) : new StatementList(token);
                this.mode = EXPRESSION;
                if (type != BaseSymbols.OPEN_CURLY_BRACKET) {
                    pm.report(token, "statement_list.open_brace");
                    pm.reparse();
                }
                return;
            }
        case LAMBDA_PARAMETERS_END:
            this.mode = LAMBDA_TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "statement_list.lambda.close_paren");
            }
            return;
        case LAMBDA_TYPE_ARROW:
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(LambdaOrTupleParser.returnTypeParser(this.lambdaExpr));
                this.mode = LAMBDA_RETURN_ARROW;
                return;
            }
        // Fallthrough
        case LAMBDA_RETURN_ARROW:
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.report(token, "statement_list.lambda.arrow");
                return;
            }
            this.mode = EXPRESSION;
            return;
        case EXPRESSION:
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
                case DyvilKeywords.LABEL:
                    this.mode = LABEL_NAME;
                    return;
            }
            this.mode = SEPARATOR;
            final MemberParser<IVariable> parser = new MemberParser<>(this).withFlags(MemberParser.NO_FIELD_PROPERTIES);
            if (this.tryParserManager.tryParse(pm, parser, token, EXIT_ON_ROOT)) {
                return;
            }
            pm.pushParser(new ExpressionParser(this));
            return;
        case LABEL_NAME:
            if (Tokens.isIdentifier(type)) {
                this.label = token.nameValue();
                this.mode = LABEL_END;
                return;
            }
            this.mode = EXPRESSION;
            if (type != BaseSymbols.COLON) {
                pm.reparse();
            }
            pm.report(token, "statement_list.label.name");
            return;
        case LABEL_END:
            switch(type) {
                case BaseSymbols.COLON:
                case BaseSymbols.SEMICOLON:
                    this.mode = EXPRESSION;
                    return;
            }
            this.mode = EXPRESSION;
            pm.reparse();
            pm.report(SourcePosition.between(token, token.next()), "statement_list.label.separator");
            return;
        case SEPARATOR:
            this.mode = EXPRESSION;
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
            }
            pm.report(token, "statement_list.semicolon");
    }
}
Also used : ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) Closure(dyvilx.tools.compiler.ast.statement.Closure) IToken(dyvilx.tools.parsing.token.IToken) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) IVariable(dyvilx.tools.compiler.ast.field.IVariable) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Aggregations

ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)20 TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)5 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)4 StatementListParser (dyvilx.tools.compiler.parser.statement.StatementListParser)4 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)3 Name (dyvil.lang.Name)2 TypeListParser (dyvilx.tools.compiler.parser.type.TypeListParser)2 IToken (dyvilx.tools.parsing.token.IToken)2 Modifiers (dyvil.reflect.Modifiers)1 LineSource (dyvil.source.LineSource)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 CodeAnnotation (dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation)1 Modifier (dyvilx.tools.compiler.ast.attribute.modifiers.Modifier)1 ITypeConsumer (dyvilx.tools.compiler.ast.consumer.ITypeConsumer)1 IContext (dyvilx.tools.compiler.ast.context.IContext)1 IValue (dyvilx.tools.compiler.ast.expression.IValue)1 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)1 MatchCase (dyvilx.tools.compiler.ast.expression.MatchCase)1 InitializerCall (dyvilx.tools.compiler.ast.expression.access.InitializerCall)1