Search in sources :

Example 1 with PureGrammarParserExtensions

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

the class HelperEmbeddedDataGrammarParser method parseEmbeddedData.

/**
 * Helper for parsing embedded data values.  To use your grammar should include the definitions from
 * DataParserGrammar.g4:
 *
 * <pre>
 *     embeddedData:               identifier ISLAND_OPEN (embeddedDataContent)*
 *     ;
 *     embeddedDataContent:        ISLAND_START | ISLAND_BRACE_OPEN | ISLAND_CONTENT | ISLAND_HASH | ISLAND_BRACE_CLOSE | ISLAND_END
 *     ;
 * </pre>
 * <p>
 * You can then call by passing the embeddedDataContext and the ParseTreeWalkerSourceInformation from your context:
 *
 * <pre>
 *     EmbeddedDataParser.parseEmbeddedData(ctx.embeddedData(), walkerSourceInformation)
 * </pre>
 */
public static EmbeddedData parseEmbeddedData(ParserRuleContext embeddedDataContext, ParseTreeWalkerSourceInformation parentSourceInformation, PureGrammarParserExtensions extensions) {
    List<ParseTree> children = embeddedDataContext.children;
    if (children.size() < 3 || !children.get(0).getClass().getSimpleName().equals("IdentifierContext") || !(children.get(1) instanceof TerminalNode)) {
        throw new IllegalStateException("Unrecognized embedded data pattern");
    }
    ParserRuleContext identifierContext = (ParserRuleContext) children.get(0);
    TerminalNode islandOpen = (TerminalNode) children.get(1);
    List<ParseTree> content = children.subList(2, children.size());
    if (!content.stream().allMatch(ch -> ch.getClass().getSimpleName().equals("EmbeddedDataContentContext"))) {
        throw new IllegalStateException("Unrecognized embedded data pattern");
    }
    String dataType = PureGrammarParserUtility.fromIdentifier(identifierContext);
    EmbeddedDataParser parser = extensions.getExtraEmbeddedDataParser(dataType);
    if (parser == null) {
        throw new EngineException("Unknown embedded data type: " + dataType, parentSourceInformation.getSourceInformation(identifierContext), EngineErrorType.PARSER);
    }
    StringBuilder builder = new StringBuilder();
    content.forEach(cc -> builder.append(cc.getText()));
    builder.setLength(Math.max(0, builder.length() - 2));
    String text = builder.toString();
    // prepare island grammar walker source information
    int startLine = islandOpen.getSymbol().getLine();
    int lineOffset = parentSourceInformation.getLineOffset() + startLine - 1;
    // only add current walker source information column offset if this is the first line
    int columnOffset = (startLine == 1 ? parentSourceInformation.getColumnOffset() : 0) + islandOpen.getSymbol().getCharPositionInLine() + islandOpen.getSymbol().getText().length();
    ParseTreeWalkerSourceInformation walkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(parentSourceInformation).withLineOffset(lineOffset).withColumnOffset(columnOffset).build();
    SourceInformation sourceInformation = parentSourceInformation.getSourceInformation(embeddedDataContext);
    if (text.isEmpty()) {
        throw new EngineException("Embedded data must not be empty", sourceInformation, EngineErrorType.PARSER);
    }
    return parser.parse(text, walkerSourceInformation, sourceInformation);
}
Also used : List(java.util.List) EmbeddedDataParser(org.finos.legend.engine.language.pure.grammar.from.extension.EmbeddedDataParser) EmbeddedData(org.finos.legend.engine.protocol.pure.v1.model.data.EmbeddedData) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) ParseTree(org.antlr.v4.runtime.tree.ParseTree) PureGrammarParserExtensions(org.finos.legend.engine.language.pure.grammar.from.extension.PureGrammarParserExtensions) PureGrammarParserUtility(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserUtility) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) SourceInformation(org.finos.legend.engine.protocol.pure.v1.model.SourceInformation) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation) SourceInformation(org.finos.legend.engine.protocol.pure.v1.model.SourceInformation) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) EmbeddedDataParser(org.finos.legend.engine.language.pure.grammar.from.extension.EmbeddedDataParser) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalkerSourceInformation(org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)

Aggregations

List (java.util.List)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 ParseTreeWalkerSourceInformation (org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)1 PureGrammarParserUtility (org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserUtility)1 EmbeddedDataParser (org.finos.legend.engine.language.pure.grammar.from.extension.EmbeddedDataParser)1 PureGrammarParserExtensions (org.finos.legend.engine.language.pure.grammar.from.extension.PureGrammarParserExtensions)1 SourceInformation (org.finos.legend.engine.protocol.pure.v1.model.SourceInformation)1 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)1 EmbeddedData (org.finos.legend.engine.protocol.pure.v1.model.data.EmbeddedData)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1