Search in sources :

Example 1 with ParseTreeWalkerSourceInformation

use of org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation in project legend-engine by finos.

the class AggregationAwareMappingParseTreeWalker method visitModelOperationContext.

private void visitModelOperationContext(AggregationAwareParserGrammar.ModelOperationContext ctx, AggregateSetImplementationContainer aggregateSetImplementationContainer) {
    int startLine = ctx.start.getLine();
    int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
    int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.BRACE_OPEN().getSymbol().getCharPositionInLine() + ctx.BRACE_OPEN().getText().length();
    MappingElementParser extraParser = this.parserContext.getPureGrammarParserExtensions().getExtraMappingElementParser("AggregateSpecification");
    String inputText = this.input.getText(new Interval(ctx.BRACE_OPEN().getSymbol().getStartIndex() + 1, ctx.MAPPING_ISLAND_BRACE_CLOSE().getSymbol().getStartIndex() - 1));
    ParseTreeWalkerSourceInformation sourceInformation = new ParseTreeWalkerSourceInformation.Builder(walkerSourceInformation.getSourceId(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
    MappingElementSourceCode sourceCode = new MappingElementSourceCode(inputText, "AggregateSpecification", sourceInformation, this.mappingElementSourceCode.mappingElementParserRuleContext, this.walkerSourceInformation);
    Object element = extraParser.parse(sourceCode, this.parserContext);
    if (element instanceof AggregateSpecification) {
        aggregateSetImplementationContainer.aggregateSpecification = (AggregateSpecification) element;
    } else {
        throw new EngineException("Invalid parser result for " + mappingElementSourceCode.name + ": " + element, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
    }
}
Also used : MappingElementParser(org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Interval(org.antlr.v4.runtime.misc.Interval) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Example 2 with ParseTreeWalkerSourceInformation

use of org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation in project legend-engine by finos.

the class AggregationAwareMappingParseTreeWalker method visitAggregateMappingContext.

private void visitAggregateMappingContext(AggregationAwareParserGrammar.AggregateMappingContext ctx, AggregateSetImplementationContainer aggregateSetImplementationContainer, AggregationAwareClassMapping parent) {
    String parserName = ctx.parserName().getText();
    int startLine = ctx.BRACE_OPEN().getSymbol().getLine();
    int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
    int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.BRACE_OPEN().getSymbol().getCharPositionInLine() + ctx.BRACE_OPEN().getText().length();
    MappingElementParser extraParser = this.parserContext.getPureGrammarParserExtensions().getExtraMappingElementParser(parserName);
    String mappingInput = this.input.getText(new Interval(ctx.BRACE_OPEN().getSymbol().getStartIndex() + 1, ctx.MAPPING_ISLAND_BRACE_CLOSE().getSymbol().getStartIndex() - 1));
    ParseTreeWalkerSourceInformation mappingElementWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(this.walkerSourceInformation.getSourceId(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
    MappingElementSourceCode mappingElementSourceCode = new MappingElementSourceCode(mappingInput, parserName, mappingElementWalkerSourceInformation, this.mappingElementSourceCode.mappingElementParserRuleContext, this.walkerSourceInformation);
    Object mappingElement = extraParser.parse(mappingElementSourceCode, this.parserContext);
    if (mappingElement instanceof ClassMapping) {
        ClassMapping classMapping = (ClassMapping) mappingElement;
        classMapping.id = classMapping.id == null ? parent.id + "_Aggregate_" + aggregateSetImplementationContainer.index : classMapping.id + "_Aggregate_" + aggregateSetImplementationContainer.index;
        aggregateSetImplementationContainer.setImplementation = classMapping;
    } else {
        throw new EngineException("Invalid parser result for " + mappingElementSourceCode.name + ": " + mappingElement, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
    }
}
Also used : ClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping) PureInstanceClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PureInstanceClassMapping) MappingElementParser(org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Interval(org.antlr.v4.runtime.misc.Interval) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Example 3 with ParseTreeWalkerSourceInformation

use of org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation in project legend-engine by finos.

the class MappingParseTreeWalker method visitMappingElement.

private Mapping visitMappingElement(MappingParserGrammar.MappingElementContext ctx, Mapping mapping) {
    String parserName = ctx.parserName().getText();
    // Construct the mapping element string (with spacing) to be dispatched to another parser
    // NOTE: we want to preserve the spacing so we can correctly produce source information in the dispatched parser
    StringBuilder mappingElementStringBuilder = new StringBuilder();
    for (MappingParserGrammar.MappingElementBodyContentContext fragment : ctx.mappingElementBody().mappingElementBodyContent()) {
        mappingElementStringBuilder.append(fragment.getText());
    }
    String mappingElementCode = mappingElementStringBuilder.length() > 0 ? mappingElementStringBuilder.substring(0, mappingElementStringBuilder.length() - 1) : mappingElementStringBuilder.toString();
    // prepare island grammar walker source information
    int startLine = ctx.mappingElementBody().BRACE_OPEN().getSymbol().getLine();
    int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
    // only add current walker source information column offset if this is the first line
    int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.mappingElementBody().BRACE_OPEN().getSymbol().getCharPositionInLine() + ctx.mappingElementBody().BRACE_OPEN().getText().length();
    ParseTreeWalkerSourceInformation mappingElementWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(mapping.getPath(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
    MappingElementSourceCode mappingElementSourceCode = new MappingElementSourceCode(mappingElementCode, parserName, mappingElementWalkerSourceInformation, ctx, this.walkerSourceInformation);
    MappingElementParser extraParser = this.extensions.getExtraMappingElementParser(mappingElementSourceCode.name);
    if (extraParser == null) {
        throw new EngineException("No parser for " + mappingElementSourceCode.name, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
    }
    Object mappingElement = extraParser.parse(mappingElementSourceCode, this.parserContext);
    if (mappingElement instanceof ClassMapping) {
        mapping.classMappings.add((ClassMapping) mappingElement);
    } else if (mappingElement instanceof EnumerationMapping) {
        mapping.enumerationMappings.add((EnumerationMapping) mappingElement);
    } else if (mappingElement instanceof AssociationMapping) {
        mapping.associationMappings.add((AssociationMapping) mappingElement);
    } else {
        throw new EngineException("Invalid parser result for " + mappingElementSourceCode.name + ": " + mappingElement, this.walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
    }
    return mapping;
}
Also used : ClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping) EnumerationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.EnumerationMapping) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) AssociationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping) MappingElementParser(org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser) MappingParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.mapping.MappingParserGrammar) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Example 4 with ParseTreeWalkerSourceInformation

use of org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation in project legend-engine by finos.

the class MappingParseTreeWalker method visitMappingTestQuery.

private Lambda visitMappingTestQuery(MappingParserGrammar.CombinedExpressionContext ctx, Mapping mapping) {
    DomainParser parser = new DomainParser();
    // prepare island grammar walker source information
    int startLine = ctx.getStart().getLine();
    int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
    // only add current walker source information column offset if this is the first line
    int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.getStart().getCharPositionInLine();
    ParseTreeWalkerSourceInformation combineExpressionWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(mapping.getPath(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
    String lambdaString = this.input.getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
    ValueSpecification valueSpecification = parser.parseCombinedExpression(lambdaString, combineExpressionWalkerSourceInformation, null);
    if (valueSpecification instanceof Lambda) {
        return (Lambda) valueSpecification;
    }
    // NOTE: If the user just provides the body of the lambda, we will wrap a lambda around it
    // we might want to reconsider this behavior and throw error if this convenience causes any trouble
    Lambda lambda = new Lambda();
    lambda.body = new ArrayList<>();
    lambda.body.add(valueSpecification);
    lambda.parameters = new ArrayList<>();
    return lambda;
}
Also used : DomainParser(org.finos.legend.engine.language.pure.grammar.from.domain.DomainParser) ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation) Interval(org.antlr.v4.runtime.misc.Interval)

Example 5 with ParseTreeWalkerSourceInformation

use of org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation in project legend-engine by finos.

the class DomainParser method parseValueSpecification.

public ValueSpecification parseValueSpecification(String input, String s, boolean returnSourceInfo) {
    PureGrammarParserContext parserContext = new PureGrammarParserContext(PureGrammarParserExtensions.fromExtensions(Lists.immutable.empty()));
    ParseTreeWalkerSourceInformation lambdaWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(s, 0, 0).withReturnSourceInfo(returnSourceInfo).build();
    String prefix = "function go():Any[*]{let x = ";
    String fullCode = prefix + input + ";}";
    ParseTreeWalkerSourceInformation walkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(lambdaWalkerSourceInformation).withColumnOffset(lambdaWalkerSourceInformation.getColumnOffset() - prefix.length()).build();
    SourceCodeParserInfo sectionParserInfo = this.getParserInfo(fullCode, null, walkerSourceInformation, true);
    DomainParseTreeWalker walker = new DomainParseTreeWalker(walkerSourceInformation, parserContext, (ImportAwareCodeSection) null);
    return (ValueSpecification) walker.combinedExpression(((DomainParserGrammar.DefinitionContext) sectionParserInfo.rootContext).elementDefinition(0).functionDefinition().codeBlock().programLine(0).letExpression().combinedExpression(), "", Lists.mutable.empty(), null, "", false, returnSourceInfo);
}
Also used : SourceCodeParserInfo(org.finos.legend.engine.language.pure.grammar.from.SourceCodeParserInfo) DomainParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar) PureGrammarParserContext(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserContext) ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Aggregations

ParseTreeWalkerSourceInformation (org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)21 Interval (org.antlr.v4.runtime.misc.Interval)9 ValueSpecification (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification)8 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)8 DomainParser (org.finos.legend.engine.language.pure.grammar.from.domain.DomainParser)7 Lambda (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda)7 SourceCodeParserInfo (org.finos.legend.engine.language.pure.grammar.from.SourceCodeParserInfo)4 MappingElementParser (org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser)4 DomainParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar)3 SourceInformation (org.finos.legend.engine.protocol.pure.v1.model.SourceInformation)3 ClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping)3 ArrayList (java.util.ArrayList)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 ParserErrorListener (org.finos.legend.engine.language.pure.grammar.from.ParserErrorListener)2 Constraint (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint)2 PureInstanceClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PureInstanceClassMapping)2 CString (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString)2 List (java.util.List)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1