2011-02-13 12:22:56 +01:00
|
|
|
(function() {
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-08-21 15:30:23 +02:00
|
|
|
module("PEG.compiler");
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
function testWithVaryingTrackLineAndColumn(name, callback) {
|
|
|
|
test(
|
|
|
|
name + " (with trackLineAndColumn: false) ",
|
|
|
|
function() { callback({ trackLineAndColumn: false }); }
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
name + " (with trackLineAndColumn: true) ",
|
|
|
|
function() { callback({ trackLineAndColumn: true }); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
testWithVaryingTrackLineAndColumn("initializer", function(options) {
|
2010-06-08 14:49:13 +02:00
|
|
|
var variableInActionParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ a = 42; }; start = "a" { return a; }',
|
|
|
|
options
|
2010-06-08 09:15:09 +02:00
|
|
|
);
|
2010-06-08 14:49:13 +02:00
|
|
|
parses(variableInActionParser, "a", 42);
|
2010-06-08 09:15:09 +02:00
|
|
|
|
2010-06-08 14:49:13 +02:00
|
|
|
var functionInActionParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ function f() { return 42; } }; start = "a" { return f(); }',
|
|
|
|
options
|
2010-06-08 09:15:09 +02:00
|
|
|
);
|
2010-06-08 14:49:13 +02:00
|
|
|
parses(functionInActionParser, "a", 42);
|
|
|
|
|
|
|
|
var variableInSemanticAndParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ a = 42; }; start = "a" &{ return a === 42; }',
|
|
|
|
options
|
2010-06-08 14:49:13 +02:00
|
|
|
);
|
|
|
|
parses(variableInSemanticAndParser, "a", ["a", ""]);
|
|
|
|
|
|
|
|
var functionInSemanticAndParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ function f() { return 42; } }; start = "a" &{ return f() === 42; }',
|
|
|
|
options
|
2010-06-08 14:49:13 +02:00
|
|
|
);
|
|
|
|
parses(functionInSemanticAndParser, "a", ["a", ""]);
|
|
|
|
|
|
|
|
var variableInSemanticNotParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ a = 42; }; start = "a" !{ return a !== 42; }',
|
|
|
|
options
|
2010-06-08 14:49:13 +02:00
|
|
|
);
|
|
|
|
parses(variableInSemanticNotParser, "a", ["a", ""]);
|
|
|
|
|
|
|
|
var functionInSemanticNotParser = PEG.buildParser(
|
2012-03-25 13:31:28 +02:00
|
|
|
'{ function f() { return 42; } }; start = "a" !{ return f() !== 42; }',
|
|
|
|
options
|
2010-06-08 14:49:13 +02:00
|
|
|
);
|
|
|
|
parses(functionInSemanticNotParser, "a", ["a", ""]);
|
2010-06-08 09:15:09 +02:00
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("cache", function(options) {
|
2012-04-16 18:27:31 +02:00
|
|
|
var grammar = [
|
|
|
|
'{ var n = 0; }',
|
|
|
|
'start = (a "b") / (a "c") { return n; }',
|
2012-04-17 12:00:58 +02:00
|
|
|
'a = "a" { n++; }'
|
2012-04-16 18:27:31 +02:00
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
/* Without cache */
|
|
|
|
|
|
|
|
parses(PEG.buildParser(grammar, options), "ac", 2);
|
|
|
|
|
|
|
|
options.cache = false;
|
|
|
|
parses(PEG.buildParser(grammar, options), "ac", 2);
|
|
|
|
|
|
|
|
/* With cache */
|
|
|
|
|
|
|
|
options.cache = true;
|
|
|
|
parses(PEG.buildParser(grammar, options), "ac", 1);
|
2010-03-07 20:41:02 +01:00
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("indempotence", function(options) {
|
|
|
|
var parser1 = PEG.buildParser('start = "abcd"', options);
|
|
|
|
var parser2 = PEG.buildParser('start = "abcd"', options);
|
2010-03-07 21:14:07 +01:00
|
|
|
|
|
|
|
strictEqual(parser1.toSource(), parser2.toSource());
|
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("error details", function(options) {
|
|
|
|
var literalParser = PEG.buildParser('start = "abcd"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
literalParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"abcd\""],
|
|
|
|
null,
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected "abcd" but end of input found.'
|
|
|
|
);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
literalParser,
|
|
|
|
"efgh",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"abcd\""],
|
|
|
|
"e",
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected "abcd" but "e" found.'
|
|
|
|
);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
literalParser,
|
|
|
|
"abcde",
|
2012-02-12 12:20:15 +01:00
|
|
|
[],
|
|
|
|
"e",
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected end of input but "e" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var classParser = PEG.buildParser('start = [a-d]', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-21 15:24:03 +01:00
|
|
|
classParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["[a-d]"],
|
|
|
|
null,
|
2010-03-21 15:24:03 +01:00
|
|
|
'Expected [a-d] but end of input found.'
|
|
|
|
);
|
2012-03-25 13:31:28 +02:00
|
|
|
var negativeClassParser = PEG.buildParser('start = [^a-d]', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-21 15:24:03 +01:00
|
|
|
negativeClassParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["[^a-d]"],
|
|
|
|
null,
|
2010-03-21 15:24:03 +01:00
|
|
|
'Expected [^a-d] but end of input found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var anyParser = PEG.buildParser('start = .', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
anyParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["any character"],
|
|
|
|
null,
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected any character but end of input found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var namedRuleWithLiteralParser = PEG.buildParser(
|
|
|
|
'start "digit" = [0-9]',
|
|
|
|
options
|
|
|
|
);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
namedRuleWithLiteralParser,
|
|
|
|
"a",
|
2012-02-12 12:20:15 +01:00
|
|
|
["digit"],
|
|
|
|
"a",
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected digit but "a" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var namedRuleWithAnyParser = PEG.buildParser('start "whatever" = .', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
namedRuleWithAnyParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["whatever"],
|
|
|
|
null,
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected whatever but end of input found.'
|
|
|
|
);
|
|
|
|
|
|
|
|
var namedRuleWithNamedRuleParser = PEG.buildParser([
|
2010-05-23 10:41:43 +02:00
|
|
|
'start "digits" = digit+',
|
|
|
|
'digit "digit" = [0-9]'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
namedRuleWithNamedRuleParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["digits"],
|
|
|
|
null,
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected digits but end of input found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var choiceParser1 = PEG.buildParser('start = "a" / "b" / "c"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-08 09:58:23 +01:00
|
|
|
choiceParser1,
|
2010-03-07 20:41:02 +01:00
|
|
|
"def",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"a\"", "\"b\"", "\"c\""],
|
|
|
|
"d",
|
2010-03-07 20:41:02 +01:00
|
|
|
'Expected "a", "b" or "c" but "d" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var choiceParser2 = PEG.buildParser('start = "a" "b" "c" / "a"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-08 09:58:23 +01:00
|
|
|
choiceParser2,
|
|
|
|
"abd",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"c\""],
|
|
|
|
"d",
|
2010-03-08 09:58:23 +01:00
|
|
|
'Expected "c" but "d" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var simpleNotParser = PEG.buildParser('start = !"a" "b"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-06-08 14:49:13 +02:00
|
|
|
simpleNotParser,
|
2010-03-08 12:15:52 +01:00
|
|
|
"c",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"b\""],
|
|
|
|
"c",
|
2010-03-08 12:15:52 +01:00
|
|
|
'Expected "b" but "c" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var simpleAndParser = PEG.buildParser('start = &"a" [a-b]', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-06-08 14:49:13 +02:00
|
|
|
simpleAndParser,
|
2010-03-08 12:15:52 +01:00
|
|
|
"c",
|
2012-02-12 12:20:15 +01:00
|
|
|
[],
|
|
|
|
"c",
|
2010-03-08 12:15:52 +01:00
|
|
|
'Expected end of input but "c" found.'
|
|
|
|
);
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var emptyParser = PEG.buildParser('start = ', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-03-07 20:41:02 +01:00
|
|
|
emptyParser,
|
|
|
|
"something",
|
2012-02-12 12:20:15 +01:00
|
|
|
[],
|
|
|
|
"s",
|
2010-03-08 09:58:23 +01:00
|
|
|
'Expected end of input but "s" found.'
|
2010-03-07 20:41:02 +01:00
|
|
|
);
|
2010-04-11 15:30:02 +02:00
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var duplicateErrorParser = PEG.buildParser('start = "a" / "a"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-04-11 15:30:02 +02:00
|
|
|
duplicateErrorParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"a\""],
|
|
|
|
null,
|
2010-04-11 15:30:02 +02:00
|
|
|
'Expected "a" but end of input found.'
|
|
|
|
);
|
2010-04-11 15:34:30 +02:00
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
var unsortedErrorsParser = PEG.buildParser('start = "b" / "a"', options);
|
2012-02-12 12:20:15 +01:00
|
|
|
doesNotParseWithDetails(
|
2010-04-11 15:34:30 +02:00
|
|
|
unsortedErrorsParser,
|
|
|
|
"",
|
2012-02-12 12:20:15 +01:00
|
|
|
["\"a\"", "\"b\""],
|
|
|
|
null,
|
2010-04-11 15:34:30 +02:00
|
|
|
'Expected "a" or "b" but end of input found.'
|
|
|
|
);
|
2010-03-07 20:41:02 +01:00
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("error positions", function(options) {
|
|
|
|
var simpleParser = PEG.buildParser('start = "a"', options);
|
2011-10-01 18:46:57 +02:00
|
|
|
|
|
|
|
/* Regular match failure */
|
2012-02-11 12:42:26 +01:00
|
|
|
doesNotParseWithPos(simpleParser, "b", 0, 1, 1);
|
2011-10-01 18:46:57 +02:00
|
|
|
|
|
|
|
/* Trailing input */
|
2012-02-11 12:42:26 +01:00
|
|
|
doesNotParseWithPos(simpleParser, "ab", 1, 1, 2);
|
2011-10-01 18:46:57 +02:00
|
|
|
|
|
|
|
var digitsParser = PEG.buildParser([
|
2010-05-31 12:40:24 +02:00
|
|
|
'start = line (("\\r" / "\\n" / "\\u2028" / "\\u2029")+ line)*',
|
|
|
|
'line = digits (" "+ digits)*',
|
|
|
|
'digits = digits:[0-9]+ { return digits.join(""); }'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2012-02-11 12:42:26 +01:00
|
|
|
doesNotParseWithPos(digitsParser, "1\n2\n\n3\n\n\n4 5 x", 13, 7, 5);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
/* Non-Unix newlines */
|
2012-02-11 12:42:26 +01:00
|
|
|
doesNotParseWithPos(digitsParser, "1\rx", 2, 2, 1); // Old Mac
|
|
|
|
doesNotParseWithPos(digitsParser, "1\r\nx", 3, 2, 1); // Windows
|
|
|
|
doesNotParseWithPos(digitsParser, "1\n\rx", 3, 3, 1); // mismatched
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
/* Strange newlines */
|
2012-02-11 12:42:26 +01:00
|
|
|
doesNotParseWithPos(digitsParser, "1\u2028x", 2, 2, 1); // line separator
|
|
|
|
doesNotParseWithPos(digitsParser, "1\u2029x", 2, 2, 1); // paragraph separator
|
2010-03-07 20:41:02 +01:00
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("start rule", function(options) {
|
2011-03-29 15:40:34 +02:00
|
|
|
var parser = PEG.buildParser([
|
|
|
|
'a = .* { return "alpha"; }',
|
|
|
|
'b = .* { return "beta"; }'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2011-03-29 15:40:34 +02:00
|
|
|
|
|
|
|
/* Default start rule = the first one */
|
|
|
|
parses(parser, "whatever", "alpha");
|
|
|
|
|
|
|
|
/* Explicit specification of the start rule */
|
|
|
|
parsesWithStartRule(parser, "whatever", "a", "alpha");
|
|
|
|
parsesWithStartRule(parser, "whatever", "b", "beta");
|
|
|
|
|
|
|
|
/* Invalid rule name */
|
|
|
|
raises(
|
2011-09-14 12:32:53 +02:00
|
|
|
function() { parser.parse("whatever", "c"); },
|
2011-03-29 15:40:34 +02:00
|
|
|
function(e) {
|
|
|
|
return e instanceof Error && e.message === "Invalid rule name: \"c\".";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2010-03-07 20:41:02 +01:00
|
|
|
/*
|
|
|
|
* Following examples are from Wikipedia, see
|
|
|
|
* http://en.wikipedia.org/w/index.php?title=Parsing_expression_grammar&oldid=335106938.
|
|
|
|
*/
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("arithmetics", function(options) {
|
2010-03-07 20:41:02 +01:00
|
|
|
/*
|
|
|
|
* Value ← [0-9]+ / '(' Expr ')'
|
|
|
|
* Product ← Value (('*' / '/') Value)*
|
|
|
|
* Sum ← Product (('+' / '-') Product)*
|
|
|
|
* Expr ← Sum
|
|
|
|
*/
|
|
|
|
var parser = PEG.buildParser([
|
2010-06-08 11:03:28 +02:00
|
|
|
'Expr = Sum',
|
|
|
|
'Sum = head:Product tail:(("+" / "-") Product)* {',
|
2010-05-31 12:40:24 +02:00
|
|
|
' var result = head;',
|
|
|
|
' for (var i = 0; i < tail.length; i++) {',
|
2010-06-08 11:03:28 +02:00
|
|
|
' if (tail[i][0] == "+") { result += tail[i][1]; }',
|
|
|
|
' if (tail[i][0] == "-") { result -= tail[i][1]; }',
|
2010-03-07 20:41:02 +01:00
|
|
|
' }',
|
|
|
|
' return result;',
|
|
|
|
' }',
|
2010-06-08 11:03:28 +02:00
|
|
|
'Product = head:Value tail:(("*" / "/") Value)* {',
|
2010-05-31 12:40:24 +02:00
|
|
|
' var result = head;',
|
|
|
|
' for (var i = 0; i < tail.length; i++) {',
|
2010-06-08 11:03:28 +02:00
|
|
|
' if (tail[i][0] == "*") { result *= tail[i][1]; }',
|
|
|
|
' if (tail[i][0] == "/") { result /= tail[i][1]; }',
|
2010-03-07 20:41:02 +01:00
|
|
|
' }',
|
|
|
|
' return result;',
|
|
|
|
' }',
|
2010-06-08 11:03:28 +02:00
|
|
|
'Value = digits:[0-9]+ { return parseInt(digits.join("")); }',
|
|
|
|
' / "(" expr:Expr ")" { return expr; }'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
/* Test "value" rule. */
|
|
|
|
parses(parser, "0", 0);
|
|
|
|
parses(parser, "123", 123);
|
|
|
|
parses(parser, "(42+43)", 42+43);
|
|
|
|
|
|
|
|
/* Test "product" rule. */
|
2010-03-19 10:28:24 +01:00
|
|
|
parses(parser, "42", 42);
|
2010-03-07 20:41:02 +01:00
|
|
|
parses(parser, "42*43", 42*43);
|
|
|
|
parses(parser, "42*43*44*45", 42*43*44*45);
|
2010-03-19 10:28:24 +01:00
|
|
|
parses(parser, "42/43", 42/43);
|
|
|
|
parses(parser, "42/43/44/45", 42/43/44/45);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
/* Test "sum" rule. */
|
2010-03-19 10:28:24 +01:00
|
|
|
parses(parser, "42*43", 42*43);
|
2010-03-07 20:41:02 +01:00
|
|
|
parses(parser, "42*43+44*45", 42*43+44*45);
|
|
|
|
parses(parser, "42*43+44*45+46*47+48*49", 42*43+44*45+46*47+48*49);
|
2010-03-19 10:28:24 +01:00
|
|
|
parses(parser, "42*43-44*45", 42*43-44*45);
|
|
|
|
parses(parser, "42*43-44*45-46*47-48*49", 42*43-44*45-46*47-48*49);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
/* Test "expr" rule. */
|
|
|
|
parses(parser, "42+43", 42+43);
|
|
|
|
|
|
|
|
/* Complex test */
|
|
|
|
parses(parser, "(1+2)*(3+4)",(1+2)*(3+4));
|
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("non-context-free language", function(options) {
|
2010-03-07 20:41:02 +01:00
|
|
|
/* The following parsing expression grammar describes the classic
|
|
|
|
* non-context-free language { a^n b^n c^n : n >= 1 }:
|
|
|
|
*
|
|
|
|
* S ← &(A c) a+ B !(a/b/c)
|
|
|
|
* A ← a A? b
|
|
|
|
* B ← b B? c
|
|
|
|
*/
|
|
|
|
var parser = PEG.buildParser([
|
2010-05-31 12:40:24 +02:00
|
|
|
'S = &(A "c") a:"a"+ B:B !("a" / "b" / "c") { return a.join("") + B; }',
|
|
|
|
'A = a:"a" A:A? b:"b" { return a + A + b; }',
|
2011-09-14 12:35:21 +02:00
|
|
|
'B = b:"b" B:B? c:"c" { return b + B + c; }'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
parses(parser, "abc", "abc");
|
|
|
|
parses(parser, "aaabbbccc", "aaabbbccc");
|
|
|
|
doesNotParse(parser, "aabbbccc");
|
|
|
|
doesNotParse(parser, "aaaabbbccc");
|
|
|
|
doesNotParse(parser, "aaabbccc");
|
|
|
|
doesNotParse(parser, "aaabbbbccc");
|
|
|
|
doesNotParse(parser, "aaabbbcc");
|
|
|
|
doesNotParse(parser, "aaabbbcccc");
|
|
|
|
});
|
|
|
|
|
2012-03-25 13:31:28 +02:00
|
|
|
testWithVaryingTrackLineAndColumn("nested comments", function(options) {
|
2010-03-07 20:41:02 +01:00
|
|
|
/*
|
|
|
|
* Begin ← "(*"
|
|
|
|
* End ← "*)"
|
|
|
|
* C ← Begin N* End
|
|
|
|
* N ← C / (!Begin !End Z)
|
|
|
|
* Z ← any single character
|
|
|
|
*/
|
|
|
|
var parser = PEG.buildParser([
|
2010-05-31 12:40:24 +02:00
|
|
|
'C = begin:Begin ns:N* end:End { return begin + ns.join("") + end; }',
|
2010-05-23 10:41:43 +02:00
|
|
|
'N = C',
|
2010-05-31 12:40:24 +02:00
|
|
|
' / !Begin !End z:Z { return z; }',
|
2010-06-08 11:03:28 +02:00
|
|
|
'Z = .',
|
|
|
|
'Begin = "(*"',
|
|
|
|
'End = "*)"'
|
2012-03-25 13:31:28 +02:00
|
|
|
].join("\n"), options);
|
2010-03-07 20:41:02 +01:00
|
|
|
|
|
|
|
parses(parser, "(**)", "(**)");
|
|
|
|
parses(parser, "(*abc*)", "(*abc*)");
|
|
|
|
parses(parser, "(*(**)*)", "(*(**)*)");
|
|
|
|
parses(
|
|
|
|
parser,
|
|
|
|
"(*abc(*def*)ghi(*(*(*jkl*)*)*)mno*)",
|
|
|
|
"(*abc(*def*)ghi(*(*(*jkl*)*)*)mno*)"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2011-02-13 12:22:56 +01:00
|
|
|
})();
|