Search in sources :

Example 1 with TupleLikeExpr

use of dyvilx.tools.compiler.ast.expression.TupleLikeExpr in project Dyvil by Dyvil.

the class LambdaOrTupleParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case OPEN_PARENTHESIS:
            // ( ...
            // ^
            /*
			 * The new version of this parser tries to find the matching closing parenthesis instead of try-parsing the
			 * expression. If it finds that parenthesis token and the next token is a lambda arrow, we can assume that
			 * the expression is a lambda expression. Thus, we directly push a Parameter List Parser that may also
			 * produce syntax errors.
			 */
            final IToken closeParen = BracketMatcher.findMatch(token);
            if (closeParen != null) {
                final IToken next = closeParen.next();
                final int nextType = next.type();
                if (nextType == DyvilSymbols.ARROW_RIGHT || nextType == DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                    // (     ... )          =>
                    // (     ... )          ->
                    // token     closeParen next
                    final LambdaExpr lambdaExpr = new LambdaExpr(next);
                    pm.pushParser(new ParameterListParser(lambdaExpr));
                    this.value = lambdaExpr;
                    this.mode = PARAMETERS_END;
                    return;
                }
            }
        // Fallthrough
        case TUPLE:
            // ( ... )
            final TupleLikeExpr tupleExpr = new TupleLikeExpr(token);
            pm.pushParser(new ArgumentListParser(tupleExpr));
            this.value = tupleExpr;
            this.mode = TUPLE_END;
            return;
        case TUPLE_END:
            this.value.expandPosition(token);
            this.consumer.setValue(this.value);
            pm.popParser();
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "tuple.close_paren");
            }
            return;
        case PARAMETERS_END:
            this.mode = TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "lambda.close_paren");
            }
            return;
        case SINGLE_PARAMETER:
            if (Tokens.isIdentifier(type)) {
                final LambdaExpr lambdaExpr = new LambdaExpr(token.next());
                final IParameter parameter = lambdaExpr.createParameter(token.raw(), token.nameValue(), Types.UNKNOWN, new AttributeList());
                lambdaExpr.getParameters().add(parameter);
                this.value = lambdaExpr;
                this.mode = TYPE_ARROW;
                return;
            }
        // Fallthrough
        case TYPE_ARROW:
            if (this.value == null) {
                this.value = new LambdaExpr(token.raw());
            }
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(returnTypeParser((LambdaExpr) this.value));
                this.mode = RETURN_ARROW;
                return;
            }
        // Fallthrough
        case RETURN_ARROW:
            pm.pushParser(new ExpressionParser(((LambdaExpr) this.value)));
            this.mode = END;
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.reparse();
                pm.report(token, "lambda.arrow");
            }
            return;
        case END:
            pm.popParser(true);
            this.consumer.setValue(this.value);
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) TupleLikeExpr(dyvilx.tools.compiler.ast.expression.TupleLikeExpr) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) IToken(dyvilx.tools.parsing.token.IToken) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr)

Aggregations

AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)1 TupleLikeExpr (dyvilx.tools.compiler.ast.expression.TupleLikeExpr)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)1 IToken (dyvilx.tools.parsing.token.IToken)1