Search in sources :

Example 1 with DefaultCodeSection

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection in project legend-engine by finos.

the class CorePureGrammarParser method parseDataSection.

private static Section parseDataSection(SectionSourceCode sectionSourceCode, Consumer<PackageableElement> elementConsumer, PureGrammarParserContext pureGrammarParserContext) {
    SourceCodeParserInfo parserInfo = getDataParserInfo(sectionSourceCode);
    DefaultCodeSection section = new DefaultCodeSection();
    section.parserName = sectionSourceCode.sectionType;
    section.sourceInformation = parserInfo.sourceInformation;
    DataParseTreeWalker walker = new DataParseTreeWalker(parserInfo.walkerSourceInformation, elementConsumer, section, pureGrammarParserContext.getPureGrammarParserExtensions());
    walker.visit((DataParserGrammar.DefinitionContext) parserInfo.rootContext);
    return section;
}
Also used : DefaultCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection) DataParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.data.DataParserGrammar)

Example 2 with DefaultCodeSection

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection in project legend-engine by finos.

the class DataSpaceParserExtension method parseSection.

private static Section parseSection(SectionSourceCode sectionSourceCode, Consumer<PackageableElement> elementConsumer, PureGrammarParserContext pureGrammarParserContext) {
    SourceCodeParserInfo parserInfo = getDataSpaceParserInfo(sectionSourceCode);
    DefaultCodeSection section = new DefaultCodeSection();
    section.parserName = sectionSourceCode.sectionType;
    section.sourceInformation = parserInfo.sourceInformation;
    DataSpaceParseTreeWalker walker = new DataSpaceParseTreeWalker(parserInfo.walkerSourceInformation, elementConsumer, section);
    walker.visit((DataSpaceParserGrammar.DefinitionContext) parserInfo.rootContext);
    return section;
}
Also used : SourceCodeParserInfo(org.finos.legend.engine.language.pure.grammar.from.SourceCodeParserInfo) DefaultCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection) DataSpaceParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.DataSpaceParserGrammar)

Example 3 with DefaultCodeSection

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection 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;
}
Also used : DefaultCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Section(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.Section) ImportAwareCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection) DefaultCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection) SourceInformation(org.finos.legend.engine.protocol.pure.v1.model.SourceInformation) CodeParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.CodeParserGrammar) ImportAwareCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection) SectionParser(org.finos.legend.engine.language.pure.grammar.from.extension.SectionParser)

Example 4 with DefaultCodeSection

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection in project legend-engine by finos.

the class TextParserExtension method parseSection.

private static Section parseSection(SectionSourceCode sectionSourceCode, Consumer<PackageableElement> elementConsumer, PureGrammarParserContext pureGrammarParserContext) {
    SourceCodeParserInfo parserInfo = getTextParserInfo(sectionSourceCode);
    DefaultCodeSection section = new DefaultCodeSection();
    section.parserName = sectionSourceCode.sectionType;
    section.sourceInformation = parserInfo.sourceInformation;
    TextParseTreeWalker walker = new TextParseTreeWalker(parserInfo.walkerSourceInformation, elementConsumer, section);
    walker.visit((TextParserGrammar.DefinitionContext) parserInfo.rootContext);
    return section;
}
Also used : SourceCodeParserInfo(org.finos.legend.engine.language.pure.grammar.from.SourceCodeParserInfo) DefaultCodeSection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection) TextParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.TextParserGrammar)

Aggregations

DefaultCodeSection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.DefaultCodeSection)4 SourceCodeParserInfo (org.finos.legend.engine.language.pure.grammar.from.SourceCodeParserInfo)2 CodeParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.CodeParserGrammar)1 DataSpaceParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.DataSpaceParserGrammar)1 TextParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.TextParserGrammar)1 DataParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.data.DataParserGrammar)1 SectionParser (org.finos.legend.engine.language.pure.grammar.from.extension.SectionParser)1 SourceInformation (org.finos.legend.engine.protocol.pure.v1.model.SourceInformation)1 ImportAwareCodeSection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection)1 Section (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.Section)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1 LogInfo (org.finos.legend.engine.shared.core.operational.logs.LogInfo)1