Search in sources :

Example 1 with Parser

use of org.finos.legend.pure.m3.serialization.grammar.Parser in project legend-pure by finos.

the class NavigationParser method initAntlrParser.

private org.finos.legend.pure.m3.inlinedsl.path.serialization.grammar.NavigationParser initAntlrParser(boolean fastParser, String code, String sourceName, int offsetLine, int offsetColumn, boolean addLines) {
    this.sourceInformation = new AntlrSourceInformation(offsetLine, offsetColumn, sourceName, addLines);
    AntlrDescriptiveErrorListener pureErrorListener = new AntlrDescriptiveErrorListener(this.sourceInformation);
    NavigationLexer lexer = new NavigationLexer(new ANTLRInputStream(code));
    lexer.removeErrorListeners();
    lexer.addErrorListener(pureErrorListener);
    org.finos.legend.pure.m3.inlinedsl.path.serialization.grammar.NavigationParser parser = new org.finos.legend.pure.m3.inlinedsl.path.serialization.grammar.NavigationParser(new CommonTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(pureErrorListener);
    parser.setErrorHandler(new PureAntlrErrorStrategy(this.sourceInformation));
    parser.getInterpreter().setPredictionMode(fastParser ? PredictionMode.SLL : PredictionMode.LL);
    return parser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AntlrDescriptiveErrorListener(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrDescriptiveErrorListener) PureAntlrErrorStrategy(org.finos.legend.pure.m4.serialization.grammar.antlr.PureAntlrErrorStrategy) NavigationLexer(org.finos.legend.pure.m3.inlinedsl.path.serialization.grammar.NavigationLexer) AntlrSourceInformation(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 2 with Parser

use of org.finos.legend.pure.m3.serialization.grammar.Parser in project legend-pure by finos.

the class TopParser method initAntlrParser.

private TopAntlrParser initAntlrParser(boolean fastParser, String code, String sourceName) {
    AntlrSourceInformation sourceInformation = new AntlrSourceInformation(0, 0, sourceName);
    AntlrDescriptiveErrorListener pureErrorListener = new AntlrDescriptiveErrorListener(sourceInformation);
    TopAntlrLexer lexer = new TopAntlrLexer(new ANTLRInputStream(code));
    lexer.removeErrorListeners();
    lexer.addErrorListener(pureErrorListener);
    TopAntlrParser parser = new TopAntlrParser(new CommonTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(pureErrorListener);
    parser.setErrorHandler(new PureAntlrErrorStrategy(sourceInformation));
    parser.getInterpreter().setPredictionMode(fastParser ? PredictionMode.SLL : PredictionMode.LL);
    return parser;
}
Also used : TopAntlrParser(org.finos.legend.pure.m3.serialization.grammar.top.antlr.TopAntlrParser) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AntlrDescriptiveErrorListener(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrDescriptiveErrorListener) TopAntlrLexer(org.finos.legend.pure.m3.serialization.grammar.top.antlr.TopAntlrLexer) PureAntlrErrorStrategy(org.finos.legend.pure.m4.serialization.grammar.antlr.PureAntlrErrorStrategy) AntlrSourceInformation(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 3 with Parser

use of org.finos.legend.pure.m3.serialization.grammar.Parser in project legend-pure by finos.

the class BinarySourceSerializer method buildSource.

private static Source buildSource(Reader reader, IntObjectMap<CoreInstance> instancesById, String[] parserNames, ParserLibrary library, Context context) {
    // Read id
    String id = reader.readString();
    try {
        // Read immutable and compiled
        boolean immutable = reader.readBoolean();
        boolean compiled = reader.readBoolean();
        // Read content
        String content = reader.readString();
        // Read elements by parser
        MutableListMultimap<Parser, CoreInstance> elementsByParser;
        int parserCount = reader.readInt();
        if (parserCount == -1) {
            elementsByParser = null;
        } else {
            elementsByParser = Multimaps.mutable.list.empty();
            for (int i = 0; i < parserCount; i++) {
                String parserName = parserNames[reader.readInt()];
                Parser parser = library.getParser(parserName);
                if (parser == null) {
                    throw new RuntimeException("Unknown parser: " + parserName);
                }
                elementsByParser.putAll(parser, readCoreInstancesById(reader, instancesById));
            }
        }
        // Build the source
        Source source = new Source(id, immutable, false, content);
        source.setCompiled(compiled);
        if (elementsByParser != null) {
            source.linkInstances(elementsByParser);
            context.registerInstancesByClassifier(source.getNewInstances());
            context.registerFunctionsByName(source.getNewInstances());
        }
        return source;
    } catch (Exception e) {
        throw new RuntimeException("Error building source " + id, e);
    }
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Parser(org.finos.legend.pure.m3.serialization.grammar.Parser)

Example 4 with Parser

use of org.finos.legend.pure.m3.serialization.grammar.Parser in project legend-pure by finos.

the class GraphLoader method updateSourceRegistry.

private void updateSourceRegistry(RichIterable<SourceDeserializationResult> results, RichIterable<DeserializationNode> nodes, Message message) {
    if (message != null) {
        message.setMessage("    Updating source registry ...");
    }
    // Index top level and packaged instances by path
    final MutableMap<String, CoreInstance> instancesByPath = Maps.mutable.empty();
    for (DeserializationNode node : nodes) {
        if (node.isTopLevel() || node.isPackaged()) {
            CoreInstance instance = node.getInstance();
            String path = PackageableElement.getUserPathForPackageableElement(instance);
            instancesByPath.put(path, instance);
        }
    }
    // Update source registry
    for (SourceDeserializationResult result : results) {
        final MutableListMultimap<Parser, CoreInstance> instancesByParser = Multimaps.mutable.list.empty();
        result.getInstancesByParser().forEachKeyMultiValues((parserName, instancePaths) -> {
            Parser parser = this.parserLibrary.getParser(parserName);
            if (parser == null) {
                throw new RuntimeException("Could not find parser: " + parserName);
            }
            for (String instancePath : instancePaths) {
                CoreInstance instance = instancesByPath.get(instancePath);
                if (instance == null) {
                    throw new RuntimeException("Could not find instance: " + instancePath);
                }
                instancesByParser.put(parser, instance);
            }
        });
        Source source = result.getSource();
        source.linkInstances(instancesByParser);
        source.setCompiled(true);
        this.sourceRegistry.registerSource(result.getSource());
    }
}
Also used : SourceDeserializationResult(org.finos.legend.pure.m3.serialization.runtime.binary.SourceDeserializationResult) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) DeserializationNode(org.finos.legend.pure.m3.serialization.runtime.binary.DeserializationNode) Parser(org.finos.legend.pure.m3.serialization.grammar.Parser)

Example 5 with Parser

use of org.finos.legend.pure.m3.serialization.grammar.Parser in project legend-pure by finos.

the class M3AntlrParser method parseMappingInfo.

public TemporaryPureSetImplementation parseMappingInfo(String content, String classPath, AntlrContextToM3CoreInstance.LambdaContext lambdaContext, String sourceName, int offset, String importId, ModelRepository repository, ProcessorSupport processorSupport, final Context context) {
    AntlrSourceInformation sourceInformation = new AntlrSourceInformation(offset, 0, sourceName, true);
    org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser parser = initAntlrParser(true, content, sourceInformation);
    ImportGroup grp = (ImportGroup) processorSupport.package_getByUserPath("system::imports::" + importId);
    M3AntlrTreeWalker visitor = new M3AntlrTreeWalker(classPath, sourceInformation, this.inlineDSLLibrary, repository, null, null, context, grp, 0, null);
    return visitor.walkMapping(parser.mapping(), lambdaContext);
}
Also used : AntlrSourceInformation(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation) ImportGroup(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup)

Aggregations

Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)28 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)16 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)13 AntlrSourceInformation (org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation)8 M3AntlrParser (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser)7 InlineDSL (org.finos.legend.pure.m3.serialization.grammar.m3parser.inlinedsl.InlineDSL)7 PureException (org.finos.legend.pure.m4.exception.PureException)7 Matcher (org.finos.legend.pure.m3.tools.matcher.Matcher)6 RichIterable (org.eclipse.collections.api.RichIterable)5 MutableList (org.eclipse.collections.api.list.MutableList)5 TopParser (org.finos.legend.pure.m3.serialization.grammar.top.TopParser)5 Test (org.junit.Test)5 WalkerState (org.finos.legend.pure.m3.compiler.unload.walk.WalkerState)4 M3ProcessorSupport (org.finos.legend.pure.m3.navigation.M3ProcessorSupport)4 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)4 Lists (org.eclipse.collections.api.factory.Lists)3 Sets (org.eclipse.collections.api.factory.Sets)3 ListIterable (org.eclipse.collections.api.list.ListIterable)3 CoreInstanceFactoryRegistry (org.finos.legend.pure.m3.coreinstance.CoreInstanceFactoryRegistry)3 ImportGroup (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup)3