-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
parse.js
948 lines (791 loc) · 28.4 KB
/
parse.js
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
"use strict";
module.exports = parse;
parse.filename = null;
parse.defaults = { keepCase: false };
var tokenize = require("./tokenize"),
Root = require("./root"),
Type = require("./type"),
Field = require("./field"),
MapField = require("./mapfield"),
OneOf = require("./oneof"),
Enum = require("./enum"),
Service = require("./service"),
Method = require("./method"),
ReflectionObject = require("./object"),
types = require("./types"),
util = require("./util");
var base10Re = /^[1-9][0-9]*$/,
base10NegRe = /^-?[1-9][0-9]*$/,
base16Re = /^0[x][0-9a-fA-F]+$/,
base16NegRe = /^-?0[x][0-9a-fA-F]+$/,
base8Re = /^0[0-7]+$/,
base8NegRe = /^-?0[0-7]+$/,
numberRe = /^(?![eE])[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?$/,
nameRe = /^[a-zA-Z_][a-zA-Z_0-9]*$/,
typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/;
/**
* Result object returned from {@link parse}.
* @interface IParserResult
* @property {string|undefined} package Package name, if declared
* @property {string[]|undefined} imports Imports, if any
* @property {string[]|undefined} weakImports Weak imports, if any
* @property {string|undefined} syntax Syntax, if specified (either `"proto2"` or `"proto3"`)
* @property {Root} root Populated root instance
*/
/**
* Options modifying the behavior of {@link parse}.
* @interface IParseOptions
* @property {boolean} [keepCase=false] Keeps field casing instead of converting to camel case
* @property {boolean} [alternateCommentMode=false] Recognize double-slash comments in addition to doc-block comments.
* @property {boolean} [preferTrailingComment=false] Use trailing comment when both leading comment and trailing comment exist.
*/
/**
* Options modifying the behavior of JSON serialization.
* @interface IToJSONOptions
* @property {boolean} [keepComments=false] Serializes comments.
*/
/**
* Parses the given .proto source and returns an object with the parsed contents.
* @param {string} source Source contents
* @param {Root} root Root to populate
* @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {IParserResult} Parser result
* @property {string} filename=null Currently processing file name for error reporting, if known
* @property {IParseOptions} defaults Default {@link IParseOptions}
*/
function parse(source, root, options) {
/* eslint-disable callback-return */
if (!(root instanceof Root)) {
options = root;
root = new Root();
}
if (!options)
options = parse.defaults;
var preferTrailingComment = options.preferTrailingComment || false;
var tn = tokenize(source, options.alternateCommentMode || false),
next = tn.next,
push = tn.push,
peek = tn.peek,
skip = tn.skip,
cmnt = tn.cmnt;
var head = true,
pkg,
imports,
weakImports,
syntax,
edition = false,
isProto3 = false;
var ptr = root;
var applyCase = options.keepCase ? function(name) { return name; } : util.camelCase;
/* istanbul ignore next */
function illegal(token, name, insideTryCatch) {
var filename = parse.filename;
if (!insideTryCatch)
parse.filename = null;
return Error("illegal " + (name || "token") + " '" + token + "' (" + (filename ? filename + ", " : "") + "line " + tn.line + ")");
}
function readString() {
var values = [],
token;
do {
/* istanbul ignore if */
if ((token = next()) !== "\"" && token !== "'")
throw illegal(token);
values.push(next());
skip(token);
token = peek();
} while (token === "\"" || token === "'");
return values.join("");
}
function readValue(acceptTypeRef) {
var token = next();
switch (token) {
case "'":
case "\"":
push(token);
return readString();
case "true": case "TRUE":
return true;
case "false": case "FALSE":
return false;
}
try {
return parseNumber(token, /* insideTryCatch */ true);
} catch (e) {
/* istanbul ignore else */
if (acceptTypeRef && typeRefRe.test(token))
return token;
/* istanbul ignore next */
throw illegal(token, "value");
}
}
function readRanges(target, acceptStrings) {
var token, start;
do {
if (acceptStrings && ((token = peek()) === "\"" || token === "'"))
target.push(readString());
else {
try {
target.push([ start = parseId(next()), skip("to", true) ? parseId(next()) : start ]);
} catch (err) {
if (typeRefRe.test(token) && edition) {
target.push(token);
} else {
throw err;
}
}
}
} while (skip(",", true));
var dummy = {options: undefined};
dummy.setOption = function(name, value) {
if (this.options === undefined) this.options = {};
this.options[name] = value;
};
ifBlock(
dummy,
function parseRange_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(dummy, token); // skip
skip(";");
} else
throw illegal(token);
},
function parseRange_line() {
parseInlineOptions(dummy); // skip
});
}
function parseNumber(token, insideTryCatch) {
var sign = 1;
if (token.charAt(0) === "-") {
sign = -1;
token = token.substring(1);
}
switch (token) {
case "inf": case "INF": case "Inf":
return sign * Infinity;
case "nan": case "NAN": case "Nan": case "NaN":
return NaN;
case "0":
return 0;
}
if (base10Re.test(token))
return sign * parseInt(token, 10);
if (base16Re.test(token))
return sign * parseInt(token, 16);
if (base8Re.test(token))
return sign * parseInt(token, 8);
/* istanbul ignore else */
if (numberRe.test(token))
return sign * parseFloat(token);
/* istanbul ignore next */
throw illegal(token, "number", insideTryCatch);
}
function parseId(token, acceptNegative) {
switch (token) {
case "max": case "MAX": case "Max":
return 536870911;
case "0":
return 0;
}
/* istanbul ignore if */
if (!acceptNegative && token.charAt(0) === "-")
throw illegal(token, "id");
if (base10NegRe.test(token))
return parseInt(token, 10);
if (base16NegRe.test(token))
return parseInt(token, 16);
/* istanbul ignore else */
if (base8NegRe.test(token))
return parseInt(token, 8);
/* istanbul ignore next */
throw illegal(token, "id");
}
function parsePackage() {
/* istanbul ignore if */
if (pkg !== undefined)
throw illegal("package");
pkg = next();
/* istanbul ignore if */
if (!typeRefRe.test(pkg))
throw illegal(pkg, "name");
ptr = ptr.define(pkg);
skip(";");
}
function parseImport() {
var token = peek();
var whichImports;
switch (token) {
case "weak":
whichImports = weakImports || (weakImports = []);
next();
break;
case "public":
next();
// eslint-disable-next-line no-fallthrough
default:
whichImports = imports || (imports = []);
break;
}
token = readString();
skip(";");
whichImports.push(token);
}
function parseSyntax() {
skip("=");
syntax = readString();
isProto3 = syntax === "proto3";
/* istanbul ignore if */
if (!isProto3 && syntax !== "proto2")
throw illegal(syntax, "syntax");
// Syntax is needed to understand the meaning of the optional field rule
// Otherwise the meaning is ambiguous between proto2 and proto3
root.setOption("syntax", syntax);
skip(";");
}
function parseEdition() {
skip("=");
edition = readString();
const supportedEditions = ["2023"];
/* istanbul ignore if */
if (!supportedEditions.includes(edition))
throw illegal(edition, "edition");
root.setOption("edition", edition);
skip(";");
}
function parseCommon(parent, token) {
switch (token) {
case "option":
parseOption(parent, token);
skip(";");
return true;
case "message":
parseType(parent, token);
return true;
case "enum":
parseEnum(parent, token);
return true;
case "service":
parseService(parent, token);
return true;
case "extend":
parseExtension(parent, token);
return true;
}
return false;
}
function ifBlock(obj, fnIf, fnElse) {
var trailingLine = tn.line;
if (obj) {
if(typeof obj.comment !== "string") {
obj.comment = cmnt(); // try block-type comment
}
obj.filename = parse.filename;
}
if (skip("{", true)) {
var token;
while ((token = next()) !== "}")
fnIf(token);
skip(";", true);
} else {
if (fnElse)
fnElse();
skip(";");
if (obj && (typeof obj.comment !== "string" || preferTrailingComment))
obj.comment = cmnt(trailingLine) || obj.comment; // try line-type comment
}
}
function parseType(parent, token) {
/* istanbul ignore if */
if (!nameRe.test(token = next()))
throw illegal(token, "type name");
var type = new Type(token);
ifBlock(type, function parseType_block(token) {
if (parseCommon(type, token))
return;
switch (token) {
case "map":
parseMapField(type, token);
break;
case "required":
if (edition)
throw illegal(token);
/* eslint-disable no-fallthrough */
case "repeated":
parseField(type, token);
break;
case "optional":
/* istanbul ignore if */
if (isProto3) {
parseField(type, "proto3_optional");
} else if (edition) {
throw illegal(token);
} else {
parseField(type, "optional");
}
break;
case "oneof":
parseOneOf(type, token);
break;
case "extensions":
readRanges(type.extensions || (type.extensions = []));
break;
case "reserved":
readRanges(type.reserved || (type.reserved = []), true);
break;
default:
/* istanbul ignore if */
if (!isProto3 && !edition || !typeRefRe.test(token)) {
throw illegal(token);
}
push(token);
parseField(type, "optional");
break;
}
});
parent.add(type);
}
function parseField(parent, rule, extend) {
var type = next();
if (type === "group") {
parseGroup(parent, rule);
return;
}
// Type names can consume multiple tokens, in multiple variants:
// package.subpackage field tokens: "package.subpackage" [TYPE NAME ENDS HERE] "field"
// package . subpackage field tokens: "package" "." "subpackage" [TYPE NAME ENDS HERE] "field"
// package. subpackage field tokens: "package." "subpackage" [TYPE NAME ENDS HERE] "field"
// package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
// Keep reading tokens until we get a type name with no period at the end,
// and the next token does not start with a period.
while (type.endsWith(".") || peek().startsWith(".")) {
type += next();
}
/* istanbul ignore if */
if (!typeRefRe.test(type))
throw illegal(type, "type");
var name = next();
/* istanbul ignore if */
if (!nameRe.test(name))
throw illegal(name, "name");
name = applyCase(name);
skip("=");
var field = new Field(name, parseId(next()), type, rule, extend);
ifBlock(field, function parseField_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(field, token);
skip(";");
} else
throw illegal(token);
}, function parseField_line() {
parseInlineOptions(field);
});
if (rule === "proto3_optional") {
// for proto3 optional fields, we create a single-member Oneof to mimic "optional" behavior
var oneof = new OneOf("_" + name);
field.setOption("proto3_optional", true);
oneof.add(field);
parent.add(oneof);
} else {
parent.add(field);
}
// JSON defaults to packed=true if not set so we have to set packed=false explicity when
// parsing proto2 descriptors without the option, where applicable. This must be done for
// all known packable types and anything that could be an enum (= is not a basic type).
if (!isProto3 && field.repeated && (types.packed[type] !== undefined || types.basic[type] === undefined))
field.setOption("packed", false, /* ifNotSet */ true);
}
function parseGroup(parent, rule) {
var name = next();
/* istanbul ignore if */
if (!nameRe.test(name))
throw illegal(name, "name");
var fieldName = util.lcFirst(name);
if (name === fieldName)
name = util.ucFirst(name);
skip("=");
var id = parseId(next());
var type = new Type(name);
type.group = true;
var field = new Field(fieldName, id, name, rule);
field.filename = parse.filename;
ifBlock(type, function parseGroup_block(token) {
switch (token) {
case "option":
parseOption(type, token);
skip(";");
break;
case "required":
case "repeated":
parseField(type, token);
break;
case "optional":
/* istanbul ignore if */
if (isProto3) {
parseField(type, "proto3_optional");
} else {
parseField(type, "optional");
}
break;
case "message":
parseType(type, token);
break;
case "enum":
parseEnum(type, token);
break;
/* istanbul ignore next */
default:
throw illegal(token); // there are no groups with proto3 semantics
}
});
parent.add(type)
.add(field);
}
function parseMapField(parent) {
skip("<");
var keyType = next();
/* istanbul ignore if */
if (types.mapKey[keyType] === undefined)
throw illegal(keyType, "type");
skip(",");
var valueType = next();
/* istanbul ignore if */
if (!typeRefRe.test(valueType))
throw illegal(valueType, "type");
skip(">");
var name = next();
/* istanbul ignore if */
if (!nameRe.test(name))
throw illegal(name, "name");
skip("=");
var field = new MapField(applyCase(name), parseId(next()), keyType, valueType);
ifBlock(field, function parseMapField_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(field, token);
skip(";");
} else
throw illegal(token);
}, function parseMapField_line() {
parseInlineOptions(field);
});
parent.add(field);
}
function parseOneOf(parent, token) {
/* istanbul ignore if */
if (!nameRe.test(token = next()))
throw illegal(token, "name");
var oneof = new OneOf(applyCase(token));
ifBlock(oneof, function parseOneOf_block(token) {
if (token === "option") {
parseOption(oneof, token);
skip(";");
} else {
push(token);
parseField(oneof, "optional");
}
});
parent.add(oneof);
}
function parseEnum(parent, token) {
/* istanbul ignore if */
if (!nameRe.test(token = next()))
throw illegal(token, "name");
var enm = new Enum(token);
ifBlock(enm, function parseEnum_block(token) {
switch(token) {
case "option":
parseOption(enm, token);
skip(";");
break;
case "reserved":
readRanges(enm.reserved || (enm.reserved = []), true);
break;
default:
parseEnumValue(enm, token);
}
});
parent.add(enm);
}
function parseEnumValue(parent, token) {
/* istanbul ignore if */
if (!nameRe.test(token))
throw illegal(token, "name");
skip("=");
var value = parseId(next(), true),
dummy = {
options: undefined
};
dummy.setOption = function(name, value) {
if (this.options === undefined)
this.options = {};
this.options[name] = value;
};
dummy.setParsedOption = function(name, value, propName) {
// In order to not change existing behavior, only calling
// this for features
if (/^features$/.test(name)) {
return ReflectionObject.prototype.setParsedOption.call(dummy, name, value, propName);
}
return undefined;
};
ifBlock(dummy, function parseEnumValue_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(dummy, token); // skip
skip(";");
} else
throw illegal(token);
}, function parseEnumValue_line() {
parseInlineOptions(dummy); // skip
});
parent.add(token, value, dummy.comment, dummy.parsedOptions || dummy.options);
}
function parseOption(parent, token) {
var option;
var propName;
var isOption = true;
if (token === "option") {
token = next();
}
while (token !== "=") {
if (token === "(") {
var parensValue = next();
skip(")");
token = "(" + parensValue + ")";
}
if (isOption) {
isOption = false;
if (token.includes(".") && !token.includes("(")) {
var tokens = token.split(".");
option = tokens[0];
token = tokens[1];
continue;
}
option = token;
} else {
propName = propName ? propName += token : token;
}
token = next();
}
var name = propName ? option.concat(propName) : option;
var optionValue = parseOptionValue(parent, name);
propName = propName && propName[0] === "." ? propName.slice(1) : propName;
option = option && option[option.length - 1] === "." ? option.slice(0, -1) : option;
setParsedOption(parent, option, optionValue, propName);
}
function parseOptionValue(parent, name) {
// { a: "foo" b { c: "bar" } }
if (skip("{", true)) {
var objectResult = {};
while (!skip("}", true)) {
/* istanbul ignore if */
if (!nameRe.test(token = next())) {
throw illegal(token, "name");
}
if (token === null) {
throw illegal(token, "end of input");
}
var value;
var propName = token;
skip(":", true);
if (peek() === "{") {
// option (my_option) = {
// repeated_value: [ "foo", "bar" ]
// };
value = parseOptionValue(parent, name + "." + token);
} else if (peek() === "[") {
value = [];
var lastValue;
if (skip("[", true)) {
do {
lastValue = readValue(true);
value.push(lastValue);
} while (skip(",", true));
skip("]");
if (typeof lastValue !== "undefined") {
setOption(parent, name + "." + token, lastValue);
}
}
} else {
value = readValue(true);
setOption(parent, name + "." + token, value);
}
var prevValue = objectResult[propName];
if (prevValue)
value = [].concat(prevValue).concat(value);
objectResult[propName] = value;
// Semicolons and commas can be optional
skip(",", true);
skip(";", true);
}
return objectResult;
}
var simpleValue = readValue(true);
setOption(parent, name, simpleValue);
return simpleValue;
// Does not enforce a delimiter to be universal
}
function setOption(parent, name, value) {
if (parent.setOption)
parent.setOption(name, value);
}
function setParsedOption(parent, name, value, propName) {
if (parent.setParsedOption)
parent.setParsedOption(name, value, propName);
}
function parseInlineOptions(parent) {
if (skip("[", true)) {
do {
parseOption(parent, "option");
} while (skip(",", true));
skip("]");
}
return parent;
}
function parseService(parent, token) {
/* istanbul ignore if */
if (!nameRe.test(token = next()))
throw illegal(token, "service name");
var service = new Service(token);
ifBlock(service, function parseService_block(token) {
if (parseCommon(service, token)) {
return;
}
/* istanbul ignore else */
if (token === "rpc")
parseMethod(service, token);
else
throw illegal(token);
});
parent.add(service);
}
function parseMethod(parent, token) {
// Get the comment of the preceding line now (if one exists) in case the
// method is defined across multiple lines.
var commentText = cmnt();
var type = token;
/* istanbul ignore if */
if (!nameRe.test(token = next()))
throw illegal(token, "name");
var name = token,
requestType, requestStream,
responseType, responseStream;
skip("(");
if (skip("stream", true))
requestStream = true;
/* istanbul ignore if */
if (!typeRefRe.test(token = next()))
throw illegal(token);
requestType = token;
skip(")"); skip("returns"); skip("(");
if (skip("stream", true))
responseStream = true;
/* istanbul ignore if */
if (!typeRefRe.test(token = next()))
throw illegal(token);
responseType = token;
skip(")");
var method = new Method(name, type, requestType, responseType, requestStream, responseStream);
method.comment = commentText;
ifBlock(method, function parseMethod_block(token) {
/* istanbul ignore else */
if (token === "option") {
parseOption(method, token);
skip(";");
} else
throw illegal(token);
});
parent.add(method);
}
function parseExtension(parent, token) {
/* istanbul ignore if */
if (!typeRefRe.test(token = next()))
throw illegal(token, "reference");
var reference = token;
ifBlock(null, function parseExtension_block(token) {
switch (token) {
case "required":
case "repeated":
parseField(parent, token, reference);
break;
case "optional":
/* istanbul ignore if */
if (isProto3) {
parseField(parent, "proto3_optional", reference);
} else {
parseField(parent, "optional", reference);
}
break;
default:
/* istanbul ignore if */
if (!isProto3 && !edition || !typeRefRe.test(token))
throw illegal(token);
push(token);
parseField(parent, "optional", reference);
break;
}
});
}
var token;
while ((token = next()) !== null) {
switch (token) {
case "package":
/* istanbul ignore if */
if (!head)
throw illegal(token);
parsePackage();
break;
case "import":
/* istanbul ignore if */
if (!head)
throw illegal(token);
parseImport();
break;
case "syntax":
/* istanbul ignore if */
if (!head)
throw illegal(token);
parseSyntax();
break;
case "edition":
/* istanbul ignore if */
if (!head)
throw illegal(token);
parseEdition();
break;
case "option":
parseOption(ptr, token);
skip(";", true);
break;
default:
/* istanbul ignore else */
if (parseCommon(ptr, token)) {
head = false;
continue;
}
/* istanbul ignore next */
throw illegal(token);
}
}
parse.filename = null;
return {
"package" : pkg,
"imports" : imports,
weakImports : weakImports,
syntax : syntax,
root : root
};
}
/**
* Parses the given .proto source and returns an object with the parsed contents.
* @name parse
* @function
* @param {string} source Source contents
* @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {IParserResult} Parser result
* @property {string} filename=null Currently processing file name for error reporting, if known
* @property {IParseOptions} defaults Default {@link IParseOptions}
* @variation 2
*/