diff --git a/lib/compiler.js b/lib/compiler.js index 12ee525..7520f08 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1274,10 +1274,50 @@ with (PEG.Grammar) { "__", null, new ZeroOrMore( - new Choice([new RuleRef("whitespace"), new RuleRef("eol")]) + new Choice([ + new RuleRef("whitespace"), + new RuleRef("eol"), + new RuleRef("comment") + ]) ) ), + /* Modelled after ECMA-262, 5th ed., 7.4. */ + comment: + new Rule( + "comment", + "comment", + new Choice([ + new RuleRef("singleLineComment"), + new RuleRef("multiLineComment") + ]) + ), + + singleLineComment: + new Rule( + "singleLineComment", + null, + new Sequence([ + new Literal("//"), + new ZeroOrMore( + new Sequence([new NotPredicate(new RuleRef("eolChar")), new Any()]) + ) + ]) + ), + + multiLineComment: + new Rule( + "multiLineComment", + null, + new Sequence([ + new Literal("/*"), + new ZeroOrMore( + new Sequence([new NotPredicate(new Literal("*/")), new Any()]) + ), + new Literal("*/") + ]) + ), + /* Modelled after ECMA-262, 5th ed., 7.3. */ eol: new Rule( diff --git a/test/compiler-test.js b/test/compiler-test.js index 9c3b536..6173aa2 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -892,14 +892,42 @@ with (PEG.Grammar) { /* Canonical __ is "\n". */ test("parses __", function() { - grammarParserParses('start:"abcd"', simpleGrammar); - grammarParserParses('start: "abcd"', simpleGrammar); - grammarParserParses('start:\n"abcd"', simpleGrammar); - grammarParserParses('start: "abcd"', simpleGrammar); + grammarParserParses('start:"abcd"', simpleGrammar); + grammarParserParses('start: "abcd"', simpleGrammar); + grammarParserParses('start:\n"abcd"', simpleGrammar); + grammarParserParses('start:/* comment */"abcd"', simpleGrammar); + grammarParserParses('start: "abcd"', simpleGrammar); }); /* Trivial character class rules are not tested. */ + /* Canonical comment is "\/* comment *\/". */ + test("parses comment", function() { + grammarParserParses('start:// comment\n"abcd"', simpleGrammar); + grammarParserParses('start:/* comment */"abcd"', simpleGrammar); + }); + /* Canonical singleLineComment is "// comment". */ + test("parses singleLineComment", function() { + grammarParserParses('start://\n"abcd"', simpleGrammar); + grammarParserParses('start://a\n"abcd"', simpleGrammar); + grammarParserParses('start://aaa\n"abcd"', simpleGrammar); + grammarParserParses('start: "abcd"//', simpleGrammar); + }); + + /* Canonical multiLineComment is "\/* comment *\/". */ + test("parses multiLineComment", function() { + grammarParserParses('start:/**/"abcd"', simpleGrammar); + grammarParserParses('start:/*a*/"abcd"', simpleGrammar); + grammarParserParses('start:/*aaa*/"abcd"', simpleGrammar); + grammarParserParses('start:/*\n*/"abcd"', simpleGrammar); + grammarParserParses('start:/***/"abcd"', simpleGrammar); + grammarParserParses('start:/*a/*/"abcd"', simpleGrammar); + + grammarParserDoesNotParse('start:/*"abcd"'); + grammarParserDoesNotParse('start:/*/"abcd"'); + grammarParserDoesNotParse('start:/*/**/*/"abcd"'); + }); + /* Canonical eol is "\n". */ test("parses eol", function() { grammarParserParses('start:\n"abcd"', simpleGrammar);