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