use of org.finos.legend.engine.language.pure.grammar.from.antlr4.CodeParserGrammar in project legend-engine by finos.
the class PureGrammarParser method parse.
private PureModelContextData parse(String code, DEPRECATED_PureGrammarParserLibrary parserLibrary, boolean returnSourceInfo) {
String fullCode = DEFAULT_SECTION_BEGIN + code;
PureGrammarParserContext parserContext = new PureGrammarParserContext(this.extensions);
ParseTreeWalkerSourceInformation walkerSourceInformation = ParseTreeWalkerSourceInformation.DEFAULT_WALKER_SOURCE_INFORMATION(returnSourceInfo);
// init the parser
ParserErrorListener errorListener = new ParserErrorListener(walkerSourceInformation);
CodeLexerGrammar lexer = new CodeLexerGrammar(CharStreams.fromString(fullCode));
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
CodeParserGrammar parser = new CodeParserGrammar(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
// create the PureModelContextData builder
PureModelContextData.Builder builder = new PureModelContextData.Builder();
// create the section index
SectionIndex sectionIndex = new SectionIndex();
// NOTE: we intentionally set section index name and package like this since we don't want to expose this feature yet to end user
// in the consumer, we should ensure this does not leak and gets persisted to SDLC or Services per se
sectionIndex.name = "SectionIndex";
sectionIndex._package = "__internal__";
sectionIndex.sections = ListIterate.collect(parser.definition().section(), sectionCtx -> this.visitSection(sectionCtx, parserLibrary, walkerSourceInformation, parserContext, builder::addElement, returnSourceInfo));
return builder.withElement(sectionIndex).build();
}
Aggregations