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