-
Notifications
You must be signed in to change notification settings - Fork 316
/
DetachedNodeModelReader.java
128 lines (110 loc) · 4.01 KB
/
DetachedNodeModelReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
* Copyright (c) 2024 Sebastian Zarnekow and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.nodemodel.detachable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl.EObjectInputStream;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.SyntaxErrorMessage;
import org.eclipse.xtext.nodemodel.impl.AbstractNode;
import org.eclipse.xtext.nodemodel.impl.CompositeNodeWithSemanticElement;
import org.eclipse.xtext.nodemodel.impl.NodeModelInput;
import org.eclipse.xtext.nodemodel.impl.RootNode;
/**
* @since 2.35
*/
public class DetachedNodeModelReader extends EObjectInputStream implements NodeModelInput {
private final List<ArrayReference> grammarElementArrays;
private final List<EObject> semanticObjects;
private final List<SyntaxErrorMessage> syntaxErrorMessages;
private GrammarElementLookup grammarElements;
private int offset;
private Tabulated<INode> externalReferences;
private Map<EObject, CompositeNodeWithSemanticElement> associations;
public DetachedNodeModelReader(NodeModelData nodeModelData) throws IOException {
super(new GZIPInputStream(new ByteArrayInputStream(nodeModelData.getBytes())), Collections.singletonMap(BinaryResourceImpl.OPTION_INTERNAL_BUFFER_CAPACITY, 8192));
this.syntaxErrorMessages = nodeModelData.getSyntaxErrors();
this.grammarElementArrays = nodeModelData.getGrammarElementArrays();
this.semanticObjects = nodeModelData.getSemanticObjects();
}
public StandardNodeModelReference deserialize(DetachableParseResult target) throws IOException {
externalReferences = new Tabulated<>(readCompressedInt());
associations = new HashMap<>();
this.grammarElements = target.getGrammarElementLookup();
RootNode rootNode = (RootNode) readNode();
return new StandardNodeModelReference(target, rootNode, associations, externalReferences);
}
@Override
public AbstractNode readNode(int type) throws IOException {
if (type == -1) {
return null;
}
boolean hasExternalId = (type & 1) == 1;
AbstractNode result = NodeModelInput.super.readNode(type >>> 1);
if (hasExternalId) {
externalReferences.set(result, readCompressedInt());
}
if (result instanceof CompositeNodeWithSemanticElement && result.hasDirectSemanticElement()) {
associations.put(result.getSemanticElement(), (CompositeNodeWithSemanticElement) result);
}
return result;
}
@Override
protected void readSignature() throws IOException {
// no signature
}
@Override
public int readLength() throws IOException {
int result = readCompressedInt();
offset += result;
return result;
}
@Override
public int currentOffset() {
return offset;
}
@Override
public Object readGrammarElement() throws IOException {
int id = readCompressedInt();
if (id == -1) {
return null;
}
if (id >= grammarElements.size()) {
return grammarElementArrays.get(id - grammarElements.size()).getArray();
}
return grammarElements.getGrammarElement(id);
}
@Override
public EObject readSemanticObject() throws IOException {
int id = readCompressedInt();
if (id == -1) {
return null;
}
return semanticObjects.get(id);
}
@Override
public SyntaxErrorMessage readSyntaxErrorMessage() throws IOException {
int id = readCompressedInt();
if (id == -1) {
return null;
}
return syntaxErrorMessages.get(id);
}
@Override
public String readContent() throws IOException {
return readSegmentedString();
}
}