Search in sources :

Example 1 with MappingElementParser

use of org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser 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 MappingElementParser

use of org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser 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 MappingElementParser

use of org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser 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 MappingElementParser

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

the class AggregationAwareMappingParseTreeWalker method visitMainMapping.

private void visitMainMapping(AggregationAwareParserGrammar.MainMappingContext ctx, AggregationAwareClassMapping mapping) {
    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 ? mapping.id + "_Main" : classMapping.id + "_Main";
        mapping.mainSetImplementation = classMapping;
        if (classMapping instanceof PureInstanceClassMapping) {
            mapping.propertyMappings = ListIterate.collect(((PureInstanceClassMapping) classMapping).propertyMappings, this::visitAggregationAwarePropertyMapping);
        }
    } 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) PureInstanceClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PureInstanceClassMapping) 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)

Aggregations

ParseTreeWalkerSourceInformation (org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)4 MappingElementParser (org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser)4 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)4 Interval (org.antlr.v4.runtime.misc.Interval)3 ClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping)3 PureInstanceClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PureInstanceClassMapping)2 MappingParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.mapping.MappingParserGrammar)1 AssociationMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping)1 EnumerationMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.EnumerationMapping)1