use of org.finos.legend.engine.language.pure.grammar.from.extension.SectionParser in project legend-engine by finos.
the class PureGrammarParser method visitSection.
private Section visitSection(CodeParserGrammar.SectionContext ctx, DEPRECATED_PureGrammarParserLibrary parserLibrary, ParseTreeWalkerSourceInformation walkerSourceInformation, PureGrammarParserContext parserContext, Consumer<PackageableElement> elementConsumer, boolean returnSourceInfo) {
// the prefix is `\n###` hence 4 characters
String parserName = ctx.SECTION_START().getText().substring(4);
SourceInformation parserNameSourceInformation = walkerSourceInformation.getSourceInformation(ctx.SECTION_START().getSymbol());
// since the CODE_BLOCK_START is `\n###` we have to subtract 1 more line than usual
int lineOffset = ctx.SECTION_START().getSymbol().getLine() - 2;
ParseTreeWalkerSourceInformation sectionWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder("", lineOffset, 0).withReturnSourceInfo(returnSourceInfo).build();
SourceInformation sectionSourceInformation = walkerSourceInformation.getSourceInformation(ctx);
if (ctx.sectionContent() != null) {
try {
StringBuilder codeBuilder = new StringBuilder();
for (CodeParserGrammar.SectionContentContext tn : ctx.sectionContent()) {
codeBuilder.append(tn.getText());
}
SectionSourceCode codeSection = new SectionSourceCode(codeBuilder.toString(), parserName, sectionSourceInformation, sectionWalkerSourceInformation);
SectionParser sectionParser = this.extensions.getExtraSectionParser(parserName);
Section section;
if (sectionParser == null) {
DEPRECATED_SectionGrammarParser legacyParser = parserLibrary.getParser(parserName, parserNameSourceInformation);
if (legacyParser == null) {
throw new EngineException("'" + parserName + "' is not a known section parser", parserNameSourceInformation, EngineErrorType.PARSER);
}
section = legacyParser.parse(legacyParser.getParserInfo(codeSection.code, codeSection.sourceInformation, codeSection.walkerSourceInformation), elementConsumer, parserContext);
} else {
section = sectionParser.parse(codeSection, elementConsumer, parserContext);
}
// remove duplicates in imports and content of the section
section.elements = ListIterate.distinct(section.elements);
if (section instanceof ImportAwareCodeSection) {
((ImportAwareCodeSection) section).imports = ListIterate.distinct(((ImportAwareCodeSection) section).imports);
}
return section;
} catch (RuntimeException e) {
EngineException engineException = EngineException.findException(e);
if (engineException != null && engineException.getSourceInformation() != null) {
throw engineException;
}
String message = e instanceof UnsupportedOperationException && (e.getMessage() == null || e.getMessage().isEmpty()) ? "Unsupported syntax" : e instanceof NullPointerException ? "An exception of type 'NullPointerException' occurred, please notify developer" : e.getMessage();
LOGGER.error(new LogInfo(null, LoggingEventType.GRAMMAR_PARSING_ERROR, message).toString(), e);
throw new EngineException(message, sectionSourceInformation, EngineErrorType.PARSER, e);
}
}
// create default section for empty section
Section section = new DefaultCodeSection();
section.parserName = parserName;
section.sourceInformation = sectionSourceInformation;
return section;
}
Aggregations