Debian Codegraph @

closure-library/closure/goog/proto2/textformatserializer.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Protocol Buffer 2 Serializer which serializes messages
17 * into a user-friendly text format. Note that this code can run a bit
18 * slowly (especially for parsing) and should therefore not be used for
19 * time or space-critical applications.
20 *
21 * @see http://goo.gl/QDmDr
22 */
23
24 goog.provide('goog.proto2.TextFormatSerializer');
25
26 goog.require('goog.array');
27 goog.require('goog.asserts');
28 goog.require('goog.math');
29 goog.require('goog.object');
30 goog.require('goog.proto2.FieldDescriptor');
31 goog.require('goog.proto2.Message');
32 goog.require('goog.proto2.Serializer');
33 goog.require('goog.string');
34
35
36
37 /**
38 * TextFormatSerializer, a serializer which turns Messages into the human
39 * readable text format.
40 * @param {boolean=} opt_ignoreMissingFields If true, then fields that cannot be
41 * found on the proto when parsing the text format will be ignored.
42 * @param {boolean=} opt_useEnumValues If true, serialization code for enums
43 * will use enum integer values instead of human-readable symbolic names.
44 * @constructor
45 * @extends {goog.proto2.Serializer}
46 * @final
47 */
48 goog.proto2.TextFormatSerializer = function(
49 opt_ignoreMissingFields, opt_useEnumValues) {
50 /**
51 * Whether to ignore fields not defined on the proto when parsing the text
52 * format.
53 * @type {boolean}
54 * @private
55 */
56 this.ignoreMissingFields_ = !!opt_ignoreMissingFields;
57
58 /**
59 * Whether to use integer enum values during enum serialization.
60 * If false, symbolic names will be used.
61 * @type {boolean}
62 * @private
63 */
64 this.useEnumValues_ = !!opt_useEnumValues;
65 };
66 goog.inherits(goog.proto2.TextFormatSerializer, goog.proto2.Serializer);
67
68
69 /**
70 * Deserializes a message from text format and places the data in the message.
71 * @param {goog.proto2.Message} message The message in which to
72 * place the information.
73 * @param {*} data The text format data.
74 * @return {?string} The parse error or null on success.
75 * @override
76 */
77 goog.proto2.TextFormatSerializer.prototype.deserializeTo = function(
78 message, data) {
79 var textData = data.toString();
80 var parser = new goog.proto2.TextFormatSerializer.Parser();
81 if (!parser.parse(message, textData, this.ignoreMissingFields_)) {
82 return parser.getError();
83 }
84
85 return null;
86 };
87
88
89 /**
90 * Serializes a message to a string.
91 * @param {goog.proto2.Message} message The message to be serialized.
92 * @return {string} The serialized form of the message.
93 * @override
94 */
95 goog.proto2.TextFormatSerializer.prototype.serialize = function(message) {
96 var printer = new goog.proto2.TextFormatSerializer.Printer_();
97 this.serializeMessage_(message, printer);
98 return printer.toString();
99 };
100
101
102 /**
103 * Serializes the message and prints the text form into the given printer.
104 * @param {goog.proto2.Message} message The message to serialize.
105 * @param {goog.proto2.TextFormatSerializer.Printer_} printer The printer to
106 * which the text format will be printed.
107 * @private
108 */
109 goog.proto2.TextFormatSerializer.prototype.serializeMessage_ = function(
110 message, printer) {
111 var descriptor = message.getDescriptor();
112 var fields = descriptor.getFields();
113
114 // Add the defined fields, recursively.
115 goog.array.forEach(fields, function(field) {
116 this.printField_(message, field, printer);
117 }, this);
118
119 // Add the unknown fields, if any.
120 message.forEachUnknown(function(tag, value) {
121 this.serializeUnknown_(tag, value, goog.asserts.assert(printer));
122 }, this);
123 };
124
125
126 /**
127 * Serializes an unknown field. When parsed from the JsPb object format, this
128 * manifests as either a primitive type, an array, or a raw object with integer
129 * keys. There is no descriptor available to interpret the types of nested
130 * messages.
131 * @param {number} tag The tag for the field. Since it's unknown, this is a
132 * number rather than a string.
133 * @param {*} value The value of the field.
134 * @param {!goog.proto2.TextFormatSerializer.Printer_} printer The printer to
135 * which the text format will be serialized.
136 * @private
137 */
138 goog.proto2.TextFormatSerializer.prototype.serializeUnknown_ = function(
139 tag, value, printer) {
140 if (!goog.isDefAndNotNull(value)) {
141 return;
142 }
143
144 if (goog.isArray(value)) {
145 goog.array.forEach(value, function(val) {
146 this.serializeUnknown_(tag, val, printer);
147 }, this);
148 return;
149 }
150
151 if (goog.isObject(value)) {
152 printer.append(tag);
153 printer.append(' {');
154 printer.appendLine();
155 printer.indent();
156 if (value instanceof goog.proto2.Message) {
157 // Note(user): This conditional is here to make the
158 // testSerializationOfUnknown unit test pass, but in practice we should
159 // never have a Message for an "unknown" field.
160 this.serializeMessage_(value, printer);
161 } else {
162 // For an unknown message, fields are keyed by positive integers. We
163 // don't have a 'length' property to use for enumeration, so go through
164 // all properties and ignore the ones that aren't legal keys.
165 for (var key in value) {
166 var keyAsNumber = goog.string.parseInt(key);
167 goog.asserts.assert(goog.math.isInt(keyAsNumber));
168 this.serializeUnknown_(keyAsNumber, value[key], printer);
169 }
170 }
171 printer.dedent();
172 printer.append('}');
173 printer.appendLine();
174 return;
175 }
176
177 if (goog.isString(value)) {
178 value = goog.string.quote(value);
179 }
180 printer.append(tag);
181 printer.append(': ');
182 printer.append(value.toString());
183 printer.appendLine();
184 };
185
186
187 /**
188 * Prints the serialized value for the given field to the printer.
189 * @param {*} value The field's value.
190 * @param {goog.proto2.FieldDescriptor} field The field whose value is being
191 * printed.
192 * @param {goog.proto2.TextFormatSerializer.Printer_} printer The printer to
193 * which the value will be printed.
194 * @private
195 */
196 goog.proto2.TextFormatSerializer.prototype.printFieldValue_ = function(
197 value, field, printer) {
198 switch (field.getFieldType()) {
199 case goog.proto2.FieldDescriptor.FieldType.DOUBLE:
200 case goog.proto2.FieldDescriptor.FieldType.FLOAT:
201 case goog.proto2.FieldDescriptor.FieldType.INT64:
202 case goog.proto2.FieldDescriptor.FieldType.UINT64:
203 case goog.proto2.FieldDescriptor.FieldType.INT32:
204 case goog.proto2.FieldDescriptor.FieldType.UINT32:
205 case goog.proto2.FieldDescriptor.FieldType.FIXED64:
206 case goog.proto2.FieldDescriptor.FieldType.FIXED32:
207 case goog.proto2.FieldDescriptor.FieldType.BOOL:
208 case goog.proto2.FieldDescriptor.FieldType.SFIXED32:
209 case goog.proto2.FieldDescriptor.FieldType.SFIXED64:
210 case goog.proto2.FieldDescriptor.FieldType.SINT32:
211 case goog.proto2.FieldDescriptor.FieldType.SINT64:
212 printer.append(value);
213 break;
214
215 case goog.proto2.FieldDescriptor.FieldType.BYTES:
216 case goog.proto2.FieldDescriptor.FieldType.STRING:
217 value = goog.string.quote(value.toString());
218 printer.append(value);
219 break;
220
221 case goog.proto2.FieldDescriptor.FieldType.ENUM:
222 if (!this.useEnumValues_) {
223 // Search the enum type for a matching key.
224 var found = false;
225 goog.object.forEach(field.getNativeType(), function(eValue, key) {
226 if (!found && eValue == value) {
227 printer.append(key);
228 found = true;
229 }
230 });
231 }
232
233 if (!found || this.useEnumValues_) {
234 // Otherwise, just print the numeric value.
235 printer.append(value.toString());
236 }
237 break;
238
239 case goog.proto2.FieldDescriptor.FieldType.GROUP:
240 case goog.proto2.FieldDescriptor.FieldType.MESSAGE:
241 this.serializeMessage_(
242 /** @type {goog.proto2.Message} */ (value), printer);
243 break;
244 }
245 };
246
247
248 /**
249 * Prints the serialized field to the printer.
250 * @param {goog.proto2.Message} message The parent message.
251 * @param {goog.proto2.FieldDescriptor} field The field to print.
252 * @param {goog.proto2.TextFormatSerializer.Printer_} printer The printer to
253 * which the field will be printed.
254 * @private
255 */
256 goog.proto2.TextFormatSerializer.prototype.printField_ = function(
257 message, field, printer) {
258 // Skip fields not present.
259 if (!message.has(field)) {
260 return;
261 }
262
263 var count = message.countOf(field);
264 for (var i = 0; i < count; ++i) {
265 // Field name.
266 printer.append(field.getName());
267
268 // Field delimiter.
269 if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||
270 field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.GROUP) {
271 printer.append(' {');
272 printer.appendLine();
273 printer.indent();
274 } else {
275 printer.append(': ');
276 }
277
278 // Write the field value.
279 this.printFieldValue_(message.get(field, i), field, printer);
280
281 // Close the field.
282 if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||
283 field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.GROUP) {
284 printer.dedent();
285 printer.append('}');
286 printer.appendLine();
287 } else {
288 printer.appendLine();
289 }
290 }
291 };
292
293
294 ////////////////////////////////////////////////////////////////////////////////
295
296
297
298 /**
299 * Helper class used by the text format serializer for pretty-printing text.
300 * @constructor
301 * @private
302 */
303 goog.proto2.TextFormatSerializer.Printer_ = function() {
304 /**
305 * The current indentation count.
306 * @type {number}
307 * @private
308 */
309 this.indentation_ = 0;
310
311 /**
312 * The buffer of string pieces.
313 * @type {Array<string>}
314 * @private
315 */
316 this.buffer_ = [];
317
318 /**
319 * Whether indentation is required before the next append of characters.
320 * @type {boolean}
321 * @private
322 */
323 this.requiresIndentation_ = true;
324 };
325
326
327 /**
328 * @return {string} The contents of the printer.
329 * @override
330 */
331 goog.proto2.TextFormatSerializer.Printer_.prototype.toString = function() {
332 return this.buffer_.join('');
333 };
334
335
336 /**
337 * Increases the indentation in the printer.
338 */
339 goog.proto2.TextFormatSerializer.Printer_.prototype.indent = function() {
340 this.indentation_ += 2;
341 };
342
343
344 /**
345 * Decreases the indentation in the printer.
346 */
347 goog.proto2.TextFormatSerializer.Printer_.prototype.dedent = function() {
348 this.indentation_ -= 2;
349 goog.asserts.assert(this.indentation_ >= 0);
350 };
351
352
353 /**
354 * Appends the given value to the printer.
355 * @param {*} value The value to append.
356 */
357 goog.proto2.TextFormatSerializer.Printer_.prototype.append = function(value) {
358 if (this.requiresIndentation_) {
359 for (var i = 0; i < this.indentation_; ++i) {
360 this.buffer_.push(' ');
361 }
362 this.requiresIndentation_ = false;
363 }
364
365 this.buffer_.push(value.toString());
366 };
367
368
369 /**
370 * Appends a newline to the printer.
371 */
372 goog.proto2.TextFormatSerializer.Printer_.prototype.appendLine = function() {
373 this.buffer_.push('\n');
374 this.requiresIndentation_ = true;
375 };
376
377
378 ////////////////////////////////////////////////////////////////////////////////
379
380
381
382 /**
383 * Helper class for tokenizing the text format.
384 * @param {string} data The string data to tokenize.
385 * @param {boolean=} opt_ignoreWhitespace If true, whitespace tokens will not
386 * be reported by the tokenizer.
387 * @param {boolean=} opt_ignoreComments If true, comment tokens will not be
388 * reported by the tokenizer.
389 * @constructor
390 * @private
391 */
392 goog.proto2.TextFormatSerializer.Tokenizer_ = function(
393 data, opt_ignoreWhitespace, opt_ignoreComments) {
394
395 /**
396 * Whether to skip whitespace tokens on output.
397 * @private {boolean}
398 */
399 this.ignoreWhitespace_ = !!opt_ignoreWhitespace;
400
401 /**
402 * Whether to skip comment tokens on output.
403 * @private {boolean}
404 */
405 this.ignoreComments_ = !!opt_ignoreComments;
406
407 /**
408 * The data being tokenized.
409 * @private {string}
410 */
411 this.data_ = data;
412
413 /**
414 * The current index in the data.
415 * @private {number}
416 */
417 this.index_ = 0;
418
419 /**
420 * The data string starting at the current index.
421 * @private {string}
422 */
423 this.currentData_ = data;
424
425 /**
426 * The current token type.
427 * @private {goog.proto2.TextFormatSerializer.Tokenizer_.Token}
428 */
429 this.current_ = {
430 type: goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes.END,
431 value: null
432 };
433 };
434
435
436 /**
437 * @typedef {{type: goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes,
438 * value: ?string}}
439 */
440 goog.proto2.TextFormatSerializer.Tokenizer_.Token;
441
442
443 /**
444 * @return {goog.proto2.TextFormatSerializer.Tokenizer_.Token} The current
445 * token.
446 */
447 goog.proto2.TextFormatSerializer.Tokenizer_.prototype.getCurrent = function() {
448 return this.current_;
449 };
450
451
452 /**
453 * An enumeration of all the token types.
454 * @enum {!RegExp}
455 */
456 goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes = {
457 END: /---end---/,
458 // Leading "-" to identify "-infinity"."
459 IDENTIFIER: /^-?[a-zA-Z][a-zA-Z0-9_]*/,
460 NUMBER: /^(0x[0-9a-f]+)|(([-])?[0-9][0-9]*(\.?[0-9]+)?(e[+-]?[0-9]+|[f])?)/,
461 COMMENT: /^#.*/,
462 OPEN_BRACE: /^{/,
463 CLOSE_BRACE: /^}/,
464 OPEN_TAG: /^</,
465 CLOSE_TAG: /^>/,
466 OPEN_LIST: /^\[/,
467 CLOSE_LIST: /^\]/,
468 STRING: new RegExp('^"([^"\\\\]|\\\\.)*"'),
469 COLON: /^:/,
470 COMMA: /^,/,
471 SEMI: /^;/,
472 WHITESPACE: /^\s/
473 };
474
475
476 /**
477 * Advances to the next token.
478 * @return {boolean} True if a valid token was found, false if the end was
479 * reached or no valid token was found.
480 */
481 goog.proto2.TextFormatSerializer.Tokenizer_.prototype.next = function() {
482 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
483
484 // Skip any whitespace if requested.
485 while (this.nextInternal_()) {
486 var type = this.getCurrent().type;
487 if ((type != types.WHITESPACE && type != types.COMMENT) ||
488 (type == types.WHITESPACE && !this.ignoreWhitespace_) ||
489 (type == types.COMMENT && !this.ignoreComments_)) {
490 return true;
491 }
492 }
493
494 // If we reach this point, set the current token to END.
495 this.current_ = {
496 type: goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes.END,
497 value: null
498 };
499
500 return false;
501 };
502
503
504 /**
505 * Internal method for determining the next token.
506 * @return {boolean} True if a next token was found, false otherwise.
507 * @private
508 */
509 goog.proto2.TextFormatSerializer.Tokenizer_.prototype.nextInternal_ =
510 function() {
511 if (this.index_ >= this.data_.length) {
512 return false;
513 }
514
515 var data = this.currentData_;
516 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
517 var next = null;
518
519 // Loop through each token type and try to match the beginning of the string
520 // with the token's regular expression.
521 goog.object.some(types, function(type, id) {
522 if (next || type == types.END) {
523 return false;
524 }
525
526 // Note: This regular expression check is at, minimum, O(n).
527 var info = type.exec(data);
528 if (info && info.index == 0) {
529 next = {type: type, value: info[0]};
530 }
531
532 return !!next;
533 });
534
535 // Advance the index by the length of the token.
536 if (next) {
537 this.current_ =
538 /** @type {goog.proto2.TextFormatSerializer.Tokenizer_.Token} */ (next);
539 this.index_ += next.value.length;
540 this.currentData_ = this.currentData_.substring(next.value.length);
541 }
542
543 return !!next;
544 };
545
546
547 ////////////////////////////////////////////////////////////////////////////////
548
549
550
551 /**
552 * Helper class for parsing the text format.
553 * @constructor
554 * @final
555 */
556 goog.proto2.TextFormatSerializer.Parser = function() {
557 /**
558 * The error during parsing, if any.
559 * @type {?string}
560 * @private
561 */
562 this.error_ = null;
563
564 /**
565 * The current tokenizer.
566 * @type {goog.proto2.TextFormatSerializer.Tokenizer_}
567 * @private
568 */
569 this.tokenizer_ = null;
570
571 /**
572 * Whether to ignore missing fields in the proto when parsing.
573 * @type {boolean}
574 * @private
575 */
576 this.ignoreMissingFields_ = false;
577 };
578
579
580 /**
581 * Parses the given data, filling the message as it goes.
582 * @param {goog.proto2.Message} message The message to fill.
583 * @param {string} data The text format data.
584 * @param {boolean=} opt_ignoreMissingFields If true, fields missing in the
585 * proto will be ignored.
586 * @return {boolean} True on success, false on failure. On failure, the
587 * getError method can be called to get the reason for failure.
588 */
589 goog.proto2.TextFormatSerializer.Parser.prototype.parse = function(
590 message, data, opt_ignoreMissingFields) {
591 this.error_ = null;
592 this.ignoreMissingFields_ = !!opt_ignoreMissingFields;
593 this.tokenizer_ =
594 new goog.proto2.TextFormatSerializer.Tokenizer_(data, true, true);
595 this.tokenizer_.next();
596 return this.consumeMessage_(message, '');
597 };
598
599
600 /**
601 * @return {?string} The parse error, if any.
602 */
603 goog.proto2.TextFormatSerializer.Parser.prototype.getError = function() {
604 return this.error_;
605 };
606
607
608 /**
609 * Reports a parse error.
610 * @param {string} msg The error message.
611 * @private
612 */
613 goog.proto2.TextFormatSerializer.Parser.prototype.reportError_ = function(msg) {
614 this.error_ = msg;
615 };
616
617
618 /**
619 * Attempts to consume the given message.
620 * @param {goog.proto2.Message} message The message to consume and fill. If
621 * null, then the message contents will be consumed without ever being set
622 * to anything.
623 * @param {string} delimiter The delimiter expected at the end of the message.
624 * @return {boolean} True on success, false otherwise.
625 * @private
626 */
627 goog.proto2.TextFormatSerializer.Parser.prototype.consumeMessage_ = function(
628 message, delimiter) {
629 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
630 while (!this.lookingAt_('>') && !this.lookingAt_('}') &&
631 !this.lookingAtType_(types.END)) {
632 if (!this.consumeField_(message)) {
633 return false;
634 }
635 }
636
637 if (delimiter) {
638 if (!this.consume_(delimiter)) {
639 return false;
640 }
641 } else {
642 if (!this.lookingAtType_(types.END)) {
643 this.reportError_('Expected END token');
644 }
645 }
646
647 return true;
648 };
649
650
651 /**
652 * Attempts to consume the value of the given field.
653 * @param {goog.proto2.Message} message The parent message.
654 * @param {goog.proto2.FieldDescriptor} field The field.
655 * @return {boolean} True on success, false otherwise.
656 * @private
657 */
658 goog.proto2.TextFormatSerializer.Parser.prototype.consumeFieldValue_ = function(
659 message, field) {
660 var value = this.getFieldValue_(field);
661 if (goog.isNull(value)) {
662 return false;
663 }
664
665 if (field.isRepeated()) {
666 message.add(field, value);
667 } else {
668 message.set(field, value);
669 }
670
671 return true;
672 };
673
674
675 /**
676 * Attempts to convert a string to a number.
677 * @param {string} num in hexadecimal or float format.
678 * @return {number} The converted number or null on error.
679 * @private
680 */
681 goog.proto2.TextFormatSerializer.Parser.getNumberFromString_ = function(num) {
682
683 var returnValue = goog.string.contains(num, '.') ?
684 parseFloat(num) : // num is a float.
685 goog.string.parseInt(num); // num is an int.
686
687 goog.asserts.assert(!isNaN(returnValue));
688 goog.asserts.assert(isFinite(returnValue));
689
690 return returnValue;
691 };
692
693
694 /**
695 * Parse NaN, positive infinity, or negative infinity from a string.
696 * @param {string} identifier An identifier string to check.
697 * @return {?number} Infinity, negative infinity, NaN, or null if none
698 * of the constants could be parsed.
699 * @private
700 */
701 goog.proto2.TextFormatSerializer.Parser.parseNumericalConstant_ = function(
702 identifier) {
703 if (/^-?inf(?:inity)?f?$/i.test(identifier)) {
704 return Infinity * (goog.string.startsWith(identifier, '-') ? -1 : 1);
705 }
706
707 if (/^nanf?$/i.test(identifier)) {
708 return NaN;
709 }
710
711 return null;
712 };
713
714
715 /**
716 * Attempts to parse the given field's value from the stream.
717 * @param {goog.proto2.FieldDescriptor} field The field.
718 * @return {*} The field's value or null if none.
719 * @private
720 */
721 goog.proto2.TextFormatSerializer.Parser.prototype.getFieldValue_ = function(
722 field) {
723 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
724 switch (field.getFieldType()) {
725 case goog.proto2.FieldDescriptor.FieldType.DOUBLE:
726 case goog.proto2.FieldDescriptor.FieldType.FLOAT:
727
728 var identifier = this.consumeIdentifier_();
729 if (identifier) {
730 var numericalIdentifier =
731 goog.proto2.TextFormatSerializer.Parser.parseNumericalConstant_(
732 identifier);
733 // Use isDefAndNotNull since !!NaN is false.
734 if (goog.isDefAndNotNull(numericalIdentifier)) {
735 return numericalIdentifier;
736 }
737 }
738
739 case goog.proto2.FieldDescriptor.FieldType.INT32:
740 case goog.proto2.FieldDescriptor.FieldType.UINT32:
741 case goog.proto2.FieldDescriptor.FieldType.FIXED32:
742 case goog.proto2.FieldDescriptor.FieldType.SFIXED32:
743 case goog.proto2.FieldDescriptor.FieldType.SINT32:
744 var num = this.consumeNumber_();
745 if (!num) {
746 return null;
747 }
748
749 return goog.proto2.TextFormatSerializer.Parser.getNumberFromString_(num);
750
751 case goog.proto2.FieldDescriptor.FieldType.INT64:
752 case goog.proto2.FieldDescriptor.FieldType.UINT64:
753 case goog.proto2.FieldDescriptor.FieldType.FIXED64:
754 case goog.proto2.FieldDescriptor.FieldType.SFIXED64:
755 case goog.proto2.FieldDescriptor.FieldType.SINT64:
756 var num = this.consumeNumber_();
757 if (!num) {
758 return null;
759 }
760
761 if (field.getNativeType() == Number) {
762 // 64-bit number stored as a number.
763 return goog.proto2.TextFormatSerializer.Parser.getNumberFromString_(
764 num);
765 }
766
767 return num; // 64-bit numbers are by default stored as strings.
768
769 case goog.proto2.FieldDescriptor.FieldType.BOOL:
770 var ident = this.consumeIdentifier_();
771 if (!ident) {
772 return null;
773 }
774
775 switch (ident) {
776 case 'true':
777 return true;
778 case 'false':
779 return false;
780 default:
781 this.reportError_('Unknown type for bool: ' + ident);
782 return null;
783 }
784
785 case goog.proto2.FieldDescriptor.FieldType.ENUM:
786 if (this.lookingAtType_(types.NUMBER)) {
787 var num = this.consumeNumber_();
788 if (!num) {
789 return null;
790 }
791
792 return goog.proto2.TextFormatSerializer.Parser.getNumberFromString_(
793 num);
794 } else {
795 // Search the enum type for a matching key.
796 var name = this.consumeIdentifier_();
797 if (!name) {
798 return null;
799 }
800
801 var enumValue = field.getNativeType()[name];
802 if (enumValue == null) {
803 this.reportError_('Unknown enum value: ' + name);
804 return null;
805 }
806
807 return enumValue;
808 }
809
810 case goog.proto2.FieldDescriptor.FieldType.BYTES:
811 case goog.proto2.FieldDescriptor.FieldType.STRING:
812 return this.consumeString_();
813 }
814 };
815
816
817 /**
818 * Attempts to consume a nested message.
819 * @param {goog.proto2.Message} message The parent message.
820 * @param {goog.proto2.FieldDescriptor} field The field.
821 * @return {boolean} True on success, false otherwise.
822 * @private
823 */
824 goog.proto2.TextFormatSerializer.Parser.prototype.consumeNestedMessage_ =
825 function(message, field) {
826 var delimiter = '';
827
828 // Messages support both < > and { } as delimiters for legacy reasons.
829 if (this.tryConsume_('<')) {
830 delimiter = '>';
831 } else {
832 if (!this.consume_('{')) {
833 return false;
834 }
835 delimiter = '}';
836 }
837
838 var msg = field.getFieldMessageType().createMessageInstance();
839 var result = this.consumeMessage_(msg, delimiter);
840 if (!result) {
841 return false;
842 }
843
844 // Add the message to the parent message.
845 if (field.isRepeated()) {
846 message.add(field, msg);
847 } else {
848 message.set(field, msg);
849 }
850
851 return true;
852 };
853
854
855 /**
856 * Attempts to consume the value of an unknown field. This method uses
857 * heuristics to try to consume just the right tokens.
858 * @return {boolean} True on success, false otherwise.
859 * @private
860 */
861 goog.proto2.TextFormatSerializer.Parser.prototype.consumeUnknownFieldValue_ =
862 function() {
863 // : is optional.
864 this.tryConsume_(':');
865
866 // Handle form: [.. , ... , ..]
867 if (this.tryConsume_('[')) {
868 while (true) {
869 this.tokenizer_.next();
870 if (this.tryConsume_(']')) {
871 break;
872 }
873 if (!this.consume_(',')) {
874 return false;
875 }
876 }
877
878 return true;
879 }
880
881 // Handle nested messages/groups.
882 if (this.tryConsume_('<')) {
883 return this.consumeMessage_(null /* unknown */, '>');
884 } else if (this.tryConsume_('{')) {
885 return this.consumeMessage_(null /* unknown */, '}');
886 } else {
887 // Otherwise, consume a single token for the field value.
888 this.tokenizer_.next();
889 }
890
891 return true;
892 };
893
894
895 /**
896 * Attempts to consume a field under a message.
897 * @param {goog.proto2.Message} message The parent message. If null, then the
898 * field value will be consumed without being assigned to anything.
899 * @return {boolean} True on success, false otherwise.
900 * @private
901 */
902 goog.proto2.TextFormatSerializer.Parser.prototype.consumeField_ = function(
903 message) {
904 var fieldName = this.consumeIdentifier_();
905 if (!fieldName) {
906 this.reportError_('Missing field name');
907 return false;
908 }
909
910 var field = null;
911 if (message) {
912 field = message.getDescriptor().findFieldByName(fieldName.toString());
913 }
914
915 if (field == null) {
916 if (this.ignoreMissingFields_) {
917 return this.consumeUnknownFieldValue_();
918 } else {
919 this.reportError_('Unknown field: ' + fieldName);
920 return false;
921 }
922 }
923
924 if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||
925 field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.GROUP) {
926 // : is optional here.
927 this.tryConsume_(':');
928 if (!this.consumeNestedMessage_(message, field)) {
929 return false;
930 }
931 } else {
932 // Long Format: "someField: 123"
933 // Short Format: "someField: [123, 456, 789]"
934 if (!this.consume_(':')) {
935 return false;
936 }
937
938 if (field.isRepeated() && this.tryConsume_('[')) {
939 // Short repeated format, e.g. "foo: [1, 2, 3]"
940 while (true) {
941 if (!this.consumeFieldValue_(message, field)) {
942 return false;
943 }
944 if (this.tryConsume_(']')) {
945 break;
946 }
947 if (!this.consume_(',')) {
948 return false;
949 }
950 }
951 } else {
952 // Normal field format.
953 if (!this.consumeFieldValue_(message, field)) {
954 return false;
955 }
956 }
957 }
958
959 // For historical reasons, fields may optionally be separated by commas or
960 // semicolons.
961 this.tryConsume_(',') || this.tryConsume_(';');
962 return true;
963 };
964
965
966 /**
967 * Attempts to consume a token with the given string value.
968 * @param {string} value The string value for the token.
969 * @return {boolean} True if the token matches and was consumed, false
970 * otherwise.
971 * @private
972 */
973 goog.proto2.TextFormatSerializer.Parser.prototype.tryConsume_ = function(
974 value) {
975 if (this.lookingAt_(value)) {
976 this.tokenizer_.next();
977 return true;
978 }
979 return false;
980 };
981
982
983 /**
984 * Consumes a token of the given type.
985 * @param {goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes} type The type
986 * of the token to consume.
987 * @return {?string} The string value of the token or null on error.
988 * @private
989 */
990 goog.proto2.TextFormatSerializer.Parser.prototype.consumeToken_ = function(
991 type) {
992 if (!this.lookingAtType_(type)) {
993 this.reportError_('Expected token type: ' + type);
994 return null;
995 }
996
997 var value = this.tokenizer_.getCurrent().value;
998 this.tokenizer_.next();
999 return value;
1000 };
1001
1002
1003 /**
1004 * Consumes an IDENTIFIER token.
1005 * @return {?string} The string value or null on error.
1006 * @private
1007 */
1008 goog.proto2.TextFormatSerializer.Parser.prototype.consumeIdentifier_ =
1009 function() {
1010 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
1011 return this.consumeToken_(types.IDENTIFIER);
1012 };
1013
1014
1015 /**
1016 * Consumes a NUMBER token.
1017 * @return {?string} The string value or null on error.
1018 * @private
1019 */
1020 goog.proto2.TextFormatSerializer.Parser.prototype.consumeNumber_ = function() {
1021 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
1022 return this.consumeToken_(types.NUMBER);
1023 };
1024
1025
1026 /**
1027 * Consumes a STRING token. Strings may come in multiple adjacent tokens which
1028 * are automatically concatenated, like in C or Python.
1029 * @return {?string} The *deescaped* string value or null on error.
1030 * @private
1031 */
1032 goog.proto2.TextFormatSerializer.Parser.prototype.consumeString_ = function() {
1033 var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
1034 var value = this.consumeToken_(types.STRING);
1035 if (!value) {
1036 return null;
1037 }
1038
1039 var stringValue = JSON.parse(/** @type {string} */ (value)).toString();
1040 while (this.lookingAtType_(types.STRING)) {
1041 value = this.consumeToken_(types.STRING);
1042 stringValue += JSON.parse(/** @type {string} */ (value)).toString();
1043 }
1044
1045 return stringValue;
1046 };
1047
1048
1049 /**
1050 * Consumes a token with the given value. If not found, reports an error.
1051 * @param {string} value The string value expected for the token.
1052 * @return {boolean} True on success, false otherwise.
1053 * @private
1054 */
1055 goog.proto2.TextFormatSerializer.Parser.prototype.consume_ = function(value) {
1056 if (!this.tryConsume_(value)) {
1057 this.reportError_('Expected token "' + value + '"');
1058 return false;
1059 }
1060
1061 return true;
1062 };
1063
1064
1065 /**
1066 * @param {string} value The value to check against.
1067 * @return {boolean} True if the current token has the given string value.
1068 * @private
1069 */
1070 goog.proto2.TextFormatSerializer.Parser.prototype.lookingAt_ = function(value) {
1071 return this.tokenizer_.getCurrent().value == value;
1072 };
1073
1074
1075 /**
1076 * @param {goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes} type The
1077 * token type.
1078 * @return {boolean} True if the current token has the given type.
1079 * @private
1080 */
1081 goog.proto2.TextFormatSerializer.Parser.prototype.lookingAtType_ = function(
1082 type) {
1083 return this.tokenizer_.getCurrent().type == type;
1084 };