--- /dev/null
+/*
+ ParseMaster, version 1.0.2 (2005-08-19)
+ Copyright 2005, Dean Edwards
+ License: http://creativecommons.org/licenses/LGPL/2.1/
+*/
+
+/* a multi-pattern parser */
+
+// KNOWN BUG: erroneous behavior when using escapeChar with a replacement value that is a function
+
+function ParseMaster() {
+ // constants
+ var $EXPRESSION = 0, $REPLACEMENT = 1, $LENGTH = 2;
+ // used to determine nesting levels
+ var $GROUPS = /\(/g, $SUB_REPLACE = /\$\d/, $INDEXED = /^\$\d+$/,
+ $TRIM = /(['"])\1\+(.*)\+\1\1$/, $$ESCAPE = /\\./g, $QUOTE = /'/,
+ $$DELETED = /\x01[^\x01]*\x01/g;
+ var self = this;
+ // public
+ this.add = function($expression, $replacement) {
+ if (!$replacement) $replacement = "";
+ // count the number of sub-expressions
+ // - add one because each pattern is itself a sub-expression
+ var $length = (_internalEscape(String($expression)).match($GROUPS) || "").length + 1;
+ // does the pattern deal with sub-expressions?
+ if ($SUB_REPLACE.test($replacement)) {
+ // a simple lookup? (e.g. "$2")
+ if ($INDEXED.test($replacement)) {
+ // store the index (used for fast retrieval of matched strings)
+ $replacement = parseInt($replacement.slice(1)) - 1;
+ } else { // a complicated lookup (e.g. "Hello $2 $1")
+ // build a function to do the lookup
+ var i = $length;
+ var $quote = $QUOTE.test(_internalEscape($replacement)) ? '"' : "'";
+ while (i) $replacement = $replacement.split("$" + i--).join($quote + "+a[o+" + i + "]+" + $quote);
+ $replacement = new Function("a,o", "return" + $quote + $replacement.replace($TRIM, "$1") + $quote);
+ }
+ }
+ // pass the modified arguments
+ _add($expression || "/^$/", $replacement, $length);
+ };
+ // execute the global replacement
+ this.exec = function($string) {
+ _escaped.length = 0;
+ return _unescape(_escape($string, this.escapeChar).replace(
+ new RegExp(_patterns, this.ignoreCase ? "gi" : "g"), _replacement), this.escapeChar).replace($$DELETED, "");
+ };
+ // clear the patterns collection so that this object may be re-used
+ this.reset = function() {
+ _patterns.length = 0;
+ };
+
+ // private
+ var _escaped = []; // escaped characters
+ var _patterns = []; // patterns stored by index
+ var _toString = function(){return "(" + String(this[$EXPRESSION]).slice(1, -1) + ")"};
+ _patterns.toString = function(){return this.join("|")};
+ // create and add a new pattern to the patterns collection
+ function _add() {
+ arguments.toString = _toString;
+ // store the pattern - as an arguments object (i think this is quicker..?)
+ _patterns[_patterns.length] = arguments;
+ }
+ // this is the global replace function (it's quite complicated)
+ function _replacement() {
+ if (!arguments[0]) return "";
+ var i = 1, j = 0, $pattern;
+ // loop through the patterns
+ while ($pattern = _patterns[j++]) {
+ // do we have a result?
+ if (arguments[i]) {
+ var $replacement = $pattern[$REPLACEMENT];
+ switch (typeof $replacement) {
+ case "function": return $replacement(arguments, i);
+ case "number": return arguments[$replacement + i];
+ }
+ var $delete = (arguments[i].indexOf(self.escapeChar) == -1) ? "" :
+ "\x01" + arguments[i] + "\x01";
+ return $delete + $replacement;
+ // skip over references to sub-expressions
+ } else i += $pattern[$LENGTH];
+ }
+ };
+ // encode escaped characters
+ function _escape($string, $escapeChar) {
+ return $escapeChar ? $string.replace(new RegExp("\\" + $escapeChar + "(.)", "g"), function($match, $char) {
+ _escaped[_escaped.length] = $char;
+ return $escapeChar;
+ }) : $string;
+ };
+ // decode escaped characters
+ function _unescape($string, $escapeChar) {
+ var i = 0;
+ return $escapeChar ? $string.replace(new RegExp("\\" + $escapeChar, "g"), function() {
+ return $escapeChar + (_escaped[i++] || "");
+ }) : $string;
+ };
+ function _internalEscape($string) {
+ return $string.replace($$ESCAPE, "");
+ };
+};
+ParseMaster.prototype = {
+ constructor: ParseMaster,
+ ignoreCase: false,
+ escapeChar: ""
+};
--- /dev/null
+/*\r packer, version 2.0.2 (2005-08-19)\r Copyright 2004-2005, Dean Edwards\r License: http://creativecommons.org/licenses/LGPL/2.1/\r*/\r\rfunction pack(_script, _encoding, _fastDecode, _specialChars) {\r // constants\r var $IGNORE = "$1";\r\r // validate parameters\r _script += "\n";\r _encoding = Math.min(parseInt(_encoding), 95);\r\r // apply all parsing routines\r function _pack($script) {\r var i, $parse;\r for (i = 0; ($parse = _parsers[i]); i++) {\r $script = $parse($script);\r }\r return $script;\r };\r\r // unpacking function - this is the boot strap function\r // data extracted from this packing routine is passed to\r // this function when decoded in the target\r var _unpack = function($packed, $ascii, $count, $keywords, $encode, $decode) {\r while ($count--)\r if ($keywords[$count])\r $packed = $packed.replace(new RegExp('\\b' + $encode($count) + '\\b', 'g'), $keywords[$count]);\r return $packed;\r };\r\r // code-snippet inserted into the unpacker to speed up decoding\r var _decode = function() {\r // does the browser support String.replace where the\r // replacement value is a function?\r if (!''.replace(/^/, String)) {\r // decode all the values we need\r while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);\r // global replacement function\r $keywords = [function($encoded){return $decode[$encoded]}];\r // generic match\r $encode = function(){return'\\w+'};\r // reset the loop counter - we are now doing a global replace\r $count = 1;\r }\r };\r\r // keep a list of parsing functions, they'll be executed all at once\r var _parsers = [];\r function _addParser($parser) {\r _parsers[_parsers.length] = $parser;\r };\r\r // zero encoding - just removal of white space and comments\r function _basicCompression($script) {\r var $parser = new ParseMaster;\r // make safe\r $parser.escapeChar = "\\";\r // protect strings\r $parser.add(/'[^'\n\r]*'/, $IGNORE);\r $parser.add(/"[^"\n\r]*"/, $IGNORE);\r // remove comments\r $parser.add(/\/\/[^\n\r]*[\n\r]/, " ");\r $parser.add(/\/\*[^*]*\*+([^\/][^*]*\*+)*\//, " ");\r // protect regular expressions\r $parser.add(/\s+(\/[^\/\n\r\*][^\/\n\r]*\/g?i?)/, "$2"); // IGNORE\r $parser.add(/[^\w\x24\/'"*)\?:]\/[^\/\n\r\*][^\/\n\r]*\/g?i?/, $IGNORE);\r // remove: ;;; doSomething();\r if (_specialChars) $parser.add(/;;;[^\n\r]+[\n\r]/);\r // remove redundant semi-colons\r $parser.add(/\(;;\)/, $IGNORE); // protect for (;;) loops\r $parser.add(/;+\s*([};])/, "$2");\r // apply the above\r $script = $parser.exec($script);\r\r // remove white-space\r $parser.add(/(\b|\x24)\s+(\b|\x24)/, "$2 $3");\r $parser.add(/([+\-])\s+([+\-])/, "$2 $3");\r $parser.add(/\s+/, "");\r // done\r return $parser.exec($script);\r };\r\r function _encodeSpecialChars($script) {\r var $parser = new ParseMaster;\r // replace: $name -> n, $$name -> na\r $parser.add(/((\x24+)([a-zA-Z$_]+))(\d*)/, function($match, $offset) {\r var $length = $match[$offset + 2].length;\r var $start = $length - Math.max($length - $match[$offset + 3].length, 0);\r return $match[$offset + 1].substr($start, $length) + $match[$offset + 4];\r });\r // replace: _name -> _0, double-underscore (__name) is ignored\r var $regexp = /\b_[A-Za-z\d]\w*/;\r // build the word list\r var $keywords = _analyze($script, _globalize($regexp), _encodePrivate);\r // quick ref\r var $encoded = $keywords.$encoded;\r $parser.add($regexp, function($match, $offset) {\r return $encoded[$match[$offset]];\r });\r return $parser.exec($script);\r };\r\r function _encodeKeywords($script) {\r // escape high-ascii values already in the script (i.e. in strings)\r if (_encoding > 62) $script = _escape95($script);\r // create the parser\r var $parser = new ParseMaster;\r var $encode = _getEncoder(_encoding);\r // for high-ascii, don't encode single character low-ascii\r var $regexp = (_encoding > 62) ? /\w\w+/ : /\w+/;\r // build the word list\r $keywords = _analyze($script, _globalize($regexp), $encode);\r var $encoded = $keywords.$encoded;\r // encode\r $parser.add($regexp, function($match, $offset) {\r return $encoded[$match[$offset]];\r });\r // if encoded, wrap the script in a decoding function\r return $script && _bootStrap($parser.exec($script), $keywords);\r };\r\r function _analyze($script, $regexp, $encode) {\r // analyse\r // retreive all words in the script\r var $all = $script.match($regexp);\r var $$sorted = []; // list of words sorted by frequency\r var $$encoded = {}; // dictionary of word->encoding\r var $$protected = {}; // instances of "protected" words\r if ($all) {\r var $unsorted = []; // same list, not sorted\r var $protected = {}; // "protected" words (dictionary of word->"word")\r var $values = {}; // dictionary of charCode->encoding (eg. 256->ff)\r var $count = {}; // word->count\r var i = $all.length, j = 0, $word;\r // count the occurrences - used for sorting later\r do {\r $word = "$" + $all[--i];\r if (!$count[$word]) {\r $count[$word] = 0;\r $unsorted[j] = $word;\r // make a dictionary of all of the protected words in this script\r // these are words that might be mistaken for encoding\r $protected["$" + ($values[j] = $encode(j))] = j++;\r }\r // increment the word counter\r $count[$word]++;\r } while (i);\r // prepare to sort the word list, first we must protect\r // words that are also used as codes. we assign them a code\r // equivalent to the word itself.\r // e.g. if "do" falls within our encoding range\r // then we store keywords["do"] = "do";\r // this avoids problems when decoding\r i = $unsorted.length;\r do {\r $word = $unsorted[--i];\r if ($protected[$word] != null) {\r $$sorted[$protected[$word]] = $word.slice(1);\r $$protected[$protected[$word]] = true;\r $count[$word] = 0;\r }\r } while (i);\r // sort the words by frequency\r $unsorted.sort(function($match1, $match2) {\r return $count[$match2] - $count[$match1];\r });\r j = 0;\r // because there are "protected" words in the list\r // we must add the sorted words around them\r do {\r if ($$sorted[i] == null) $$sorted[i] = $unsorted[j++].slice(1);\r $$encoded[$$sorted[i]] = $values[i];\r } while (++i < $unsorted.length);\r }\r return {$sorted: $$sorted, $encoded: $$encoded, $protected: $$protected};\r };\r\r // build the boot function used for loading and decoding\r function _bootStrap($packed, $keywords) {\r var $ENCODE = _safeRegExp("$encode\\($count\\)", "g");\r\r // $packed: the packed script\r $packed = "'" + _escape($packed) + "'";\r\r // $ascii: base for encoding\r var $ascii = Math.min($keywords.$sorted.length, _encoding) || 1;\r\r // $count: number of words contained in the script\r var $count = $keywords.$sorted.length;\r\r // $keywords: list of words contained in the script\r for (var i in $keywords.$protected) $keywords.$sorted[i] = "";\r // convert from a string to an array\r $keywords = "'" + $keywords.$sorted.join("|") + "'.split('|')";\r\r // $encode: encoding function (used for decoding the script)\r var $encode = _encoding > 62 ? _encode95 : _getEncoder($ascii);\r $encode = String($encode).replace(/_encoding/g, "$ascii").replace(/arguments\.callee/g, "$encode");\r var $inline = "$count" + ($ascii > 10 ? ".toString($ascii)" : "");\r\r // $decode: code snippet to speed up decoding\r if (_fastDecode) {\r // create the decoder\r var $decode = _getFunctionBody(_decode);\r if (_encoding > 62) $decode = $decode.replace(/\\\\w/g, "[\\xa1-\\xff]");\r // perform the encoding inline for lower ascii values\r else if ($ascii < 36) $decode = $decode.replace($ENCODE, $inline);\r // special case: when $count==0 there are no keywords. I want to keep\r // the basic shape of the unpacking funcion so i'll frig the code...\r if (!$count) $decode = $decode.replace(_safeRegExp("($count)\\s*=\\s*1"), "$1=0");\r }\r\r // boot function\r var $unpack = String(_unpack);\r if (_fastDecode) {\r // insert the decoder\r $unpack = $unpack.replace(/\{/, "{" + $decode + ";");\r }\r $unpack = $unpack.replace(/"/g, "'");\r if (_encoding > 62) { // high-ascii\r // get rid of the word-boundaries for regexp matches\r $unpack = $unpack.replace(/'\\\\b'\s*\+|\+\s*'\\\\b'/g, "");\r }\r if ($ascii > 36 || _encoding > 62 || _fastDecode) {\r // insert the encode function\r $unpack = $unpack.replace(/\{/, "{$encode=" + $encode + ";");\r } else {\r // perform the encoding inline\r $unpack = $unpack.replace($ENCODE, $inline);\r }\r // pack the boot function too\r $unpack = pack($unpack, 0, false, true);\r\r // arguments\r var $params = [$packed, $ascii, $count, $keywords];\r if (_fastDecode) {\r // insert placeholders for the decoder\r $params = $params.concat(0, "{}");\r }\r\r // the whole thing\r return "eval(" + $unpack + "(" + $params + "))\n";\r };\r\r // mmm.. ..which one do i need ??\r function _getEncoder($ascii) {\r return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ? _encode95 : _encode62 : _encode36 : _encode10;\r };\r\r // zero encoding\r // characters: 0123456789\r var _encode10 = function($charCode) {\r return $charCode;\r };\r\r // inherent base36 support\r // characters: 0123456789abcdefghijklmnopqrstuvwxyz\r var _encode36 = function($charCode) {\r return $charCode.toString(36);\r };\r\r // hitch a ride on base36 and add the upper case alpha characters\r // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r var _encode62 = function($charCode) {\r return ($charCode < _encoding ? '' : arguments.callee(parseInt($charCode / _encoding))) +\r (($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));\r };\r\r // use high-ascii values\r var _encode95 = function($charCode) {\r return ($charCode < _encoding ? '' : arguments.callee($charCode / _encoding)) +\r String.fromCharCode($charCode % _encoding + 161);\r };\r\r // special _chars\r var _encodePrivate = function($charCode) {\r return "_" + $charCode;\r };\r\r // protect characters used by the parser\r function _escape($script) {\r return $script.replace(/([\\'])/g, "\\$1");\r };\r\r // protect high-ascii characters already in the script\r function _escape95($script) {\r return $script.replace(/[\xa1-\xff]/g, function($match) {\r return "\\x" + $match.charCodeAt(0).toString(16);\r });\r };\r\r function _safeRegExp($string, $flags) {\r return new RegExp($string.replace(/\$/g, "\\$"), $flags);\r };\r\r // extract the body of a function\r function _getFunctionBody($function) {\r with (String($function)) return slice(indexOf("{") + 1, lastIndexOf("}"));\r };\r\r // set the global flag on a RegExp (you have to create a new one)\r function _globalize($regexp) {\r return new RegExp(String($regexp).slice(1, -1), "g");\r };\r\r // build the parsing routine\r _addParser(_basicCompression);\r if (_specialChars) _addParser(_encodeSpecialChars);\r if (_encoding) _addParser(_encodeKeywords);\r\r // go!\r return _pack(_script);\r};
\ No newline at end of file