Implemented negative character classes (e.g. [^a-z]).

redux
David Majda 15 years ago
parent b3381b9352
commit 4f4bb34ded

File diff suppressed because it is too large Load Diff

@ -63,7 +63,7 @@ braced: "{" (braced / nonBraceCharacter)* "}" { return $1 + $2.join("") + $3; }
nonBraceCharacters: nonBraceCharacter+ { return $1.join(""); }
nonBraceCharacter: !("{" / "}") . { return $2; }
nonBraceCharacter: [^{}] { return $1; }
colon: ":" __ { return $1; }
slash: "/" __ { return $1; }
@ -125,8 +125,8 @@ singleQuotedCharacter
simpleSingleQuotedCharacter: !("'" / "\\" / eolChar) . { return $2; }
class "character class": "[" (classCharacterRange / classCharacter)* "]" __ {
return $2.join("");
class "character class": "[" "^"? (classCharacterRange / classCharacter)* "]" __ {
return $2 + $3.join("");
}
classCharacterRange: bracketDelimitedCharacter "-" bracketDelimitedCharacter {

@ -205,6 +205,19 @@ test("classes", function() {
doesNotParse(nonEmptyClassParser, "");
doesNotParse(nonEmptyClassParser, "ab");
var invertedEmptyClassParser = PEG.buildParser('start: []');
doesNotParse(invertedEmptyClassParser, "");
doesNotParse(invertedEmptyClassParser, "a");
doesNotParse(invertedEmptyClassParser, "ab");
var invertedNonEmptyClassParser = PEG.buildParser('start: [^ab-d]');
doesNotParse(invertedNonEmptyClassParser, "a", "a");
doesNotParse(invertedNonEmptyClassParser, "b", "b");
doesNotParse(invertedNonEmptyClassParser, "c", "c");
doesNotParse(invertedNonEmptyClassParser, "d", "d");
doesNotParse(invertedNonEmptyClassParser, "");
doesNotParse(invertedNonEmptyClassParser, "ab");
/*
* Test that the parsing position moves forward after successful parsing of
* a class.

@ -276,6 +276,7 @@ with (PEG.Grammar) {
test("parses class", function() {
grammarParserParses("start: []", classGrammar(""));
grammarParserParses("start: [a-d]", classGrammar("a-d"));
grammarParserParses("start: [^a-d]", classGrammar("^a-d"));
grammarParserParses("start: [a]", classGrammar("a"));
grammarParserParses("start: [a-de-hi-l]", classGrammar("a-de-hi-l"));

Loading…
Cancel
Save