Debian Codegraph @

spec/unit/parser.spec.js

raw symbols SCIP
1 /* global peg */
2
3 "use strict";
4
5 describe("PEG.js grammar parser", function() {
6 var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false },
7 literalEfgh = { type: "literal", value: "efgh", ignoreCase: false },
8 literalIjkl = { type: "literal", value: "ijkl", ignoreCase: false },
9 literalMnop = { type: "literal", value: "mnop", ignoreCase: false },
10 semanticAnd = { type: "semantic_and", code: " code " },
11 semanticNot = { type: "semantic_not", code: " code " },
12 optional = { type: "optional", expression: literalAbcd },
13 zeroOrMore = { type: "zero_or_more", expression: literalAbcd },
14 oneOrMore = { type: "one_or_more", expression: literalAbcd },
15 textOptional = { type: "text", expression: optional },
16 simpleNotAbcd = { type: "simple_not", expression: literalAbcd },
17 simpleAndOptional = { type: "simple_and", expression: optional },
18 simpleNotOptional = { type: "simple_not", expression: optional },
19 labeledAbcd = { type: "labeled", label: "a", expression: literalAbcd },
20 labeledEfgh = { type: "labeled", label: "b", expression: literalEfgh },
21 labeledIjkl = { type: "labeled", label: "c", expression: literalIjkl },
22 labeledMnop = { type: "labeled", label: "d", expression: literalMnop },
23 labeledSimpleNot = { type: "labeled", label: "a", expression: simpleNotAbcd },
24 sequence = {
25 type: "sequence",
26 elements: [literalAbcd, literalEfgh, literalIjkl]
27 },
28 sequence2 = {
29 type: "sequence",
30 elements: [labeledAbcd, labeledEfgh]
31 },
32 sequence4 = {
33 type: "sequence",
34 elements: [labeledAbcd, labeledEfgh, labeledIjkl, labeledMnop]
35 },
36 groupLabeled = { type: "group", expression: labeledAbcd },
37 groupSequence = { type: "group", expression: sequence },
38 actionAbcd = { type: "action", expression: literalAbcd, code: " code " },
39 actionEfgh = { type: "action", expression: literalEfgh, code: " code " },
40 actionIjkl = { type: "action", expression: literalIjkl, code: " code " },
41 actionMnop = { type: "action", expression: literalMnop, code: " code " },
42 actionSequence = { type: "action", expression: sequence, code: " code " },
43 choice = {
44 type: "choice",
45 alternatives: [literalAbcd, literalEfgh, literalIjkl]
46 },
47 choice2 = {
48 type: "choice",
49 alternatives: [actionAbcd, actionEfgh]
50 },
51 choice4 = {
52 type: "choice",
53 alternatives: [actionAbcd, actionEfgh, actionIjkl, actionMnop]
54 },
55 named = { type: "named", name: "start rule", expression: literalAbcd },
56 ruleA = { type: "rule", name: "a", expression: literalAbcd },
57 ruleB = { type: "rule", name: "b", expression: literalEfgh },
58 ruleC = { type: "rule", name: "c", expression: literalIjkl },
59 ruleStart = { type: "rule", name: "start", expression: literalAbcd },
60 initializer = { type: "initializer", code: " code " };
61
62 function oneRuleGrammar(expression) {
63 return {
64 type: "grammar",
65 initializer: null,
66 rules: [{ type: "rule", name: "start", expression: expression }]
67 };
68 }
69
70 function actionGrammar(code) {
71 return oneRuleGrammar(
72 { type: "action", expression: literalAbcd, code: code }
73 );
74 }
75
76 function literalGrammar(value, ignoreCase) {
77 return oneRuleGrammar(
78 { type: "literal", value: value, ignoreCase: ignoreCase }
79 );
80 }
81
82 function classGrammar(parts, inverted, ignoreCase) {
83 return oneRuleGrammar({
84 type: "class",
85 parts: parts,
86 inverted: inverted,
87 ignoreCase: ignoreCase
88 });
89 }
90
91 function anyGrammar() {
92 return oneRuleGrammar({ type: "any" });
93 }
94
95 function ruleRefGrammar(name) {
96 return oneRuleGrammar({ type: "rule_ref", name: name });
97 }
98
99 var trivialGrammar = literalGrammar("abcd", false),
100 twoRuleGrammar = {
101 type: "grammar",
102 initializer: null,
103 rules: [ruleA, ruleB]
104 };
105
106 var stripLocation = (function() {
107 function buildVisitor(functions) {
108 return function(node) {
109 return functions[node.type].apply(null, arguments);
110 };
111 }
112
113 function stripLeaf(node) {
114 delete node.location;
115 }
116
117 function stripExpression(node) {
118 delete node.location;
119
120 strip(node.expression);
121 }
122
123 function stripChildren(property) {
124 return function(node) {
125 var i;
126
127 delete node.location;
128
129 for (i = 0; i < node[property].length; i++) {
130 strip(node[property][i]);
131 }
132 };
133 }
134
135 var strip = buildVisitor({
136 grammar: function(node) {
137 var i;
138
139 delete node.location;
140
141 if (node.initializer) {
142 strip(node.initializer);
143 }
144
145 for (i = 0; i < node.rules.length; i++) {
146 strip(node.rules[i]);
147 }
148 },
149
150 initializer: stripLeaf,
151 rule: stripExpression,
152 named: stripExpression,
153 choice: stripChildren("alternatives"),
154 action: stripExpression,
155 sequence: stripChildren("elements"),
156 labeled: stripExpression,
157 text: stripExpression,
158 simple_and: stripExpression,
159 simple_not: stripExpression,
160 optional: stripExpression,
161 zero_or_more: stripExpression,
162 one_or_more: stripExpression,
163 group: stripExpression,
164 semantic_and: stripLeaf,
165 semantic_not: stripLeaf,
166 rule_ref: stripLeaf,
167 literal: stripLeaf,
168 "class": stripLeaf,
169 any: stripLeaf
170 });
171
172 return strip;
173 })();
174
175 beforeEach(function() {
176 this.addMatchers({
177 toParseAs: function(expected) {
178 var result;
179
180 try {
181 result = peg.parser.parse(this.actual);
182 } catch (e) {
183 this.message = function() {
184 return "Expected " + jasmine.pp(this.actual) + " "
185 + "to parse as " + jasmine.pp(expected) + ", "
186 + "but it failed to parse with message "
187 + jasmine.pp(e.message) + ".";
188 };
189
190 return false;
191 }
192
193 stripLocation(result);
194
195 this.message = function() {
196 return "Expected " + jasmine.pp(this.actual) + " "
197 + (this.isNot ? "not " : "")
198 + "to parse as " + jasmine.pp(expected) + ", "
199 + "but it parsed as " + jasmine.pp(result) + ".";
200 };
201
202 return this.env.equals_(result, expected);
203 },
204
205 toFailToParse: function(details) {
206 var result, key;
207
208 try {
209 result = peg.parser.parse(this.actual);
210 } catch (e) {
211 if (this.isNot) {
212 this.message = function() {
213 return "Expected " + jasmine.pp(this.actual) + " to parse, "
214 + "but it failed with message "
215 + jasmine.pp(e.message) + ".";
216 };
217 } else {
218 if (details) {
219 for (key in details) {
220 if (details.hasOwnProperty(key)) {
221 if (!this.env.equals_(e[key], details[key])) {
222 this.message = function() {
223 return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
224 + (details ? " with details " + jasmine.pp(details) : "") + ", "
225 + "but " + jasmine.pp(key) + " "
226 + "is " + jasmine.pp(e[key]) + ".";
227 };
228
229 return false;
230 }
231 }
232 }
233 }
234 }
235
236 return true;
237 }
238
239 stripLocation(result);
240
241 this.message = function() {
242 return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
243 + (details ? " with details " + jasmine.pp(details) : "") + ", "
244 + "but it parsed as " + jasmine.pp(result) + ".";
245 };
246
247 return false;
248 }
249 });
250 });
251
252 /* Canonical Grammar is "a = \"abcd\"; b = \"efgh\"; c = \"ijkl\";". */
253 it("parses Grammar", function() {
254 expect('\na = "abcd";\n').toParseAs(
255 { type: "grammar", initializer: null, rules: [ruleA] }
256 );
257 expect('\na = "abcd";\nb = "efgh";\nc = "ijkl";\n').toParseAs(
258 { type: "grammar", initializer: null, rules: [ruleA, ruleB, ruleC] }
259 );
260 expect('\n{ code };\na = "abcd";\n').toParseAs(
261 { type: "grammar", initializer: initializer, rules: [ruleA] }
262 );
263 });
264
265 /* Canonical Initializer is "{ code }". */
266 it("parses Initializer", function() {
267 expect('{ code };start = "abcd"').toParseAs(
268 { type: "grammar", initializer: initializer, rules: [ruleStart] }
269 );
270 });
271
272 /* Canonical Rule is "a = \"abcd\";". */
273 it("parses Rule", function() {
274 expect('start\n=\n"abcd";').toParseAs(
275 oneRuleGrammar(literalAbcd)
276 );
277 expect('start\n"start rule"\n=\n"abcd";').toParseAs(
278 oneRuleGrammar(named)
279 );
280 });
281
282 /* Canonical Expression is "\"abcd\"". */
283 it("parses Expression", function() {
284 expect('start = "abcd" / "efgh" / "ijkl"').toParseAs(
285 oneRuleGrammar(choice)
286 );
287 });
288
289 /* Canonical ChoiceExpression is "\"abcd\" / \"efgh\" / \"ijkl\"". */
290 it("parses ChoiceExpression", function() {
291 expect('start = "abcd" { code }').toParseAs(
292 oneRuleGrammar(actionAbcd)
293 );
294 expect('start = "abcd" { code }\n/\n"efgh" { code }').toParseAs(
295 oneRuleGrammar(choice2)
296 );
297 expect(
298 'start = "abcd" { code }\n/\n"efgh" { code }\n/\n"ijkl" { code }\n/\n"mnop" { code }'
299 ).toParseAs(
300 oneRuleGrammar(choice4)
301 );
302 });
303
304 /* Canonical ActionExpression is "\"abcd\" { code }". */
305 it("parses ActionExpression", function() {
306 expect('start = "abcd" "efgh" "ijkl"').toParseAs(
307 oneRuleGrammar(sequence)
308 );
309 expect('start = "abcd" "efgh" "ijkl"\n{ code }').toParseAs(
310 oneRuleGrammar(actionSequence)
311 );
312 });
313
314 /* Canonical SequenceExpression is "\"abcd\" \"efgh\" \"ijkl\"". */
315 it("parses SequenceExpression", function() {
316 expect('start = a:"abcd"').toParseAs(
317 oneRuleGrammar(labeledAbcd)
318 );
319 expect('start = a:"abcd"\nb:"efgh"').toParseAs(
320 oneRuleGrammar(sequence2)
321 );
322 expect('start = a:"abcd"\nb:"efgh"\nc:"ijkl"\nd:"mnop"').toParseAs(
323 oneRuleGrammar(sequence4)
324 );
325 });
326
327 /* Canonical LabeledExpression is "a:\"abcd\"". */
328 it("parses LabeledExpression", function() {
329 expect('start = a\n:\n!"abcd"').toParseAs(oneRuleGrammar(labeledSimpleNot));
330 expect('start = !"abcd"' ).toParseAs(oneRuleGrammar(simpleNotAbcd));
331 });
332
333 /* Canonical PrefixedExpression is "!\"abcd\"". */
334 it("parses PrefixedExpression", function() {
335 expect('start = !\n"abcd"?' ).toParseAs(oneRuleGrammar(simpleNotOptional));
336 expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optional));
337 });
338
339 /* Canonical PrefixedOperator is "!". */
340 it("parses PrefixedOperator", function() {
341 expect('start = $"abcd"?').toParseAs(oneRuleGrammar(textOptional));
342 expect('start = &"abcd"?').toParseAs(oneRuleGrammar(simpleAndOptional));
343 expect('start = !"abcd"?').toParseAs(oneRuleGrammar(simpleNotOptional));
344 });
345
346 /* Canonical SuffixedExpression is "\"ebcd\"?". */
347 it("parses SuffixedExpression", function() {
348 expect('start = "abcd"\n?').toParseAs(oneRuleGrammar(optional));
349 expect('start = "abcd"' ).toParseAs(oneRuleGrammar(literalAbcd));
350 });
351
352 /* Canonical SuffixedOperator is "?". */
353 it("parses SuffixedOperator", function() {
354 expect('start = "abcd"?').toParseAs(oneRuleGrammar(optional));
355 expect('start = "abcd"*').toParseAs(oneRuleGrammar(zeroOrMore));
356 expect('start = "abcd"+').toParseAs(oneRuleGrammar(oneOrMore));
357 });
358
359 /* Canonical PrimaryExpression is "\"abcd\"". */
360 it("parses PrimaryExpression", function() {
361 expect('start = "abcd"' ).toParseAs(trivialGrammar);
362 expect('start = [a-d]' ).toParseAs(classGrammar([["a", "d"]], false, false));
363 expect('start = .' ).toParseAs(anyGrammar());
364 expect('start = a' ).toParseAs(ruleRefGrammar("a"));
365 expect('start = &{ code }').toParseAs(oneRuleGrammar(semanticAnd));
366
367 expect('start = (\na:"abcd"\n)' ).toParseAs(oneRuleGrammar(groupLabeled));
368 expect('start = (\n"abcd" "efgh" "ijkl"\n)').toParseAs(oneRuleGrammar(groupSequence));
369 expect('start = (\n"abcd"\n)' ).toParseAs(trivialGrammar);
370 });
371
372 /* Canonical RuleReferenceExpression is "a". */
373 it("parses RuleReferenceExpression", function() {
374 expect('start = a').toParseAs(ruleRefGrammar("a"));
375
376 expect('start = a\n=' ).toFailToParse();
377 expect('start = a\n"abcd"\n=').toFailToParse();
378 });
379
380 /* Canonical SemanticPredicateExpression is "!{ code }". */
381 it("parses SemanticPredicateExpression", function() {
382 expect('start = !\n{ code }').toParseAs(oneRuleGrammar(semanticNot));
383 });
384
385 /* Canonical SemanticPredicateOperator is "!". */
386 it("parses SemanticPredicateOperator", function() {
387 expect('start = &{ code }').toParseAs(oneRuleGrammar(semanticAnd));
388 expect('start = !{ code }').toParseAs(oneRuleGrammar(semanticNot));
389 });
390
391 /* The SourceCharacter rule is not tested. */
392
393 /* Canonical WhiteSpace is " ". */
394 it("parses WhiteSpace", function() {
395 expect('start =\t"abcd"' ).toParseAs(trivialGrammar);
396 expect('start =\x0B"abcd"' ).toParseAs(trivialGrammar); // no "\v" in IE
397 expect('start =\f"abcd"' ).toParseAs(trivialGrammar);
398 expect('start = "abcd"' ).toParseAs(trivialGrammar);
399 expect('start =\u00A0"abcd"').toParseAs(trivialGrammar);
400 expect('start =\uFEFF"abcd"').toParseAs(trivialGrammar);
401 expect('start =\u1680"abcd"').toParseAs(trivialGrammar);
402 });
403
404 /* Canonical LineTerminator is "\n". */
405 it("parses LineTerminator", function() {
406 expect('start = "\n"' ).toFailToParse();
407 expect('start = "\r"' ).toFailToParse();
408 expect('start = "\u2028"').toFailToParse();
409 expect('start = "\u2029"').toFailToParse();
410 });
411
412 /* Canonical LineTerminatorSequence is "\r\n". */
413 it("parses LineTerminatorSequence", function() {
414 expect('start =\n"abcd"' ).toParseAs(trivialGrammar);
415 expect('start =\r\n"abcd"' ).toParseAs(trivialGrammar);
416 expect('start =\r"abcd"' ).toParseAs(trivialGrammar);
417 expect('start =\u2028"abcd"').toParseAs(trivialGrammar);
418 expect('start =\u2029"abcd"').toParseAs(trivialGrammar);
419 });
420
421 // Canonical Comment is "/* comment */".
422 it("parses Comment", function() {
423 expect('start =// comment\n"abcd"' ).toParseAs(trivialGrammar);
424 expect('start =/* comment */"abcd"').toParseAs(trivialGrammar);
425 });
426
427 // Canonical MultiLineComment is "/* comment */".
428 it("parses MultiLineComment", function() {
429 expect('start =/**/"abcd"' ).toParseAs(trivialGrammar);
430 expect('start =/*a*/"abcd"' ).toParseAs(trivialGrammar);
431 expect('start =/*abc*/"abcd"').toParseAs(trivialGrammar);
432
433 expect('start =/**/*/"abcd"').toFailToParse();
434 });
435
436 // Canonical MultiLineCommentNoLineTerminator is "/* comment */".
437 it("parses MultiLineCommentNoLineTerminator", function() {
438 expect('a = "abcd"/**/\r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
439 expect('a = "abcd"/*a*/\r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
440 expect('a = "abcd"/*abc*/\r\nb = "efgh"').toParseAs(twoRuleGrammar);
441
442 expect('a = "abcd"/**/*/\r\nb = "efgh"').toFailToParse();
443 expect('a = "abcd"/*\n*/\r\nb = "efgh"').toFailToParse();
444 });
445
446 /* Canonical SingleLineComment is "// comment". */
447 it("parses SingleLineComment", function() {
448 expect('start =//\n"abcd"' ).toParseAs(trivialGrammar);
449 expect('start =//a\n"abcd"' ).toParseAs(trivialGrammar);
450 expect('start =//abc\n"abcd"').toParseAs(trivialGrammar);
451
452 expect('start =//\n@\n"abcd"').toFailToParse();
453 });
454
455 /* Canonical Identifier is "a". */
456 it("parses Identifier", function() {
457 expect('start = a:"abcd"').toParseAs(oneRuleGrammar(labeledAbcd));
458 });
459
460 /* Canonical IdentifierName is "a". */
461 it("parses IdentifierName", function() {
462 expect('start = a' ).toParseAs(ruleRefGrammar("a"));
463 expect('start = ab' ).toParseAs(ruleRefGrammar("ab"));
464 expect('start = abcd').toParseAs(ruleRefGrammar("abcd"));
465 });
466
467 /* Canonical IdentifierStart is "a". */
468 it("parses IdentifierStart", function() {
469 expect('start = a' ).toParseAs(ruleRefGrammar("a"));
470 expect('start = $' ).toParseAs(ruleRefGrammar("$"));
471 expect('start = _' ).toParseAs(ruleRefGrammar("_"));
472 expect('start = \\u0061').toParseAs(ruleRefGrammar("a"));
473 });
474
475 /* Canonical IdentifierPart is "a". */
476 it("parses IdentifierPart", function() {
477 expect('start = aa' ).toParseAs(ruleRefGrammar("aa"));
478 expect('start = a\u0300').toParseAs(ruleRefGrammar("a\u0300"));
479 expect('start = a0' ).toParseAs(ruleRefGrammar("a0"));
480 expect('start = a\u203F').toParseAs(ruleRefGrammar("a\u203F"));
481 expect('start = a\u200C').toParseAs(ruleRefGrammar("a\u200C"));
482 expect('start = a\u200D').toParseAs(ruleRefGrammar("a\u200D"));
483 });
484
485 /* Unicode rules and reserved word rules are not tested. */
486
487 /* Canonical LiteralMatcher is "\"abcd\"". */
488 it("parses LiteralMatcher", function() {
489 expect('start = "abcd"' ).toParseAs(literalGrammar("abcd", false));
490 expect('start = "abcd"i').toParseAs(literalGrammar("abcd", true));
491 });
492
493 /* Canonical StringLiteral is "\"abcd\"". */
494 it("parses StringLiteral", function() {
495 expect('start = ""' ).toParseAs(literalGrammar("", false));
496 expect('start = "a"' ).toParseAs(literalGrammar("a", false));
497 expect('start = "abc"').toParseAs(literalGrammar("abc", false));
498
499 expect("start = ''" ).toParseAs(literalGrammar("", false));
500 expect("start = 'a'" ).toParseAs(literalGrammar("a", false));
501 expect("start = 'abc'").toParseAs(literalGrammar("abc", false));
502 });
503
504 /* Canonical DoubleStringCharacter is "a". */
505 it("parses DoubleStringCharacter", function() {
506 expect('start = "a"' ).toParseAs(literalGrammar("a", false));
507 expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false));
508 expect('start = "\\\n"').toParseAs(literalGrammar("", false));
509
510 expect('start = """' ).toFailToParse();
511 expect('start = "\\"').toFailToParse();
512 expect('start = "\n"').toFailToParse();
513 });
514
515 /* Canonical SingleStringCharacter is "a". */
516 it("parses SingleStringCharacter", function() {
517 expect("start = 'a'" ).toParseAs(literalGrammar("a", false));
518 expect("start = '\\n'" ).toParseAs(literalGrammar("\n", false));
519 expect("start = '\\\n'").toParseAs(literalGrammar("", false));
520
521 expect("start = '''" ).toFailToParse();
522 expect("start = '\\'").toFailToParse();
523 expect("start = '\n'").toFailToParse();
524 });
525
526 /* Canonical CharacterClassMatcher is "[a-d]". */
527 it("parses CharacterClassMatcher", function() {
528 expect('start = []').toParseAs(
529 classGrammar([], false, false)
530 );
531 expect('start = [a-d]').toParseAs(
532 classGrammar([["a", "d"]], false, false)
533 );
534 expect('start = [a]').toParseAs(
535 classGrammar(["a"], false, false)
536 );
537 expect('start = [a-de-hi-l]').toParseAs(
538 classGrammar(
539 [["a", "d"], ["e", "h"], ["i", "l"]],
540 false,
541 false
542 )
543 );
544 expect('start = [^a-d]').toParseAs(
545 classGrammar([["a", "d"]], true, false)
546 );
547 expect('start = [a-d]i').toParseAs(
548 classGrammar([["a", "d"]], false, true)
549 );
550
551 expect('start = [\\\n]').toParseAs(
552 classGrammar([], false, false)
553 );
554 });
555
556 /* Canonical ClassCharacterRange is "a-d". */
557 it("parses ClassCharacterRange", function() {
558 expect('start = [a-d]').toParseAs(classGrammar([["a", "d"]], false, false));
559
560 expect('start = [a-a]').toParseAs(classGrammar([["a", "a"]], false, false));
561 expect('start = [b-a]').toFailToParse({
562 message: "Invalid character range: b-a."
563 });
564 });
565
566 /* Canonical ClassCharacter is "a". */
567 it("parses ClassCharacter", function() {
568 expect('start = [a]' ).toParseAs(classGrammar(["a"], false, false));
569 expect('start = [\\n]' ).toParseAs(classGrammar(["\n"], false, false));
570 expect('start = [\\\n]').toParseAs(classGrammar([], false, false));
571
572 expect('start = []]' ).toFailToParse();
573 expect('start = [\\]').toFailToParse();
574 expect('start = [\n]').toFailToParse();
575 });
576
577 /* Canonical LineContinuation is "\\\n". */
578 it("parses LineContinuation", function() {
579 expect('start = "\\\r\n"').toParseAs(literalGrammar("", false));
580 });
581
582 /* Canonical EscapeSequence is "n". */
583 it("parses EscapeSequence", function() {
584 expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false));
585 expect('start = "\\0"' ).toParseAs(literalGrammar("\x00", false));
586 expect('start = "\\xFF"' ).toParseAs(literalGrammar("\xFF", false));
587 expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false));
588
589 expect('start = "\\09"').toFailToParse();
590 });
591
592 /* Canonical CharacterEscapeSequence is "n". */
593 it("parses CharacterEscapeSequence", function() {
594 expect('start = "\\n"').toParseAs(literalGrammar("\n", false));
595 expect('start = "\\a"').toParseAs(literalGrammar("a", false));
596 });
597
598 /* Canonical SingleEscapeCharacter is "n". */
599 it("parses SingleEscapeCharacter", function() {
600 expect('start = "\\\'"').toParseAs(literalGrammar("'", false));
601 expect('start = "\\""' ).toParseAs(literalGrammar('"', false));
602 expect('start = "\\\\"').toParseAs(literalGrammar("\\", false));
603 expect('start = "\\b"' ).toParseAs(literalGrammar("\b", false));
604 expect('start = "\\f"' ).toParseAs(literalGrammar("\f", false));
605 expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false));
606 expect('start = "\\r"' ).toParseAs(literalGrammar("\r", false));
607 expect('start = "\\t"' ).toParseAs(literalGrammar("\t", false));
608 expect('start = "\\v"' ).toParseAs(literalGrammar("\x0B", false)); // no "\v" in IE
609 });
610
611 /* Canonical NonEscapeCharacter is "a". */
612 it("parses NonEscapeCharacter", function() {
613 expect('start = "\\a"').toParseAs(literalGrammar("a", false));
614
615 /*
616 * The negative predicate is impossible to test with PEG.js grammar
617 * structure.
618 */
619 });
620
621 /*
622 * The EscapeCharacter rule is impossible to test with PEG.js grammar
623 * structure.
624 */
625
626 /* Canonical HexEscapeSequence is "xFF". */
627 it("parses HexEscapeSequence", function() {
628 expect('start = "\\xFF"').toParseAs(literalGrammar("\xFF", false));
629 });
630
631 /* Canonical UnicodeEscapeSequence is "uFFFF". */
632 it("parses UnicodeEscapeSequence", function() {
633 expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false));
634 });
635
636 /* Digit rules are not tested. */
637
638 /* Canonical AnyMatcher is ".". */
639 it("parses AnyMatcher", function() {
640 expect('start = .').toParseAs(anyGrammar());
641 });
642
643 /* Canonical CodeBlock is "{ code }". */
644 it("parses CodeBlock", function() {
645 expect('start = "abcd" { code }').toParseAs(actionGrammar(" code "));
646 });
647
648 /* Canonical Code is " code ". */
649 it("parses Code", function() {
650 expect('start = "abcd" {a}' ).toParseAs(actionGrammar("a"));
651 expect('start = "abcd" {abc}' ).toParseAs(actionGrammar("abc"));
652 expect('start = "abcd" {{a}}' ).toParseAs(actionGrammar("{a}"));
653 expect('start = "abcd" {{a}{b}{c}}').toParseAs(actionGrammar("{a}{b}{c}"));
654
655 expect('start = "abcd" {{}').toFailToParse();
656 expect('start = "abcd" {}}').toFailToParse();
657 });
658
659 /* Unicode character category rules and token rules are not tested. */
660
661 /* Canonical __ is "\n". */
662 it("parses __", function() {
663 expect('start ="abcd"' ).toParseAs(trivialGrammar);
664 expect('start = "abcd"' ).toParseAs(trivialGrammar);
665 expect('start =\r\n"abcd"' ).toParseAs(trivialGrammar);
666 expect('start =/* comment */"abcd"').toParseAs(trivialGrammar);
667 expect('start = "abcd"' ).toParseAs(trivialGrammar);
668 });
669
670 /* Canonical _ is " ". */
671 it("parses _", function() {
672 expect('a = "abcd"\r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
673 expect('a = "abcd" \r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
674 expect('a = "abcd"/* comment */\r\nb = "efgh"').toParseAs(twoRuleGrammar);
675 expect('a = "abcd" \r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
676 });
677
678 /* Canonical EOS is ";". */
679 it("parses EOS", function() {
680 expect('a = "abcd"\n;b = "efgh"' ).toParseAs(twoRuleGrammar);
681 expect('a = "abcd" \r\nb = "efgh"' ).toParseAs(twoRuleGrammar);
682 expect('a = "abcd" // comment\r\nb = "efgh"').toParseAs(twoRuleGrammar);
683 expect('a = "abcd"\nb = "efgh"' ).toParseAs(twoRuleGrammar);
684 });
685
686 /* Canonical EOF is the end of input. */
687 it("parses EOF", function() {
688 expect('start = "abcd"\n').toParseAs(trivialGrammar);
689 });
690 });