Browse Source

Metagrammar recognizes JavaScript-like comments.

redux
David Majda 12 years ago
parent
commit
636ceb2719
  1. 42
      lib/compiler.js
  2. 36
      test/compiler-test.js

42
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(

36
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);

Loading…
Cancel
Save