Search in sources :

Example 1 with GraphFetchTreeLexerGrammar

use of org.finos.legend.engine.language.pure.grammar.from.antlr4.graphFetchTree.GraphFetchTreeLexerGrammar in project legend-engine by finos.

the class DomainParseTreeWalker method visitGraphFetchTree.

// TODO: add another island mode in M3 for this when we support path (which starts with #/)
public ListIterable<ValueSpecification> visitGraphFetchTree(DomainParserGrammar.DslGraphFetchContext ctx) {
    // NOTE: we want to preserve the spacing so we can correctly produce source information in the dispatched parser
    StringBuilder graphFetchStringBuilder = new StringBuilder();
    for (DomainParserGrammar.DslContentContext fragment : ctx.dslContent()) {
        graphFetchStringBuilder.append(fragment.getText());
    }
    String graphFetchString = graphFetchStringBuilder.length() > 0 ? graphFetchStringBuilder.substring(0, graphFetchStringBuilder.length() - 2) : graphFetchStringBuilder.toString();
    if (graphFetchString.isEmpty()) {
        throw new EngineException("Graph fetch tree must not be empty", walkerSourceInformation.getSourceInformation(ctx), EngineErrorType.PARSER);
    }
    // prepare island grammar walker source information
    int startLine = ctx.ISLAND_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.ISLAND_OPEN().getSymbol().getCharPositionInLine() + ctx.ISLAND_OPEN().getText().length();
    ParseTreeWalkerSourceInformation graphFetchWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(this.walkerSourceInformation.getSourceId(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
    ParserErrorListener errorListener = new ParserErrorListener(graphFetchWalkerSourceInformation);
    GraphFetchTreeLexerGrammar graphLexer = new GraphFetchTreeLexerGrammar(CharStreams.fromString(graphFetchString));
    graphLexer.removeErrorListeners();
    graphLexer.addErrorListener(errorListener);
    GraphFetchTreeParserGrammar graphParser = new GraphFetchTreeParserGrammar(new CommonTokenStream(graphLexer));
    graphParser.removeErrorListeners();
    graphParser.addErrorListener(errorListener);
    graphParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    return Lists.mutable.with(new GraphFetchTreeParseTreeWalker(graphFetchWalkerSourceInformation).visitDefinition(graphParser.definition()));
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) GraphFetchTreeParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.graphFetchTree.GraphFetchTreeParserGrammar) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) GraphFetchTreeLexerGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.graphFetchTree.GraphFetchTreeLexerGrammar) CString(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString) Constraint(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint) ParserErrorListener(org.finos.legend.engine.language.pure.grammar.from.ParserErrorListener) DomainParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTreeWalkerSourceInformation (org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)1 ParserErrorListener (org.finos.legend.engine.language.pure.grammar.from.ParserErrorListener)1 DomainParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar)1 GraphFetchTreeLexerGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.graphFetchTree.GraphFetchTreeLexerGrammar)1 GraphFetchTreeParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.graphFetchTree.GraphFetchTreeParserGrammar)1 Constraint (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint)1 CString (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1