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);
}
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations