Search in sources :

Example 1 with ExpressionContext

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method buildArithmeticWithListParam.

// Build arithmetic op which can handle list of params (like lisp eg (+ u v z y z))
private SimpleFunctionExpression buildArithmeticWithListParam(ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
    MutableList<CoreInstance> others = Lists.mutable.empty();
    Function0<List<TerminalNode>> getTokens;
    Function<Integer, TerminalNode> getToken;
    switch(op) {
        case PLUS:
            getTokens = ctx::PLUS;
            getToken = ctx::PLUS;
            break;
        case TIMES:
            getTokens = ctx::STAR;
            getToken = ctx::STAR;
            break;
        case MINUS:
            getTokens = ctx::MINUS;
            getToken = ctx::MINUS;
            break;
        default:
            throw new IllegalStateException("Unexpected arithmetic operation for buildArithmeticWithListParam: " + op);
    }
    String op_str = op.toString().toLowerCase();
    for (ExpressionContext eCtx : ctx.expression()) {
        others.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines));
    }
    TerminalNode newOp = getToken.apply(getTokens.value().size() - 1);
    SimpleFunctionExpression sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(newOp.getSymbol()), null, null, importId, null);
    sfe._functionName(op_str);
    sfe._parametersValues(Lists.mutable.<ValueSpecification>of(this.doWrap(Lists.mutable.with(initialValue).withAll(others))));
    return sfe;
}
Also used : NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) List(java.util.List) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 2 with ExpressionContext

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method buildArithmeticDivide.

// Handles divide, since dive is built up in a tree: eg x / y /z is div(x, div(y,z))
private SimpleFunctionExpression buildArithmeticDivide(ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
    MutableList<CoreInstance> others = Lists.mutable.empty();
    for (ExpressionContext eCtx : ctx.expression()) {
        others.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines));
    }
    SimpleFunctionExpression sfe = null;
    for (CoreInstance other : others) {
        sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.DIVIDE(ctx.DIVIDE().size() - 1).getSymbol()), null, null, importId, null);
        sfe._functionName("divide");
        sfe._parametersValues(Lists.mutable.of((ValueSpecification) initialValue, (ValueSpecification) other));
        initialValue = sfe;
    }
    return sfe;
}
Also used : NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)

Example 3 with ExpressionContext

use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method expression.

private CoreInstance expression(ExpressionContext ctx, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
    CoreInstance result = null;
    CoreInstance end = null;
    CoreInstance step = null;
    MutableList<CoreInstance> expressions = Lists.mutable.of();
    MutableList<ValueSpecification> parameters = Lists.mutable.empty();
    if (ctx.combinedExpression() != null) {
        return this.combinedExpression(ctx.combinedExpression(), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
    }
    if (ctx.atomicExpression() != null) {
        result = this.atomicExpression(ctx.atomicExpression(), typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
    } else if (ctx.notExpression() != null) {
        result = this.notExpression(ctx.notExpression(), exprName, typeParametersNames, lambdaContext, space, importId, addLines);
    } else if (ctx.signedExpression() != null) {
        result = this.signedExpression(ctx.signedExpression(), exprName, typeParametersNames, lambdaContext, space, importId, addLines);
    } else if (ctx.expressionsArray() != null) {
        for (ExpressionContext eCtx : ctx.expressionsArray().expression()) {
            expressions.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, false, importId, addLines));
        }
        result = this.doWrap(expressions, ctx.expressionsArray().getStart().getLine(), ctx.expressionsArray().getStart().getCharPositionInLine(), ctx.getStop().getLine(), ctx.getStop().getCharPositionInLine());
    } else {
        switch(ctx.sliceExpression().expression().size()) {
            case // :end
            1:
                {
                    end = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    break;
                }
            case // start:end
            2:
                {
                    result = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    expressions.add(result);
                    end = this.expression(ctx.sliceExpression().expression(1), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    break;
                }
            case // start:end:step
            3:
                {
                    result = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    expressions.add(result);
                    end = this.expression(ctx.sliceExpression().expression(1), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    step = this.expression(ctx.sliceExpression().expression(2), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
                    break;
                }
            default:
                {
                    // Not reachable. coded just for comment
                    break;
                }
        }
        MutableList<ValueSpecification> params = Lists.mutable.empty();
        if (result != null) {
            params.add(this.doWrap(Lists.mutable.of(result)));
        }
        params.add(this.doWrap(Lists.mutable.of(end)));
        if (step != null) {
            params.add(this.doWrap(Lists.mutable.of(step)));
        }
        SimpleFunctionExpressionInstance sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.getStart()), null, null, importId, null);
        sfe._functionName("range");
        sfe._parametersValues(params);
        result = sfe;
    }
    if (ctx.propertyOrFunctionExpression() != null) {
        for (PropertyOrFunctionExpressionContext pfCtx : ctx.propertyOrFunctionExpression()) {
            if (pfCtx.propertyExpression() != null) {
                result = propertyExpression(pfCtx.propertyExpression(), result, parameters, typeParametersNames, lambdaContext, space, importId);
            } else {
                for (int i = 0; i < pfCtx.functionExpression().qualifiedName().size(); i++) {
                    parameters = this.functionExpressionParameters(pfCtx.functionExpression().functionExpressionParameters(i), typeParametersNames, importId, lambdaContext, addLines, spacePlusTabs(space, 4));
                    parameters.add(0, (ValueSpecification) result);
                    result = this.functionExpression(pfCtx.functionExpression().qualifiedName(i), parameters, importId);
                }
            }
        }
    }
    if (ctx.equalNotEqual() != null) {
        result = this.equalNotEqual(ctx.equalNotEqual(), (ValueSpecification) result, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
    }
    return result;
}
Also used : MutableList(org.eclipse.collections.api.list.MutableList) SimpleFunctionExpressionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpressionInstance) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint)

Aggregations

AtomicExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext)3 BuildMilestoningVariableExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext)3 CombinedExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext)3 DefaultValueExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext)3 ExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext)3 FunctionVariableExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext)3 LetExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext)3 NotExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext)3 PropertyExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext)3 PropertyOrFunctionExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext)3 SignedExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)3 MutableList (org.eclipse.collections.api.list.MutableList)2 SimpleFunctionExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression)2 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)2 List (java.util.List)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 FastList (org.eclipse.collections.impl.list.mutable.FastList)1 Constraint (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint)1