/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */

define([], function () {
    /* jshint ignore:start */

    function parseMapped(input) {
        var ret = parse(input);

        if (ret && ret.length) {
            ret.forEach(function (component) {
                if (component.extension === "jpeg") {
                    component.extension = "jpg";
                    if (component.name) {
                        component.name = component.name.replace(/\.jpeg$/, ".jpg");
                    }
                    if (component.file) {
                        component.file = component.file.replace(/\.jpeg$/, ".jpg");
                    }
                }
            });
        }

        return ret;
    }

    /*
     * Generated by PEG.js 0.8.0.
     *
     * http://pegjs.majda.cz/
     */

    function peg$subclass(child, parent) {
        function ctor() { this.constructor = child; }
        ctor.prototype = parent.prototype;
        child.prototype = new ctor();
    }

    function SyntaxError(message, expected, found, offset, line, column) {
        this.message  = message;
        this.expected = expected;
        this.found    = found;
        this.offset   = offset;
        this.line     = line;
        this.column   = column;

        this.name     = "SyntaxError";
    }

    peg$subclass(SyntaxError, Error);

    function parse(input) {
        var options = arguments.length > 1 ? arguments[1] : {},

            peg$FAILED = {},

            peg$startRuleFunctions = { start: peg$parsestart },
            peg$startRuleFunction  = peg$parsestart,

            peg$c0 = { type: "other", description: "Either a default asset specification or a layer asset specification" },
            peg$c1 = { type: "other", description: "A document defaults specification" },
            peg$c2 = peg$FAILED,
            peg$c3 = "default",
            peg$c4 = { type: "literal", value: "default", description: "\"default\"" },
            peg$c5 = function(defaults) {
                return defaults;
            },
            peg$c6 = { type: "other", description: "List of default specification components" },
            peg$c7 = /^[+,]/,
            peg$c8 = { type: "class", value: "[+,]", description: "[+,]" },
            peg$c9 = function(first, rest) {
                rest.unshift(first);
                return rest;
            },
            peg$c10 = function(only) {
                return [only];
            },
            peg$c11 = { type: "other", description: "A single default specification component" },
            peg$c12 = [],
            peg$c13 = null,
            peg$c14 = function(size, folders, suffix) { return size || folders.length > 0 || (suffix && suffix.trim().length > 0)},
            peg$c15 = void 0,
            peg$c16 = function(size, folders, suffix) { // require at least one spec
                var result = {
                    "default": true,
                    name: text().trim()
                };

                if (folders.length > 0) {
                    result.folder = folders;
                }

                if (suffix) {
                    suffix = suffix.trim();
                    if (suffix.length > 0) {
                        result.suffix = suffix;
                    }
                }

                mergeSize(size, result);

                return result;
            },
            peg$c17 = { type: "other", description: "List of layer specifications" },
            peg$c18 = function(first, rest) {
                rest.unshift(first);
                return rest;
            },
            peg$c19 = { type: "other", description: "Layer specification" },
            peg$c20 = function(layername) { // Unparsed layer name part
                return {
                    name: layername.trim()
                };
            },
            peg$c21 = { type: "other", description: "A single folder name that ends with a slash and does not begin with a dot" },
            peg$c22 = "/",
            peg$c23 = { type: "literal", value: "/", description: "\"/\"" },
            peg$c24 = function(chars) { return chars[0] == "."; },
            peg$c25 = function(chars) {
                return chars;
            },
            peg$c26 = { type: "other", description: "A size-and-file specification" },
            peg$c27 = function(size, folders, filepart) { // Parsed layer name part
                var result = {
                    name: text().trim(),
                    file: filepart.filename.replace(/[\\":*?<>!|]/g,'_'),
                    extension: filepart.extension,
                }

                if (folders.length > 0) {
                    result.folder = folders;
                }

                if (filepart.hasOwnProperty("quality")) {
                    result.quality = filepart.quality;
                }

                mergeSize(size, result);

                return result;
            },
            peg$c28 = { type: "other", description: "Filename and quality suffix" },
            peg$c29 = function(nameparts, suffix) {
                var filename = String.prototype.concat.apply("", nameparts) + suffix.extension;
                if (filename.match(/^\s/)) {
                    error("Filename begins with whitespace");
                }

                var result = {
                    filename: filename.trim(),
                    extension: suffix.extension.toLowerCase()
                };

                if (suffix.hasOwnProperty("quality")) {
                    result.quality = suffix.quality;
                }

                return result;
            },
            peg$c30 = { type: "other", description: "File extension and quality suffix" },
            peg$c31 = /^[a-zA-Z]/,
            peg$c32 = { type: "class", value: "[a-zA-Z]", description: "[a-zA-Z]" },
            peg$c33 = function(extension, quality) {
                var result = {
                    extension: extension.join(""),
                };

                if (quality) {
                    result.quality = quality;
                }

                return result;
            },
            peg$c34 = { type: "other", description: "Quality parameter that follows a file extension" },
            peg$c35 = "-",
            peg$c36 = { type: "literal", value: "-", description: "\"-\"" },
            peg$c37 = /^[a-z]/,
            peg$c38 = { type: "class", value: "[a-z]", description: "[a-z]" },
            peg$c39 = "%",
            peg$c40 = { type: "literal", value: "%", description: "\"%\"" },
            peg$c41 = function(param, ext) {
                return param.join("") + (ext || "");
            },
            peg$c42 = { type: "other", description: "Relative or absolute scale" },
            peg$c43 = " ",
            peg$c44 = { type: "literal", value: " ", description: "\" \"" },
            peg$c45 = function(abs) {
                return abs;
            },
            peg$c46 = { type: "other", description: "Relative scale, like 0.3" },
            peg$c47 = function(scale) {
                return {
                    scale: scale
                };
            },
            peg$c48 = { type: "other", description: "Absolute scale, like 50x100cm" },
            peg$c49 = "x",
            peg$c50 = { type: "literal", value: "x", description: "\"x\"" },
            peg$c51 = function(width, height) {
                var result = {};

                if (width.hasOwnProperty("value")) {
                    result.width = width.value;
                }

                if (width.hasOwnProperty("unit")) {
                    result.widthUnit = width.unit;
                }

                if (height.hasOwnProperty("value")) {
                    result.height = height.value;
                }

                if (height.hasOwnProperty("unit")) {
                    result.heightUnit = height.unit;
                }

                return result;
            },
            peg$c52 = { type: "other", description: "Absolute scale component, like 100cm" },
            peg$c53 = function(value, unit) {
                var result = {
                    value: value,
                };

                if (unit) {
                    result.unit = unit;
                }

                return result;
            },
            peg$c54 = "?",
            peg$c55 = { type: "literal", value: "?", description: "\"?\"" },
            peg$c56 = function() { // wildcard component
                return {
                    // no unit
                };
            },
            peg$c57 = { type: "other", description: "Unit abbreviation" },
            peg$c58 = /^[a-z]/i,
            peg$c59 = { type: "class", value: "[a-z]i", description: "[a-z]i" },
            peg$c60 = function(first, second) {
                return first + second;
            },
            peg$c61 = { type: "other", description: "A percentage, like 30%" },
            peg$c62 = function(num) {
                return num / 100;
            },
            peg$c63 = function(chars) {
                return chars.join("")
            },
            peg$c64 = { type: "other", description: "A good character or a dot" },
            peg$c65 = ".",
            peg$c66 = { type: "literal", value: ".", description: "\".\"" },
            peg$c67 = { type: "other", description: "A sequence of characters that ends with a dot" },
            peg$c68 = function(chars) {
                return chars.concat(".");
            },
            peg$c69 = { type: "other", description: "A sequence of characters, including dots" },
            peg$c70 = function(chars) {
                return chars.join("");
            },
            peg$c71 = { type: "other", description: "A sequence of characters, excluding dots" },
            peg$c72 = { type: "other", description: "A character, including dots" },
            peg$c73 = /^[^,+]/,
            peg$c74 = { type: "class", value: "[^,+]", description: "[^,+]" },
            peg$c75 = { type: "other", description: "A character, excluding dots and other weird things" },
            peg$c76 = /^[^+,.\/\0-\x1F]/,
            peg$c77 = { type: "class", value: "[^+,.\\/\\0-\\x1F]", description: "[^+,.\\/\\0-\\x1F]" },
            peg$c78 = { type: "other", description: "A nonnegative number, which may or may not have leading zeros" },
            peg$c79 = function(parts) { return parseFloat(parts); },
            peg$c80 = function(parts) { return parseFloat("0" + parts); },
            peg$c81 = /^[0-9]/,
            peg$c82 = { type: "class", value: "[0-9]", description: "[0-9]" },
            peg$c83 = { type: "other", description: "whitespace" },
            peg$c84 = /^[ \t\n\r]/,
            peg$c85 = { type: "class", value: "[ \\t\\n\\r]", description: "[ \\t\\n\\r]" },

            peg$currPos          = 0,
            peg$reportedPos      = 0,
            peg$cachedPos        = 0,
            peg$cachedPosDetails = { line: 1, column: 1, seenCR: false },
            peg$maxFailPos       = 0,
            peg$maxFailExpected  = [],
            peg$silentFails      = 0,

            peg$cache = {},
            peg$result;

        if ("startRule" in options) {
            if (!(options.startRule in peg$startRuleFunctions)) {
                throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
            }

            peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
        }

        function text() {
            return input.substring(peg$reportedPos, peg$currPos);
        }

        function offset() {
            return peg$reportedPos;
        }

        function line() {
            return peg$computePosDetails(peg$reportedPos).line;
        }

        function column() {
            return peg$computePosDetails(peg$reportedPos).column;
        }

        function expected(description) {
            throw peg$buildException(
                null,
                [{ type: "other", description: description }],
                peg$reportedPos
            );
        }

        function error(message) {
            throw peg$buildException(message, null, peg$reportedPos);
        }

        function peg$computePosDetails(pos) {
            function advance(details, startPos, endPos) {
                var p, ch;

                for (p = startPos; p < endPos; p++) {
                    ch = input.charAt(p);
                    if (ch === "\n") {
                        if (!details.seenCR) { details.line++; }
                        details.column = 1;
                        details.seenCR = false;
                    } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {
                        details.line++;
                        details.column = 1;
                        details.seenCR = true;
                    } else {
                        details.column++;
                        details.seenCR = false;
                    }
                }
            }

            if (peg$cachedPos !== pos) {
                if (peg$cachedPos > pos) {
                    peg$cachedPos = 0;
                    peg$cachedPosDetails = { line: 1, column: 1, seenCR: false };
                }
                advance(peg$cachedPosDetails, peg$cachedPos, pos);
                peg$cachedPos = pos;
            }

            return peg$cachedPosDetails;
        }

        function peg$fail(expected) {
            if (peg$currPos < peg$maxFailPos) { return; }

            if (peg$currPos > peg$maxFailPos) {
                peg$maxFailPos = peg$currPos;
                peg$maxFailExpected = [];
            }

            peg$maxFailExpected.push(expected);
        }

        function peg$buildException(message, expected, pos) {
            function cleanupExpected(expected) {
                var i = 1;

                expected.sort(function(a, b) {
                    if (a.description < b.description) {
                        return -1;
                    } else if (a.description > b.description) {
                        return 1;
                    } else {
                        return 0;
                    }
                });

                while (i < expected.length) {
                    if (expected[i - 1] === expected[i]) {
                        expected.splice(i, 1);
                    } else {
                        i++;
                    }
                }
            }

            function buildMessage(expected, found) {
                function stringEscape(s) {
                    function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }

                    return s
                        .replace(/\\/g,   '\\\\')
                        .replace(/"/g,    '\\"')
                        .replace(/\x08/g, '\\b')
                        .replace(/\t/g,   '\\t')
                        .replace(/\n/g,   '\\n')
                        .replace(/\f/g,   '\\f')
                        .replace(/\r/g,   '\\r')
                        .replace(/[\x00-\x07\x0B\x0E\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
                        .replace(/[\x10-\x1F\x80-\xFF]/g,    function(ch) { return '\\x'  + hex(ch); })
                        .replace(/[\u0180-\u0FFF]/g,         function(ch) { return '\\u0' + hex(ch); })
                        .replace(/[\u1080-\uFFFF]/g,         function(ch) { return '\\u'  + hex(ch); });
                }

                var expectedDescs = new Array(expected.length),
                    expectedDesc, foundDesc, i;

                for (i = 0; i < expected.length; i++) {
                    expectedDescs[i] = expected[i].description;
                }

                expectedDesc = expected.length > 1
                    ? expectedDescs.slice(0, -1).join(", ")
                    + " or "
                    + expectedDescs[expected.length - 1]
                    : expectedDescs[0];

                foundDesc = found ? "\"" + stringEscape(found) + "\"" : "end of input";

                return "Expected " + expectedDesc + " but " + foundDesc + " found.";
            }

            var posDetails = peg$computePosDetails(pos),
                found      = pos < input.length ? input.charAt(pos) : null;

            if (expected !== null) {
                cleanupExpected(expected);
            }

            return new SyntaxError(
                    message !== null ? message : buildMessage(expected, found),
                expected,
                found,
                pos,
                posDetails.line,
                posDetails.column
            );
        }

        function peg$parsestart() {
            var s0, s1;

            var key    = peg$currPos * 29 + 0,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$parsedefaults();
            if (s0 === peg$FAILED) {
                s0 = peg$parsespeclist();
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c0); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsedefaults() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 1,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            if (input.substr(peg$currPos, 7) === peg$c3) {
                s1 = peg$c3;
                peg$currPos += 7;
            } else {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c4); }
            }
            if (s1 !== peg$FAILED) {
                s2 = peg$parsedefaultspeclist();
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c5(s2);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c1); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsedefaultspeclist() {
            var s0, s1, s2, s3;

            var key    = peg$currPos * 29 + 2,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsedefaultspec();
            if (s1 !== peg$FAILED) {
                if (peg$c7.test(input.charAt(peg$currPos))) {
                    s2 = input.charAt(peg$currPos);
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c8); }
                }
                if (s2 !== peg$FAILED) {
                    s3 = peg$parsedefaultspeclist();
                    if (s3 !== peg$FAILED) {
                        peg$reportedPos = s0;
                        s1 = peg$c9(s1, s3);
                        s0 = s1;
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                s1 = peg$parsedefaultspec();
                if (s1 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c10(s1);
                }
                s0 = s1;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c6); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsedefaultspec() {
            var s0, s1, s2, s3, s4, s5, s6, s7;

            var key    = peg$currPos * 29 + 3,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = [];
            s2 = peg$parsewhitespace();
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    s2 = peg$parsewhitespace();
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                s2 = peg$parsescale();
                if (s2 === peg$FAILED) {
                    s2 = peg$c13;
                }
                if (s2 !== peg$FAILED) {
                    s3 = peg$parse_();
                    if (s3 !== peg$FAILED) {
                        s4 = [];
                        s5 = peg$parsefolder();
                        while (s5 !== peg$FAILED) {
                            s4.push(s5);
                            s5 = peg$parsefolder();
                        }
                        if (s4 !== peg$FAILED) {
                            s5 = peg$parsegoodcharsanddots();
                            if (s5 === peg$FAILED) {
                                s5 = peg$c13;
                            }
                            if (s5 !== peg$FAILED) {
                                s6 = peg$parse_();
                                if (s6 !== peg$FAILED) {
                                    peg$reportedPos = peg$currPos;
                                    s7 = peg$c14(s2, s4, s5);
                                    if (s7) {
                                        s7 = peg$c15;
                                    } else {
                                        s7 = peg$c2;
                                    }
                                    if (s7 !== peg$FAILED) {
                                        peg$reportedPos = s0;
                                        s1 = peg$c16(s2, s4, s5);
                                        s0 = s1;
                                    } else {
                                        peg$currPos = s0;
                                        s0 = peg$c2;
                                    }
                                } else {
                                    peg$currPos = s0;
                                    s0 = peg$c2;
                                }
                            } else {
                                peg$currPos = s0;
                                s0 = peg$c2;
                            }
                        } else {
                            peg$currPos = s0;
                            s0 = peg$c2;
                        }
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c11); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsespeclist() {
            var s0, s1, s2, s3;

            var key    = peg$currPos * 29 + 4,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsespec();
            if (s1 !== peg$FAILED) {
                if (peg$c7.test(input.charAt(peg$currPos))) {
                    s2 = input.charAt(peg$currPos);
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c8); }
                }
                if (s2 !== peg$FAILED) {
                    s3 = peg$parsespeclist();
                    if (s3 !== peg$FAILED) {
                        peg$reportedPos = s0;
                        s1 = peg$c18(s1, s3);
                        s0 = s1;
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                s1 = peg$parsespec();
                if (s1 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c10(s1);
                }
                s0 = s1;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c17); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsespec() {
            var s0, s1, s2, s3;

            var key    = peg$currPos * 29 + 5,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$parsefilespec();
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                s1 = peg$parse_();
                if (s1 !== peg$FAILED) {
                    s2 = peg$parsechars();
                    if (s2 !== peg$FAILED) {
                        s3 = peg$parse_();
                        if (s3 !== peg$FAILED) {
                            peg$reportedPos = s0;
                            s1 = peg$c20(s2);
                            s0 = s1;
                        } else {
                            peg$currPos = s0;
                            s0 = peg$c2;
                        }
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c19); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsefolder() {
            var s0, s1, s2, s3;

            var key    = peg$currPos * 29 + 6,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsegoodcharsanddots();
            if (s1 !== peg$FAILED) {
                if (input.charCodeAt(peg$currPos) === 47) {
                    s2 = peg$c22;
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c23); }
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = peg$currPos;
                    s3 = peg$c24(s1);
                    if (s3) {
                        s3 = peg$c2;
                    } else {
                        s3 = peg$c15;
                    }
                    if (s3 !== peg$FAILED) {
                        peg$reportedPos = s0;
                        s1 = peg$c25(s1);
                        s0 = s1;
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c21); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsefilespec() {
            var s0, s1, s2, s3, s4, s5, s6;

            var key    = peg$currPos * 29 + 7,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parse_();
            if (s1 !== peg$FAILED) {
                s2 = peg$parsescale();
                if (s2 === peg$FAILED) {
                    s2 = peg$c13;
                }
                if (s2 !== peg$FAILED) {
                    s3 = peg$parse_();
                    if (s3 !== peg$FAILED) {
                        s4 = [];
                        s5 = peg$parsefolder();
                        while (s5 !== peg$FAILED) {
                            s4.push(s5);
                            s5 = peg$parsefolder();
                        }
                        if (s4 !== peg$FAILED) {
                            s5 = peg$parsefilename();
                            if (s5 !== peg$FAILED) {
                                s6 = peg$parse_();
                                if (s6 !== peg$FAILED) {
                                    peg$reportedPos = s0;
                                    s1 = peg$c27(s2, s4, s5);
                                    s0 = s1;
                                } else {
                                    peg$currPos = s0;
                                    s0 = peg$c2;
                                }
                            } else {
                                peg$currPos = s0;
                                s0 = peg$c2;
                            }
                        } else {
                            peg$currPos = s0;
                            s0 = peg$c2;
                        }
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c26); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsefilename() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 8,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = [];
            s2 = peg$parsegoodcharsthendot();
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    s2 = peg$parsegoodcharsthendot();
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                s2 = peg$parsefileext();
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c29(s1, s2);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c28); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsefileext() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 9,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = [];
            if (peg$c31.test(input.charAt(peg$currPos))) {
                s2 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s2 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c32); }
            }
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    if (peg$c31.test(input.charAt(peg$currPos))) {
                        s2 = input.charAt(peg$currPos);
                        peg$currPos++;
                    } else {
                        s2 = peg$FAILED;
                        if (peg$silentFails === 0) { peg$fail(peg$c32); }
                    }
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                s2 = peg$parsequality();
                if (s2 === peg$FAILED) {
                    s2 = peg$c13;
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c33(s1, s2);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c30); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsequality() {
            var s0, s1, s2, s3;

            var key    = peg$currPos * 29 + 10,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            if (input.charCodeAt(peg$currPos) === 45) {
                s1 = peg$c35;
                peg$currPos++;
            } else {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c36); }
            }
            if (s1 === peg$FAILED) {
                s1 = peg$c13;
            }
            if (s1 !== peg$FAILED) {
                s2 = peg$parsedigits();
                if (s2 !== peg$FAILED) {
                    if (peg$c37.test(input.charAt(peg$currPos))) {
                        s3 = input.charAt(peg$currPos);
                        peg$currPos++;
                    } else {
                        s3 = peg$FAILED;
                        if (peg$silentFails === 0) { peg$fail(peg$c38); }
                    }
                    if (s3 === peg$FAILED) {
                        if (input.charCodeAt(peg$currPos) === 37) {
                            s3 = peg$c39;
                            peg$currPos++;
                        } else {
                            s3 = peg$FAILED;
                            if (peg$silentFails === 0) { peg$fail(peg$c40); }
                        }
                    }
                    if (s3 === peg$FAILED) {
                        s3 = peg$c13;
                    }
                    if (s3 !== peg$FAILED) {
                        peg$reportedPos = s0;
                        s1 = peg$c41(s2, s3);
                        s0 = s1;
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c34); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsescale() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 11,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$parserelscale();
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                s1 = peg$parseabsscale();
                if (s1 !== peg$FAILED) {
                    if (input.charCodeAt(peg$currPos) === 32) {
                        s2 = peg$c43;
                        peg$currPos++;
                    } else {
                        s2 = peg$FAILED;
                        if (peg$silentFails === 0) { peg$fail(peg$c44); }
                    }
                    if (s2 !== peg$FAILED) {
                        peg$reportedPos = s0;
                        s1 = peg$c45(s1);
                        s0 = s1;
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c42); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parserelscale() {
            var s0, s1;

            var key    = peg$currPos * 29 + 12,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsepercent();
            if (s1 !== peg$FAILED) {
                peg$reportedPos = s0;
                s1 = peg$c47(s1);
            }
            s0 = s1;
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c46); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parseabsscale() {
            var s0, s1, s2, s3, s4, s5;

            var key    = peg$currPos * 29 + 13,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parseabscomp();
            if (s1 !== peg$FAILED) {
                s2 = peg$parse_();
                if (s2 !== peg$FAILED) {
                    if (input.substr(peg$currPos, 1).toLowerCase() === peg$c49) {
                        s3 = input.charAt(peg$currPos);
                        peg$currPos++;
                    } else {
                        s3 = peg$FAILED;
                        if (peg$silentFails === 0) { peg$fail(peg$c50); }
                    }
                    if (s3 !== peg$FAILED) {
                        s4 = peg$parse_();
                        if (s4 !== peg$FAILED) {
                            s5 = peg$parseabscomp();
                            if (s5 !== peg$FAILED) {
                                peg$reportedPos = s0;
                                s1 = peg$c51(s1, s5);
                                s0 = s1;
                            } else {
                                peg$currPos = s0;
                                s0 = peg$c2;
                            }
                        } else {
                            peg$currPos = s0;
                            s0 = peg$c2;
                        }
                    } else {
                        peg$currPos = s0;
                        s0 = peg$c2;
                    }
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c48); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parseabscomp() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 14,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsenumber();
            if (s1 !== peg$FAILED) {
                s2 = peg$parseunit();
                if (s2 === peg$FAILED) {
                    s2 = peg$c13;
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c53(s1, s2);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                if (input.charCodeAt(peg$currPos) === 63) {
                    s1 = peg$c54;
                    peg$currPos++;
                } else {
                    s1 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c55); }
                }
                if (s1 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c56();
                }
                s0 = s1;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c52); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parseunit() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 15,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            if (peg$c58.test(input.charAt(peg$currPos))) {
                s1 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c59); }
            }
            if (s1 !== peg$FAILED) {
                if (peg$c58.test(input.charAt(peg$currPos))) {
                    s2 = input.charAt(peg$currPos);
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c59); }
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c60(s1, s2);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c57); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsepercent() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 16,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsenumber();
            if (s1 !== peg$FAILED) {
                if (input.charCodeAt(peg$currPos) === 37) {
                    s2 = peg$c39;
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c40); }
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c62(s1);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c61); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsegoodcharsanddots() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 17,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            s0 = peg$currPos;
            s1 = [];
            s2 = peg$parsegoodcharanddot();
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    s2 = peg$parsegoodcharanddot();
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                peg$reportedPos = s0;
                s1 = peg$c63(s1);
            }
            s0 = s1;

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsegoodcharanddot() {
            var s0, s1;

            var key    = peg$currPos * 29 + 18,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$parsegoodchar();
            if (s0 === peg$FAILED) {
                if (input.charCodeAt(peg$currPos) === 46) {
                    s0 = peg$c65;
                    peg$currPos++;
                } else {
                    s0 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c66); }
                }
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c64); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsegoodcharsthendot() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 19,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$parsegoodchars();
            if (s1 !== peg$FAILED) {
                if (input.charCodeAt(peg$currPos) === 46) {
                    s2 = peg$c65;
                    peg$currPos++;
                } else {
                    s2 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c66); }
                }
                if (s2 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c68(s1);
                    s0 = s1;
                } else {
                    peg$currPos = s0;
                    s0 = peg$c2;
                }
            } else {
                peg$currPos = s0;
                s0 = peg$c2;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c67); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsechars() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 20,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = [];
            s2 = peg$parsechar();
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    s2 = peg$parsechar();
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                peg$reportedPos = s0;
                s1 = peg$c70(s1);
            }
            s0 = s1;
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c69); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsegoodchars() {
            var s0, s1, s2;

            var key    = peg$currPos * 29 + 21,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = [];
            s2 = peg$parsegoodchar();
            if (s2 !== peg$FAILED) {
                while (s2 !== peg$FAILED) {
                    s1.push(s2);
                    s2 = peg$parsegoodchar();
                }
            } else {
                s1 = peg$c2;
            }
            if (s1 !== peg$FAILED) {
                peg$reportedPos = s0;
                s1 = peg$c70(s1);
            }
            s0 = s1;
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c71); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsechar() {
            var s0, s1;

            var key    = peg$currPos * 29 + 22,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            if (peg$c73.test(input.charAt(peg$currPos))) {
                s0 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s0 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c74); }
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c72); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsegoodchar() {
            var s0, s1;

            var key    = peg$currPos * 29 + 23,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            if (peg$c76.test(input.charAt(peg$currPos))) {
                s0 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s0 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c77); }
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c75); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsenumber() {
            var s0, s1, s2, s3, s4, s5, s6;

            var key    = peg$currPos * 29 + 24,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = peg$currPos;
            s1 = peg$currPos;
            s2 = peg$currPos;
            s3 = peg$parsedigits();
            if (s3 !== peg$FAILED) {
                s4 = peg$currPos;
                if (input.charCodeAt(peg$currPos) === 46) {
                    s5 = peg$c65;
                    peg$currPos++;
                } else {
                    s5 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c66); }
                }
                if (s5 !== peg$FAILED) {
                    s6 = peg$parsedigits();
                    if (s6 !== peg$FAILED) {
                        s5 = [s5, s6];
                        s4 = s5;
                    } else {
                        peg$currPos = s4;
                        s4 = peg$c2;
                    }
                } else {
                    peg$currPos = s4;
                    s4 = peg$c2;
                }
                if (s4 === peg$FAILED) {
                    s4 = peg$c13;
                }
                if (s4 !== peg$FAILED) {
                    s3 = [s3, s4];
                    s2 = s3;
                } else {
                    peg$currPos = s2;
                    s2 = peg$c2;
                }
            } else {
                peg$currPos = s2;
                s2 = peg$c2;
            }
            if (s2 !== peg$FAILED) {
                s2 = input.substring(s1, peg$currPos);
            }
            s1 = s2;
            if (s1 !== peg$FAILED) {
                peg$reportedPos = s0;
                s1 = peg$c79(s1);
            }
            s0 = s1;
            if (s0 === peg$FAILED) {
                s0 = peg$currPos;
                s1 = peg$currPos;
                s2 = peg$currPos;
                if (input.charCodeAt(peg$currPos) === 46) {
                    s3 = peg$c65;
                    peg$currPos++;
                } else {
                    s3 = peg$FAILED;
                    if (peg$silentFails === 0) { peg$fail(peg$c66); }
                }
                if (s3 !== peg$FAILED) {
                    s4 = peg$parsedigits();
                    if (s4 !== peg$FAILED) {
                        s3 = [s3, s4];
                        s2 = s3;
                    } else {
                        peg$currPos = s2;
                        s2 = peg$c2;
                    }
                } else {
                    peg$currPos = s2;
                    s2 = peg$c2;
                }
                if (s2 !== peg$FAILED) {
                    s2 = input.substring(s1, peg$currPos);
                }
                s1 = s2;
                if (s1 !== peg$FAILED) {
                    peg$reportedPos = s0;
                    s1 = peg$c80(s1);
                }
                s0 = s1;
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c78); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsedigits() {
            var s0, s1;

            var key    = peg$currPos * 29 + 25,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            s0 = [];
            s1 = peg$parsedigit();
            if (s1 !== peg$FAILED) {
                while (s1 !== peg$FAILED) {
                    s0.push(s1);
                    s1 = peg$parsedigit();
                }
            } else {
                s0 = peg$c2;
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsedigit() {
            var s0;

            var key    = peg$currPos * 29 + 26,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            if (peg$c81.test(input.charAt(peg$currPos))) {
                s0 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s0 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c82); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parse_() {
            var s0, s1;

            var key    = peg$currPos * 29 + 27,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            peg$silentFails++;
            s0 = [];
            s1 = peg$parsewhitespace();
            while (s1 !== peg$FAILED) {
                s0.push(s1);
                s1 = peg$parsewhitespace();
            }
            peg$silentFails--;
            if (s0 === peg$FAILED) {
                s1 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c83); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }

        function peg$parsewhitespace() {
            var s0;

            var key    = peg$currPos * 29 + 28,
                cached = peg$cache[key];

            if (cached) {
                peg$currPos = cached.nextPos;
                return cached.result;
            }

            if (peg$c84.test(input.charAt(peg$currPos))) {
                s0 = input.charAt(peg$currPos);
                peg$currPos++;
            } else {
                s0 = peg$FAILED;
                if (peg$silentFails === 0) { peg$fail(peg$c85); }
            }

            peg$cache[key] = { nextPos: peg$currPos, result: s0 };

            return s0;
        }


        /*
         * Merge a size object into a results object, taking care to only copy defined values.
         */
        function mergeSize(size, result) {
            if (size) {
                if (size.hasOwnProperty("scale")) {
                    result.scale = size.scale;
                }

                if (size.hasOwnProperty("width")) {
                    result.width = size.width;
                }

                if (size.hasOwnProperty("widthUnit")) {
                    result.widthUnit = size.widthUnit;
                }

                if (size.hasOwnProperty("height")) {
                    result.height = size.height;
                }

                if (size.hasOwnProperty("heightUnit")) {
                    result.heightUnit = size.heightUnit;
                }
            }
        }


        peg$result = peg$startRuleFunction();

        if (peg$result !== peg$FAILED && peg$currPos === input.length) {
            return peg$result;
        } else {
            if (peg$result !== peg$FAILED && peg$currPos < input.length) {
                peg$fail({ type: "end", description: "end of input" });
            }

            throw peg$buildException(null, peg$maxFailExpected, peg$maxFailPos);
        }
    }

    return {
        SyntaxError: SyntaxError,
        parse:       parseMapped
    };

    /* jshint ignore:end */
});