From 6abda95a99f1dc03b15f33930f6f42fc050eeeac Mon Sep 17 00:00:00 2001 From: David Majda Date: Wed, 14 Apr 2010 21:08:35 +0200 Subject: [PATCH] Made regexps generated for empty character classes ("[]" and "[^]") work in IE. --- lib/compiler.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/compiler.js b/lib/compiler.js index 678fd3c..2b82e0b 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -734,6 +734,18 @@ PEG.Grammar.Literal.prototype.compile = function(resultVar) { }; PEG.Grammar.Class.prototype.compile = function(resultVar) { + /* + * Stupid IE considers regexps /[]/ and /[^]/ syntactically invalid, so we + * translate them into euqivalents it can handle. + */ + if (this._characters === "") { + var regexp = "/^(?!)/"; + } else if (this._characters === "^") { + var regexp = "/^[\\S\\s]/"; + } else { + var regexp = "/^[" + this._characters + "]/"; + } + return PEG.Compiler.formatCode( "if (this._input.substr(this._pos).match(${regexp}) !== null) {", " var ${resultVar} = this._input[this._pos];", @@ -746,7 +758,7 @@ PEG.Grammar.Class.prototype.compile = function(resultVar) { "}", { characters: this._characters, - regexp: "/^[" + this._characters + "]/", + regexp: regexp, resultVar: resultVar } );