summaryrefslogtreecommitdiff
path: root/yarp/templates/java/org/yarp/Loader.java.erb
blob: f78770508afeac8289604d5ddf2573801fe31492 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.yarp;

import org.yarp.ParseResult;

import java.lang.Short;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;

// GENERATED BY <%= File.basename(__FILE__) %>
// @formatter:off
public class Loader {

    public static ParseResult load(byte[] serialized, Nodes.Source source) {
        return new Loader(serialized, source).load();
    }

    private static final class ConstantPool {

        private final byte[] source;
        private final int bufferOffset;
        private final byte[][] cache;

        ConstantPool(byte[] source, int bufferOffset, int length) {
            this.source = source;
            this.bufferOffset = bufferOffset;
            cache = new byte[length][];
        }

        byte[] get(ByteBuffer buffer, int oneBasedIndex) {
            int index = oneBasedIndex - 1;
            byte[] constant = cache[index];
            if (constant == null) {
                int offset = bufferOffset + index * 8;
                int start = buffer.getInt(offset);
                int length = buffer.getInt(offset + 4);

                constant = new byte[length];
                System.arraycopy(source, start, constant, 0, length);
                cache[index] = constant;
            }
            return constant;
        }

    }

    private final ByteBuffer buffer;
    private ConstantPool constantPool;
    private final Nodes.Source source;

    private byte MAJOR_VERSION = (byte) 0;
    private byte MINOR_VERSION = (byte) 7;
    private byte PATCH_VERSION = (byte) 0;

    private Loader(byte[] serialized, Nodes.Source source) {
        this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
        this.source = source;
    }

    private ParseResult load() {
        expect((byte) 'Y');
        expect((byte) 'A');
        expect((byte) 'R');
        expect((byte) 'P');

        expect(MAJOR_VERSION);
        expect(MINOR_VERSION);
        expect(PATCH_VERSION);

        // This loads the name of the encoding. We don't actually do anything
        // with it just yet.
        int encodingLength = loadVarInt();
        byte[] encodingName = new byte[encodingLength];
        buffer.get(encodingName);

        ParseResult.Comment[] comments = loadComments();
        ParseResult.Error[] errors = loadSyntaxErrors();
        ParseResult.Warning[] warnings = loadWarnings();

        int constantPoolBufferOffset = buffer.getInt();
        int constantPoolLength = loadVarInt();
        this.constantPool = new ConstantPool(source.bytes, constantPoolBufferOffset, constantPoolLength);

        Nodes.Node node = loadNode();

        int left = constantPoolBufferOffset - buffer.position();
        if (left != 0) {
            throw new Error("Expected to consume all bytes while deserializing but there were " + left + " bytes left");
        }

        boolean[] newlineMarked = new boolean[1 + source.getLineCount()];
        MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source, newlineMarked);
        node.accept(visitor);

        return new ParseResult(node, comments, errors, warnings);
    }

    private byte[] loadString() {
        int length = loadVarInt();
        byte[] string = new byte[length];
        buffer.get(string);
        return string;
    }

    private ParseResult.Comment[] loadComments() {
        int count = loadVarInt();
        ParseResult.Comment[] comments = new ParseResult.Comment[count];

        for (int i = 0; i < count; i++) {
            ParseResult.CommentType type = ParseResult.CommentType.VALUES[buffer.get()];
            Nodes.Location location = loadLocation();

            ParseResult.Comment comment = new ParseResult.Comment(type, location);
            comments[i] = comment;
        }

        return comments;
    }

    private ParseResult.Error[] loadSyntaxErrors() {
        int count = loadVarInt();
        ParseResult.Error[] errors = new ParseResult.Error[count];

        // error messages only contain ASCII characters
        for (int i = 0; i < count; i++) {
            byte[] bytes = loadString();
            String message = new String(bytes, StandardCharsets.US_ASCII);
            Nodes.Location location = loadLocation();

            ParseResult.Error error = new ParseResult.Error(message, location);
            errors[i] = error;
        }

        return errors;
    }

    private ParseResult.Warning[] loadWarnings() {
        int count = loadVarInt();
        ParseResult.Warning[] warnings = new ParseResult.Warning[count];

        // warning messages only contain ASCII characters
        for (int i = 0; i < count; i++) {
            byte[] bytes = loadString();
            String message = new String(bytes, StandardCharsets.US_ASCII);
            Nodes.Location location = loadLocation();

            ParseResult.Warning warning = new ParseResult.Warning(message, location);
            warnings[i] = warning;
        }

        return warnings;
    }

    private Nodes.Node loadOptionalNode() {
        if (buffer.get(buffer.position()) != 0) {
            return loadNode();
        } else {
            buffer.position(buffer.position() + 1); // continue after the 0 byte
            return null;
        }
    }

    private Nodes.Location[] loadLocations() {
        int length = loadVarInt();
        if (length == 0) {
            return Nodes.Location.EMPTY_ARRAY;
        }
        Nodes.Location[] locations = new Nodes.Location[length];
        for (int i = 0; i < length; i++) {
            locations[i] = loadLocation();
        }
        return locations;
    }

    private byte[] loadConstant() {
        return constantPool.get(buffer, loadVarInt());
    }

    private byte[][] loadConstants() {
        int length = loadVarInt();
        if (length == 0) {
            return Nodes.EMPTY_BYTE_ARRAY_ARRAY;
        }
        byte[][] constants = new byte[length][];
        for (int i = 0; i < length; i++) {
            constants[i] = constantPool.get(buffer, loadVarInt());
        }
        return constants;
    }

    private Nodes.Node[] loadNodes() {
        int length = loadVarInt();
        if (length == 0) {
            return Nodes.Node.EMPTY_ARRAY;
        }
        Nodes.Node[] nodes = new Nodes.Node[length];
        for (int i = 0; i < length; i++) {
            nodes[i] = loadNode();
        }
        return nodes;
    }

    private Nodes.Location loadLocation() {
        return new Nodes.Location(loadVarInt(), loadVarInt());
    }

    private Nodes.Location loadOptionalLocation() {
        if (buffer.get() != 0) {
            return loadLocation();
        } else {
            return null;
        }
    }

    // From https://github.com/protocolbuffers/protobuf/blob/v23.1/java/core/src/main/java/com/google/protobuf/BinaryReader.java#L1507
    private int loadVarInt() {
        int x;
        if ((x = buffer.get()) >= 0) {
            return x;
        } else if ((x ^= (buffer.get() << 7)) < 0) {
            x ^= (~0 << 7);
        } else if ((x ^= (buffer.get() << 14)) >= 0) {
            x ^= (~0 << 7) ^ (~0 << 14);
        } else if ((x ^= (buffer.get() << 21)) < 0) {
            x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21);
        } else {
            x ^= buffer.get() << 28;
            x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28);
        }
        return x;
    }

    private short loadFlags() {
        int flags = loadVarInt();
        assert flags >= 0 && flags <= Short.MAX_VALUE;
        return (short) flags;
    }

    private Nodes.Node loadNode() {
        int type = buffer.get() & 0xFF;
        int startOffset = loadVarInt();
        int length = loadVarInt();

        switch (type) {
            <%- nodes.each_with_index do |node, index| -%>
            case <%= index + 1 %>:
            <%-
            params = node.needs_serialized_length? ? ["buffer.getInt()"] : []
            params.concat node.params.map { |param|
              case param
              when NodeParam then "#{param.java_cast}loadNode()"
              when OptionalNodeParam then "#{param.java_cast}loadOptionalNode()"
              when StringParam then "loadString()"
              when NodeListParam then "loadNodes()"
              when LocationListParam then "loadLocations()"
              when ConstantParam then "loadConstant()"
              when ConstantListParam then "loadConstants()"
              when LocationParam then "loadLocation()"
              when OptionalLocationParam then "loadOptionalLocation()"
              when UInt32Param then "loadVarInt()"
              when FlagsParam then "loadFlags()"
              else raise
              end
            }
            params.concat ["startOffset", "length"]
            -%>
                return new Nodes.<%= node.name %>(<%= params.join(", ") -%>);
            <%- end -%>
            default:
                throw new Error("Unknown node type: " + type);
        }
    }

    private void expect(byte value) {
        byte b = buffer.get();
        if (b != value) {
            throw new Error("Expected " + value + " but was " + b + " at position " + buffer.position());
        }
    }

}
// @formatter:on