1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Support for parsing binary sequences encoded as readable strings
6 // or ".data" files. The input format is described here:
7 // mojo/public/cpp/bindings/tests/validation_test_input_parser.h
10 "mojo/public/js/bindings/buffer"
13 // Files and Lines represent the raw text from an input string
16 function InputError(message, line) {
17 this.message = message;
21 InputError.prototype.toString = function() {
22 var s = 'Error: ' + this.message;
25 (this.line.number + 1) + ': "' + this.line.contents + '"';
29 function File(contents) {
30 this.contents = contents;
35 File.prototype.endReached = function() {
36 return this.index >= this.contents.length;
39 File.prototype.nextLine = function() {
40 if (this.endReached())
42 var start = this.index;
43 var end = this.contents.indexOf('\n', start);
45 end = this.contents.length;
47 return new Line(this.contents.substring(start, end), this.lineNumber++);
50 function Line(contents, number) {
51 var i = contents.indexOf('//');
52 var s = (i == -1) ? contents.trim() : contents.substring(0, i).trim();
53 this.contents = contents;
54 this.items = (s.length > 0) ? s.split(/\s+/) : [];
59 Line.prototype.endReached = function() {
60 return this.index >= this.items.length;
63 var ITEM_TYPE_SIZES = {
64 u1: 1, u2: 2, u4: 4, u8: 8, s1: 1, s2: 2, s4: 4, s8: 8, b: 1, f: 4, d: 8,
65 dist4: 4, dist8: 8, anchr: 0, handles: 0
68 function isValidItemType(type) {
69 return ITEM_TYPE_SIZES[type] !== undefined;
72 Line.prototype.nextItem = function() {
73 if (this.endReached())
76 var itemString = this.items[this.index++];
78 var value = itemString;
80 if (itemString.charAt(0) == '[') {
81 var i = itemString.indexOf(']');
82 if (i != -1 && i + 1 < itemString.length) {
83 type = itemString.substring(1, i);
84 value = itemString.substring(i + 1);
86 throw new InputError('invalid item', this);
89 if (!isValidItemType(type))
90 throw new InputError('invalid item type', this);
92 return new Item(this, type, value);
95 // The text for each whitespace delimited binary data "item" is represented
98 function Item(line, type, value) {
102 this.size = ITEM_TYPE_SIZES[type];
105 Item.prototype.isFloat = function() {
106 return this.type == 'f' || this.type == 'd';
109 Item.prototype.isInteger = function() {
110 return ['u1', 'u2', 'u4', 'u8',
111 's1', 's2', 's4', 's8'].indexOf(this.type) != -1;
114 Item.prototype.isNumber = function() {
115 return this.isFloat() || this.isInteger();
118 Item.prototype.isByte = function() {
119 return this.type == 'b';
122 Item.prototype.isDistance = function() {
123 return this.type == 'dist4' || this.type == 'dist8';
126 Item.prototype.isAnchor = function() {
127 return this.type == 'anchr';
130 Item.prototype.isHandles = function() {
131 return this.type == 'handles';
134 // A TestMessage represents the complete binary message loaded from an input
135 // string or ".data" file. The parseTestMessage() function below constructs
136 // a TestMessage from a File.
138 function TestMessage(byteLength) {
140 this.buffer = new buffer.Buffer(byteLength);
142 this.handleCount = 0;
145 function checkItemNumberValue(item, n, min, max) {
146 if (n < min || n > max)
147 throw new InputError('invalid item value', item.line);
150 TestMessage.prototype.addNumber = function(item) {
151 var n = item.isInteger() ? parseInt(item.value) : parseFloat(item.value);
153 throw new InputError("can't parse item value", item.line);
157 checkItemNumberValue(item, n, 0, 0xFF);
158 this.buffer.setUint8(this.index, n);
161 checkItemNumberValue(item, n, 0, 0xFFFF);
162 this.buffer.setUint16(this.index, n);
165 checkItemNumberValue(item, n, 0, 0xFFFFFFFF);
166 this.buffer.setUint32(this.index, n);
169 checkItemNumberValue(item, n, 0, Number.MAX_SAFE_INTEGER);
170 this.buffer.setUint64(this.index, n);
173 checkItemNumberValue(item, n, -128, 127);
174 this.buffer.setInt8(this.index, n);
177 checkItemNumberValue(item, n, -32768, 32767);
178 this.buffer.setInt16(this.index, n);
181 checkItemNumberValue(item, n, -2147483648, 2147483647);
182 this.buffer.setInt32(this.index, n);
185 checkItemNumberValue(item, n,
186 Number.MIN_SAFE_INTEGER,
187 Number.MAX_SAFE_INTEGER);
188 this.buffer.setInt64(this.index, n);
191 this.buffer.setFloat32(this.index, n);
194 this.buffer.setFloat64(this.index, n);
198 throw new InputError('unrecognized item type', item.line);
202 TestMessage.prototype.addByte = function(item) {
203 if (!/^[01]{8}$/.test(item.value))
204 throw new InputError('invalid byte item value', item.line);
206 return (item.value.charAt(7 - i) == '1') ? 1 << i : 0;
208 var n = b(0) | b(1) | b(2) | b(3) | b(4) | b(5) | b(6) | b(7);
209 this.buffer.setUint8(this.index, n);
212 TestMessage.prototype.addDistance = function(item) {
213 if (this.distances[item.value])
214 throw new InputError('duplicate distance item', item.line);
215 this.distances[item.value] = {index: this.index, item: item};
218 TestMessage.prototype.addAnchor = function(item) {
219 var dist = this.distances[item.value];
221 throw new InputError('unmatched anchor item', item.line);
222 delete this.distances[item.value];
224 var n = this.index - dist.index;
225 // TODO(hansmuller): validate n
227 if (dist.item.type == 'dist4')
228 this.buffer.setUint32(dist.index, n);
229 else if (dist.item.type == 'dist8')
230 this.buffer.setUint64(dist.index, n);
232 throw new InputError('unrecognzed distance item type', dist.item.line);
235 TestMessage.prototype.addHandles = function(item) {
236 this.handleCount = parseInt(item.value);
237 if (Number.isNaN(this.handleCount))
238 throw new InputError("can't parse handleCount", item.line);
241 TestMessage.prototype.addItem = function(item) {
243 this.addNumber(item);
244 else if (item.isByte())
246 else if (item.isDistance())
247 this.addDistance(item);
248 else if (item.isAnchor())
249 this.addAnchor(item);
250 else if (item.isHandles())
251 this.addHandles(item);
253 throw new InputError('unrecognized item type', item.line);
255 this.index += item.size;
258 TestMessage.prototype.unanchoredDistances = function() {
260 for (var name in this.distances) {
261 if (this.distances.hasOwnProperty(name))
262 names = (names === null) ? name : names + ' ' + name;
267 function parseTestMessage(text) {
268 var file = new File(text);
270 var messageLength = 0;
271 while(!file.endReached()) {
272 var line = file.nextLine();
273 while (!line.endReached()) {
274 var item = line.nextItem();
275 if (item.isHandles() && items.length > 0)
276 throw new InputError('handles item is not first');
277 messageLength += item.size;
282 var msg = new TestMessage(messageLength);
283 for (var i = 0; i < items.length; i++)
284 msg.addItem(items[i]);
286 if (messageLength != msg.index)
287 throw new InputError('failed to compute message length');
288 var names = msg.unanchoredDistances();
290 throw new InputError('no anchors for ' + names, 0);
296 exports.parseTestMessage = parseTestMessage;
297 exports.InputError = InputError;