Best JavaScript code snippet using wpt
dust-full-1.2.3.js
Source:dust-full-1.2.3.js
1//2// Dust - Asynchronous Templating v1.2.33// http://akdubya.github.com/dustjs4//5// Copyright (c) 2010, Aleksander Williams6// Released under the MIT License.7//8var dust = {};9function getGlobal(){10 return (function(){11 return this.dust;12 }).call(null);13}14(function(dust) {15dust.helpers = {};16dust.cache = {};17dust.register = function(name, tmpl) {18 if (!name) return;19 dust.cache[name] = tmpl;20};21dust.render = function(name, context, callback) {22 var chunk = new Stub(callback).head;23 dust.load(name, chunk, Context.wrap(context)).end();24};25dust.stream = function(name, context) {26 var stream = new Stream();27 dust.nextTick(function() {28 dust.load(name, stream.head, Context.wrap(context)).end();29 });30 return stream;31};32dust.renderSource = function(source, context, callback) {33 return dust.compileFn(source)(context, callback);34};35dust.compileFn = function(source, name) {36 var tmpl = dust.loadSource(dust.compile(source, name));37 return function(context, callback) {38 var master = callback ? new Stub(callback) : new Stream();39 dust.nextTick(function() {40 tmpl(master.head, Context.wrap(context)).end();41 });42 return master;43 };44};45dust.load = function(name, chunk, context) {46 var tmpl = dust.cache[name];47 if (tmpl) {48 return tmpl(chunk, context);49 } else {50 if (dust.onLoad) {51 return chunk.map(function(chunk) {52 dust.onLoad(name, function(err, src) {53 if (err) return chunk.setError(err);54 if (!dust.cache[name]) dust.loadSource(dust.compile(src, name));55 dust.cache[name](chunk, context).end();56 });57 });58 }59 return chunk.setError(new Error("Template Not Found: " + name));60 }61};62dust.loadSource = function(source, path) {63 return eval(source);64};65if (Array.isArray) {66 dust.isArray = Array.isArray;67} else {68 dust.isArray = function(arr) {69 return Object.prototype.toString.call(arr) == "[object Array]";70 };71}72dust.nextTick = (function() {73 if (typeof process !== "undefined") {74 return process.nextTick;75 } else {76 return function(callback) {77 setTimeout(callback,0);78 };79 }80} )();81dust.isEmpty = function(value) {82 if (dust.isArray(value) && !value.length) return true;83 if (value === 0) return false;84 return (!value);85};86// apply the filter chain and return the output string87dust.filter = function(string, auto, filters) {88 if (filters) {89 for (var i=0, len=filters.length; i<len; i++) {90 var name = filters[i];91 if (name === "s") {92 auto = null;93 }94 // fail silently for invalid filters95 else if (typeof dust.filters[name] === 'function') {96 string = dust.filters[name](string);97 }98 }99 }100 // by default always apply the h filter, unless asked to unescape with |s101 if (auto) {102 string = dust.filters[auto](string);103 }104 return string;105};106dust.filters = {107 h: function(value) { return dust.escapeHtml(value); },108 j: function(value) { return dust.escapeJs(value); },109 u: encodeURI,110 uc: encodeURIComponent,111 js: function(value) { if (!JSON) { return value; } return JSON.stringify(value); },112 jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); }113};114function Context(stack, global, blocks) {115 this.stack = stack;116 this.global = global;117 this.blocks = blocks;118}119dust.makeBase = function(global) {120 return new Context(new Stack(), global);121};122Context.wrap = function(context) {123 if (context instanceof Context) {124 return context;125 }126 return new Context(new Stack(context));127};128Context.prototype.get = function(key) {129 var ctx = this.stack, value;130 while(ctx) {131 if (ctx.isObject) {132 value = ctx.head[key];133 if (!(value === undefined)) {134 return value;135 }136 }137 ctx = ctx.tail;138 }139 return this.global ? this.global[key] : undefined;140};141Context.prototype.getPath = function(cur, down) {142 var ctx = this.stack,143 len = down.length;144 if (cur && len === 0) return ctx.head;145 ctx = ctx.head;146 var i = 0;147 while(ctx && i < len) {148 ctx = ctx[down[i]];149 i++;150 }151 return ctx;152};153Context.prototype.push = function(head, idx, len) {154 return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);155};156Context.prototype.rebase = function(head) {157 return new Context(new Stack(head), this.global, this.blocks);158};159Context.prototype.current = function() {160 return this.stack.head;161};162Context.prototype.getBlock = function(key, chk, ctx) {163 if (typeof key === "function") {164 key = key(chk, ctx).data.join("");165 chk.data = []; //ie7 perf166 }167 var blocks = this.blocks;168 if (!blocks) return;169 var len = blocks.length, fn;170 while (len--) {171 fn = blocks[len][key];172 if (fn) return fn;173 }174};175Context.prototype.shiftBlocks = function(locals) {176 var blocks = this.blocks,177 newBlocks;178 if (locals) {179 if (!blocks) {180 newBlocks = [locals];181 } else {182 newBlocks = blocks.concat([locals]);183 }184 return new Context(this.stack, this.global, newBlocks);185 }186 return this;187};188function Stack(head, tail, idx, len) {189 this.tail = tail;190 this.isObject = !dust.isArray(head) && head && typeof head === "object";191 this.head = head;192 this.index = idx;193 this.of = len;194}195function Stub(callback) {196 this.head = new Chunk(this);197 this.callback = callback;198 this.out = '';199}200Stub.prototype.flush = function() {201 var chunk = this.head;202 while (chunk) {203 if (chunk.flushable) {204 this.out += chunk.data.join(""); //ie7 perf205 } else if (chunk.error) {206 this.callback(chunk.error);207 this.flush = function() {};208 return;209 } else {210 return;211 }212 chunk = chunk.next;213 this.head = chunk;214 }215 this.callback(null, this.out);216};217function Stream() {218 this.head = new Chunk(this);219}220Stream.prototype.flush = function() {221 var chunk = this.head;222 while(chunk) {223 if (chunk.flushable) {224 this.emit('data', chunk.data.join("")); //ie7 perf225 } else if (chunk.error) {226 this.emit('error', chunk.error);227 this.flush = function() {};228 return;229 } else {230 return;231 }232 chunk = chunk.next;233 this.head = chunk;234 }235 this.emit('end');236};237Stream.prototype.emit = function(type, data) {238 if (!this.events) return false;239 var handler = this.events[type];240 if (!handler) return false;241 if (typeof handler == 'function') {242 handler(data);243 } else {244 var listeners = handler.slice(0);245 for (var i = 0, l = listeners.length; i < l; i++) {246 listeners[i](data);247 }248 }249};250Stream.prototype.on = function(type, callback) {251 if (!this.events) {252 this.events = {};253 }254 if (!this.events[type]) {255 this.events[type] = callback;256 } else if(typeof this.events[type] === 'function') {257 this.events[type] = [this.events[type], callback];258 } else {259 this.events[type].push(callback);260 }261 return this;262};263Stream.prototype.pipe = function(stream) {264 this.on("data", function(data) {265 stream.write(data, "utf8");266 }).on("end", function() {267 stream.end();268 }).on("error", function(err) {269 stream.error(err);270 });271 return this;272};273function Chunk(root, next, taps) {274 this.root = root;275 this.next = next;276 this.data = []; //ie7 perf277 this.flushable = false;278 this.taps = taps;279}280Chunk.prototype.write = function(data) {281 var taps = this.taps;282 if (taps) {283 data = taps.go(data);284 }285 this.data.push(data);286 return this;287};288Chunk.prototype.end = function(data) {289 if (data) {290 this.write(data);291 }292 this.flushable = true;293 this.root.flush();294 return this;295};296Chunk.prototype.map = function(callback) {297 var cursor = new Chunk(this.root, this.next, this.taps),298 branch = new Chunk(this.root, cursor, this.taps);299 this.next = branch;300 this.flushable = true;301 callback(branch);302 return cursor;303};304Chunk.prototype.tap = function(tap) {305 var taps = this.taps;306 if (taps) {307 this.taps = taps.push(tap);308 } else {309 this.taps = new Tap(tap);310 }311 return this;312};313Chunk.prototype.untap = function() {314 this.taps = this.taps.tail;315 return this;316};317Chunk.prototype.render = function(body, context) {318 return body(this, context);319};320Chunk.prototype.reference = function(elem, context, auto, filters) {321 if (typeof elem === "function") {322 elem.isFunction = true;323 // Changed the function calling to use apply with the current context to make sure 324 // that "this" is wat we expect it to be inside the function325 elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]);326 if (elem instanceof Chunk) {327 return elem;328 }329 }330 if (!dust.isEmpty(elem)) {331 return this.write(dust.filter(elem, auto, filters));332 } else {333 return this;334 }335};336Chunk.prototype.section = function(elem, context, bodies, params) {337 // anonymous functions338 if (typeof elem === "function") {339 elem = elem.apply(context.current(), [this, context, bodies, params]);340 // functions that return chunks are assumed to have handled the body and/or have modified the chunk341 // use that return value as the current chunk and go to the next method in the chain342 if (elem instanceof Chunk) {343 return elem;344 }345 }346 var body = bodies.block,347 skip = bodies['else'];348 // a.k.a Inline parameters in the Dust documentations349 if (params) {350 context = context.push(params);351 }352 /*353 Dust's default behavior is to enumerate over the array elem, passing each object in the array to the block.354 When elem resolves to a value or object instead of an array, Dust sets the current context to the value 355 and renders the block one time.356 */357 //non empty array is truthy, empty array is falsy358 if (dust.isArray(elem)) {359 if (body) {360 var len = elem.length, chunk = this;361 if (len > 0) {362 // any custom helper can blow up the stack 363 // and store a flattened context, guard defensively364 if(context.stack.head) {365 context.stack.head['$len'] = len;366 }367 for (var i=0; i<len; i++) {368 if(context.stack.head) {369 context.stack.head['$idx'] = i;370 }371 chunk = body(chunk, context.push(elem[i], i, len));372 }373 if(context.stack.head) {374 context.stack.head['$idx'] = undefined;375 context.stack.head['$len'] = undefined;376 }377 return chunk;378 } 379 else if (skip) {380 return skip(this, context);381 }382 }383 }384 // true is truthy but does not change context385 else if (elem === true) {386 if (body) { 387 return body(this, context);388 }389 }390 // everything that evaluates to true are truthy ( e.g. Non-empty strings and Empty objects are truthy. )391 // zero is truthy392 // for anonymous functions that did not returns a chunk, truthiness is evaluated based on the return value393 //394 else if (elem || elem === 0) {395 if (body) return body(this, context.push(elem));396 // nonexistent, scalar false value, scalar empty string, null,397 // undefined are all falsy398 } else if (skip) {399 return skip(this, context);400 } 401 return this;402};403Chunk.prototype.exists = function(elem, context, bodies) {404 var body = bodies.block,405 skip = bodies['else'];406 if (!dust.isEmpty(elem)) {407 if (body) return body(this, context);408 } else if (skip) {409 return skip(this, context);410 }411 return this;412};413Chunk.prototype.notexists = function(elem, context, bodies) {414 var body = bodies.block,415 skip = bodies['else'];416 if (dust.isEmpty(elem)) {417 if (body) return body(this, context);418 } else if (skip) {419 return skip(this, context);420 }421 return this;422};423Chunk.prototype.block = function(elem, context, bodies) {424 var body = bodies.block;425 if (elem) {426 body = elem;427 }428 if (body) {429 return body(this, context);430 }431 return this;432};433Chunk.prototype.partial = function(elem, context, params) {434 var partialContext;435 if (params){436 //put the params context second to match what section does. {.} matches the current context without parameters437 // start with an empty context438 partialContext = dust.makeBase(context.global);439 partialContext.blocks = context.blocks;440 if (context.stack && context.stack.tail){441 // grab the stack(tail) off of the previous context if we have it442 partialContext.stack = context.stack.tail;443 }444 //put params on445 partialContext = partialContext.push(params);446 //reattach the head447 partialContext = partialContext.push(context.stack.head);448 } else {449 partialContext = context;450 }451 if (typeof elem === "function") {452 return this.capture(elem, partialContext, function(name, chunk) {453 dust.load(name, chunk, partialContext).end();454 });455 }456 return dust.load(elem, this, partialContext);457};458Chunk.prototype.helper = function(name, context, bodies, params) {459 // handle invalid helpers, similar to invalid filters460 if( dust.helpers[name]){461 return dust.helpers[name](this, context, bodies, params);462 } else {463 return this;464 }465};466Chunk.prototype.capture = function(body, context, callback) {467 return this.map(function(chunk) {468 var stub = new Stub(function(err, out) {469 if (err) {470 chunk.setError(err);471 } else {472 callback(out, chunk);473 }474 });475 body(stub.head, context).end();476 });477};478Chunk.prototype.setError = function(err) {479 this.error = err;480 this.root.flush();481 return this;482};483function Tap(head, tail) {484 this.head = head;485 this.tail = tail;486}487Tap.prototype.push = function(tap) {488 return new Tap(tap, this);489};490Tap.prototype.go = function(value) {491 var tap = this;492 while(tap) {493 value = tap.head(value);494 tap = tap.tail;495 }496 return value;497};498var HCHARS = new RegExp(/[&<>\"\']/),499 AMP = /&/g,500 LT = /</g,501 GT = />/g,502 QUOT = /\"/g,503 SQUOT = /\'/g;504dust.escapeHtml = function(s) {505 if (typeof s === "string") {506 if (!HCHARS.test(s)) {507 return s;508 }509 return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, ''');510 }511 return s;512};513var BS = /\\/g,514 FS = /\//g,515 CR = /\r/g,516 LS = /\u2028/g,517 PS = /\u2029/g,518 NL = /\n/g,519 LF = /\f/g,520 SQ = /'/g,521 DQ = /"/g,522 TB = /\t/g;523dust.escapeJs = function(s) {524 if (typeof s === "string") {525 return s526 .replace(BS, '\\\\')527 .replace(FS, '\\/')528 .replace(DQ, '\\"')529 .replace(SQ, "\\'")530 .replace(CR, '\\r')531 .replace(LS, '\\u2028')532 .replace(PS, '\\u2029')533 .replace(NL, '\\n')534 .replace(LF, '\\f')535 .replace(TB, "\\t");536 }537 return s;538};539})(dust);540if (typeof exports !== "undefined") {541 if (typeof process !== "undefined") {542 require('./server')(dust);543 }544 module.exports = dust;545}546var dustCompiler = (function(dust) {547dust.compile = function(source, name, strip) {548 try {549 if (strip) {550 source = source.replace(/^\s+/mg, '').replace(/\n/mg, '');551 }552 var ast = filterAST(dust.parse(source));553 return compile(ast, name);554 }555 catch(err)556 {557 if(!err.line || !err.column) throw err; 558 throw new SyntaxError(err.message + " At line : " + err.line + ", column : " + err.column);559 }560};561function filterAST(ast) {562 var context = {};563 return dust.filterNode(context, ast);564};565dust.filterNode = function(context, node) {566 return dust.optimizers[node[0]](context, node);567};568dust.optimizers = {569 body: compactBuffers,570 buffer: noop,571 special: convertSpecial,572 format: nullify, // TODO: convert format573 reference: visit,574 "#": visit,575 "?": visit,576 "^": visit,577 "<": visit,578 "+": visit,579 "@": visit,580 "%": visit,581 partial: visit,582 context: visit,583 params: visit,584 bodies: visit,585 param: visit,586 filters: noop,587 key: noop,588 path: noop,589 literal: noop,590 comment: nullify591};592dust.pragmas = {593 esc: function(compiler, context, bodies, params) {594 var old = compiler.auto;595 if (!context) context = 'h';596 compiler.auto = (context === 's') ? '' : context;597 var out = compileParts(compiler, bodies.block);598 compiler.auto = old;599 return out;600 }601};602function visit(context, node) {603 var out = [node[0]];604 for (var i=1, len=node.length; i<len; i++) {605 var res = dust.filterNode(context, node[i]);606 if (res) out.push(res);607 }608 return out;609};610// Compacts consecutive buffer nodes into a single node611function compactBuffers(context, node) {612 var out = [node[0]], memo;613 for (var i=1, len=node.length; i<len; i++) {614 var res = dust.filterNode(context, node[i]);615 if (res) {616 if (res[0] === 'buffer') {617 if (memo) {618 memo[1] += res[1];619 } else {620 memo = res;621 out.push(res);622 }623 } else {624 memo = null;625 out.push(res);626 }627 }628 }629 return out;630};631var specialChars = {632 "s": " ",633 "n": "\n",634 "r": "\r",635 "lb": "{",636 "rb": "}"637};638function convertSpecial(context, node) { return ['buffer', specialChars[node[1]]] };639function noop(context, node) { return node };640function nullify(){};641function compile(ast, name) {642 var context = {643 name: name,644 bodies: [],645 blocks: {},646 index: 0,647 auto: "h"648 }649 return "(function(){dust.register("650 + (name ? "\"" + name + "\"" : "null") + ","651 + dust.compileNode(context, ast)652 + ");"653 + compileBlocks(context)654 + compileBodies(context)655 + "return body_0;"656 + "})();";657};658function compileBlocks(context) {659 var out = [],660 blocks = context.blocks;661 for (var name in blocks) {662 out.push("'" + name + "':" + blocks[name]);663 }664 if (out.length) {665 context.blocks = "ctx=ctx.shiftBlocks(blocks);";666 return "var blocks={" + out.join(',') + "};";667 }668 return context.blocks = "";669};670function compileBodies(context) {671 var out = [],672 bodies = context.bodies,673 blx = context.blocks;674 for (var i=0, len=bodies.length; i<len; i++) {675 out[i] = "function body_" + i + "(chk,ctx){"676 + blx + "return chk" + bodies[i] + ";}";677 }678 return out.join('');679};680function compileParts(context, body) {681 var parts = '';682 for (var i=1, len=body.length; i<len; i++) {683 parts += dust.compileNode(context, body[i]);684 }685 return parts;686};687dust.compileNode = function(context, node) {688 return dust.nodes[node[0]](context, node);689};690dust.nodes = {691 body: function(context, node) {692 var id = context.index++, name = "body_" + id;693 context.bodies[id] = compileParts(context, node);694 return name;695 },696 buffer: function(context, node) {697 return ".write(" + escape(node[1]) + ")";698 },699 format: function(context, node) {700 return ".write(" + escape(node[1] + node[2]) + ")";701 },702 reference: function(context, node) {703 return ".reference(" + dust.compileNode(context, node[1])704 + ",ctx," + dust.compileNode(context, node[2]) + ")";705 },706 "#": function(context, node) {707 return compileSection(context, node, "section");708 },709 "?": function(context, node) {710 return compileSection(context, node, "exists");711 },712 "^": function(context, node) {713 return compileSection(context, node, "notexists");714 },715 "<": function(context, node) {716 var bodies = node[4];717 for (var i=1, len=bodies.length; i<len; i++) {718 var param = bodies[i],719 type = param[1][1];720 if (type === "block") {721 context.blocks[node[1].text] = dust.compileNode(context, param[2]);722 return '';723 }724 }725 return '';726 },727 "+": function(context, node) {728 if(typeof(node[1].text) === "undefined" && typeof(node[4]) === "undefined"){729 return ".block(ctx.getBlock("730 + dust.compileNode(context, node[1])731 + ",chk, ctx)," + dust.compileNode(context, node[2]) + ", {},"732 + dust.compileNode(context, node[3])733 + ")";734 }else {735 return ".block(ctx.getBlock("736 + escape(node[1].text)737 + ")," + dust.compileNode(context, node[2]) + ","738 + dust.compileNode(context, node[4]) + ","739 + dust.compileNode(context, node[3])740 + ")";741 }742 },743 "@": function(context, node) {744 return ".helper("745 + escape(node[1].text)746 + "," + dust.compileNode(context, node[2]) + ","747 + dust.compileNode(context, node[4]) + ","748 + dust.compileNode(context, node[3])749 + ")";750 },751 "%": function(context, node) {752 // TODO: Move these hacks into pragma precompiler753 var name = node[1][1];754 if (!dust.pragmas[name]) return '';755 var rawBodies = node[4];756 var bodies = {};757 for (var i=1, len=rawBodies.length; i<len; i++) {758 var b = rawBodies[i];759 bodies[b[1][1]] = b[2];760 }761 var rawParams = node[3];762 var params = {};763 for (var i=1, len=rawParams.length; i<len; i++) {764 var p = rawParams[i];765 params[p[1][1]] = p[2][1];766 }767 var ctx = node[2][1] ? node[2][1].text : null;768 return dust.pragmas[name](context, ctx, bodies, params);769 },770 partial: function(context, node) {771 return ".partial("772 + dust.compileNode(context, node[1])773 + "," + dust.compileNode(context, node[2])774 + "," + dust.compileNode(context, node[3]) + ")";775 },776 context: function(context, node) {777 if (node[1]) {778 return "ctx.rebase(" + dust.compileNode(context, node[1]) + ")";779 }780 return "ctx";781 },782 params: function(context, node) {783 var out = [];784 for (var i=1, len=node.length; i<len; i++) {785 out.push(dust.compileNode(context, node[i]));786 }787 if (out.length) {788 return "{" + out.join(',') + "}";789 }790 return "null";791 },792 bodies: function(context, node) {793 var out = [];794 for (var i=1, len=node.length; i<len; i++) {795 out.push(dust.compileNode(context, node[i]));796 }797 return "{" + out.join(',') + "}";798 },799 param: function(context, node) {800 return dust.compileNode(context, node[1]) + ":" + dust.compileNode(context, node[2]);801 },802 filters: function(context, node) {803 var list = [];804 for (var i=1, len=node.length; i<len; i++) {805 var filter = node[i];806 list.push("\"" + filter + "\"");807 }808 return "\"" + context.auto + "\""809 + (list.length ? ",[" + list.join(',') + "]" : '');810 },811 key: function(context, node) {812 return "ctx.get(\"" + node[1] + "\")";813 },814 path: function(context, node) {815 var current = node[1],816 keys = node[2],817 list = [];818 for (var i=0,len=keys.length; i<len; i++) {819 if (dust.isArray(keys[i]))820 list.push(dust.compileNode(context, keys[i]));821 else822 list.push("\"" + keys[i] + "\"");823 }824 return "ctx.getPath(" + current + ",[" + list.join(',') + "])";825 },826 literal: function(context, node) {827 return escape(node[1]);828 }829};830function compileSection(context, node, cmd) {831 return "." + cmd + "("832 + dust.compileNode(context, node[1])833 + "," + dust.compileNode(context, node[2]) + ","834 + dust.compileNode(context, node[4]) + ","835 + dust.compileNode(context, node[3])836 + ")";837};838var escape = (typeof JSON === "undefined")839 ? function(str) { return "\"" + dust.escapeJs(str) + "\"" }840 : JSON.stringify;841 return dust;842});843if (typeof exports !== 'undefined') {844 module.exports = dustCompiler;845} else {846 dustCompiler(getGlobal());847}848(function(dust){849var parser = (function(){850 /*851 * Generated by PEG.js 0.7.0.852 *853 * http://pegjs.majda.cz/854 */855 856 function quote(s) {857 /*858 * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a859 * string literal except for the closing quote character, backslash,860 * carriage return, line separator, paragraph separator, and line feed.861 * Any character may appear in the form of an escape sequence.862 *863 * For portability, we also escape escape all control and non-ASCII864 * characters. Note that "\0" and "\v" escape sequences are not used865 * because JSHint does not like the first and IE the second.866 */867 return '"' + s868 .replace(/\\/g, '\\\\') // backslash869 .replace(/"/g, '\\"') // closing quote character870 .replace(/\x08/g, '\\b') // backspace871 .replace(/\t/g, '\\t') // horizontal tab872 .replace(/\n/g, '\\n') // line feed873 .replace(/\f/g, '\\f') // form feed874 .replace(/\r/g, '\\r') // carriage return875 .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)876 + '"';877 }878 879 var result = {880 /*881 * Parses the input with a generated parser. If the parsing is successfull,882 * returns a value explicitly or implicitly specified by the grammar from883 * which the parser was generated (see |PEG.buildParser|). If the parsing is884 * unsuccessful, throws |PEG.parser.SyntaxError| describing the error.885 */886 parse: function(input, startRule) {887 var parseFunctions = {888 "body": parse_body,889 "part": parse_part,890 "section": parse_section,891 "sec_tag_start": parse_sec_tag_start,892 "end_tag": parse_end_tag,893 "context": parse_context,894 "params": parse_params,895 "bodies": parse_bodies,896 "reference": parse_reference,897 "partial": parse_partial,898 "filters": parse_filters,899 "special": parse_special,900 "identifier": parse_identifier,901 "number": parse_number,902 "frac": parse_frac,903 "integer": parse_integer,904 "path": parse_path,905 "key": parse_key,906 "array": parse_array,907 "array_part": parse_array_part,908 "inline": parse_inline,909 "inline_part": parse_inline_part,910 "buffer": parse_buffer,911 "literal": parse_literal,912 "esc": parse_esc,913 "comment": parse_comment,914 "tag": parse_tag,915 "ld": parse_ld,916 "rd": parse_rd,917 "lb": parse_lb,918 "rb": parse_rb,919 "eol": parse_eol,920 "ws": parse_ws921 };922 923 if (startRule !== undefined) {924 if (parseFunctions[startRule] === undefined) {925 throw new Error("Invalid rule name: " + quote(startRule) + ".");926 }927 } else {928 startRule = "body";929 }930 931 var pos = { offset: 0, line: 1, column: 1, seenCR: false };932 var reportFailures = 0;933 var rightmostFailuresPos = { offset: 0, line: 1, column: 1, seenCR: false };934 var rightmostFailuresExpected = [];935 936 function padLeft(input, padding, length) {937 var result = input;938 939 var padLength = length - input.length;940 for (var i = 0; i < padLength; i++) {941 result = padding + result;942 }943 944 return result;945 }946 947 function escape(ch) {948 var charCode = ch.charCodeAt(0);949 var escapeChar;950 var length;951 952 if (charCode <= 0xFF) {953 escapeChar = 'x';954 length = 2;955 } else {956 escapeChar = 'u';957 length = 4;958 }959 960 return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);961 }962 963 function clone(object) {964 var result = {};965 for (var key in object) {966 result[key] = object[key];967 }968 return result;969 }970 971 function advance(pos, n) {972 var endOffset = pos.offset + n;973 974 for (var offset = pos.offset; offset < endOffset; offset++) {975 var ch = input.charAt(offset);976 if (ch === "\n") {977 if (!pos.seenCR) { pos.line++; }978 pos.column = 1;979 pos.seenCR = false;980 } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {981 pos.line++;982 pos.column = 1;983 pos.seenCR = true;984 } else {985 pos.column++;986 pos.seenCR = false;987 }988 }989 990 pos.offset += n;991 }992 993 function matchFailed(failure) {994 if (pos.offset < rightmostFailuresPos.offset) {995 return;996 }997 998 if (pos.offset > rightmostFailuresPos.offset) {999 rightmostFailuresPos = clone(pos);1000 rightmostFailuresExpected = [];1001 }1002 1003 rightmostFailuresExpected.push(failure);1004 }1005 1006 function parse_body() {1007 var result0, result1;1008 var pos0;1009 1010 pos0 = clone(pos);1011 result0 = [];1012 result1 = parse_part();1013 while (result1 !== null) {1014 result0.push(result1);1015 result1 = parse_part();1016 }1017 if (result0 !== null) {1018 result0 = (function(offset, line, column, p) { return ["body"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1019 }1020 if (result0 === null) {1021 pos = clone(pos0);1022 }1023 return result0;1024 }1025 1026 function parse_part() {1027 var result0;1028 1029 result0 = parse_comment();1030 if (result0 === null) {1031 result0 = parse_section();1032 if (result0 === null) {1033 result0 = parse_partial();1034 if (result0 === null) {1035 result0 = parse_special();1036 if (result0 === null) {1037 result0 = parse_reference();1038 if (result0 === null) {1039 result0 = parse_buffer();1040 }1041 }1042 }1043 }1044 }1045 return result0;1046 }1047 1048 function parse_section() {1049 var result0, result1, result2, result3, result4, result5, result6;1050 var pos0, pos1;1051 1052 reportFailures++;1053 pos0 = clone(pos);1054 pos1 = clone(pos);1055 result0 = parse_sec_tag_start();1056 if (result0 !== null) {1057 result1 = [];1058 result2 = parse_ws();1059 while (result2 !== null) {1060 result1.push(result2);1061 result2 = parse_ws();1062 }1063 if (result1 !== null) {1064 result2 = parse_rd();1065 if (result2 !== null) {1066 result3 = parse_body();1067 if (result3 !== null) {1068 result4 = parse_bodies();1069 if (result4 !== null) {1070 result5 = parse_end_tag();1071 result5 = result5 !== null ? result5 : "";1072 if (result5 !== null) {1073 result6 = (function(offset, line, column, t, b, e, n) {if( (!n) || (t[1].text !== n.text) ) { throw new Error("Expected end tag for "+t[1].text+" but it was not found. At line : "+line+", column : " + column)} return true;})(pos.offset, pos.line, pos.column, result0, result3, result4, result5) ? "" : null;1074 if (result6 !== null) {1075 result0 = [result0, result1, result2, result3, result4, result5, result6];1076 } else {1077 result0 = null;1078 pos = clone(pos1);1079 }1080 } else {1081 result0 = null;1082 pos = clone(pos1);1083 }1084 } else {1085 result0 = null;1086 pos = clone(pos1);1087 }1088 } else {1089 result0 = null;1090 pos = clone(pos1);1091 }1092 } else {1093 result0 = null;1094 pos = clone(pos1);1095 }1096 } else {1097 result0 = null;1098 pos = clone(pos1);1099 }1100 } else {1101 result0 = null;1102 pos = clone(pos1);1103 }1104 if (result0 !== null) {1105 result0 = (function(offset, line, column, t, b, e, n) { e.push(["param", ["literal", "block"], b]); t.push(e); return t })(pos0.offset, pos0.line, pos0.column, result0[0], result0[3], result0[4], result0[5]);1106 }1107 if (result0 === null) {1108 pos = clone(pos0);1109 }1110 if (result0 === null) {1111 pos0 = clone(pos);1112 pos1 = clone(pos);1113 result0 = parse_sec_tag_start();1114 if (result0 !== null) {1115 result1 = [];1116 result2 = parse_ws();1117 while (result2 !== null) {1118 result1.push(result2);1119 result2 = parse_ws();1120 }1121 if (result1 !== null) {1122 if (input.charCodeAt(pos.offset) === 47) {1123 result2 = "/";1124 advance(pos, 1);1125 } else {1126 result2 = null;1127 if (reportFailures === 0) {1128 matchFailed("\"/\"");1129 }1130 }1131 if (result2 !== null) {1132 result3 = parse_rd();1133 if (result3 !== null) {1134 result0 = [result0, result1, result2, result3];1135 } else {1136 result0 = null;1137 pos = clone(pos1);1138 }1139 } else {1140 result0 = null;1141 pos = clone(pos1);1142 }1143 } else {1144 result0 = null;1145 pos = clone(pos1);1146 }1147 } else {1148 result0 = null;1149 pos = clone(pos1);1150 }1151 if (result0 !== null) {1152 result0 = (function(offset, line, column, t) { t.push(["bodies"]); return t })(pos0.offset, pos0.line, pos0.column, result0[0]);1153 }1154 if (result0 === null) {1155 pos = clone(pos0);1156 }1157 }1158 reportFailures--;1159 if (reportFailures === 0 && result0 === null) {1160 matchFailed("section");1161 }1162 return result0;1163 }1164 1165 function parse_sec_tag_start() {1166 var result0, result1, result2, result3, result4, result5;1167 var pos0, pos1;1168 1169 pos0 = clone(pos);1170 pos1 = clone(pos);1171 result0 = parse_ld();1172 if (result0 !== null) {1173 if (/^[#?^<+@%]/.test(input.charAt(pos.offset))) {1174 result1 = input.charAt(pos.offset);1175 advance(pos, 1);1176 } else {1177 result1 = null;1178 if (reportFailures === 0) {1179 matchFailed("[#?^<+@%]");1180 }1181 }1182 if (result1 !== null) {1183 result2 = [];1184 result3 = parse_ws();1185 while (result3 !== null) {1186 result2.push(result3);1187 result3 = parse_ws();1188 }1189 if (result2 !== null) {1190 result3 = parse_identifier();1191 if (result3 !== null) {1192 result4 = parse_context();1193 if (result4 !== null) {1194 result5 = parse_params();1195 if (result5 !== null) {1196 result0 = [result0, result1, result2, result3, result4, result5];1197 } else {1198 result0 = null;1199 pos = clone(pos1);1200 }1201 } else {1202 result0 = null;1203 pos = clone(pos1);1204 }1205 } else {1206 result0 = null;1207 pos = clone(pos1);1208 }1209 } else {1210 result0 = null;1211 pos = clone(pos1);1212 }1213 } else {1214 result0 = null;1215 pos = clone(pos1);1216 }1217 } else {1218 result0 = null;1219 pos = clone(pos1);1220 }1221 if (result0 !== null) {1222 result0 = (function(offset, line, column, t, n, c, p) { return [t, n, c, p] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[3], result0[4], result0[5]);1223 }1224 if (result0 === null) {1225 pos = clone(pos0);1226 }1227 return result0;1228 }1229 1230 function parse_end_tag() {1231 var result0, result1, result2, result3, result4, result5;1232 var pos0, pos1;1233 1234 reportFailures++;1235 pos0 = clone(pos);1236 pos1 = clone(pos);1237 result0 = parse_ld();1238 if (result0 !== null) {1239 if (input.charCodeAt(pos.offset) === 47) {1240 result1 = "/";1241 advance(pos, 1);1242 } else {1243 result1 = null;1244 if (reportFailures === 0) {1245 matchFailed("\"/\"");1246 }1247 }1248 if (result1 !== null) {1249 result2 = [];1250 result3 = parse_ws();1251 while (result3 !== null) {1252 result2.push(result3);1253 result3 = parse_ws();1254 }1255 if (result2 !== null) {1256 result3 = parse_identifier();1257 if (result3 !== null) {1258 result4 = [];1259 result5 = parse_ws();1260 while (result5 !== null) {1261 result4.push(result5);1262 result5 = parse_ws();1263 }1264 if (result4 !== null) {1265 result5 = parse_rd();1266 if (result5 !== null) {1267 result0 = [result0, result1, result2, result3, result4, result5];1268 } else {1269 result0 = null;1270 pos = clone(pos1);1271 }1272 } else {1273 result0 = null;1274 pos = clone(pos1);1275 }1276 } else {1277 result0 = null;1278 pos = clone(pos1);1279 }1280 } else {1281 result0 = null;1282 pos = clone(pos1);1283 }1284 } else {1285 result0 = null;1286 pos = clone(pos1);1287 }1288 } else {1289 result0 = null;1290 pos = clone(pos1);1291 }1292 if (result0 !== null) {1293 result0 = (function(offset, line, column, n) { return n })(pos0.offset, pos0.line, pos0.column, result0[3]);1294 }1295 if (result0 === null) {1296 pos = clone(pos0);1297 }1298 reportFailures--;1299 if (reportFailures === 0 && result0 === null) {1300 matchFailed("end tag");1301 }1302 return result0;1303 }1304 1305 function parse_context() {1306 var result0, result1;1307 var pos0, pos1, pos2;1308 1309 pos0 = clone(pos);1310 pos1 = clone(pos);1311 pos2 = clone(pos);1312 if (input.charCodeAt(pos.offset) === 58) {1313 result0 = ":";1314 advance(pos, 1);1315 } else {1316 result0 = null;1317 if (reportFailures === 0) {1318 matchFailed("\":\"");1319 }1320 }1321 if (result0 !== null) {1322 result1 = parse_identifier();1323 if (result1 !== null) {1324 result0 = [result0, result1];1325 } else {1326 result0 = null;1327 pos = clone(pos2);1328 }1329 } else {1330 result0 = null;1331 pos = clone(pos2);1332 }1333 if (result0 !== null) {1334 result0 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result0[1]);1335 }1336 if (result0 === null) {1337 pos = clone(pos1);1338 }1339 result0 = result0 !== null ? result0 : "";1340 if (result0 !== null) {1341 result0 = (function(offset, line, column, n) { return n ? ["context", n] : ["context"] })(pos0.offset, pos0.line, pos0.column, result0);1342 }1343 if (result0 === null) {1344 pos = clone(pos0);1345 }1346 return result0;1347 }1348 1349 function parse_params() {1350 var result0, result1, result2, result3, result4;1351 var pos0, pos1, pos2;1352 1353 reportFailures++;1354 pos0 = clone(pos);1355 result0 = [];1356 pos1 = clone(pos);1357 pos2 = clone(pos);1358 result2 = parse_ws();1359 if (result2 !== null) {1360 result1 = [];1361 while (result2 !== null) {1362 result1.push(result2);1363 result2 = parse_ws();1364 }1365 } else {1366 result1 = null;1367 }1368 if (result1 !== null) {1369 result2 = parse_key();1370 if (result2 !== null) {1371 if (input.charCodeAt(pos.offset) === 61) {1372 result3 = "=";1373 advance(pos, 1);1374 } else {1375 result3 = null;1376 if (reportFailures === 0) {1377 matchFailed("\"=\"");1378 }1379 }1380 if (result3 !== null) {1381 result4 = parse_number();1382 if (result4 === null) {1383 result4 = parse_identifier();1384 if (result4 === null) {1385 result4 = parse_inline();1386 }1387 }1388 if (result4 !== null) {1389 result1 = [result1, result2, result3, result4];1390 } else {1391 result1 = null;1392 pos = clone(pos2);1393 }1394 } else {1395 result1 = null;1396 pos = clone(pos2);1397 }1398 } else {1399 result1 = null;1400 pos = clone(pos2);1401 }1402 } else {1403 result1 = null;1404 pos = clone(pos2);1405 }1406 if (result1 !== null) {1407 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[1], result1[3]);1408 }1409 if (result1 === null) {1410 pos = clone(pos1);1411 }1412 while (result1 !== null) {1413 result0.push(result1);1414 pos1 = clone(pos);1415 pos2 = clone(pos);1416 result2 = parse_ws();1417 if (result2 !== null) {1418 result1 = [];1419 while (result2 !== null) {1420 result1.push(result2);1421 result2 = parse_ws();1422 }1423 } else {1424 result1 = null;1425 }1426 if (result1 !== null) {1427 result2 = parse_key();1428 if (result2 !== null) {1429 if (input.charCodeAt(pos.offset) === 61) {1430 result3 = "=";1431 advance(pos, 1);1432 } else {1433 result3 = null;1434 if (reportFailures === 0) {1435 matchFailed("\"=\"");1436 }1437 }1438 if (result3 !== null) {1439 result4 = parse_number();1440 if (result4 === null) {1441 result4 = parse_identifier();1442 if (result4 === null) {1443 result4 = parse_inline();1444 }1445 }1446 if (result4 !== null) {1447 result1 = [result1, result2, result3, result4];1448 } else {1449 result1 = null;1450 pos = clone(pos2);1451 }1452 } else {1453 result1 = null;1454 pos = clone(pos2);1455 }1456 } else {1457 result1 = null;1458 pos = clone(pos2);1459 }1460 } else {1461 result1 = null;1462 pos = clone(pos2);1463 }1464 if (result1 !== null) {1465 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[1], result1[3]);1466 }1467 if (result1 === null) {1468 pos = clone(pos1);1469 }1470 }1471 if (result0 !== null) {1472 result0 = (function(offset, line, column, p) { return ["params"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1473 }1474 if (result0 === null) {1475 pos = clone(pos0);1476 }1477 reportFailures--;1478 if (reportFailures === 0 && result0 === null) {1479 matchFailed("params");1480 }1481 return result0;1482 }1483 1484 function parse_bodies() {1485 var result0, result1, result2, result3, result4, result5;1486 var pos0, pos1, pos2;1487 1488 reportFailures++;1489 pos0 = clone(pos);1490 result0 = [];1491 pos1 = clone(pos);1492 pos2 = clone(pos);1493 result1 = parse_ld();1494 if (result1 !== null) {1495 if (input.charCodeAt(pos.offset) === 58) {1496 result2 = ":";1497 advance(pos, 1);1498 } else {1499 result2 = null;1500 if (reportFailures === 0) {1501 matchFailed("\":\"");1502 }1503 }1504 if (result2 !== null) {1505 result3 = parse_key();1506 if (result3 !== null) {1507 result4 = parse_rd();1508 if (result4 !== null) {1509 result5 = parse_body();1510 if (result5 !== null) {1511 result1 = [result1, result2, result3, result4, result5];1512 } else {1513 result1 = null;1514 pos = clone(pos2);1515 }1516 } else {1517 result1 = null;1518 pos = clone(pos2);1519 }1520 } else {1521 result1 = null;1522 pos = clone(pos2);1523 }1524 } else {1525 result1 = null;1526 pos = clone(pos2);1527 }1528 } else {1529 result1 = null;1530 pos = clone(pos2);1531 }1532 if (result1 !== null) {1533 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[2], result1[4]);1534 }1535 if (result1 === null) {1536 pos = clone(pos1);1537 }1538 while (result1 !== null) {1539 result0.push(result1);1540 pos1 = clone(pos);1541 pos2 = clone(pos);1542 result1 = parse_ld();1543 if (result1 !== null) {1544 if (input.charCodeAt(pos.offset) === 58) {1545 result2 = ":";1546 advance(pos, 1);1547 } else {1548 result2 = null;1549 if (reportFailures === 0) {1550 matchFailed("\":\"");1551 }1552 }1553 if (result2 !== null) {1554 result3 = parse_key();1555 if (result3 !== null) {1556 result4 = parse_rd();1557 if (result4 !== null) {1558 result5 = parse_body();1559 if (result5 !== null) {1560 result1 = [result1, result2, result3, result4, result5];1561 } else {1562 result1 = null;1563 pos = clone(pos2);1564 }1565 } else {1566 result1 = null;1567 pos = clone(pos2);1568 }1569 } else {1570 result1 = null;1571 pos = clone(pos2);1572 }1573 } else {1574 result1 = null;1575 pos = clone(pos2);1576 }1577 } else {1578 result1 = null;1579 pos = clone(pos2);1580 }1581 if (result1 !== null) {1582 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[2], result1[4]);1583 }1584 if (result1 === null) {1585 pos = clone(pos1);1586 }1587 }1588 if (result0 !== null) {1589 result0 = (function(offset, line, column, p) { return ["bodies"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1590 }1591 if (result0 === null) {1592 pos = clone(pos0);1593 }1594 reportFailures--;1595 if (reportFailures === 0 && result0 === null) {1596 matchFailed("bodies");1597 }1598 return result0;1599 }1600 1601 function parse_reference() {1602 var result0, result1, result2, result3;1603 var pos0, pos1;1604 1605 reportFailures++;1606 pos0 = clone(pos);1607 pos1 = clone(pos);1608 result0 = parse_ld();1609 if (result0 !== null) {1610 result1 = parse_identifier();1611 if (result1 !== null) {1612 result2 = parse_filters();1613 if (result2 !== null) {1614 result3 = parse_rd();1615 if (result3 !== null) {1616 result0 = [result0, result1, result2, result3];1617 } else {1618 result0 = null;1619 pos = clone(pos1);1620 }1621 } else {1622 result0 = null;1623 pos = clone(pos1);1624 }1625 } else {1626 result0 = null;1627 pos = clone(pos1);1628 }1629 } else {1630 result0 = null;1631 pos = clone(pos1);1632 }1633 if (result0 !== null) {1634 result0 = (function(offset, line, column, n, f) { return ["reference", n, f] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[2]);1635 }1636 if (result0 === null) {1637 pos = clone(pos0);1638 }1639 reportFailures--;1640 if (reportFailures === 0 && result0 === null) {1641 matchFailed("reference");1642 }1643 return result0;1644 }1645 1646 function parse_partial() {1647 var result0, result1, result2, result3, result4, result5, result6, result7, result8;1648 var pos0, pos1, pos2;1649 1650 reportFailures++;1651 pos0 = clone(pos);1652 pos1 = clone(pos);1653 result0 = parse_ld();1654 if (result0 !== null) {1655 if (input.charCodeAt(pos.offset) === 62) {1656 result1 = ">";1657 advance(pos, 1);1658 } else {1659 result1 = null;1660 if (reportFailures === 0) {1661 matchFailed("\">\"");1662 }1663 }1664 if (result1 === null) {1665 if (input.charCodeAt(pos.offset) === 43) {1666 result1 = "+";1667 advance(pos, 1);1668 } else {1669 result1 = null;1670 if (reportFailures === 0) {1671 matchFailed("\"+\"");1672 }1673 }1674 }1675 if (result1 !== null) {1676 result2 = [];1677 result3 = parse_ws();1678 while (result3 !== null) {1679 result2.push(result3);1680 result3 = parse_ws();1681 }1682 if (result2 !== null) {1683 pos2 = clone(pos);1684 result3 = parse_key();1685 if (result3 !== null) {1686 result3 = (function(offset, line, column, k) {return ["literal", k]})(pos2.offset, pos2.line, pos2.column, result3);1687 }1688 if (result3 === null) {1689 pos = clone(pos2);1690 }1691 if (result3 === null) {1692 result3 = parse_inline();1693 }1694 if (result3 !== null) {1695 result4 = parse_context();1696 if (result4 !== null) {1697 result5 = parse_params();1698 if (result5 !== null) {1699 result6 = [];1700 result7 = parse_ws();1701 while (result7 !== null) {1702 result6.push(result7);1703 result7 = parse_ws();1704 }1705 if (result6 !== null) {1706 if (input.charCodeAt(pos.offset) === 47) {1707 result7 = "/";1708 advance(pos, 1);1709 } else {1710 result7 = null;1711 if (reportFailures === 0) {1712 matchFailed("\"/\"");1713 }1714 }1715 if (result7 !== null) {1716 result8 = parse_rd();1717 if (result8 !== null) {1718 result0 = [result0, result1, result2, result3, result4, result5, result6, result7, result8];1719 } else {1720 result0 = null;1721 pos = clone(pos1);1722 }1723 } else {1724 result0 = null;1725 pos = clone(pos1);1726 }1727 } else {1728 result0 = null;1729 pos = clone(pos1);1730 }1731 } else {1732 result0 = null;1733 pos = clone(pos1);1734 }1735 } else {1736 result0 = null;1737 pos = clone(pos1);1738 }1739 } else {1740 result0 = null;1741 pos = clone(pos1);1742 }1743 } else {1744 result0 = null;1745 pos = clone(pos1);1746 }1747 } else {1748 result0 = null;1749 pos = clone(pos1);1750 }1751 } else {1752 result0 = null;1753 pos = clone(pos1);1754 }1755 if (result0 !== null) {1756 result0 = (function(offset, line, column, s, n, c, p) { var key = (s ===">")? "partial" : s; return [key, n, c, p] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[3], result0[4], result0[5]);1757 }1758 if (result0 === null) {1759 pos = clone(pos0);1760 }1761 reportFailures--;1762 if (reportFailures === 0 && result0 === null) {1763 matchFailed("partial");1764 }1765 return result0;1766 }1767 1768 function parse_filters() {1769 var result0, result1, result2;1770 var pos0, pos1, pos2;1771 1772 reportFailures++;1773 pos0 = clone(pos);1774 result0 = [];1775 pos1 = clone(pos);1776 pos2 = clone(pos);1777 if (input.charCodeAt(pos.offset) === 124) {1778 result1 = "|";1779 advance(pos, 1);1780 } else {1781 result1 = null;1782 if (reportFailures === 0) {1783 matchFailed("\"|\"");1784 }1785 }1786 if (result1 !== null) {1787 result2 = parse_key();1788 if (result2 !== null) {1789 result1 = [result1, result2];1790 } else {1791 result1 = null;1792 pos = clone(pos2);1793 }1794 } else {1795 result1 = null;1796 pos = clone(pos2);1797 }1798 if (result1 !== null) {1799 result1 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result1[1]);1800 }1801 if (result1 === null) {1802 pos = clone(pos1);1803 }1804 while (result1 !== null) {1805 result0.push(result1);1806 pos1 = clone(pos);1807 pos2 = clone(pos);1808 if (input.charCodeAt(pos.offset) === 124) {1809 result1 = "|";1810 advance(pos, 1);1811 } else {1812 result1 = null;1813 if (reportFailures === 0) {1814 matchFailed("\"|\"");1815 }1816 }1817 if (result1 !== null) {1818 result2 = parse_key();1819 if (result2 !== null) {1820 result1 = [result1, result2];1821 } else {1822 result1 = null;1823 pos = clone(pos2);1824 }1825 } else {1826 result1 = null;1827 pos = clone(pos2);1828 }1829 if (result1 !== null) {1830 result1 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result1[1]);1831 }1832 if (result1 === null) {1833 pos = clone(pos1);1834 }1835 }1836 if (result0 !== null) {1837 result0 = (function(offset, line, column, f) { return ["filters"].concat(f) })(pos0.offset, pos0.line, pos0.column, result0);1838 }1839 if (result0 === null) {1840 pos = clone(pos0);1841 }1842 reportFailures--;1843 if (reportFailures === 0 && result0 === null) {1844 matchFailed("filters");1845 }1846 return result0;1847 }1848 1849 function parse_special() {1850 var result0, result1, result2, result3;1851 var pos0, pos1;1852 1853 reportFailures++;1854 pos0 = clone(pos);1855 pos1 = clone(pos);1856 result0 = parse_ld();1857 if (result0 !== null) {1858 if (input.charCodeAt(pos.offset) === 126) {1859 result1 = "~";1860 advance(pos, 1);1861 } else {1862 result1 = null;1863 if (reportFailures === 0) {1864 matchFailed("\"~\"");1865 }1866 }1867 if (result1 !== null) {1868 result2 = parse_key();1869 if (result2 !== null) {1870 result3 = parse_rd();1871 if (result3 !== null) {1872 result0 = [result0, result1, result2, result3];1873 } else {1874 result0 = null;1875 pos = clone(pos1);1876 }1877 } else {1878 result0 = null;1879 pos = clone(pos1);1880 }1881 } else {1882 result0 = null;1883 pos = clone(pos1);1884 }1885 } else {1886 result0 = null;1887 pos = clone(pos1);1888 }1889 if (result0 !== null) {1890 result0 = (function(offset, line, column, k) { return ["special", k] })(pos0.offset, pos0.line, pos0.column, result0[2]);1891 }1892 if (result0 === null) {1893 pos = clone(pos0);1894 }1895 reportFailures--;1896 if (reportFailures === 0 && result0 === null) {1897 matchFailed("special");1898 }1899 return result0;1900 }1901 1902 function parse_identifier() {1903 var result0;1904 var pos0;1905 1906 reportFailures++;1907 pos0 = clone(pos);1908 result0 = parse_path();1909 if (result0 !== null) {1910 result0 = (function(offset, line, column, p) { var arr = ["path"].concat(p); arr.text = p[1].join('.'); return arr; })(pos0.offset, pos0.line, pos0.column, result0);1911 }1912 if (result0 === null) {1913 pos = clone(pos0);1914 }1915 if (result0 === null) {1916 pos0 = clone(pos);1917 result0 = parse_key();1918 if (result0 !== null) {1919 result0 = (function(offset, line, column, k) { var arr = ["key", k]; arr.text = k; return arr; })(pos0.offset, pos0.line, pos0.column, result0);1920 }1921 if (result0 === null) {1922 pos = clone(pos0);1923 }1924 }1925 reportFailures--;1926 if (reportFailures === 0 && result0 === null) {1927 matchFailed("identifier");1928 }1929 return result0;1930 }1931 1932 function parse_number() {1933 var result0;1934 var pos0;1935 1936 reportFailures++;1937 pos0 = clone(pos);1938 result0 = parse_frac();1939 if (result0 === null) {1940 result0 = parse_integer();1941 }1942 if (result0 !== null) {1943 result0 = (function(offset, line, column, n) { return ['literal', n]; })(pos0.offset, pos0.line, pos0.column, result0);1944 }1945 if (result0 === null) {1946 pos = clone(pos0);1947 }1948 reportFailures--;1949 if (reportFailures === 0 && result0 === null) {1950 matchFailed("number");1951 }1952 return result0;1953 }1954 1955 function parse_frac() {1956 var result0, result1, result2, result3;1957 var pos0, pos1;1958 1959 reportFailures++;1960 pos0 = clone(pos);1961 pos1 = clone(pos);1962 result0 = parse_integer();1963 if (result0 !== null) {1964 if (input.charCodeAt(pos.offset) === 46) {1965 result1 = ".";1966 advance(pos, 1);1967 } else {1968 result1 = null;1969 if (reportFailures === 0) {1970 matchFailed("\".\"");1971 }1972 }1973 if (result1 !== null) {1974 result3 = parse_integer();1975 if (result3 !== null) {1976 result2 = [];1977 while (result3 !== null) {1978 result2.push(result3);1979 result3 = parse_integer();1980 }1981 } else {1982 result2 = null;1983 }1984 if (result2 !== null) {1985 result0 = [result0, result1, result2];1986 } else {1987 result0 = null;1988 pos = clone(pos1);1989 }1990 } else {1991 result0 = null;1992 pos = clone(pos1);1993 }1994 } else {1995 result0 = null;1996 pos = clone(pos1);1997 }1998 if (result0 !== null) {1999 result0 = (function(offset, line, column, l, r) { return parseFloat(l + "." + r.join('')); })(pos0.offset, pos0.line, pos0.column, result0[0], result0[2]);2000 }2001 if (result0 === null) {2002 pos = clone(pos0);2003 }2004 reportFailures--;2005 if (reportFailures === 0 && result0 === null) {2006 matchFailed("frac");2007 }2008 return result0;2009 }2010 2011 function parse_integer() {2012 var result0, result1;2013 var pos0;2014 2015 reportFailures++;2016 pos0 = clone(pos);2017 if (/^[0-9]/.test(input.charAt(pos.offset))) {2018 result1 = input.charAt(pos.offset);2019 advance(pos, 1);2020 } else {2021 result1 = null;2022 if (reportFailures === 0) {2023 matchFailed("[0-9]");2024 }2025 }2026 if (result1 !== null) {2027 result0 = [];2028 while (result1 !== null) {2029 result0.push(result1);2030 if (/^[0-9]/.test(input.charAt(pos.offset))) {2031 result1 = input.charAt(pos.offset);2032 advance(pos, 1);2033 } else {2034 result1 = null;2035 if (reportFailures === 0) {2036 matchFailed("[0-9]");2037 }2038 }2039 }2040 } else {2041 result0 = null;2042 }2043 if (result0 !== null) {2044 result0 = (function(offset, line, column, digits) { return parseInt(digits.join(""), 10); })(pos0.offset, pos0.line, pos0.column, result0);2045 }2046 if (result0 === null) {2047 pos = clone(pos0);2048 }2049 reportFailures--;2050 if (reportFailures === 0 && result0 === null) {2051 matchFailed("integer");2052 }2053 return result0;2054 }2055 2056 function parse_path() {2057 var result0, result1, result2;2058 var pos0, pos1;2059 2060 reportFailures++;2061 pos0 = clone(pos);2062 pos1 = clone(pos);2063 result0 = parse_key();2064 result0 = result0 !== null ? result0 : "";2065 if (result0 !== null) {2066 result2 = parse_array_part();2067 if (result2 === null) {2068 result2 = parse_array();2069 }2070 if (result2 !== null) {2071 result1 = [];2072 while (result2 !== null) {2073 result1.push(result2);2074 result2 = parse_array_part();2075 if (result2 === null) {2076 result2 = parse_array();2077 }2078 }2079 } else {2080 result1 = null;2081 }2082 if (result1 !== null) {2083 result0 = [result0, result1];2084 } else {2085 result0 = null;2086 pos = clone(pos1);2087 }2088 } else {2089 result0 = null;2090 pos = clone(pos1);2091 }2092 if (result0 !== null) {2093 result0 = (function(offset, line, column, k, d) {2094 d = d[0]; 2095 if (k && d) {2096 d.unshift(k);2097 return [false, d];2098 }2099 return [true, d];2100 })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2101 }2102 if (result0 === null) {2103 pos = clone(pos0);2104 }2105 if (result0 === null) {2106 pos0 = clone(pos);2107 pos1 = clone(pos);2108 if (input.charCodeAt(pos.offset) === 46) {2109 result0 = ".";2110 advance(pos, 1);2111 } else {2112 result0 = null;2113 if (reportFailures === 0) {2114 matchFailed("\".\"");2115 }2116 }2117 if (result0 !== null) {2118 result1 = [];2119 result2 = parse_array_part();2120 if (result2 === null) {2121 result2 = parse_array();2122 }2123 while (result2 !== null) {2124 result1.push(result2);2125 result2 = parse_array_part();2126 if (result2 === null) {2127 result2 = parse_array();2128 }2129 }2130 if (result1 !== null) {2131 result0 = [result0, result1];2132 } else {2133 result0 = null;2134 pos = clone(pos1);2135 }2136 } else {2137 result0 = null;2138 pos = clone(pos1);2139 }2140 if (result0 !== null) {2141 result0 = (function(offset, line, column, d) {2142 if (d.length > 0) {2143 return [true, d[0]];2144 }2145 return [true, []] 2146 })(pos0.offset, pos0.line, pos0.column, result0[1]);2147 }2148 if (result0 === null) {2149 pos = clone(pos0);2150 }2151 }2152 reportFailures--;2153 if (reportFailures === 0 && result0 === null) {2154 matchFailed("path");2155 }2156 return result0;2157 }2158 2159 function parse_key() {2160 var result0, result1, result2;2161 var pos0, pos1;2162 2163 reportFailures++;2164 pos0 = clone(pos);2165 pos1 = clone(pos);2166 if (/^[a-zA-Z_$]/.test(input.charAt(pos.offset))) {2167 result0 = input.charAt(pos.offset);2168 advance(pos, 1);2169 } else {2170 result0 = null;2171 if (reportFailures === 0) {2172 matchFailed("[a-zA-Z_$]");2173 }2174 }2175 if (result0 !== null) {2176 result1 = [];2177 if (/^[0-9a-zA-Z_$\-]/.test(input.charAt(pos.offset))) {2178 result2 = input.charAt(pos.offset);2179 advance(pos, 1);2180 } else {2181 result2 = null;2182 if (reportFailures === 0) {2183 matchFailed("[0-9a-zA-Z_$\\-]");2184 }2185 }2186 while (result2 !== null) {2187 result1.push(result2);2188 if (/^[0-9a-zA-Z_$\-]/.test(input.charAt(pos.offset))) {2189 result2 = input.charAt(pos.offset);2190 advance(pos, 1);2191 } else {2192 result2 = null;2193 if (reportFailures === 0) {2194 matchFailed("[0-9a-zA-Z_$\\-]");2195 }2196 }2197 }2198 if (result1 !== null) {2199 result0 = [result0, result1];2200 } else {2201 result0 = null;2202 pos = clone(pos1);2203 }2204 } else {2205 result0 = null;2206 pos = clone(pos1);2207 }2208 if (result0 !== null) {2209 result0 = (function(offset, line, column, h, t) { return h + t.join('') })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2210 }2211 if (result0 === null) {2212 pos = clone(pos0);2213 }2214 reportFailures--;2215 if (reportFailures === 0 && result0 === null) {2216 matchFailed("key");2217 }2218 return result0;2219 }2220 2221 function parse_array() {2222 var result0, result1, result2;2223 var pos0, pos1, pos2, pos3, pos4;2224 2225 reportFailures++;2226 pos0 = clone(pos);2227 pos1 = clone(pos);2228 pos2 = clone(pos);2229 pos3 = clone(pos);2230 result0 = parse_lb();2231 if (result0 !== null) {2232 pos4 = clone(pos);2233 if (/^[0-9]/.test(input.charAt(pos.offset))) {2234 result2 = input.charAt(pos.offset);2235 advance(pos, 1);2236 } else {2237 result2 = null;2238 if (reportFailures === 0) {2239 matchFailed("[0-9]");2240 }2241 }2242 if (result2 !== null) {2243 result1 = [];2244 while (result2 !== null) {2245 result1.push(result2);2246 if (/^[0-9]/.test(input.charAt(pos.offset))) {2247 result2 = input.charAt(pos.offset);2248 advance(pos, 1);2249 } else {2250 result2 = null;2251 if (reportFailures === 0) {2252 matchFailed("[0-9]");2253 }2254 }2255 }2256 } else {2257 result1 = null;2258 }2259 if (result1 !== null) {2260 result1 = (function(offset, line, column, n) {return n.join('')})(pos4.offset, pos4.line, pos4.column, result1);2261 }2262 if (result1 === null) {2263 pos = clone(pos4);2264 }2265 if (result1 === null) {2266 result1 = parse_identifier();2267 }2268 if (result1 !== null) {2269 result2 = parse_rb();2270 if (result2 !== null) {2271 result0 = [result0, result1, result2];2272 } else {2273 result0 = null;2274 pos = clone(pos3);2275 }2276 } else {2277 result0 = null;2278 pos = clone(pos3);2279 }2280 } else {2281 result0 = null;2282 pos = clone(pos3);2283 }2284 if (result0 !== null) {2285 result0 = (function(offset, line, column, a) {return a; })(pos2.offset, pos2.line, pos2.column, result0[1]);2286 }2287 if (result0 === null) {2288 pos = clone(pos2);2289 }2290 if (result0 !== null) {2291 result1 = parse_array_part();2292 result1 = result1 !== null ? result1 : "";2293 if (result1 !== null) {2294 result0 = [result0, result1];2295 } else {2296 result0 = null;2297 pos = clone(pos1);2298 }2299 } else {2300 result0 = null;2301 pos = clone(pos1);2302 }2303 if (result0 !== null) {2304 result0 = (function(offset, line, column, i, nk) { if(nk) { nk.unshift(i); } else {nk = [i] } return nk; })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2305 }2306 if (result0 === null) {2307 pos = clone(pos0);2308 }2309 reportFailures--;2310 if (reportFailures === 0 && result0 === null) {2311 matchFailed("array");2312 }2313 return result0;2314 }2315 2316 function parse_array_part() {2317 var result0, result1, result2;2318 var pos0, pos1, pos2, pos3;2319 2320 reportFailures++;2321 pos0 = clone(pos);2322 pos1 = clone(pos);2323 pos2 = clone(pos);2324 pos3 = clone(pos);2325 if (input.charCodeAt(pos.offset) === 46) {2326 result1 = ".";2327 advance(pos, 1);2328 } else {2329 result1 = null;2330 if (reportFailures === 0) {2331 matchFailed("\".\"");2332 }2333 }2334 if (result1 !== null) {2335 result2 = parse_key();2336 if (result2 !== null) {2337 result1 = [result1, result2];2338 } else {2339 result1 = null;2340 pos = clone(pos3);2341 }2342 } else {2343 result1 = null;2344 pos = clone(pos3);2345 }2346 if (result1 !== null) {2347 result1 = (function(offset, line, column, k) {return k})(pos2.offset, pos2.line, pos2.column, result1[1]);2348 }2349 if (result1 === null) {2350 pos = clone(pos2);2351 }2352 if (result1 !== null) {2353 result0 = [];2354 while (result1 !== null) {2355 result0.push(result1);2356 pos2 = clone(pos);2357 pos3 = clone(pos);2358 if (input.charCodeAt(pos.offset) === 46) {2359 result1 = ".";2360 advance(pos, 1);2361 } else {2362 result1 = null;2363 if (reportFailures === 0) {2364 matchFailed("\".\"");2365 }2366 }2367 if (result1 !== null) {2368 result2 = parse_key();2369 if (result2 !== null) {2370 result1 = [result1, result2];2371 } else {2372 result1 = null;2373 pos = clone(pos3);2374 }2375 } else {2376 result1 = null;2377 pos = clone(pos3);2378 }2379 if (result1 !== null) {2380 result1 = (function(offset, line, column, k) {return k})(pos2.offset, pos2.line, pos2.column, result1[1]);2381 }2382 if (result1 === null) {2383 pos = clone(pos2);2384 }2385 }2386 } else {2387 result0 = null;2388 }2389 if (result0 !== null) {2390 result1 = parse_array();2391 result1 = result1 !== null ? result1 : "";2392 if (result1 !== null) {2393 result0 = [result0, result1];2394 } else {2395 result0 = null;2396 pos = clone(pos1);2397 }2398 } else {2399 result0 = null;2400 pos = clone(pos1);2401 }2402 if (result0 !== null) {2403 result0 = (function(offset, line, column, d, a) { if (a) { return d.concat(a); } else { return d; } })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2404 }2405 if (result0 === null) {2406 pos = clone(pos0);2407 }2408 reportFailures--;2409 if (reportFailures === 0 && result0 === null) {2410 matchFailed("array_part");2411 }2412 return result0;2413 }2414 2415 function parse_inline() {2416 var result0, result1, result2;2417 var pos0, pos1;2418 2419 reportFailures++;2420 pos0 = clone(pos);2421 pos1 = clone(pos);2422 if (input.charCodeAt(pos.offset) === 34) {2423 result0 = "\"";2424 advance(pos, 1);2425 } else {2426 result0 = null;2427 if (reportFailures === 0) {2428 matchFailed("\"\\\"\"");2429 }2430 }2431 if (result0 !== null) {2432 if (input.charCodeAt(pos.offset) === 34) {2433 result1 = "\"";2434 advance(pos, 1);2435 } else {2436 result1 = null;2437 if (reportFailures === 0) {2438 matchFailed("\"\\\"\"");2439 }2440 }2441 if (result1 !== null) {2442 result0 = [result0, result1];2443 } else {2444 result0 = null;2445 pos = clone(pos1);2446 }2447 } else {2448 result0 = null;2449 pos = clone(pos1);2450 }2451 if (result0 !== null) {2452 result0 = (function(offset, line, column) { return ["literal", ""] })(pos0.offset, pos0.line, pos0.column);2453 }2454 if (result0 === null) {2455 pos = clone(pos0);2456 }2457 if (result0 === null) {2458 pos0 = clone(pos);2459 pos1 = clone(pos);2460 if (input.charCodeAt(pos.offset) === 34) {2461 result0 = "\"";2462 advance(pos, 1);2463 } else {2464 result0 = null;2465 if (reportFailures === 0) {2466 matchFailed("\"\\\"\"");2467 }2468 }2469 if (result0 !== null) {2470 result1 = parse_literal();2471 if (result1 !== null) {2472 if (input.charCodeAt(pos.offset) === 34) {2473 result2 = "\"";2474 advance(pos, 1);2475 } else {2476 result2 = null;2477 if (reportFailures === 0) {2478 matchFailed("\"\\\"\"");2479 }2480 }2481 if (result2 !== null) {2482 result0 = [result0, result1, result2];2483 } else {2484 result0 = null;2485 pos = clone(pos1);2486 }2487 } else {2488 result0 = null;2489 pos = clone(pos1);2490 }2491 } else {2492 result0 = null;2493 pos = clone(pos1);2494 }2495 if (result0 !== null) {2496 result0 = (function(offset, line, column, l) { return ["literal", l] })(pos0.offset, pos0.line, pos0.column, result0[1]);2497 }2498 if (result0 === null) {2499 pos = clone(pos0);2500 }2501 if (result0 === null) {2502 pos0 = clone(pos);2503 pos1 = clone(pos);2504 if (input.charCodeAt(pos.offset) === 34) {2505 result0 = "\"";2506 advance(pos, 1);2507 } else {2508 result0 = null;2509 if (reportFailures === 0) {2510 matchFailed("\"\\\"\"");2511 }2512 }2513 if (result0 !== null) {2514 result2 = parse_inline_part();2515 if (result2 !== null) {2516 result1 = [];2517 while (result2 !== null) {2518 result1.push(result2);2519 result2 = parse_inline_part();2520 }2521 } else {2522 result1 = null;2523 }2524 if (result1 !== null) {2525 if (input.charCodeAt(pos.offset) === 34) {2526 result2 = "\"";2527 advance(pos, 1);2528 } else {2529 result2 = null;2530 if (reportFailures === 0) {2531 matchFailed("\"\\\"\"");2532 }2533 }2534 if (result2 !== null) {2535 result0 = [result0, result1, result2];2536 } else {2537 result0 = null;2538 pos = clone(pos1);2539 }2540 } else {2541 result0 = null;2542 pos = clone(pos1);2543 }2544 } else {2545 result0 = null;2546 pos = clone(pos1);2547 }2548 if (result0 !== null) {2549 result0 = (function(offset, line, column, p) { return ["body"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0[1]);2550 }2551 if (result0 === null) {2552 pos = clone(pos0);2553 }2554 }2555 }2556 reportFailures--;2557 if (reportFailures === 0 && result0 === null) {2558 matchFailed("inline");2559 }2560 return result0;2561 }2562 2563 function parse_inline_part() {2564 var result0;2565 var pos0;2566 2567 result0 = parse_special();2568 if (result0 === null) {2569 result0 = parse_reference();2570 if (result0 === null) {2571 pos0 = clone(pos);2572 result0 = parse_literal();2573 if (result0 !== null) {2574 result0 = (function(offset, line, column, l) { return ["buffer", l] })(pos0.offset, pos0.line, pos0.column, result0);2575 }2576 if (result0 === null) {2577 pos = clone(pos0);2578 }2579 }2580 }2581 return result0;2582 }2583 2584 function parse_buffer() {2585 var result0, result1, result2, result3, result4;2586 var pos0, pos1, pos2, pos3;2587 2588 reportFailures++;2589 pos0 = clone(pos);2590 pos1 = clone(pos);2591 result0 = parse_eol();2592 if (result0 !== null) {2593 result1 = [];2594 result2 = parse_ws();2595 while (result2 !== null) {2596 result1.push(result2);2597 result2 = parse_ws();2598 }2599 if (result1 !== null) {2600 result0 = [result0, result1];2601 } else {2602 result0 = null;2603 pos = clone(pos1);2604 }2605 } else {2606 result0 = null;2607 pos = clone(pos1);2608 }2609 if (result0 !== null) {2610 result0 = (function(offset, line, column, e, w) { return ["format", e, w.join('')] })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2611 }2612 if (result0 === null) {2613 pos = clone(pos0);2614 }2615 if (result0 === null) {2616 pos0 = clone(pos);2617 pos1 = clone(pos);2618 pos2 = clone(pos);2619 pos3 = clone(pos);2620 reportFailures++;2621 result1 = parse_tag();2622 reportFailures--;2623 if (result1 === null) {2624 result1 = "";2625 } else {2626 result1 = null;2627 pos = clone(pos3);2628 }2629 if (result1 !== null) {2630 pos3 = clone(pos);2631 reportFailures++;2632 result2 = parse_comment();2633 reportFailures--;2634 if (result2 === null) {2635 result2 = "";2636 } else {2637 result2 = null;2638 pos = clone(pos3);2639 }2640 if (result2 !== null) {2641 pos3 = clone(pos);2642 reportFailures++;2643 result3 = parse_eol();2644 reportFailures--;2645 if (result3 === null) {2646 result3 = "";2647 } else {2648 result3 = null;2649 pos = clone(pos3);2650 }2651 if (result3 !== null) {2652 if (input.length > pos.offset) {2653 result4 = input.charAt(pos.offset);2654 advance(pos, 1);2655 } else {2656 result4 = null;2657 if (reportFailures === 0) {2658 matchFailed("any character");2659 }2660 }2661 if (result4 !== null) {2662 result1 = [result1, result2, result3, result4];2663 } else {2664 result1 = null;2665 pos = clone(pos2);2666 }2667 } else {2668 result1 = null;2669 pos = clone(pos2);2670 }2671 } else {2672 result1 = null;2673 pos = clone(pos2);2674 }2675 } else {2676 result1 = null;2677 pos = clone(pos2);2678 }2679 if (result1 !== null) {2680 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[3]);2681 }2682 if (result1 === null) {2683 pos = clone(pos1);2684 }2685 if (result1 !== null) {2686 result0 = [];2687 while (result1 !== null) {2688 result0.push(result1);2689 pos1 = clone(pos);2690 pos2 = clone(pos);2691 pos3 = clone(pos);2692 reportFailures++;2693 result1 = parse_tag();2694 reportFailures--;2695 if (result1 === null) {2696 result1 = "";2697 } else {2698 result1 = null;2699 pos = clone(pos3);2700 }2701 if (result1 !== null) {2702 pos3 = clone(pos);2703 reportFailures++;2704 result2 = parse_comment();2705 reportFailures--;2706 if (result2 === null) {2707 result2 = "";2708 } else {2709 result2 = null;2710 pos = clone(pos3);2711 }2712 if (result2 !== null) {2713 pos3 = clone(pos);2714 reportFailures++;2715 result3 = parse_eol();2716 reportFailures--;2717 if (result3 === null) {2718 result3 = "";2719 } else {2720 result3 = null;2721 pos = clone(pos3);2722 }2723 if (result3 !== null) {2724 if (input.length > pos.offset) {2725 result4 = input.charAt(pos.offset);2726 advance(pos, 1);2727 } else {2728 result4 = null;2729 if (reportFailures === 0) {2730 matchFailed("any character");2731 }2732 }2733 if (result4 !== null) {2734 result1 = [result1, result2, result3, result4];2735 } else {2736 result1 = null;2737 pos = clone(pos2);2738 }2739 } else {2740 result1 = null;2741 pos = clone(pos2);2742 }2743 } else {2744 result1 = null;2745 pos = clone(pos2);2746 }2747 } else {2748 result1 = null;2749 pos = clone(pos2);2750 }2751 if (result1 !== null) {2752 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[3]);2753 }2754 if (result1 === null) {2755 pos = clone(pos1);2756 }2757 }2758 } else {2759 result0 = null;2760 }2761 if (result0 !== null) {2762 result0 = (function(offset, line, column, b) { return ["buffer", b.join('')] })(pos0.offset, pos0.line, pos0.column, result0);2763 }2764 if (result0 === null) {2765 pos = clone(pos0);2766 }2767 }2768 reportFailures--;2769 if (reportFailures === 0 && result0 === null) {2770 matchFailed("buffer");2771 }2772 return result0;2773 }2774 2775 function parse_literal() {2776 var result0, result1, result2;2777 var pos0, pos1, pos2, pos3;2778 2779 reportFailures++;2780 pos0 = clone(pos);2781 pos1 = clone(pos);2782 pos2 = clone(pos);2783 pos3 = clone(pos);2784 reportFailures++;2785 result1 = parse_tag();2786 reportFailures--;2787 if (result1 === null) {2788 result1 = "";2789 } else {2790 result1 = null;2791 pos = clone(pos3);2792 }2793 if (result1 !== null) {2794 result2 = parse_esc();2795 if (result2 === null) {2796 if (/^[^"]/.test(input.charAt(pos.offset))) {2797 result2 = input.charAt(pos.offset);2798 advance(pos, 1);2799 } else {2800 result2 = null;2801 if (reportFailures === 0) {2802 matchFailed("[^\"]");2803 }2804 }2805 }2806 if (result2 !== null) {2807 result1 = [result1, result2];2808 } else {2809 result1 = null;2810 pos = clone(pos2);2811 }2812 } else {2813 result1 = null;2814 pos = clone(pos2);2815 }2816 if (result1 !== null) {2817 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[1]);2818 }2819 if (result1 === null) {2820 pos = clone(pos1);2821 }2822 if (result1 !== null) {2823 result0 = [];2824 while (result1 !== null) {2825 result0.push(result1);2826 pos1 = clone(pos);2827 pos2 = clone(pos);2828 pos3 = clone(pos);2829 reportFailures++;2830 result1 = parse_tag();2831 reportFailures--;2832 if (result1 === null) {2833 result1 = "";2834 } else {2835 result1 = null;2836 pos = clone(pos3);2837 }2838 if (result1 !== null) {2839 result2 = parse_esc();2840 if (result2 === null) {2841 if (/^[^"]/.test(input.charAt(pos.offset))) {2842 result2 = input.charAt(pos.offset);2843 advance(pos, 1);2844 } else {2845 result2 = null;2846 if (reportFailures === 0) {2847 matchFailed("[^\"]");2848 }2849 }2850 }2851 if (result2 !== null) {2852 result1 = [result1, result2];2853 } else {2854 result1 = null;2855 pos = clone(pos2);2856 }2857 } else {2858 result1 = null;2859 pos = clone(pos2);2860 }2861 if (result1 !== null) {2862 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[1]);2863 }2864 if (result1 === null) {2865 pos = clone(pos1);2866 }2867 }2868 } else {2869 result0 = null;2870 }2871 if (result0 !== null) {2872 result0 = (function(offset, line, column, b) { return b.join('') })(pos0.offset, pos0.line, pos0.column, result0);2873 }2874 if (result0 === null) {2875 pos = clone(pos0);2876 }2877 reportFailures--;2878 if (reportFailures === 0 && result0 === null) {2879 matchFailed("literal");2880 }2881 return result0;2882 }2883 2884 function parse_esc() {2885 var result0;2886 var pos0;2887 2888 pos0 = clone(pos);2889 if (input.substr(pos.offset, 2) === "\\\"") {2890 result0 = "\\\"";2891 advance(pos, 2);2892 } else {2893 result0 = null;2894 if (reportFailures === 0) {2895 matchFailed("\"\\\\\\\"\"");2896 }2897 }2898 if (result0 !== null) {2899 result0 = (function(offset, line, column) { return '"' })(pos0.offset, pos0.line, pos0.column);2900 }2901 if (result0 === null) {2902 pos = clone(pos0);2903 }2904 return result0;2905 }2906 2907 function parse_comment() {2908 var result0, result1, result2, result3;2909 var pos0, pos1, pos2, pos3, pos4;2910 2911 reportFailures++;2912 pos0 = clone(pos);2913 pos1 = clone(pos);2914 if (input.substr(pos.offset, 2) === "{!") {2915 result0 = "{!";2916 advance(pos, 2);2917 } else {2918 result0 = null;2919 if (reportFailures === 0) {2920 matchFailed("\"{!\"");2921 }2922 }2923 if (result0 !== null) {2924 result1 = [];2925 pos2 = clone(pos);2926 pos3 = clone(pos);2927 pos4 = clone(pos);2928 reportFailures++;2929 if (input.substr(pos.offset, 2) === "!}") {2930 result2 = "!}";2931 advance(pos, 2);2932 } else {2933 result2 = null;2934 if (reportFailures === 0) {2935 matchFailed("\"!}\"");2936 }2937 }2938 reportFailures--;2939 if (result2 === null) {2940 result2 = "";2941 } else {2942 result2 = null;2943 pos = clone(pos4);2944 }2945 if (result2 !== null) {2946 if (input.length > pos.offset) {2947 result3 = input.charAt(pos.offset);2948 advance(pos, 1);2949 } else {2950 result3 = null;2951 if (reportFailures === 0) {2952 matchFailed("any character");2953 }2954 }2955 if (result3 !== null) {2956 result2 = [result2, result3];2957 } else {2958 result2 = null;2959 pos = clone(pos3);2960 }2961 } else {2962 result2 = null;2963 pos = clone(pos3);2964 }2965 if (result2 !== null) {2966 result2 = (function(offset, line, column, c) {return c})(pos2.offset, pos2.line, pos2.column, result2[1]);2967 }2968 if (result2 === null) {2969 pos = clone(pos2);2970 }2971 while (result2 !== null) {2972 result1.push(result2);2973 pos2 = clone(pos);2974 pos3 = clone(pos);2975 pos4 = clone(pos);2976 reportFailures++;2977 if (input.substr(pos.offset, 2) === "!}") {2978 result2 = "!}";2979 advance(pos, 2);2980 } else {2981 result2 = null;2982 if (reportFailures === 0) {2983 matchFailed("\"!}\"");2984 }2985 }2986 reportFailures--;2987 if (result2 === null) {2988 result2 = "";2989 } else {2990 result2 = null;2991 pos = clone(pos4);2992 }2993 if (result2 !== null) {2994 if (input.length > pos.offset) {2995 result3 = input.charAt(pos.offset);2996 advance(pos, 1);2997 } else {2998 result3 = null;2999 if (reportFailures === 0) {3000 matchFailed("any character");3001 }3002 }3003 if (result3 !== null) {3004 result2 = [result2, result3];3005 } else {3006 result2 = null;3007 pos = clone(pos3);3008 }3009 } else {3010 result2 = null;3011 pos = clone(pos3);3012 }3013 if (result2 !== null) {3014 result2 = (function(offset, line, column, c) {return c})(pos2.offset, pos2.line, pos2.column, result2[1]);3015 }3016 if (result2 === null) {3017 pos = clone(pos2);3018 }3019 }3020 if (result1 !== null) {3021 if (input.substr(pos.offset, 2) === "!}") {3022 result2 = "!}";3023 advance(pos, 2);3024 } else {3025 result2 = null;3026 if (reportFailures === 0) {3027 matchFailed("\"!}\"");3028 }3029 }3030 if (result2 !== null) {3031 result0 = [result0, result1, result2];3032 } else {3033 result0 = null;3034 pos = clone(pos1);3035 }3036 } else {3037 result0 = null;3038 pos = clone(pos1);3039 }3040 } else {3041 result0 = null;3042 pos = clone(pos1);3043 }3044 if (result0 !== null) {3045 result0 = (function(offset, line, column, c) { return ["comment", c.join('')] })(pos0.offset, pos0.line, pos0.column, result0[1]);3046 }3047 if (result0 === null) {3048 pos = clone(pos0);3049 }3050 reportFailures--;3051 if (reportFailures === 0 && result0 === null) {3052 matchFailed("comment");3053 }3054 return result0;3055 }3056 3057 function parse_tag() {3058 var result0, result1, result2, result3, result4, result5, result6, result7;3059 var pos0, pos1, pos2;3060 3061 pos0 = clone(pos);3062 result0 = parse_ld();3063 if (result0 !== null) {3064 result1 = [];3065 result2 = parse_ws();3066 while (result2 !== null) {3067 result1.push(result2);3068 result2 = parse_ws();3069 }3070 if (result1 !== null) {3071 if (/^[#?^><+%:@\/~%]/.test(input.charAt(pos.offset))) {3072 result2 = input.charAt(pos.offset);3073 advance(pos, 1);3074 } else {3075 result2 = null;3076 if (reportFailures === 0) {3077 matchFailed("[#?^><+%:@\\/~%]");3078 }3079 }3080 if (result2 !== null) {3081 result3 = [];3082 result4 = parse_ws();3083 while (result4 !== null) {3084 result3.push(result4);3085 result4 = parse_ws();3086 }3087 if (result3 !== null) {3088 pos1 = clone(pos);3089 pos2 = clone(pos);3090 reportFailures++;3091 result5 = parse_rd();3092 reportFailures--;3093 if (result5 === null) {3094 result5 = "";3095 } else {3096 result5 = null;3097 pos = clone(pos2);3098 }3099 if (result5 !== null) {3100 pos2 = clone(pos);3101 reportFailures++;3102 result6 = parse_eol();3103 reportFailures--;3104 if (result6 === null) {3105 result6 = "";3106 } else {3107 result6 = null;3108 pos = clone(pos2);3109 }3110 if (result6 !== null) {3111 if (input.length > pos.offset) {3112 result7 = input.charAt(pos.offset);3113 advance(pos, 1);3114 } else {3115 result7 = null;3116 if (reportFailures === 0) {3117 matchFailed("any character");3118 }3119 }3120 if (result7 !== null) {3121 result5 = [result5, result6, result7];3122 } else {3123 result5 = null;3124 pos = clone(pos1);3125 }3126 } else {3127 result5 = null;3128 pos = clone(pos1);3129 }3130 } else {3131 result5 = null;3132 pos = clone(pos1);3133 }3134 if (result5 !== null) {3135 result4 = [];3136 while (result5 !== null) {3137 result4.push(result5);3138 pos1 = clone(pos);3139 pos2 = clone(pos);3140 reportFailures++;3141 result5 = parse_rd();3142 reportFailures--;3143 if (result5 === null) {3144 result5 = "";3145 } else {3146 result5 = null;3147 pos = clone(pos2);3148 }3149 if (result5 !== null) {3150 pos2 = clone(pos);3151 reportFailures++;3152 result6 = parse_eol();3153 reportFailures--;3154 if (result6 === null) {3155 result6 = "";3156 } else {3157 result6 = null;3158 pos = clone(pos2);3159 }3160 if (result6 !== null) {3161 if (input.length > pos.offset) {3162 result7 = input.charAt(pos.offset);3163 advance(pos, 1);3164 } else {3165 result7 = null;3166 if (reportFailures === 0) {3167 matchFailed("any character");3168 }3169 }3170 if (result7 !== null) {3171 result5 = [result5, result6, result7];3172 } else {3173 result5 = null;3174 pos = clone(pos1);3175 }3176 } else {3177 result5 = null;3178 pos = clone(pos1);3179 }3180 } else {3181 result5 = null;3182 pos = clone(pos1);3183 }3184 }3185 } else {3186 result4 = null;3187 }3188 if (result4 !== null) {3189 result5 = [];3190 result6 = parse_ws();3191 while (result6 !== null) {3192 result5.push(result6);3193 result6 = parse_ws();3194 }3195 if (result5 !== null) {3196 result6 = parse_rd();3197 if (result6 !== null) {3198 result0 = [result0, result1, result2, result3, result4, result5, result6];3199 } else {3200 result0 = null;3201 pos = clone(pos0);3202 }3203 } else {3204 result0 = null;3205 pos = clone(pos0);3206 }3207 } else {3208 result0 = null;3209 pos = clone(pos0);3210 }3211 } else {3212 result0 = null;3213 pos = clone(pos0);3214 }3215 } else {3216 result0 = null;3217 pos = clone(pos0);3218 }3219 } else {3220 result0 = null;3221 pos = clone(pos0);3222 }3223 } else {3224 result0 = null;3225 pos = clone(pos0);3226 }3227 if (result0 === null) {3228 result0 = parse_reference();3229 }3230 return result0;3231 }3232 3233 function parse_ld() {3234 var result0;3235 3236 if (input.charCodeAt(pos.offset) === 123) {3237 result0 = "{";3238 advance(pos, 1);3239 } else {3240 result0 = null;3241 if (reportFailures === 0) {3242 matchFailed("\"{\"");3243 }3244 }3245 return result0;3246 }3247 3248 function parse_rd() {3249 var result0;3250 3251 if (input.charCodeAt(pos.offset) === 125) {3252 result0 = "}";3253 advance(pos, 1);3254 } else {3255 result0 = null;3256 if (reportFailures === 0) {3257 matchFailed("\"}\"");3258 }3259 }3260 return result0;3261 }3262 3263 function parse_lb() {3264 var result0;3265 3266 if (input.charCodeAt(pos.offset) === 91) {3267 result0 = "[";3268 advance(pos, 1);3269 } else {3270 result0 = null;3271 if (reportFailures === 0) {3272 matchFailed("\"[\"");3273 }3274 }3275 return result0;3276 }3277 3278 function parse_rb() {3279 var result0;3280 3281 if (input.charCodeAt(pos.offset) === 93) {3282 result0 = "]";3283 advance(pos, 1);3284 } else {3285 result0 = null;3286 if (reportFailures === 0) {3287 matchFailed("\"]\"");3288 }3289 }3290 return result0;3291 }3292 3293 function parse_eol() {3294 var result0;3295 3296 if (input.charCodeAt(pos.offset) === 10) {3297 result0 = "\n";3298 advance(pos, 1);3299 } else {3300 result0 = null;3301 if (reportFailures === 0) {3302 matchFailed("\"\\n\"");3303 }3304 }3305 if (result0 === null) {3306 if (input.substr(pos.offset, 2) === "\r\n") {3307 result0 = "\r\n";3308 advance(pos, 2);3309 } else {3310 result0 = null;3311 if (reportFailures === 0) {3312 matchFailed("\"\\r\\n\"");3313 }3314 }3315 if (result0 === null) {3316 if (input.charCodeAt(pos.offset) === 13) {3317 result0 = "\r";3318 advance(pos, 1);3319 } else {3320 result0 = null;3321 if (reportFailures === 0) {3322 matchFailed("\"\\r\"");3323 }3324 }3325 if (result0 === null) {3326 if (input.charCodeAt(pos.offset) === 8232) {3327 result0 = "\u2028";3328 advance(pos, 1);3329 } else {3330 result0 = null;3331 if (reportFailures === 0) {3332 matchFailed("\"\\u2028\"");3333 }3334 }3335 if (result0 === null) {3336 if (input.charCodeAt(pos.offset) === 8233) {3337 result0 = "\u2029";3338 advance(pos, 1);3339 } else {3340 result0 = null;3341 if (reportFailures === 0) {3342 matchFailed("\"\\u2029\"");3343 }3344 }3345 }3346 }3347 }3348 }3349 return result0;3350 }3351 3352 function parse_ws() {3353 var result0;3354 3355 if (/^[\t\x0B\f \xA0\uFEFF]/.test(input.charAt(pos.offset))) {3356 result0 = input.charAt(pos.offset);3357 advance(pos, 1);3358 } else {3359 result0 = null;3360 if (reportFailures === 0) {3361 matchFailed("[\\t\\x0B\\f \\xA0\\uFEFF]");3362 }3363 }3364 if (result0 === null) {3365 result0 = parse_eol();3366 }3367 return result0;3368 }3369 3370 3371 function cleanupExpected(expected) {3372 expected.sort();3373 3374 var lastExpected = null;3375 var cleanExpected = [];3376 for (var i = 0; i < expected.length; i++) {3377 if (expected[i] !== lastExpected) {3378 cleanExpected.push(expected[i]);3379 lastExpected = expected[i];3380 }3381 }3382 return cleanExpected;3383 }3384 3385 3386 3387 var result = parseFunctions[startRule]();3388 3389 /*3390 * The parser is now in one of the following three states:3391 *3392 * 1. The parser successfully parsed the whole input.3393 *3394 * - |result !== null|3395 * - |pos.offset === input.length|3396 * - |rightmostFailuresExpected| may or may not contain something3397 *3398 * 2. The parser successfully parsed only a part of the input.3399 *3400 * - |result !== null|3401 * - |pos.offset < input.length|3402 * - |rightmostFailuresExpected| may or may not contain something3403 *3404 * 3. The parser did not successfully parse any part of the input.3405 *3406 * - |result === null|3407 * - |pos.offset === 0|3408 * - |rightmostFailuresExpected| contains at least one failure3409 *3410 * All code following this comment (including called functions) must3411 * handle these states.3412 */3413 if (result === null || pos.offset !== input.length) {3414 var offset = Math.max(pos.offset, rightmostFailuresPos.offset);3415 var found = offset < input.length ? input.charAt(offset) : null;3416 var errorPosition = pos.offset > rightmostFailuresPos.offset ? pos : rightmostFailuresPos;3417 3418 throw new parser.SyntaxError(3419 cleanupExpected(rightmostFailuresExpected),3420 found,3421 offset,3422 errorPosition.line,3423 errorPosition.column3424 );3425 }3426 3427 return result;3428 },3429 3430 /* Returns the parser source code. */3431 toSource: function() { return this._source; }3432 };3433 3434 /* Thrown when a parser encounters a syntax error. */3435 3436 result.SyntaxError = function(expected, found, offset, line, column) {3437 function buildMessage(expected, found) {3438 var expectedHumanized, foundHumanized;3439 3440 switch (expected.length) {3441 case 0:3442 expectedHumanized = "end of input";3443 break;3444 case 1:3445 expectedHumanized = expected[0];3446 break;3447 default:3448 expectedHumanized = expected.slice(0, expected.length - 1).join(", ")3449 + " or "3450 + expected[expected.length - 1];3451 }3452 3453 foundHumanized = found ? quote(found) : "end of input";3454 3455 return "Expected " + expectedHumanized + " but " + foundHumanized + " found.";3456 }3457 3458 this.name = "SyntaxError";3459 this.expected = expected;3460 this.found = found;3461 this.message = buildMessage(expected, found);3462 this.offset = offset;3463 this.line = line;3464 this.column = column;3465 };3466 3467 result.SyntaxError.prototype = Error.prototype;3468 3469 return result;3470})();3471dust.parse = parser.parse;...
dust-full-1.2.2.js
Source:dust-full-1.2.2.js
1//2// Dust - Asynchronous Templating v1.2.23// http://akdubya.github.com/dustjs4//5// Copyright (c) 2010, Aleksander Williams6// Released under the MIT License.7//8var dust = {};9function getGlobal(){10 return (function(){11 return this.dust;12 }).call(null);13}14(function(dust) {15dust.helpers = {};16dust.cache = {};17dust.register = function(name, tmpl) {18 if (!name) return;19 dust.cache[name] = tmpl;20};21dust.render = function(name, context, callback) {22 var chunk = new Stub(callback).head;23 dust.load(name, chunk, Context.wrap(context)).end();24};25dust.stream = function(name, context) {26 var stream = new Stream();27 dust.nextTick(function() {28 dust.load(name, stream.head, Context.wrap(context)).end();29 });30 return stream;31};32dust.renderSource = function(source, context, callback) {33 return dust.compileFn(source)(context, callback);34};35dust.compileFn = function(source, name) {36 var tmpl = dust.loadSource(dust.compile(source, name));37 return function(context, callback) {38 var master = callback ? new Stub(callback) : new Stream();39 dust.nextTick(function() {40 tmpl(master.head, Context.wrap(context)).end();41 });42 return master;43 };44};45dust.load = function(name, chunk, context) {46 var tmpl = dust.cache[name];47 if (tmpl) {48 return tmpl(chunk, context);49 } else {50 if (dust.onLoad) {51 return chunk.map(function(chunk) {52 dust.onLoad(name, function(err, src) {53 if (err) return chunk.setError(err);54 if (!dust.cache[name]) dust.loadSource(dust.compile(src, name));55 dust.cache[name](chunk, context).end();56 });57 });58 }59 return chunk.setError(new Error("Template Not Found: " + name));60 }61};62dust.loadSource = function(source, path) {63 return eval(source);64};65if (Array.isArray) {66 dust.isArray = Array.isArray;67} else {68 dust.isArray = function(arr) {69 return Object.prototype.toString.call(arr) == "[object Array]";70 };71}72dust.nextTick = (function() {73 if (typeof process !== "undefined") {74 return process.nextTick;75 } else {76 return function(callback) {77 setTimeout(callback,0);78 };79 }80} )();81dust.isEmpty = function(value) {82 if (dust.isArray(value) && !value.length) return true;83 if (value === 0) return false;84 return (!value);85};86// apply the filter chain and return the output string87dust.filter = function(string, auto, filters) {88 if (filters) {89 for (var i=0, len=filters.length; i<len; i++) {90 var name = filters[i];91 if (name === "s") {92 auto = null;93 }94 // fail silently for invalid filters95 else if (typeof dust.filters[name] === 'function') {96 string = dust.filters[name](string);97 }98 }99 }100 // by default always apply the h filter, unless asked to unescape with |s101 if (auto) {102 string = dust.filters[auto](string);103 }104 return string;105};106dust.filters = {107 h: function(value) { return dust.escapeHtml(value); },108 j: function(value) { return dust.escapeJs(value); },109 u: encodeURI,110 uc: encodeURIComponent,111 js: function(value) { if (!JSON) { return value; } return JSON.stringify(value); },112 jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); }113};114function Context(stack, global, blocks) {115 this.stack = stack;116 this.global = global;117 this.blocks = blocks;118}119dust.makeBase = function(global) {120 return new Context(new Stack(), global);121};122Context.wrap = function(context) {123 if (context instanceof Context) {124 return context;125 }126 return new Context(new Stack(context));127};128Context.prototype.get = function(key) {129 var ctx = this.stack, value;130 while(ctx) {131 if (ctx.isObject) {132 value = ctx.head[key];133 if (!(value === undefined)) {134 return value;135 }136 }137 ctx = ctx.tail;138 }139 return this.global ? this.global[key] : undefined;140};141Context.prototype.getPath = function(cur, down) {142 var ctx = this.stack,143 len = down.length;144 if (cur && len === 0) return ctx.head;145 ctx = ctx.head;146 var i = 0;147 while(ctx && i < len) {148 ctx = ctx[down[i]];149 i++;150 }151 return ctx;152};153Context.prototype.push = function(head, idx, len) {154 return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);155};156Context.prototype.rebase = function(head) {157 return new Context(new Stack(head), this.global, this.blocks);158};159Context.prototype.current = function() {160 return this.stack.head;161};162Context.prototype.getBlock = function(key, chk, ctx) {163 if (typeof key === "function") {164 key = key(chk, ctx).data;165 chk.data = "";166 }167 var blocks = this.blocks;168 if (!blocks) return;169 var len = blocks.length, fn;170 while (len--) {171 fn = blocks[len][key];172 if (fn) return fn;173 }174};175Context.prototype.shiftBlocks = function(locals) {176 var blocks = this.blocks,177 newBlocks;178 if (locals) {179 if (!blocks) {180 newBlocks = [locals];181 } else {182 newBlocks = blocks.concat([locals]);183 }184 return new Context(this.stack, this.global, newBlocks);185 }186 return this;187};188function Stack(head, tail, idx, len) {189 this.tail = tail;190 this.isObject = !dust.isArray(head) && head && typeof head === "object";191 this.head = head;192 this.index = idx;193 this.of = len;194}195function Stub(callback) {196 this.head = new Chunk(this);197 this.callback = callback;198 this.out = '';199}200Stub.prototype.flush = function() {201 var chunk = this.head;202 while (chunk) {203 if (chunk.flushable) {204 this.out += chunk.data;205 } else if (chunk.error) {206 this.callback(chunk.error);207 this.flush = function() {};208 return;209 } else {210 return;211 }212 chunk = chunk.next;213 this.head = chunk;214 }215 this.callback(null, this.out);216};217function Stream() {218 this.head = new Chunk(this);219}220Stream.prototype.flush = function() {221 var chunk = this.head;222 while(chunk) {223 if (chunk.flushable) {224 this.emit('data', chunk.data);225 } else if (chunk.error) {226 this.emit('error', chunk.error);227 this.flush = function() {};228 return;229 } else {230 return;231 }232 chunk = chunk.next;233 this.head = chunk;234 }235 this.emit('end');236};237Stream.prototype.emit = function(type, data) {238 if (!this.events) return false;239 var handler = this.events[type];240 if (!handler) return false;241 if (typeof handler == 'function') {242 handler(data);243 } else {244 var listeners = handler.slice(0);245 for (var i = 0, l = listeners.length; i < l; i++) {246 listeners[i](data);247 }248 }249};250Stream.prototype.on = function(type, callback) {251 if (!this.events) {252 this.events = {};253 }254 if (!this.events[type]) {255 this.events[type] = callback;256 } else if(typeof this.events[type] === 'function') {257 this.events[type] = [this.events[type], callback];258 } else {259 this.events[type].push(callback);260 }261 return this;262};263Stream.prototype.pipe = function(stream) {264 this.on("data", function(data) {265 stream.write(data, "utf8");266 }).on("end", function() {267 stream.end();268 }).on("error", function(err) {269 stream.error(err);270 });271 return this;272};273function Chunk(root, next, taps) {274 this.root = root;275 this.next = next;276 this.data = '';277 this.flushable = false;278 this.taps = taps;279}280Chunk.prototype.write = function(data) {281 var taps = this.taps;282 if (taps) {283 data = taps.go(data);284 }285 this.data += data;286 return this;287};288Chunk.prototype.end = function(data) {289 if (data) {290 this.write(data);291 }292 this.flushable = true;293 this.root.flush();294 return this;295};296Chunk.prototype.map = function(callback) {297 var cursor = new Chunk(this.root, this.next, this.taps),298 branch = new Chunk(this.root, cursor, this.taps);299 this.next = branch;300 this.flushable = true;301 callback(branch);302 return cursor;303};304Chunk.prototype.tap = function(tap) {305 var taps = this.taps;306 if (taps) {307 this.taps = taps.push(tap);308 } else {309 this.taps = new Tap(tap);310 }311 return this;312};313Chunk.prototype.untap = function() {314 this.taps = this.taps.tail;315 return this;316};317Chunk.prototype.render = function(body, context) {318 return body(this, context);319};320Chunk.prototype.reference = function(elem, context, auto, filters) {321 if (typeof elem === "function") {322 elem.isFunction = true;323 // Changed the function calling to use apply with the current context to make sure 324 // that "this" is wat we expect it to be inside the function325 elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]);326 if (elem instanceof Chunk) {327 return elem;328 }329 }330 if (!dust.isEmpty(elem)) {331 return this.write(dust.filter(elem, auto, filters));332 } else {333 return this;334 }335};336Chunk.prototype.section = function(elem, context, bodies, params) {337 // anonymous functions338 if (typeof elem === "function") {339 elem = elem.apply(context.current(), [this, context, bodies, params]);340 // functions that return chunks are assumed to have handled the body and/or have modified the chunk341 // use that return value as the current chunk and go to the next method in the chain342 if (elem instanceof Chunk) {343 return elem;344 }345 }346 var body = bodies.block,347 skip = bodies['else'];348 // a.k.a Inline parameters in the Dust documentations349 if (params) {350 context = context.push(params);351 }352 /*353 Dust's default behavior is to enumerate over the array elem, passing each object in the array to the block.354 When elem resolves to a value or object instead of an array, Dust sets the current context to the value 355 and renders the block one time.356 */357 //non empty array is truthy, empty array is falsy358 if (dust.isArray(elem)) {359 if (body) {360 var len = elem.length, chunk = this;361 if (len > 0) {362 // any custom helper can blow up the stack 363 // and store a flattened context, guard defensively364 if(context.stack.head) {365 context.stack.head['$len'] = len;366 }367 for (var i=0; i<len; i++) {368 if(context.stack.head) {369 context.stack.head['$idx'] = i;370 }371 chunk = body(chunk, context.push(elem[i], i, len));372 }373 if(context.stack.head) {374 context.stack.head['$idx'] = undefined;375 context.stack.head['$len'] = undefined;376 }377 return chunk;378 } 379 else if (skip) {380 return skip(this, context);381 }382 }383 }384 // true is truthy but does not change context385 else if (elem === true) {386 if (body) { 387 return body(this, context);388 }389 }390 // everything that evaluates to true are truthy ( e.g. Non-empty strings and Empty objects are truthy. )391 // zero is truthy392 // for anonymous functions that did not returns a chunk, truthiness is evaluated based on the return value393 //394 else if (elem || elem === 0) {395 if (body) return body(this, context.push(elem));396 // nonexistent, scalar false value, scalar empty string, null,397 // undefined are all falsy398 } else if (skip) {399 return skip(this, context);400 } 401 return this;402};403Chunk.prototype.exists = function(elem, context, bodies) {404 var body = bodies.block,405 skip = bodies['else'];406 if (!dust.isEmpty(elem)) {407 if (body) return body(this, context);408 } else if (skip) {409 return skip(this, context);410 }411 return this;412};413Chunk.prototype.notexists = function(elem, context, bodies) {414 var body = bodies.block,415 skip = bodies['else'];416 if (dust.isEmpty(elem)) {417 if (body) return body(this, context);418 } else if (skip) {419 return skip(this, context);420 }421 return this;422};423Chunk.prototype.block = function(elem, context, bodies) {424 var body = bodies.block;425 if (elem) {426 body = elem;427 }428 if (body) {429 return body(this, context);430 }431 return this;432};433Chunk.prototype.partial = function(elem, context, params) {434 var partialContext;435 if (params){436 //put the params context second to match what section does. {.} matches the current context without parameters437 // start with an empty context438 partialContext = dust.makeBase(context.global);439 partialContext.blocks = context.blocks;440 if (context.stack && context.stack.tail){441 // grab the stack(tail) off of the previous context if we have it442 partialContext.stack = context.stack.tail;443 }444 //put params on445 partialContext = partialContext.push(params);446 //reattach the head447 partialContext = partialContext.push(context.stack.head);448 } else {449 partialContext = context;450 }451 if (typeof elem === "function") {452 return this.capture(elem, partialContext, function(name, chunk) {453 dust.load(name, chunk, partialContext).end();454 });455 }456 return dust.load(elem, this, partialContext);457};458Chunk.prototype.helper = function(name, context, bodies, params) {459 // handle invalid helpers, similar to invalid filters460 if( dust.helpers[name]){461 return dust.helpers[name](this, context, bodies, params);462 } else {463 return this;464 }465};466Chunk.prototype.capture = function(body, context, callback) {467 return this.map(function(chunk) {468 var stub = new Stub(function(err, out) {469 if (err) {470 chunk.setError(err);471 } else {472 callback(out, chunk);473 }474 });475 body(stub.head, context).end();476 });477};478Chunk.prototype.setError = function(err) {479 this.error = err;480 this.root.flush();481 return this;482};483function Tap(head, tail) {484 this.head = head;485 this.tail = tail;486}487Tap.prototype.push = function(tap) {488 return new Tap(tap, this);489};490Tap.prototype.go = function(value) {491 var tap = this;492 while(tap) {493 value = tap.head(value);494 tap = tap.tail;495 }496 return value;497};498var HCHARS = new RegExp(/[&<>\"\']/),499 AMP = /&/g,500 LT = /</g,501 GT = />/g,502 QUOT = /\"/g,503 SQUOT = /\'/g;504dust.escapeHtml = function(s) {505 if (typeof s === "string") {506 if (!HCHARS.test(s)) {507 return s;508 }509 return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, ''');510 }511 return s;512};513var BS = /\\/g,514 CR = /\r/g,515 LS = /\u2028/g,516 PS = /\u2029/g,517 NL = /\n/g,518 LF = /\f/g,519 SQ = /'/g,520 DQ = /"/g,521 TB = /\t/g;522dust.escapeJs = function(s) {523 if (typeof s === "string") {524 return s525 .replace(BS, '\\\\')526 .replace(DQ, '\\"')527 .replace(SQ, "\\'")528 .replace(CR, '\\r')529 .replace(LS, '\\u2028')530 .replace(PS, '\\u2029')531 .replace(NL, '\\n')532 .replace(LF, '\\f')533 .replace(TB, "\\t");534 }535 return s;536};537})(dust);538if (typeof exports !== "undefined") {539 if (typeof process !== "undefined") {540 require('./server')(dust);541 }542 module.exports = dust;543}544var dustCompiler = (function(dust) {545dust.compile = function(source, name, strip) {546 try {547 if (strip) {548 source = source.replace(/^\s+/mg, '').replace(/\n/mg, '');549 }550 var ast = filterAST(dust.parse(source));551 return compile(ast, name);552 }553 catch(err)554 {555 if(!err.line || !err.column) throw err; 556 throw new SyntaxError(err.message + " At line : " + err.line + ", column : " + err.column);557 }558};559function filterAST(ast) {560 var context = {};561 return dust.filterNode(context, ast);562};563dust.filterNode = function(context, node) {564 return dust.optimizers[node[0]](context, node);565};566dust.optimizers = {567 body: compactBuffers,568 buffer: noop,569 special: convertSpecial,570 format: nullify, // TODO: convert format571 reference: visit,572 "#": visit,573 "?": visit,574 "^": visit,575 "<": visit,576 "+": visit,577 "@": visit,578 "%": visit,579 partial: visit,580 context: visit,581 params: visit,582 bodies: visit,583 param: visit,584 filters: noop,585 key: noop,586 path: noop,587 literal: noop,588 comment: nullify589};590dust.pragmas = {591 esc: function(compiler, context, bodies, params) {592 var old = compiler.auto;593 if (!context) context = 'h';594 compiler.auto = (context === 's') ? '' : context;595 var out = compileParts(compiler, bodies.block);596 compiler.auto = old;597 return out;598 }599};600function visit(context, node) {601 var out = [node[0]];602 for (var i=1, len=node.length; i<len; i++) {603 var res = dust.filterNode(context, node[i]);604 if (res) out.push(res);605 }606 return out;607};608// Compacts consecutive buffer nodes into a single node609function compactBuffers(context, node) {610 var out = [node[0]], memo;611 for (var i=1, len=node.length; i<len; i++) {612 var res = dust.filterNode(context, node[i]);613 if (res) {614 if (res[0] === 'buffer') {615 if (memo) {616 memo[1] += res[1];617 } else {618 memo = res;619 out.push(res);620 }621 } else {622 memo = null;623 out.push(res);624 }625 }626 }627 return out;628};629var specialChars = {630 "s": " ",631 "n": "\n",632 "r": "\r",633 "lb": "{",634 "rb": "}"635};636function convertSpecial(context, node) { return ['buffer', specialChars[node[1]]] };637function noop(context, node) { return node };638function nullify(){};639function compile(ast, name) {640 var context = {641 name: name,642 bodies: [],643 blocks: {},644 index: 0,645 auto: "h"646 }647 return "(function(){dust.register("648 + (name ? "\"" + name + "\"" : "null") + ","649 + dust.compileNode(context, ast)650 + ");"651 + compileBlocks(context)652 + compileBodies(context)653 + "return body_0;"654 + "})();";655};656function compileBlocks(context) {657 var out = [],658 blocks = context.blocks;659 for (var name in blocks) {660 out.push("'" + name + "':" + blocks[name]);661 }662 if (out.length) {663 context.blocks = "ctx=ctx.shiftBlocks(blocks);";664 return "var blocks={" + out.join(',') + "};";665 }666 return context.blocks = "";667};668function compileBodies(context) {669 var out = [],670 bodies = context.bodies,671 blx = context.blocks;672 for (var i=0, len=bodies.length; i<len; i++) {673 out[i] = "function body_" + i + "(chk,ctx){"674 + blx + "return chk" + bodies[i] + ";}";675 }676 return out.join('');677};678function compileParts(context, body) {679 var parts = '';680 for (var i=1, len=body.length; i<len; i++) {681 parts += dust.compileNode(context, body[i]);682 }683 return parts;684};685dust.compileNode = function(context, node) {686 return dust.nodes[node[0]](context, node);687};688dust.nodes = {689 body: function(context, node) {690 var id = context.index++, name = "body_" + id;691 context.bodies[id] = compileParts(context, node);692 return name;693 },694 buffer: function(context, node) {695 return ".write(" + escape(node[1]) + ")";696 },697 format: function(context, node) {698 return ".write(" + escape(node[1] + node[2]) + ")";699 },700 reference: function(context, node) {701 return ".reference(" + dust.compileNode(context, node[1])702 + ",ctx," + dust.compileNode(context, node[2]) + ")";703 },704 "#": function(context, node) {705 return compileSection(context, node, "section");706 },707 "?": function(context, node) {708 return compileSection(context, node, "exists");709 },710 "^": function(context, node) {711 return compileSection(context, node, "notexists");712 },713 "<": function(context, node) {714 var bodies = node[4];715 for (var i=1, len=bodies.length; i<len; i++) {716 var param = bodies[i],717 type = param[1][1];718 if (type === "block") {719 context.blocks[node[1].text] = dust.compileNode(context, param[2]);720 return '';721 }722 }723 return '';724 },725 "+": function(context, node) {726 if(typeof(node[1].text) === "undefined" && typeof(node[4]) === "undefined"){727 return ".block(ctx.getBlock("728 + dust.compileNode(context, node[1])729 + ",chk, ctx)," + dust.compileNode(context, node[2]) + ", {},"730 + dust.compileNode(context, node[3])731 + ")";732 }else {733 return ".block(ctx.getBlock("734 + escape(node[1].text)735 + ")," + dust.compileNode(context, node[2]) + ","736 + dust.compileNode(context, node[4]) + ","737 + dust.compileNode(context, node[3])738 + ")";739 }740 },741 "@": function(context, node) {742 return ".helper("743 + escape(node[1].text)744 + "," + dust.compileNode(context, node[2]) + ","745 + dust.compileNode(context, node[4]) + ","746 + dust.compileNode(context, node[3])747 + ")";748 },749 "%": function(context, node) {750 // TODO: Move these hacks into pragma precompiler751 var name = node[1][1];752 if (!dust.pragmas[name]) return '';753 var rawBodies = node[4];754 var bodies = {};755 for (var i=1, len=rawBodies.length; i<len; i++) {756 var b = rawBodies[i];757 bodies[b[1][1]] = b[2];758 }759 var rawParams = node[3];760 var params = {};761 for (var i=1, len=rawParams.length; i<len; i++) {762 var p = rawParams[i];763 params[p[1][1]] = p[2][1];764 }765 var ctx = node[2][1] ? node[2][1].text : null;766 return dust.pragmas[name](context, ctx, bodies, params);767 },768 partial: function(context, node) {769 return ".partial("770 + dust.compileNode(context, node[1])771 + "," + dust.compileNode(context, node[2])772 + "," + dust.compileNode(context, node[3]) + ")";773 },774 context: function(context, node) {775 if (node[1]) {776 return "ctx.rebase(" + dust.compileNode(context, node[1]) + ")";777 }778 return "ctx";779 },780 params: function(context, node) {781 var out = [];782 for (var i=1, len=node.length; i<len; i++) {783 out.push(dust.compileNode(context, node[i]));784 }785 if (out.length) {786 return "{" + out.join(',') + "}";787 }788 return "null";789 },790 bodies: function(context, node) {791 var out = [];792 for (var i=1, len=node.length; i<len; i++) {793 out.push(dust.compileNode(context, node[i]));794 }795 return "{" + out.join(',') + "}";796 },797 param: function(context, node) {798 return dust.compileNode(context, node[1]) + ":" + dust.compileNode(context, node[2]);799 },800 filters: function(context, node) {801 var list = [];802 for (var i=1, len=node.length; i<len; i++) {803 var filter = node[i];804 list.push("\"" + filter + "\"");805 }806 return "\"" + context.auto + "\""807 + (list.length ? ",[" + list.join(',') + "]" : '');808 },809 key: function(context, node) {810 return "ctx.get(\"" + node[1] + "\")";811 },812 path: function(context, node) {813 var current = node[1],814 keys = node[2],815 list = [];816 for (var i=0,len=keys.length; i<len; i++) {817 if (dust.isArray(keys[i]))818 list.push(dust.compileNode(context, keys[i]));819 else820 list.push("\"" + keys[i] + "\"");821 }822 return "ctx.getPath(" + current + ",[" + list.join(',') + "])";823 },824 literal: function(context, node) {825 return escape(node[1]);826 }827};828function compileSection(context, node, cmd) {829 return "." + cmd + "("830 + dust.compileNode(context, node[1])831 + "," + dust.compileNode(context, node[2]) + ","832 + dust.compileNode(context, node[4]) + ","833 + dust.compileNode(context, node[3])834 + ")";835};836var escape = (typeof JSON === "undefined")837 ? function(str) { return "\"" + dust.escapeJs(str) + "\"" }838 : JSON.stringify;839 return dust;840});841if (typeof exports !== 'undefined') {842 module.exports = dustCompiler;843} else {844 dustCompiler(getGlobal());845}846(function(dust){847var parser = (function(){848 /*849 * Generated by PEG.js 0.7.0.850 *851 * http://pegjs.majda.cz/852 */853 854 function quote(s) {855 /*856 * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a857 * string literal except for the closing quote character, backslash,858 * carriage return, line separator, paragraph separator, and line feed.859 * Any character may appear in the form of an escape sequence.860 *861 * For portability, we also escape escape all control and non-ASCII862 * characters. Note that "\0" and "\v" escape sequences are not used863 * because JSHint does not like the first and IE the second.864 */865 return '"' + s866 .replace(/\\/g, '\\\\') // backslash867 .replace(/"/g, '\\"') // closing quote character868 .replace(/\x08/g, '\\b') // backspace869 .replace(/\t/g, '\\t') // horizontal tab870 .replace(/\n/g, '\\n') // line feed871 .replace(/\f/g, '\\f') // form feed872 .replace(/\r/g, '\\r') // carriage return873 .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)874 + '"';875 }876 877 var result = {878 /*879 * Parses the input with a generated parser. If the parsing is successfull,880 * returns a value explicitly or implicitly specified by the grammar from881 * which the parser was generated (see |PEG.buildParser|). If the parsing is882 * unsuccessful, throws |PEG.parser.SyntaxError| describing the error.883 */884 parse: function(input, startRule) {885 var parseFunctions = {886 "body": parse_body,887 "part": parse_part,888 "section": parse_section,889 "sec_tag_start": parse_sec_tag_start,890 "end_tag": parse_end_tag,891 "context": parse_context,892 "params": parse_params,893 "bodies": parse_bodies,894 "reference": parse_reference,895 "partial": parse_partial,896 "filters": parse_filters,897 "special": parse_special,898 "identifier": parse_identifier,899 "number": parse_number,900 "frac": parse_frac,901 "integer": parse_integer,902 "path": parse_path,903 "key": parse_key,904 "array": parse_array,905 "array_part": parse_array_part,906 "inline": parse_inline,907 "inline_part": parse_inline_part,908 "buffer": parse_buffer,909 "literal": parse_literal,910 "esc": parse_esc,911 "comment": parse_comment,912 "tag": parse_tag,913 "ld": parse_ld,914 "rd": parse_rd,915 "lb": parse_lb,916 "rb": parse_rb,917 "eol": parse_eol,918 "ws": parse_ws919 };920 921 if (startRule !== undefined) {922 if (parseFunctions[startRule] === undefined) {923 throw new Error("Invalid rule name: " + quote(startRule) + ".");924 }925 } else {926 startRule = "body";927 }928 929 var pos = { offset: 0, line: 1, column: 1, seenCR: false };930 var reportFailures = 0;931 var rightmostFailuresPos = { offset: 0, line: 1, column: 1, seenCR: false };932 var rightmostFailuresExpected = [];933 934 function padLeft(input, padding, length) {935 var result = input;936 937 var padLength = length - input.length;938 for (var i = 0; i < padLength; i++) {939 result = padding + result;940 }941 942 return result;943 }944 945 function escape(ch) {946 var charCode = ch.charCodeAt(0);947 var escapeChar;948 var length;949 950 if (charCode <= 0xFF) {951 escapeChar = 'x';952 length = 2;953 } else {954 escapeChar = 'u';955 length = 4;956 }957 958 return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);959 }960 961 function clone(object) {962 var result = {};963 for (var key in object) {964 result[key] = object[key];965 }966 return result;967 }968 969 function advance(pos, n) {970 var endOffset = pos.offset + n;971 972 for (var offset = pos.offset; offset < endOffset; offset++) {973 var ch = input.charAt(offset);974 if (ch === "\n") {975 if (!pos.seenCR) { pos.line++; }976 pos.column = 1;977 pos.seenCR = false;978 } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {979 pos.line++;980 pos.column = 1;981 pos.seenCR = true;982 } else {983 pos.column++;984 pos.seenCR = false;985 }986 }987 988 pos.offset += n;989 }990 991 function matchFailed(failure) {992 if (pos.offset < rightmostFailuresPos.offset) {993 return;994 }995 996 if (pos.offset > rightmostFailuresPos.offset) {997 rightmostFailuresPos = clone(pos);998 rightmostFailuresExpected = [];999 }1000 1001 rightmostFailuresExpected.push(failure);1002 }1003 1004 function parse_body() {1005 var result0, result1;1006 var pos0;1007 1008 pos0 = clone(pos);1009 result0 = [];1010 result1 = parse_part();1011 while (result1 !== null) {1012 result0.push(result1);1013 result1 = parse_part();1014 }1015 if (result0 !== null) {1016 result0 = (function(offset, line, column, p) { return ["body"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1017 }1018 if (result0 === null) {1019 pos = clone(pos0);1020 }1021 return result0;1022 }1023 1024 function parse_part() {1025 var result0;1026 1027 result0 = parse_comment();1028 if (result0 === null) {1029 result0 = parse_section();1030 if (result0 === null) {1031 result0 = parse_partial();1032 if (result0 === null) {1033 result0 = parse_special();1034 if (result0 === null) {1035 result0 = parse_reference();1036 if (result0 === null) {1037 result0 = parse_buffer();1038 }1039 }1040 }1041 }1042 }1043 return result0;1044 }1045 1046 function parse_section() {1047 var result0, result1, result2, result3, result4, result5, result6;1048 var pos0, pos1;1049 1050 reportFailures++;1051 pos0 = clone(pos);1052 pos1 = clone(pos);1053 result0 = parse_sec_tag_start();1054 if (result0 !== null) {1055 result1 = [];1056 result2 = parse_ws();1057 while (result2 !== null) {1058 result1.push(result2);1059 result2 = parse_ws();1060 }1061 if (result1 !== null) {1062 result2 = parse_rd();1063 if (result2 !== null) {1064 result3 = parse_body();1065 if (result3 !== null) {1066 result4 = parse_bodies();1067 if (result4 !== null) {1068 result5 = parse_end_tag();1069 result5 = result5 !== null ? result5 : "";1070 if (result5 !== null) {1071 result6 = (function(offset, line, column, t, b, e, n) {if( (!n) || (t[1].text !== n.text) ) { throw new Error("Expected end tag for "+t[1].text+" but it was not found. At line : "+line+", column : " + column)} return true;})(pos.offset, pos.line, pos.column, result0, result3, result4, result5) ? "" : null;1072 if (result6 !== null) {1073 result0 = [result0, result1, result2, result3, result4, result5, result6];1074 } else {1075 result0 = null;1076 pos = clone(pos1);1077 }1078 } else {1079 result0 = null;1080 pos = clone(pos1);1081 }1082 } else {1083 result0 = null;1084 pos = clone(pos1);1085 }1086 } else {1087 result0 = null;1088 pos = clone(pos1);1089 }1090 } else {1091 result0 = null;1092 pos = clone(pos1);1093 }1094 } else {1095 result0 = null;1096 pos = clone(pos1);1097 }1098 } else {1099 result0 = null;1100 pos = clone(pos1);1101 }1102 if (result0 !== null) {1103 result0 = (function(offset, line, column, t, b, e, n) { e.push(["param", ["literal", "block"], b]); t.push(e); return t })(pos0.offset, pos0.line, pos0.column, result0[0], result0[3], result0[4], result0[5]);1104 }1105 if (result0 === null) {1106 pos = clone(pos0);1107 }1108 if (result0 === null) {1109 pos0 = clone(pos);1110 pos1 = clone(pos);1111 result0 = parse_sec_tag_start();1112 if (result0 !== null) {1113 result1 = [];1114 result2 = parse_ws();1115 while (result2 !== null) {1116 result1.push(result2);1117 result2 = parse_ws();1118 }1119 if (result1 !== null) {1120 if (input.charCodeAt(pos.offset) === 47) {1121 result2 = "/";1122 advance(pos, 1);1123 } else {1124 result2 = null;1125 if (reportFailures === 0) {1126 matchFailed("\"/\"");1127 }1128 }1129 if (result2 !== null) {1130 result3 = parse_rd();1131 if (result3 !== null) {1132 result0 = [result0, result1, result2, result3];1133 } else {1134 result0 = null;1135 pos = clone(pos1);1136 }1137 } else {1138 result0 = null;1139 pos = clone(pos1);1140 }1141 } else {1142 result0 = null;1143 pos = clone(pos1);1144 }1145 } else {1146 result0 = null;1147 pos = clone(pos1);1148 }1149 if (result0 !== null) {1150 result0 = (function(offset, line, column, t) { t.push(["bodies"]); return t })(pos0.offset, pos0.line, pos0.column, result0[0]);1151 }1152 if (result0 === null) {1153 pos = clone(pos0);1154 }1155 }1156 reportFailures--;1157 if (reportFailures === 0 && result0 === null) {1158 matchFailed("section");1159 }1160 return result0;1161 }1162 1163 function parse_sec_tag_start() {1164 var result0, result1, result2, result3, result4, result5;1165 var pos0, pos1;1166 1167 pos0 = clone(pos);1168 pos1 = clone(pos);1169 result0 = parse_ld();1170 if (result0 !== null) {1171 if (/^[#?^<+@%]/.test(input.charAt(pos.offset))) {1172 result1 = input.charAt(pos.offset);1173 advance(pos, 1);1174 } else {1175 result1 = null;1176 if (reportFailures === 0) {1177 matchFailed("[#?^<+@%]");1178 }1179 }1180 if (result1 !== null) {1181 result2 = [];1182 result3 = parse_ws();1183 while (result3 !== null) {1184 result2.push(result3);1185 result3 = parse_ws();1186 }1187 if (result2 !== null) {1188 result3 = parse_identifier();1189 if (result3 !== null) {1190 result4 = parse_context();1191 if (result4 !== null) {1192 result5 = parse_params();1193 if (result5 !== null) {1194 result0 = [result0, result1, result2, result3, result4, result5];1195 } else {1196 result0 = null;1197 pos = clone(pos1);1198 }1199 } else {1200 result0 = null;1201 pos = clone(pos1);1202 }1203 } else {1204 result0 = null;1205 pos = clone(pos1);1206 }1207 } else {1208 result0 = null;1209 pos = clone(pos1);1210 }1211 } else {1212 result0 = null;1213 pos = clone(pos1);1214 }1215 } else {1216 result0 = null;1217 pos = clone(pos1);1218 }1219 if (result0 !== null) {1220 result0 = (function(offset, line, column, t, n, c, p) { return [t, n, c, p] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[3], result0[4], result0[5]);1221 }1222 if (result0 === null) {1223 pos = clone(pos0);1224 }1225 return result0;1226 }1227 1228 function parse_end_tag() {1229 var result0, result1, result2, result3, result4, result5;1230 var pos0, pos1;1231 1232 reportFailures++;1233 pos0 = clone(pos);1234 pos1 = clone(pos);1235 result0 = parse_ld();1236 if (result0 !== null) {1237 if (input.charCodeAt(pos.offset) === 47) {1238 result1 = "/";1239 advance(pos, 1);1240 } else {1241 result1 = null;1242 if (reportFailures === 0) {1243 matchFailed("\"/\"");1244 }1245 }1246 if (result1 !== null) {1247 result2 = [];1248 result3 = parse_ws();1249 while (result3 !== null) {1250 result2.push(result3);1251 result3 = parse_ws();1252 }1253 if (result2 !== null) {1254 result3 = parse_identifier();1255 if (result3 !== null) {1256 result4 = [];1257 result5 = parse_ws();1258 while (result5 !== null) {1259 result4.push(result5);1260 result5 = parse_ws();1261 }1262 if (result4 !== null) {1263 result5 = parse_rd();1264 if (result5 !== null) {1265 result0 = [result0, result1, result2, result3, result4, result5];1266 } else {1267 result0 = null;1268 pos = clone(pos1);1269 }1270 } else {1271 result0 = null;1272 pos = clone(pos1);1273 }1274 } else {1275 result0 = null;1276 pos = clone(pos1);1277 }1278 } else {1279 result0 = null;1280 pos = clone(pos1);1281 }1282 } else {1283 result0 = null;1284 pos = clone(pos1);1285 }1286 } else {1287 result0 = null;1288 pos = clone(pos1);1289 }1290 if (result0 !== null) {1291 result0 = (function(offset, line, column, n) { return n })(pos0.offset, pos0.line, pos0.column, result0[3]);1292 }1293 if (result0 === null) {1294 pos = clone(pos0);1295 }1296 reportFailures--;1297 if (reportFailures === 0 && result0 === null) {1298 matchFailed("end tag");1299 }1300 return result0;1301 }1302 1303 function parse_context() {1304 var result0, result1;1305 var pos0, pos1, pos2;1306 1307 pos0 = clone(pos);1308 pos1 = clone(pos);1309 pos2 = clone(pos);1310 if (input.charCodeAt(pos.offset) === 58) {1311 result0 = ":";1312 advance(pos, 1);1313 } else {1314 result0 = null;1315 if (reportFailures === 0) {1316 matchFailed("\":\"");1317 }1318 }1319 if (result0 !== null) {1320 result1 = parse_identifier();1321 if (result1 !== null) {1322 result0 = [result0, result1];1323 } else {1324 result0 = null;1325 pos = clone(pos2);1326 }1327 } else {1328 result0 = null;1329 pos = clone(pos2);1330 }1331 if (result0 !== null) {1332 result0 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result0[1]);1333 }1334 if (result0 === null) {1335 pos = clone(pos1);1336 }1337 result0 = result0 !== null ? result0 : "";1338 if (result0 !== null) {1339 result0 = (function(offset, line, column, n) { return n ? ["context", n] : ["context"] })(pos0.offset, pos0.line, pos0.column, result0);1340 }1341 if (result0 === null) {1342 pos = clone(pos0);1343 }1344 return result0;1345 }1346 1347 function parse_params() {1348 var result0, result1, result2, result3, result4;1349 var pos0, pos1, pos2;1350 1351 reportFailures++;1352 pos0 = clone(pos);1353 result0 = [];1354 pos1 = clone(pos);1355 pos2 = clone(pos);1356 result2 = parse_ws();1357 if (result2 !== null) {1358 result1 = [];1359 while (result2 !== null) {1360 result1.push(result2);1361 result2 = parse_ws();1362 }1363 } else {1364 result1 = null;1365 }1366 if (result1 !== null) {1367 result2 = parse_key();1368 if (result2 !== null) {1369 if (input.charCodeAt(pos.offset) === 61) {1370 result3 = "=";1371 advance(pos, 1);1372 } else {1373 result3 = null;1374 if (reportFailures === 0) {1375 matchFailed("\"=\"");1376 }1377 }1378 if (result3 !== null) {1379 result4 = parse_number();1380 if (result4 === null) {1381 result4 = parse_identifier();1382 if (result4 === null) {1383 result4 = parse_inline();1384 }1385 }1386 if (result4 !== null) {1387 result1 = [result1, result2, result3, result4];1388 } else {1389 result1 = null;1390 pos = clone(pos2);1391 }1392 } else {1393 result1 = null;1394 pos = clone(pos2);1395 }1396 } else {1397 result1 = null;1398 pos = clone(pos2);1399 }1400 } else {1401 result1 = null;1402 pos = clone(pos2);1403 }1404 if (result1 !== null) {1405 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[1], result1[3]);1406 }1407 if (result1 === null) {1408 pos = clone(pos1);1409 }1410 while (result1 !== null) {1411 result0.push(result1);1412 pos1 = clone(pos);1413 pos2 = clone(pos);1414 result2 = parse_ws();1415 if (result2 !== null) {1416 result1 = [];1417 while (result2 !== null) {1418 result1.push(result2);1419 result2 = parse_ws();1420 }1421 } else {1422 result1 = null;1423 }1424 if (result1 !== null) {1425 result2 = parse_key();1426 if (result2 !== null) {1427 if (input.charCodeAt(pos.offset) === 61) {1428 result3 = "=";1429 advance(pos, 1);1430 } else {1431 result3 = null;1432 if (reportFailures === 0) {1433 matchFailed("\"=\"");1434 }1435 }1436 if (result3 !== null) {1437 result4 = parse_number();1438 if (result4 === null) {1439 result4 = parse_identifier();1440 if (result4 === null) {1441 result4 = parse_inline();1442 }1443 }1444 if (result4 !== null) {1445 result1 = [result1, result2, result3, result4];1446 } else {1447 result1 = null;1448 pos = clone(pos2);1449 }1450 } else {1451 result1 = null;1452 pos = clone(pos2);1453 }1454 } else {1455 result1 = null;1456 pos = clone(pos2);1457 }1458 } else {1459 result1 = null;1460 pos = clone(pos2);1461 }1462 if (result1 !== null) {1463 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[1], result1[3]);1464 }1465 if (result1 === null) {1466 pos = clone(pos1);1467 }1468 }1469 if (result0 !== null) {1470 result0 = (function(offset, line, column, p) { return ["params"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1471 }1472 if (result0 === null) {1473 pos = clone(pos0);1474 }1475 reportFailures--;1476 if (reportFailures === 0 && result0 === null) {1477 matchFailed("params");1478 }1479 return result0;1480 }1481 1482 function parse_bodies() {1483 var result0, result1, result2, result3, result4, result5;1484 var pos0, pos1, pos2;1485 1486 reportFailures++;1487 pos0 = clone(pos);1488 result0 = [];1489 pos1 = clone(pos);1490 pos2 = clone(pos);1491 result1 = parse_ld();1492 if (result1 !== null) {1493 if (input.charCodeAt(pos.offset) === 58) {1494 result2 = ":";1495 advance(pos, 1);1496 } else {1497 result2 = null;1498 if (reportFailures === 0) {1499 matchFailed("\":\"");1500 }1501 }1502 if (result2 !== null) {1503 result3 = parse_key();1504 if (result3 !== null) {1505 result4 = parse_rd();1506 if (result4 !== null) {1507 result5 = parse_body();1508 if (result5 !== null) {1509 result1 = [result1, result2, result3, result4, result5];1510 } else {1511 result1 = null;1512 pos = clone(pos2);1513 }1514 } else {1515 result1 = null;1516 pos = clone(pos2);1517 }1518 } else {1519 result1 = null;1520 pos = clone(pos2);1521 }1522 } else {1523 result1 = null;1524 pos = clone(pos2);1525 }1526 } else {1527 result1 = null;1528 pos = clone(pos2);1529 }1530 if (result1 !== null) {1531 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[2], result1[4]);1532 }1533 if (result1 === null) {1534 pos = clone(pos1);1535 }1536 while (result1 !== null) {1537 result0.push(result1);1538 pos1 = clone(pos);1539 pos2 = clone(pos);1540 result1 = parse_ld();1541 if (result1 !== null) {1542 if (input.charCodeAt(pos.offset) === 58) {1543 result2 = ":";1544 advance(pos, 1);1545 } else {1546 result2 = null;1547 if (reportFailures === 0) {1548 matchFailed("\":\"");1549 }1550 }1551 if (result2 !== null) {1552 result3 = parse_key();1553 if (result3 !== null) {1554 result4 = parse_rd();1555 if (result4 !== null) {1556 result5 = parse_body();1557 if (result5 !== null) {1558 result1 = [result1, result2, result3, result4, result5];1559 } else {1560 result1 = null;1561 pos = clone(pos2);1562 }1563 } else {1564 result1 = null;1565 pos = clone(pos2);1566 }1567 } else {1568 result1 = null;1569 pos = clone(pos2);1570 }1571 } else {1572 result1 = null;1573 pos = clone(pos2);1574 }1575 } else {1576 result1 = null;1577 pos = clone(pos2);1578 }1579 if (result1 !== null) {1580 result1 = (function(offset, line, column, k, v) {return ["param", ["literal", k], v]})(pos1.offset, pos1.line, pos1.column, result1[2], result1[4]);1581 }1582 if (result1 === null) {1583 pos = clone(pos1);1584 }1585 }1586 if (result0 !== null) {1587 result0 = (function(offset, line, column, p) { return ["bodies"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0);1588 }1589 if (result0 === null) {1590 pos = clone(pos0);1591 }1592 reportFailures--;1593 if (reportFailures === 0 && result0 === null) {1594 matchFailed("bodies");1595 }1596 return result0;1597 }1598 1599 function parse_reference() {1600 var result0, result1, result2, result3;1601 var pos0, pos1;1602 1603 reportFailures++;1604 pos0 = clone(pos);1605 pos1 = clone(pos);1606 result0 = parse_ld();1607 if (result0 !== null) {1608 result1 = parse_identifier();1609 if (result1 !== null) {1610 result2 = parse_filters();1611 if (result2 !== null) {1612 result3 = parse_rd();1613 if (result3 !== null) {1614 result0 = [result0, result1, result2, result3];1615 } else {1616 result0 = null;1617 pos = clone(pos1);1618 }1619 } else {1620 result0 = null;1621 pos = clone(pos1);1622 }1623 } else {1624 result0 = null;1625 pos = clone(pos1);1626 }1627 } else {1628 result0 = null;1629 pos = clone(pos1);1630 }1631 if (result0 !== null) {1632 result0 = (function(offset, line, column, n, f) { return ["reference", n, f] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[2]);1633 }1634 if (result0 === null) {1635 pos = clone(pos0);1636 }1637 reportFailures--;1638 if (reportFailures === 0 && result0 === null) {1639 matchFailed("reference");1640 }1641 return result0;1642 }1643 1644 function parse_partial() {1645 var result0, result1, result2, result3, result4, result5, result6, result7, result8;1646 var pos0, pos1, pos2;1647 1648 reportFailures++;1649 pos0 = clone(pos);1650 pos1 = clone(pos);1651 result0 = parse_ld();1652 if (result0 !== null) {1653 if (input.charCodeAt(pos.offset) === 62) {1654 result1 = ">";1655 advance(pos, 1);1656 } else {1657 result1 = null;1658 if (reportFailures === 0) {1659 matchFailed("\">\"");1660 }1661 }1662 if (result1 === null) {1663 if (input.charCodeAt(pos.offset) === 43) {1664 result1 = "+";1665 advance(pos, 1);1666 } else {1667 result1 = null;1668 if (reportFailures === 0) {1669 matchFailed("\"+\"");1670 }1671 }1672 }1673 if (result1 !== null) {1674 result2 = [];1675 result3 = parse_ws();1676 while (result3 !== null) {1677 result2.push(result3);1678 result3 = parse_ws();1679 }1680 if (result2 !== null) {1681 pos2 = clone(pos);1682 result3 = parse_key();1683 if (result3 !== null) {1684 result3 = (function(offset, line, column, k) {return ["literal", k]})(pos2.offset, pos2.line, pos2.column, result3);1685 }1686 if (result3 === null) {1687 pos = clone(pos2);1688 }1689 if (result3 === null) {1690 result3 = parse_inline();1691 }1692 if (result3 !== null) {1693 result4 = parse_context();1694 if (result4 !== null) {1695 result5 = parse_params();1696 if (result5 !== null) {1697 result6 = [];1698 result7 = parse_ws();1699 while (result7 !== null) {1700 result6.push(result7);1701 result7 = parse_ws();1702 }1703 if (result6 !== null) {1704 if (input.charCodeAt(pos.offset) === 47) {1705 result7 = "/";1706 advance(pos, 1);1707 } else {1708 result7 = null;1709 if (reportFailures === 0) {1710 matchFailed("\"/\"");1711 }1712 }1713 if (result7 !== null) {1714 result8 = parse_rd();1715 if (result8 !== null) {1716 result0 = [result0, result1, result2, result3, result4, result5, result6, result7, result8];1717 } else {1718 result0 = null;1719 pos = clone(pos1);1720 }1721 } else {1722 result0 = null;1723 pos = clone(pos1);1724 }1725 } else {1726 result0 = null;1727 pos = clone(pos1);1728 }1729 } else {1730 result0 = null;1731 pos = clone(pos1);1732 }1733 } else {1734 result0 = null;1735 pos = clone(pos1);1736 }1737 } else {1738 result0 = null;1739 pos = clone(pos1);1740 }1741 } else {1742 result0 = null;1743 pos = clone(pos1);1744 }1745 } else {1746 result0 = null;1747 pos = clone(pos1);1748 }1749 } else {1750 result0 = null;1751 pos = clone(pos1);1752 }1753 if (result0 !== null) {1754 result0 = (function(offset, line, column, s, n, c, p) { var key = (s ===">")? "partial" : s; return [key, n, c, p] })(pos0.offset, pos0.line, pos0.column, result0[1], result0[3], result0[4], result0[5]);1755 }1756 if (result0 === null) {1757 pos = clone(pos0);1758 }1759 reportFailures--;1760 if (reportFailures === 0 && result0 === null) {1761 matchFailed("partial");1762 }1763 return result0;1764 }1765 1766 function parse_filters() {1767 var result0, result1, result2;1768 var pos0, pos1, pos2;1769 1770 reportFailures++;1771 pos0 = clone(pos);1772 result0 = [];1773 pos1 = clone(pos);1774 pos2 = clone(pos);1775 if (input.charCodeAt(pos.offset) === 124) {1776 result1 = "|";1777 advance(pos, 1);1778 } else {1779 result1 = null;1780 if (reportFailures === 0) {1781 matchFailed("\"|\"");1782 }1783 }1784 if (result1 !== null) {1785 result2 = parse_key();1786 if (result2 !== null) {1787 result1 = [result1, result2];1788 } else {1789 result1 = null;1790 pos = clone(pos2);1791 }1792 } else {1793 result1 = null;1794 pos = clone(pos2);1795 }1796 if (result1 !== null) {1797 result1 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result1[1]);1798 }1799 if (result1 === null) {1800 pos = clone(pos1);1801 }1802 while (result1 !== null) {1803 result0.push(result1);1804 pos1 = clone(pos);1805 pos2 = clone(pos);1806 if (input.charCodeAt(pos.offset) === 124) {1807 result1 = "|";1808 advance(pos, 1);1809 } else {1810 result1 = null;1811 if (reportFailures === 0) {1812 matchFailed("\"|\"");1813 }1814 }1815 if (result1 !== null) {1816 result2 = parse_key();1817 if (result2 !== null) {1818 result1 = [result1, result2];1819 } else {1820 result1 = null;1821 pos = clone(pos2);1822 }1823 } else {1824 result1 = null;1825 pos = clone(pos2);1826 }1827 if (result1 !== null) {1828 result1 = (function(offset, line, column, n) {return n})(pos1.offset, pos1.line, pos1.column, result1[1]);1829 }1830 if (result1 === null) {1831 pos = clone(pos1);1832 }1833 }1834 if (result0 !== null) {1835 result0 = (function(offset, line, column, f) { return ["filters"].concat(f) })(pos0.offset, pos0.line, pos0.column, result0);1836 }1837 if (result0 === null) {1838 pos = clone(pos0);1839 }1840 reportFailures--;1841 if (reportFailures === 0 && result0 === null) {1842 matchFailed("filters");1843 }1844 return result0;1845 }1846 1847 function parse_special() {1848 var result0, result1, result2, result3;1849 var pos0, pos1;1850 1851 reportFailures++;1852 pos0 = clone(pos);1853 pos1 = clone(pos);1854 result0 = parse_ld();1855 if (result0 !== null) {1856 if (input.charCodeAt(pos.offset) === 126) {1857 result1 = "~";1858 advance(pos, 1);1859 } else {1860 result1 = null;1861 if (reportFailures === 0) {1862 matchFailed("\"~\"");1863 }1864 }1865 if (result1 !== null) {1866 result2 = parse_key();1867 if (result2 !== null) {1868 result3 = parse_rd();1869 if (result3 !== null) {1870 result0 = [result0, result1, result2, result3];1871 } else {1872 result0 = null;1873 pos = clone(pos1);1874 }1875 } else {1876 result0 = null;1877 pos = clone(pos1);1878 }1879 } else {1880 result0 = null;1881 pos = clone(pos1);1882 }1883 } else {1884 result0 = null;1885 pos = clone(pos1);1886 }1887 if (result0 !== null) {1888 result0 = (function(offset, line, column, k) { return ["special", k] })(pos0.offset, pos0.line, pos0.column, result0[2]);1889 }1890 if (result0 === null) {1891 pos = clone(pos0);1892 }1893 reportFailures--;1894 if (reportFailures === 0 && result0 === null) {1895 matchFailed("special");1896 }1897 return result0;1898 }1899 1900 function parse_identifier() {1901 var result0;1902 var pos0;1903 1904 reportFailures++;1905 pos0 = clone(pos);1906 result0 = parse_path();1907 if (result0 !== null) {1908 result0 = (function(offset, line, column, p) { var arr = ["path"].concat(p); arr.text = p[1].join('.'); return arr; })(pos0.offset, pos0.line, pos0.column, result0);1909 }1910 if (result0 === null) {1911 pos = clone(pos0);1912 }1913 if (result0 === null) {1914 pos0 = clone(pos);1915 result0 = parse_key();1916 if (result0 !== null) {1917 result0 = (function(offset, line, column, k) { var arr = ["key", k]; arr.text = k; return arr; })(pos0.offset, pos0.line, pos0.column, result0);1918 }1919 if (result0 === null) {1920 pos = clone(pos0);1921 }1922 }1923 reportFailures--;1924 if (reportFailures === 0 && result0 === null) {1925 matchFailed("identifier");1926 }1927 return result0;1928 }1929 1930 function parse_number() {1931 var result0;1932 var pos0;1933 1934 reportFailures++;1935 pos0 = clone(pos);1936 result0 = parse_frac();1937 if (result0 === null) {1938 result0 = parse_integer();1939 }1940 if (result0 !== null) {1941 result0 = (function(offset, line, column, n) { return ['literal', n]; })(pos0.offset, pos0.line, pos0.column, result0);1942 }1943 if (result0 === null) {1944 pos = clone(pos0);1945 }1946 reportFailures--;1947 if (reportFailures === 0 && result0 === null) {1948 matchFailed("number");1949 }1950 return result0;1951 }1952 1953 function parse_frac() {1954 var result0, result1, result2, result3;1955 var pos0, pos1;1956 1957 reportFailures++;1958 pos0 = clone(pos);1959 pos1 = clone(pos);1960 result0 = parse_integer();1961 if (result0 !== null) {1962 if (input.charCodeAt(pos.offset) === 46) {1963 result1 = ".";1964 advance(pos, 1);1965 } else {1966 result1 = null;1967 if (reportFailures === 0) {1968 matchFailed("\".\"");1969 }1970 }1971 if (result1 !== null) {1972 result3 = parse_integer();1973 if (result3 !== null) {1974 result2 = [];1975 while (result3 !== null) {1976 result2.push(result3);1977 result3 = parse_integer();1978 }1979 } else {1980 result2 = null;1981 }1982 if (result2 !== null) {1983 result0 = [result0, result1, result2];1984 } else {1985 result0 = null;1986 pos = clone(pos1);1987 }1988 } else {1989 result0 = null;1990 pos = clone(pos1);1991 }1992 } else {1993 result0 = null;1994 pos = clone(pos1);1995 }1996 if (result0 !== null) {1997 result0 = (function(offset, line, column, l, r) { return parseFloat(l + "." + r.join('')); })(pos0.offset, pos0.line, pos0.column, result0[0], result0[2]);1998 }1999 if (result0 === null) {2000 pos = clone(pos0);2001 }2002 reportFailures--;2003 if (reportFailures === 0 && result0 === null) {2004 matchFailed("frac");2005 }2006 return result0;2007 }2008 2009 function parse_integer() {2010 var result0, result1;2011 var pos0;2012 2013 reportFailures++;2014 pos0 = clone(pos);2015 if (/^[0-9]/.test(input.charAt(pos.offset))) {2016 result1 = input.charAt(pos.offset);2017 advance(pos, 1);2018 } else {2019 result1 = null;2020 if (reportFailures === 0) {2021 matchFailed("[0-9]");2022 }2023 }2024 if (result1 !== null) {2025 result0 = [];2026 while (result1 !== null) {2027 result0.push(result1);2028 if (/^[0-9]/.test(input.charAt(pos.offset))) {2029 result1 = input.charAt(pos.offset);2030 advance(pos, 1);2031 } else {2032 result1 = null;2033 if (reportFailures === 0) {2034 matchFailed("[0-9]");2035 }2036 }2037 }2038 } else {2039 result0 = null;2040 }2041 if (result0 !== null) {2042 result0 = (function(offset, line, column, digits) { return parseInt(digits.join(""), 10); })(pos0.offset, pos0.line, pos0.column, result0);2043 }2044 if (result0 === null) {2045 pos = clone(pos0);2046 }2047 reportFailures--;2048 if (reportFailures === 0 && result0 === null) {2049 matchFailed("integer");2050 }2051 return result0;2052 }2053 2054 function parse_path() {2055 var result0, result1, result2;2056 var pos0, pos1;2057 2058 reportFailures++;2059 pos0 = clone(pos);2060 pos1 = clone(pos);2061 result0 = parse_key();2062 result0 = result0 !== null ? result0 : "";2063 if (result0 !== null) {2064 result2 = parse_array_part();2065 if (result2 === null) {2066 result2 = parse_array();2067 }2068 if (result2 !== null) {2069 result1 = [];2070 while (result2 !== null) {2071 result1.push(result2);2072 result2 = parse_array_part();2073 if (result2 === null) {2074 result2 = parse_array();2075 }2076 }2077 } else {2078 result1 = null;2079 }2080 if (result1 !== null) {2081 result0 = [result0, result1];2082 } else {2083 result0 = null;2084 pos = clone(pos1);2085 }2086 } else {2087 result0 = null;2088 pos = clone(pos1);2089 }2090 if (result0 !== null) {2091 result0 = (function(offset, line, column, k, d) {2092 d = d[0]; 2093 if (k && d) {2094 d.unshift(k);2095 return [false, d];2096 }2097 return [true, d];2098 })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2099 }2100 if (result0 === null) {2101 pos = clone(pos0);2102 }2103 if (result0 === null) {2104 pos0 = clone(pos);2105 pos1 = clone(pos);2106 if (input.charCodeAt(pos.offset) === 46) {2107 result0 = ".";2108 advance(pos, 1);2109 } else {2110 result0 = null;2111 if (reportFailures === 0) {2112 matchFailed("\".\"");2113 }2114 }2115 if (result0 !== null) {2116 result1 = [];2117 result2 = parse_array_part();2118 if (result2 === null) {2119 result2 = parse_array();2120 }2121 while (result2 !== null) {2122 result1.push(result2);2123 result2 = parse_array_part();2124 if (result2 === null) {2125 result2 = parse_array();2126 }2127 }2128 if (result1 !== null) {2129 result0 = [result0, result1];2130 } else {2131 result0 = null;2132 pos = clone(pos1);2133 }2134 } else {2135 result0 = null;2136 pos = clone(pos1);2137 }2138 if (result0 !== null) {2139 result0 = (function(offset, line, column, d) {2140 if (d.length > 0) {2141 return [true, d[0]];2142 }2143 return [true, []] 2144 })(pos0.offset, pos0.line, pos0.column, result0[1]);2145 }2146 if (result0 === null) {2147 pos = clone(pos0);2148 }2149 }2150 reportFailures--;2151 if (reportFailures === 0 && result0 === null) {2152 matchFailed("path");2153 }2154 return result0;2155 }2156 2157 function parse_key() {2158 var result0, result1, result2;2159 var pos0, pos1;2160 2161 reportFailures++;2162 pos0 = clone(pos);2163 pos1 = clone(pos);2164 if (/^[a-zA-Z_$]/.test(input.charAt(pos.offset))) {2165 result0 = input.charAt(pos.offset);2166 advance(pos, 1);2167 } else {2168 result0 = null;2169 if (reportFailures === 0) {2170 matchFailed("[a-zA-Z_$]");2171 }2172 }2173 if (result0 !== null) {2174 result1 = [];2175 if (/^[0-9a-zA-Z_$\-]/.test(input.charAt(pos.offset))) {2176 result2 = input.charAt(pos.offset);2177 advance(pos, 1);2178 } else {2179 result2 = null;2180 if (reportFailures === 0) {2181 matchFailed("[0-9a-zA-Z_$\\-]");2182 }2183 }2184 while (result2 !== null) {2185 result1.push(result2);2186 if (/^[0-9a-zA-Z_$\-]/.test(input.charAt(pos.offset))) {2187 result2 = input.charAt(pos.offset);2188 advance(pos, 1);2189 } else {2190 result2 = null;2191 if (reportFailures === 0) {2192 matchFailed("[0-9a-zA-Z_$\\-]");2193 }2194 }2195 }2196 if (result1 !== null) {2197 result0 = [result0, result1];2198 } else {2199 result0 = null;2200 pos = clone(pos1);2201 }2202 } else {2203 result0 = null;2204 pos = clone(pos1);2205 }2206 if (result0 !== null) {2207 result0 = (function(offset, line, column, h, t) { return h + t.join('') })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2208 }2209 if (result0 === null) {2210 pos = clone(pos0);2211 }2212 reportFailures--;2213 if (reportFailures === 0 && result0 === null) {2214 matchFailed("key");2215 }2216 return result0;2217 }2218 2219 function parse_array() {2220 var result0, result1, result2;2221 var pos0, pos1, pos2, pos3, pos4;2222 2223 reportFailures++;2224 pos0 = clone(pos);2225 pos1 = clone(pos);2226 pos2 = clone(pos);2227 pos3 = clone(pos);2228 result0 = parse_lb();2229 if (result0 !== null) {2230 pos4 = clone(pos);2231 if (/^[0-9]/.test(input.charAt(pos.offset))) {2232 result2 = input.charAt(pos.offset);2233 advance(pos, 1);2234 } else {2235 result2 = null;2236 if (reportFailures === 0) {2237 matchFailed("[0-9]");2238 }2239 }2240 if (result2 !== null) {2241 result1 = [];2242 while (result2 !== null) {2243 result1.push(result2);2244 if (/^[0-9]/.test(input.charAt(pos.offset))) {2245 result2 = input.charAt(pos.offset);2246 advance(pos, 1);2247 } else {2248 result2 = null;2249 if (reportFailures === 0) {2250 matchFailed("[0-9]");2251 }2252 }2253 }2254 } else {2255 result1 = null;2256 }2257 if (result1 !== null) {2258 result1 = (function(offset, line, column, n) {return n.join('')})(pos4.offset, pos4.line, pos4.column, result1);2259 }2260 if (result1 === null) {2261 pos = clone(pos4);2262 }2263 if (result1 === null) {2264 result1 = parse_identifier();2265 }2266 if (result1 !== null) {2267 result2 = parse_rb();2268 if (result2 !== null) {2269 result0 = [result0, result1, result2];2270 } else {2271 result0 = null;2272 pos = clone(pos3);2273 }2274 } else {2275 result0 = null;2276 pos = clone(pos3);2277 }2278 } else {2279 result0 = null;2280 pos = clone(pos3);2281 }2282 if (result0 !== null) {2283 result0 = (function(offset, line, column, a) {return a; })(pos2.offset, pos2.line, pos2.column, result0[1]);2284 }2285 if (result0 === null) {2286 pos = clone(pos2);2287 }2288 if (result0 !== null) {2289 result1 = parse_array_part();2290 result1 = result1 !== null ? result1 : "";2291 if (result1 !== null) {2292 result0 = [result0, result1];2293 } else {2294 result0 = null;2295 pos = clone(pos1);2296 }2297 } else {2298 result0 = null;2299 pos = clone(pos1);2300 }2301 if (result0 !== null) {2302 result0 = (function(offset, line, column, i, nk) { if(nk) { nk.unshift(i); } else {nk = [i] } return nk; })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2303 }2304 if (result0 === null) {2305 pos = clone(pos0);2306 }2307 reportFailures--;2308 if (reportFailures === 0 && result0 === null) {2309 matchFailed("array");2310 }2311 return result0;2312 }2313 2314 function parse_array_part() {2315 var result0, result1, result2;2316 var pos0, pos1, pos2, pos3;2317 2318 reportFailures++;2319 pos0 = clone(pos);2320 pos1 = clone(pos);2321 pos2 = clone(pos);2322 pos3 = clone(pos);2323 if (input.charCodeAt(pos.offset) === 46) {2324 result1 = ".";2325 advance(pos, 1);2326 } else {2327 result1 = null;2328 if (reportFailures === 0) {2329 matchFailed("\".\"");2330 }2331 }2332 if (result1 !== null) {2333 result2 = parse_key();2334 if (result2 !== null) {2335 result1 = [result1, result2];2336 } else {2337 result1 = null;2338 pos = clone(pos3);2339 }2340 } else {2341 result1 = null;2342 pos = clone(pos3);2343 }2344 if (result1 !== null) {2345 result1 = (function(offset, line, column, k) {return k})(pos2.offset, pos2.line, pos2.column, result1[1]);2346 }2347 if (result1 === null) {2348 pos = clone(pos2);2349 }2350 if (result1 !== null) {2351 result0 = [];2352 while (result1 !== null) {2353 result0.push(result1);2354 pos2 = clone(pos);2355 pos3 = clone(pos);2356 if (input.charCodeAt(pos.offset) === 46) {2357 result1 = ".";2358 advance(pos, 1);2359 } else {2360 result1 = null;2361 if (reportFailures === 0) {2362 matchFailed("\".\"");2363 }2364 }2365 if (result1 !== null) {2366 result2 = parse_key();2367 if (result2 !== null) {2368 result1 = [result1, result2];2369 } else {2370 result1 = null;2371 pos = clone(pos3);2372 }2373 } else {2374 result1 = null;2375 pos = clone(pos3);2376 }2377 if (result1 !== null) {2378 result1 = (function(offset, line, column, k) {return k})(pos2.offset, pos2.line, pos2.column, result1[1]);2379 }2380 if (result1 === null) {2381 pos = clone(pos2);2382 }2383 }2384 } else {2385 result0 = null;2386 }2387 if (result0 !== null) {2388 result1 = parse_array();2389 result1 = result1 !== null ? result1 : "";2390 if (result1 !== null) {2391 result0 = [result0, result1];2392 } else {2393 result0 = null;2394 pos = clone(pos1);2395 }2396 } else {2397 result0 = null;2398 pos = clone(pos1);2399 }2400 if (result0 !== null) {2401 result0 = (function(offset, line, column, d, a) { if (a) { return d.concat(a); } else { return d; } })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2402 }2403 if (result0 === null) {2404 pos = clone(pos0);2405 }2406 reportFailures--;2407 if (reportFailures === 0 && result0 === null) {2408 matchFailed("array_part");2409 }2410 return result0;2411 }2412 2413 function parse_inline() {2414 var result0, result1, result2;2415 var pos0, pos1;2416 2417 reportFailures++;2418 pos0 = clone(pos);2419 pos1 = clone(pos);2420 if (input.charCodeAt(pos.offset) === 34) {2421 result0 = "\"";2422 advance(pos, 1);2423 } else {2424 result0 = null;2425 if (reportFailures === 0) {2426 matchFailed("\"\\\"\"");2427 }2428 }2429 if (result0 !== null) {2430 if (input.charCodeAt(pos.offset) === 34) {2431 result1 = "\"";2432 advance(pos, 1);2433 } else {2434 result1 = null;2435 if (reportFailures === 0) {2436 matchFailed("\"\\\"\"");2437 }2438 }2439 if (result1 !== null) {2440 result0 = [result0, result1];2441 } else {2442 result0 = null;2443 pos = clone(pos1);2444 }2445 } else {2446 result0 = null;2447 pos = clone(pos1);2448 }2449 if (result0 !== null) {2450 result0 = (function(offset, line, column) { return ["literal", ""] })(pos0.offset, pos0.line, pos0.column);2451 }2452 if (result0 === null) {2453 pos = clone(pos0);2454 }2455 if (result0 === null) {2456 pos0 = clone(pos);2457 pos1 = clone(pos);2458 if (input.charCodeAt(pos.offset) === 34) {2459 result0 = "\"";2460 advance(pos, 1);2461 } else {2462 result0 = null;2463 if (reportFailures === 0) {2464 matchFailed("\"\\\"\"");2465 }2466 }2467 if (result0 !== null) {2468 result1 = parse_literal();2469 if (result1 !== null) {2470 if (input.charCodeAt(pos.offset) === 34) {2471 result2 = "\"";2472 advance(pos, 1);2473 } else {2474 result2 = null;2475 if (reportFailures === 0) {2476 matchFailed("\"\\\"\"");2477 }2478 }2479 if (result2 !== null) {2480 result0 = [result0, result1, result2];2481 } else {2482 result0 = null;2483 pos = clone(pos1);2484 }2485 } else {2486 result0 = null;2487 pos = clone(pos1);2488 }2489 } else {2490 result0 = null;2491 pos = clone(pos1);2492 }2493 if (result0 !== null) {2494 result0 = (function(offset, line, column, l) { return ["literal", l] })(pos0.offset, pos0.line, pos0.column, result0[1]);2495 }2496 if (result0 === null) {2497 pos = clone(pos0);2498 }2499 if (result0 === null) {2500 pos0 = clone(pos);2501 pos1 = clone(pos);2502 if (input.charCodeAt(pos.offset) === 34) {2503 result0 = "\"";2504 advance(pos, 1);2505 } else {2506 result0 = null;2507 if (reportFailures === 0) {2508 matchFailed("\"\\\"\"");2509 }2510 }2511 if (result0 !== null) {2512 result2 = parse_inline_part();2513 if (result2 !== null) {2514 result1 = [];2515 while (result2 !== null) {2516 result1.push(result2);2517 result2 = parse_inline_part();2518 }2519 } else {2520 result1 = null;2521 }2522 if (result1 !== null) {2523 if (input.charCodeAt(pos.offset) === 34) {2524 result2 = "\"";2525 advance(pos, 1);2526 } else {2527 result2 = null;2528 if (reportFailures === 0) {2529 matchFailed("\"\\\"\"");2530 }2531 }2532 if (result2 !== null) {2533 result0 = [result0, result1, result2];2534 } else {2535 result0 = null;2536 pos = clone(pos1);2537 }2538 } else {2539 result0 = null;2540 pos = clone(pos1);2541 }2542 } else {2543 result0 = null;2544 pos = clone(pos1);2545 }2546 if (result0 !== null) {2547 result0 = (function(offset, line, column, p) { return ["body"].concat(p) })(pos0.offset, pos0.line, pos0.column, result0[1]);2548 }2549 if (result0 === null) {2550 pos = clone(pos0);2551 }2552 }2553 }2554 reportFailures--;2555 if (reportFailures === 0 && result0 === null) {2556 matchFailed("inline");2557 }2558 return result0;2559 }2560 2561 function parse_inline_part() {2562 var result0;2563 var pos0;2564 2565 result0 = parse_special();2566 if (result0 === null) {2567 result0 = parse_reference();2568 if (result0 === null) {2569 pos0 = clone(pos);2570 result0 = parse_literal();2571 if (result0 !== null) {2572 result0 = (function(offset, line, column, l) { return ["buffer", l] })(pos0.offset, pos0.line, pos0.column, result0);2573 }2574 if (result0 === null) {2575 pos = clone(pos0);2576 }2577 }2578 }2579 return result0;2580 }2581 2582 function parse_buffer() {2583 var result0, result1, result2, result3, result4;2584 var pos0, pos1, pos2, pos3;2585 2586 reportFailures++;2587 pos0 = clone(pos);2588 pos1 = clone(pos);2589 result0 = parse_eol();2590 if (result0 !== null) {2591 result1 = [];2592 result2 = parse_ws();2593 while (result2 !== null) {2594 result1.push(result2);2595 result2 = parse_ws();2596 }2597 if (result1 !== null) {2598 result0 = [result0, result1];2599 } else {2600 result0 = null;2601 pos = clone(pos1);2602 }2603 } else {2604 result0 = null;2605 pos = clone(pos1);2606 }2607 if (result0 !== null) {2608 result0 = (function(offset, line, column, e, w) { return ["format", e, w.join('')] })(pos0.offset, pos0.line, pos0.column, result0[0], result0[1]);2609 }2610 if (result0 === null) {2611 pos = clone(pos0);2612 }2613 if (result0 === null) {2614 pos0 = clone(pos);2615 pos1 = clone(pos);2616 pos2 = clone(pos);2617 pos3 = clone(pos);2618 reportFailures++;2619 result1 = parse_tag();2620 reportFailures--;2621 if (result1 === null) {2622 result1 = "";2623 } else {2624 result1 = null;2625 pos = clone(pos3);2626 }2627 if (result1 !== null) {2628 pos3 = clone(pos);2629 reportFailures++;2630 result2 = parse_comment();2631 reportFailures--;2632 if (result2 === null) {2633 result2 = "";2634 } else {2635 result2 = null;2636 pos = clone(pos3);2637 }2638 if (result2 !== null) {2639 pos3 = clone(pos);2640 reportFailures++;2641 result3 = parse_eol();2642 reportFailures--;2643 if (result3 === null) {2644 result3 = "";2645 } else {2646 result3 = null;2647 pos = clone(pos3);2648 }2649 if (result3 !== null) {2650 if (input.length > pos.offset) {2651 result4 = input.charAt(pos.offset);2652 advance(pos, 1);2653 } else {2654 result4 = null;2655 if (reportFailures === 0) {2656 matchFailed("any character");2657 }2658 }2659 if (result4 !== null) {2660 result1 = [result1, result2, result3, result4];2661 } else {2662 result1 = null;2663 pos = clone(pos2);2664 }2665 } else {2666 result1 = null;2667 pos = clone(pos2);2668 }2669 } else {2670 result1 = null;2671 pos = clone(pos2);2672 }2673 } else {2674 result1 = null;2675 pos = clone(pos2);2676 }2677 if (result1 !== null) {2678 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[3]);2679 }2680 if (result1 === null) {2681 pos = clone(pos1);2682 }2683 if (result1 !== null) {2684 result0 = [];2685 while (result1 !== null) {2686 result0.push(result1);2687 pos1 = clone(pos);2688 pos2 = clone(pos);2689 pos3 = clone(pos);2690 reportFailures++;2691 result1 = parse_tag();2692 reportFailures--;2693 if (result1 === null) {2694 result1 = "";2695 } else {2696 result1 = null;2697 pos = clone(pos3);2698 }2699 if (result1 !== null) {2700 pos3 = clone(pos);2701 reportFailures++;2702 result2 = parse_comment();2703 reportFailures--;2704 if (result2 === null) {2705 result2 = "";2706 } else {2707 result2 = null;2708 pos = clone(pos3);2709 }2710 if (result2 !== null) {2711 pos3 = clone(pos);2712 reportFailures++;2713 result3 = parse_eol();2714 reportFailures--;2715 if (result3 === null) {2716 result3 = "";2717 } else {2718 result3 = null;2719 pos = clone(pos3);2720 }2721 if (result3 !== null) {2722 if (input.length > pos.offset) {2723 result4 = input.charAt(pos.offset);2724 advance(pos, 1);2725 } else {2726 result4 = null;2727 if (reportFailures === 0) {2728 matchFailed("any character");2729 }2730 }2731 if (result4 !== null) {2732 result1 = [result1, result2, result3, result4];2733 } else {2734 result1 = null;2735 pos = clone(pos2);2736 }2737 } else {2738 result1 = null;2739 pos = clone(pos2);2740 }2741 } else {2742 result1 = null;2743 pos = clone(pos2);2744 }2745 } else {2746 result1 = null;2747 pos = clone(pos2);2748 }2749 if (result1 !== null) {2750 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[3]);2751 }2752 if (result1 === null) {2753 pos = clone(pos1);2754 }2755 }2756 } else {2757 result0 = null;2758 }2759 if (result0 !== null) {2760 result0 = (function(offset, line, column, b) { return ["buffer", b.join('')] })(pos0.offset, pos0.line, pos0.column, result0);2761 }2762 if (result0 === null) {2763 pos = clone(pos0);2764 }2765 }2766 reportFailures--;2767 if (reportFailures === 0 && result0 === null) {2768 matchFailed("buffer");2769 }2770 return result0;2771 }2772 2773 function parse_literal() {2774 var result0, result1, result2;2775 var pos0, pos1, pos2, pos3;2776 2777 reportFailures++;2778 pos0 = clone(pos);2779 pos1 = clone(pos);2780 pos2 = clone(pos);2781 pos3 = clone(pos);2782 reportFailures++;2783 result1 = parse_tag();2784 reportFailures--;2785 if (result1 === null) {2786 result1 = "";2787 } else {2788 result1 = null;2789 pos = clone(pos3);2790 }2791 if (result1 !== null) {2792 result2 = parse_esc();2793 if (result2 === null) {2794 if (/^[^"]/.test(input.charAt(pos.offset))) {2795 result2 = input.charAt(pos.offset);2796 advance(pos, 1);2797 } else {2798 result2 = null;2799 if (reportFailures === 0) {2800 matchFailed("[^\"]");2801 }2802 }2803 }2804 if (result2 !== null) {2805 result1 = [result1, result2];2806 } else {2807 result1 = null;2808 pos = clone(pos2);2809 }2810 } else {2811 result1 = null;2812 pos = clone(pos2);2813 }2814 if (result1 !== null) {2815 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[1]);2816 }2817 if (result1 === null) {2818 pos = clone(pos1);2819 }2820 if (result1 !== null) {2821 result0 = [];2822 while (result1 !== null) {2823 result0.push(result1);2824 pos1 = clone(pos);2825 pos2 = clone(pos);2826 pos3 = clone(pos);2827 reportFailures++;2828 result1 = parse_tag();2829 reportFailures--;2830 if (result1 === null) {2831 result1 = "";2832 } else {2833 result1 = null;2834 pos = clone(pos3);2835 }2836 if (result1 !== null) {2837 result2 = parse_esc();2838 if (result2 === null) {2839 if (/^[^"]/.test(input.charAt(pos.offset))) {2840 result2 = input.charAt(pos.offset);2841 advance(pos, 1);2842 } else {2843 result2 = null;2844 if (reportFailures === 0) {2845 matchFailed("[^\"]");2846 }2847 }2848 }2849 if (result2 !== null) {2850 result1 = [result1, result2];2851 } else {2852 result1 = null;2853 pos = clone(pos2);2854 }2855 } else {2856 result1 = null;2857 pos = clone(pos2);2858 }2859 if (result1 !== null) {2860 result1 = (function(offset, line, column, c) {return c})(pos1.offset, pos1.line, pos1.column, result1[1]);2861 }2862 if (result1 === null) {2863 pos = clone(pos1);2864 }2865 }2866 } else {2867 result0 = null;2868 }2869 if (result0 !== null) {2870 result0 = (function(offset, line, column, b) { return b.join('') })(pos0.offset, pos0.line, pos0.column, result0);2871 }2872 if (result0 === null) {2873 pos = clone(pos0);2874 }2875 reportFailures--;2876 if (reportFailures === 0 && result0 === null) {2877 matchFailed("literal");2878 }2879 return result0;2880 }2881 2882 function parse_esc() {2883 var result0;2884 var pos0;2885 2886 pos0 = clone(pos);2887 if (input.substr(pos.offset, 2) === "\\\"") {2888 result0 = "\\\"";2889 advance(pos, 2);2890 } else {2891 result0 = null;2892 if (reportFailures === 0) {2893 matchFailed("\"\\\\\\\"\"");2894 }2895 }2896 if (result0 !== null) {2897 result0 = (function(offset, line, column) { return '"' })(pos0.offset, pos0.line, pos0.column);2898 }2899 if (result0 === null) {2900 pos = clone(pos0);2901 }2902 return result0;2903 }2904 2905 function parse_comment() {2906 var result0, result1, result2, result3;2907 var pos0, pos1, pos2, pos3, pos4;2908 2909 reportFailures++;2910 pos0 = clone(pos);2911 pos1 = clone(pos);2912 if (input.substr(pos.offset, 2) === "{!") {2913 result0 = "{!";2914 advance(pos, 2);2915 } else {2916 result0 = null;2917 if (reportFailures === 0) {2918 matchFailed("\"{!\"");2919 }2920 }2921 if (result0 !== null) {2922 result1 = [];2923 pos2 = clone(pos);2924 pos3 = clone(pos);2925 pos4 = clone(pos);2926 reportFailures++;2927 if (input.substr(pos.offset, 2) === "!}") {2928 result2 = "!}";2929 advance(pos, 2);2930 } else {2931 result2 = null;2932 if (reportFailures === 0) {2933 matchFailed("\"!}\"");2934 }2935 }2936 reportFailures--;2937 if (result2 === null) {2938 result2 = "";2939 } else {2940 result2 = null;2941 pos = clone(pos4);2942 }2943 if (result2 !== null) {2944 if (input.length > pos.offset) {2945 result3 = input.charAt(pos.offset);2946 advance(pos, 1);2947 } else {2948 result3 = null;2949 if (reportFailures === 0) {2950 matchFailed("any character");2951 }2952 }2953 if (result3 !== null) {2954 result2 = [result2, result3];2955 } else {2956 result2 = null;2957 pos = clone(pos3);2958 }2959 } else {2960 result2 = null;2961 pos = clone(pos3);2962 }2963 if (result2 !== null) {2964 result2 = (function(offset, line, column, c) {return c})(pos2.offset, pos2.line, pos2.column, result2[1]);2965 }2966 if (result2 === null) {2967 pos = clone(pos2);2968 }2969 while (result2 !== null) {2970 result1.push(result2);2971 pos2 = clone(pos);2972 pos3 = clone(pos);2973 pos4 = clone(pos);2974 reportFailures++;2975 if (input.substr(pos.offset, 2) === "!}") {2976 result2 = "!}";2977 advance(pos, 2);2978 } else {2979 result2 = null;2980 if (reportFailures === 0) {2981 matchFailed("\"!}\"");2982 }2983 }2984 reportFailures--;2985 if (result2 === null) {2986 result2 = "";2987 } else {2988 result2 = null;2989 pos = clone(pos4);2990 }2991 if (result2 !== null) {2992 if (input.length > pos.offset) {2993 result3 = input.charAt(pos.offset);2994 advance(pos, 1);2995 } else {2996 result3 = null;2997 if (reportFailures === 0) {2998 matchFailed("any character");2999 }3000 }3001 if (result3 !== null) {3002 result2 = [result2, result3];3003 } else {3004 result2 = null;3005 pos = clone(pos3);3006 }3007 } else {3008 result2 = null;3009 pos = clone(pos3);3010 }3011 if (result2 !== null) {3012 result2 = (function(offset, line, column, c) {return c})(pos2.offset, pos2.line, pos2.column, result2[1]);3013 }3014 if (result2 === null) {3015 pos = clone(pos2);3016 }3017 }3018 if (result1 !== null) {3019 if (input.substr(pos.offset, 2) === "!}") {3020 result2 = "!}";3021 advance(pos, 2);3022 } else {3023 result2 = null;3024 if (reportFailures === 0) {3025 matchFailed("\"!}\"");3026 }3027 }3028 if (result2 !== null) {3029 result0 = [result0, result1, result2];3030 } else {3031 result0 = null;3032 pos = clone(pos1);3033 }3034 } else {3035 result0 = null;3036 pos = clone(pos1);3037 }3038 } else {3039 result0 = null;3040 pos = clone(pos1);3041 }3042 if (result0 !== null) {3043 result0 = (function(offset, line, column, c) { return ["comment", c.join('')] })(pos0.offset, pos0.line, pos0.column, result0[1]);3044 }3045 if (result0 === null) {3046 pos = clone(pos0);3047 }3048 reportFailures--;3049 if (reportFailures === 0 && result0 === null) {3050 matchFailed("comment");3051 }3052 return result0;3053 }3054 3055 function parse_tag() {3056 var result0, result1, result2, result3, result4, result5, result6, result7;3057 var pos0, pos1, pos2;3058 3059 pos0 = clone(pos);3060 result0 = parse_ld();3061 if (result0 !== null) {3062 result1 = [];3063 result2 = parse_ws();3064 while (result2 !== null) {3065 result1.push(result2);3066 result2 = parse_ws();3067 }3068 if (result1 !== null) {3069 if (/^[#?^><+%:@\/~%]/.test(input.charAt(pos.offset))) {3070 result2 = input.charAt(pos.offset);3071 advance(pos, 1);3072 } else {3073 result2 = null;3074 if (reportFailures === 0) {3075 matchFailed("[#?^><+%:@\\/~%]");3076 }3077 }3078 if (result2 !== null) {3079 result3 = [];3080 result4 = parse_ws();3081 while (result4 !== null) {3082 result3.push(result4);3083 result4 = parse_ws();3084 }3085 if (result3 !== null) {3086 pos1 = clone(pos);3087 pos2 = clone(pos);3088 reportFailures++;3089 result5 = parse_rd();3090 reportFailures--;3091 if (result5 === null) {3092 result5 = "";3093 } else {3094 result5 = null;3095 pos = clone(pos2);3096 }3097 if (result5 !== null) {3098 pos2 = clone(pos);3099 reportFailures++;3100 result6 = parse_eol();3101 reportFailures--;3102 if (result6 === null) {3103 result6 = "";3104 } else {3105 result6 = null;3106 pos = clone(pos2);3107 }3108 if (result6 !== null) {3109 if (input.length > pos.offset) {3110 result7 = input.charAt(pos.offset);3111 advance(pos, 1);3112 } else {3113 result7 = null;3114 if (reportFailures === 0) {3115 matchFailed("any character");3116 }3117 }3118 if (result7 !== null) {3119 result5 = [result5, result6, result7];3120 } else {3121 result5 = null;3122 pos = clone(pos1);3123 }3124 } else {3125 result5 = null;3126 pos = clone(pos1);3127 }3128 } else {3129 result5 = null;3130 pos = clone(pos1);3131 }3132 if (result5 !== null) {3133 result4 = [];3134 while (result5 !== null) {3135 result4.push(result5);3136 pos1 = clone(pos);3137 pos2 = clone(pos);3138 reportFailures++;3139 result5 = parse_rd();3140 reportFailures--;3141 if (result5 === null) {3142 result5 = "";3143 } else {3144 result5 = null;3145 pos = clone(pos2);3146 }3147 if (result5 !== null) {3148 pos2 = clone(pos);3149 reportFailures++;3150 result6 = parse_eol();3151 reportFailures--;3152 if (result6 === null) {3153 result6 = "";3154 } else {3155 result6 = null;3156 pos = clone(pos2);3157 }3158 if (result6 !== null) {3159 if (input.length > pos.offset) {3160 result7 = input.charAt(pos.offset);3161 advance(pos, 1);3162 } else {3163 result7 = null;3164 if (reportFailures === 0) {3165 matchFailed("any character");3166 }3167 }3168 if (result7 !== null) {3169 result5 = [result5, result6, result7];3170 } else {3171 result5 = null;3172 pos = clone(pos1);3173 }3174 } else {3175 result5 = null;3176 pos = clone(pos1);3177 }3178 } else {3179 result5 = null;3180 pos = clone(pos1);3181 }3182 }3183 } else {3184 result4 = null;3185 }3186 if (result4 !== null) {3187 result5 = [];3188 result6 = parse_ws();3189 while (result6 !== null) {3190 result5.push(result6);3191 result6 = parse_ws();3192 }3193 if (result5 !== null) {3194 result6 = parse_rd();3195 if (result6 !== null) {3196 result0 = [result0, result1, result2, result3, result4, result5, result6];3197 } else {3198 result0 = null;3199 pos = clone(pos0);3200 }3201 } else {3202 result0 = null;3203 pos = clone(pos0);3204 }3205 } else {3206 result0 = null;3207 pos = clone(pos0);3208 }3209 } else {3210 result0 = null;3211 pos = clone(pos0);3212 }3213 } else {3214 result0 = null;3215 pos = clone(pos0);3216 }3217 } else {3218 result0 = null;3219 pos = clone(pos0);3220 }3221 } else {3222 result0 = null;3223 pos = clone(pos0);3224 }3225 if (result0 === null) {3226 result0 = parse_reference();3227 }3228 return result0;3229 }3230 3231 function parse_ld() {3232 var result0;3233 3234 if (input.charCodeAt(pos.offset) === 123) {3235 result0 = "{";3236 advance(pos, 1);3237 } else {3238 result0 = null;3239 if (reportFailures === 0) {3240 matchFailed("\"{\"");3241 }3242 }3243 return result0;3244 }3245 3246 function parse_rd() {3247 var result0;3248 3249 if (input.charCodeAt(pos.offset) === 125) {3250 result0 = "}";3251 advance(pos, 1);3252 } else {3253 result0 = null;3254 if (reportFailures === 0) {3255 matchFailed("\"}\"");3256 }3257 }3258 return result0;3259 }3260 3261 function parse_lb() {3262 var result0;3263 3264 if (input.charCodeAt(pos.offset) === 91) {3265 result0 = "[";3266 advance(pos, 1);3267 } else {3268 result0 = null;3269 if (reportFailures === 0) {3270 matchFailed("\"[\"");3271 }3272 }3273 return result0;3274 }3275 3276 function parse_rb() {3277 var result0;3278 3279 if (input.charCodeAt(pos.offset) === 93) {3280 result0 = "]";3281 advance(pos, 1);3282 } else {3283 result0 = null;3284 if (reportFailures === 0) {3285 matchFailed("\"]\"");3286 }3287 }3288 return result0;3289 }3290 3291 function parse_eol() {3292 var result0;3293 3294 if (input.charCodeAt(pos.offset) === 10) {3295 result0 = "\n";3296 advance(pos, 1);3297 } else {3298 result0 = null;3299 if (reportFailures === 0) {3300 matchFailed("\"\\n\"");3301 }3302 }3303 if (result0 === null) {3304 if (input.substr(pos.offset, 2) === "\r\n") {3305 result0 = "\r\n";3306 advance(pos, 2);3307 } else {3308 result0 = null;3309 if (reportFailures === 0) {3310 matchFailed("\"\\r\\n\"");3311 }3312 }3313 if (result0 === null) {3314 if (input.charCodeAt(pos.offset) === 13) {3315 result0 = "\r";3316 advance(pos, 1);3317 } else {3318 result0 = null;3319 if (reportFailures === 0) {3320 matchFailed("\"\\r\"");3321 }3322 }3323 if (result0 === null) {3324 if (input.charCodeAt(pos.offset) === 8232) {3325 result0 = "\u2028";3326 advance(pos, 1);3327 } else {3328 result0 = null;3329 if (reportFailures === 0) {3330 matchFailed("\"\\u2028\"");3331 }3332 }3333 if (result0 === null) {3334 if (input.charCodeAt(pos.offset) === 8233) {3335 result0 = "\u2029";3336 advance(pos, 1);3337 } else {3338 result0 = null;3339 if (reportFailures === 0) {3340 matchFailed("\"\\u2029\"");3341 }3342 }3343 }3344 }3345 }3346 }3347 return result0;3348 }3349 3350 function parse_ws() {3351 var result0;3352 3353 if (/^[\t\x0B\f \xA0\uFEFF]/.test(input.charAt(pos.offset))) {3354 result0 = input.charAt(pos.offset);3355 advance(pos, 1);3356 } else {3357 result0 = null;3358 if (reportFailures === 0) {3359 matchFailed("[\\t\\x0B\\f \\xA0\\uFEFF]");3360 }3361 }3362 if (result0 === null) {3363 result0 = parse_eol();3364 }3365 return result0;3366 }3367 3368 3369 function cleanupExpected(expected) {3370 expected.sort();3371 3372 var lastExpected = null;3373 var cleanExpected = [];3374 for (var i = 0; i < expected.length; i++) {3375 if (expected[i] !== lastExpected) {3376 cleanExpected.push(expected[i]);3377 lastExpected = expected[i];3378 }3379 }3380 return cleanExpected;3381 }3382 3383 3384 3385 var result = parseFunctions[startRule]();3386 3387 /*3388 * The parser is now in one of the following three states:3389 *3390 * 1. The parser successfully parsed the whole input.3391 *3392 * - |result !== null|3393 * - |pos.offset === input.length|3394 * - |rightmostFailuresExpected| may or may not contain something3395 *3396 * 2. The parser successfully parsed only a part of the input.3397 *3398 * - |result !== null|3399 * - |pos.offset < input.length|3400 * - |rightmostFailuresExpected| may or may not contain something3401 *3402 * 3. The parser did not successfully parse any part of the input.3403 *3404 * - |result === null|3405 * - |pos.offset === 0|3406 * - |rightmostFailuresExpected| contains at least one failure3407 *3408 * All code following this comment (including called functions) must3409 * handle these states.3410 */3411 if (result === null || pos.offset !== input.length) {3412 var offset = Math.max(pos.offset, rightmostFailuresPos.offset);3413 var found = offset < input.length ? input.charAt(offset) : null;3414 var errorPosition = pos.offset > rightmostFailuresPos.offset ? pos : rightmostFailuresPos;3415 3416 throw new parser.SyntaxError(3417 cleanupExpected(rightmostFailuresExpected),3418 found,3419 offset,3420 errorPosition.line,3421 errorPosition.column3422 );3423 }3424 3425 return result;3426 },3427 3428 /* Returns the parser source code. */3429 toSource: function() { return this._source; }3430 };3431 3432 /* Thrown when a parser encounters a syntax error. */3433 3434 result.SyntaxError = function(expected, found, offset, line, column) {3435 function buildMessage(expected, found) {3436 var expectedHumanized, foundHumanized;3437 3438 switch (expected.length) {3439 case 0:3440 expectedHumanized = "end of input";3441 break;3442 case 1:3443 expectedHumanized = expected[0];3444 break;3445 default:3446 expectedHumanized = expected.slice(0, expected.length - 1).join(", ")3447 + " or "3448 + expected[expected.length - 1];3449 }3450 3451 foundHumanized = found ? quote(found) : "end of input";3452 3453 return "Expected " + expectedHumanized + " but " + foundHumanized + " found.";3454 }3455 3456 this.name = "SyntaxError";3457 this.expected = expected;3458 this.found = found;3459 this.message = buildMessage(expected, found);3460 this.offset = offset;3461 this.line = line;3462 this.column = column;3463 };3464 3465 result.SyntaxError.prototype = Error.prototype;3466 3467 return result;3468})();3469dust.parse = parser.parse;...
tests.js
Source:tests.js
1const {2 printResult,3 setLabel,4 getLabel,5 run,6 andThen,7 orElse,8 choice,9 mapP,10 returnP,11 applyP,12 lift2,13 sequence,14 many,15 many1,16 pint,17 opt,18 andThen1,19 andThen2,20 between,21 sepBy1,22 sepBy,23 bindP,24 pchar,25 anyOf,26 charListToStr,27 manyChars,28 manyChars1,29 pstring,30 spaces,31 spaces1,32 pfloat,33} = require("../src/basic-parser");34const {35 None,36 Some,37 Success,38 Failure,39 Parser,40 ParserPosition,41 InputState,42} = require("../src/types");43const { expect } = require("chai");44const { curry } = require("../src/helpers");45describe("Tests for basic parsers", () => {46 it("printResult works", () => {47 const s = Success.of(5);48 const result1 = printResult(s);49 expect(result1).to.equal(5);50 const exampleError = Failure.of(51 "identifier",52 "unexpected |",53 ParserPosition.of("123 ab|cd", 1, 6)54 );55 const result2 = printResult(exampleError);56 expect(result2).to.equal(57 "Line:1 Col:6 Error parsing identifier\n123 ab|cd\n ^unexpected |"58 );59 });60 it("pchar works", () => {61 const parseA = pchar("A");62 const result1 = run(parseA)("ABC");63 const result2 = run(parseA)("ZBC");64 const result3 = run(parseA)("");65 expect(result1).to.be.instanceOf(Success);66 expect(result1.val[0]).to.equal("A");67 expect(result1.val[1]).to.be.instanceOf(InputState);68 expect(result2).to.be.instanceOf(Failure);69 expect(result2.val.slice(0, 2)).to.eql(["A", "Unexpected 'Z'"]);70 expect(printResult(result2)).to.equal(71 "Line:0 Col:0 Error parsing A\nZBC\n^Unexpected 'Z'"72 );73 expect(result3).to.be.instanceOf(Failure);74 expect(result3.val.slice(0, 2)).to.eql(["A", "No more input"]);75 });76 it("andThen works", () => {77 const parseA = pchar("A");78 const parseB = pchar("B");79 const parseAThenB = andThen(parseA)(parseB);80 const result1 = run(parseAThenB)("ABC");81 const result2 = run(parseAThenB)("ZBC");82 const result3 = run(parseAThenB)("AZC");83 expect(result1).to.be.instanceOf(Success);84 expect(result1.val[0]).to.eql(["A", "B"]);85 expect(result2).to.be.instanceOf(Failure);86 expect(result2.val.slice(0, 2)).to.eql(["A andThen B", "Unexpected 'Z'"]);87 expect(result3).to.be.instanceOf(Failure);88 expect(result3.val.slice(0, 2)).to.eql(["A andThen B", "Unexpected 'Z'"]);89 });90 it("infix andThen works", () => {91 const parseA = pchar("A");92 const parseB = pchar("B");93 const parseAThenB = parseA.andThen(parseB);94 const result1 = run(parseAThenB)("ABC");95 const result2 = run(parseAThenB)("ZBC");96 const result3 = run(parseAThenB)("AZC");97 expect(result1).to.be.instanceOf(Success);98 expect(result1.val[0]).to.eql(["A", "B"]);99 expect(result2).to.be.instanceOf(Failure);100 expect(result2.val.slice(0, 2)).to.eql(["A andThen B", "Unexpected 'Z'"]);101 expect(result3).to.be.instanceOf(Failure);102 expect(result3.val.slice(0, 2)).to.eql(["A andThen B", "Unexpected 'Z'"]);103 });104 it("orElse works", () => {105 const parseA = pchar("A");106 const parseB = pchar("B");107 const parseAOrElseB = orElse(parseA, parseB);108 const result1 = run(parseAOrElseB)("AZZ");109 const result2 = run(parseAOrElseB)("BZZ");110 const result3 = run(parseAOrElseB)("CZZ");111 expect(result1).to.be.instanceOf(Success);112 expect(result1.val[0]).to.eql("A");113 expect(result2).to.be.instanceOf(Success);114 expect(result2.val[0]).to.eql("B");115 expect(result3).to.be.instanceOf(Failure);116 expect(result3.val.slice(0, 2)).to.eql(["B", "Unexpected 'C'"]);117 });118 it("infix orElse works", () => {119 const parseA = pchar("A");120 const parseB = pchar("B");121 const parseAOrElseB = parseA.orElse(parseB);122 const result1 = run(parseAOrElseB)("AZZ");123 const result2 = run(parseAOrElseB)("BZZ");124 const result3 = run(parseAOrElseB)("CZZ");125 expect(result1).to.be.instanceOf(Success);126 expect(result1.val[0]).to.eql("A");127 expect(result2).to.be.instanceOf(Success);128 expect(result2.val[0]).to.eql("B");129 expect(result3).to.be.instanceOf(Failure);130 expect(result3.val.slice(0, 2)).to.eql(["B", "Unexpected 'C'"]);131 });132 it("choice works", () => {133 const parseA = pchar("A");134 const parseB = pchar("B");135 const parseC = pchar("C");136 const parseABOrC = choice([parseA, parseB, parseC]);137 const result1 = run(parseABOrC)("AZZ");138 const result2 = run(parseABOrC)("BZZ");139 const result3 = run(parseABOrC)("CZZ");140 const result4 = run(parseABOrC)("ZZZ");141 expect(result1).to.be.instanceOf(Success);142 expect(result1.val[0]).to.eql("A");143 expect(result2).to.be.instanceOf(Success);144 expect(result2.val[0]).to.eql("B");145 expect(result3).to.be.instanceOf(Success);146 expect(result3.val[0]).to.eql("C");147 expect(result4).to.be.instanceOf(Failure);148 expect(result4.val.slice(0, 2)).to.eql(["C", "Unexpected 'Z'"]);149 });150 it("anyOf works", () => {151 const parceABOrC = anyOf(["A", "B", "C"]);152 const result1 = run(parceABOrC)("AZZ");153 const result2 = run(parceABOrC)("BZZ");154 const result3 = run(parceABOrC)("CZZ");155 const result4 = run(parceABOrC)("ZZZ");156 expect(result1).to.be.instanceOf(Success);157 expect(result1.val[0]).to.eql("A");158 expect(result2).to.be.instanceOf(Success);159 expect(result2.val[0]).to.eql("B");160 expect(result3).to.be.instanceOf(Success);161 expect(result3.val[0]).to.eql("C");162 expect(result4).to.be.instanceOf(Failure);163 expect(result4.val.slice(0, 2)).to.eql(["any of A,B,C", "Unexpected 'Z'"]);164 });165 it("mapP works", () => {166 const parseDigit = anyOf(["1", "2", "3"]);167 // create a parser that returns a tuple168 const tupleParser = andThen(andThen(parseDigit, parseDigit), parseDigit);169 // create a function that turns the tuple into a string170 const transformTuple = ([[c1, c2], c3]) => "" + c1 + c2 + c3;171 // use "map" to combine them172 const parseThreeDigitsAsStr = mapP(transformTuple, tupleParser);173 const result1 = run(parseThreeDigitsAsStr, "123A");174 const result2 = run(parseThreeDigitsAsStr, "12AA");175 expect(result1).to.be.instanceOf(Success);176 expect(result1.val[0]).to.eql("123");177 expect(result2).to.be.instanceOf(Failure);178 expect(result2.val.slice(0, 2)).to.eql([179 "any of 1,2,3 andThen any of 1,2,3 andThen any of 1,2,3",180 "Unexpected 'A'",181 ]);182 });183 it("pipe in mapP works", () => {184 const parseDigit = anyOf(["1", "2", "3"]);185 // create a parser that returns a tuple186 const tupleParser = andThen(andThen(parseDigit, parseDigit), parseDigit);187 // create a function that turns the tuple into a string188 const transformTuple = ([[c1, c2], c3]) => "" + c1 + c2 + c3;189 // use "map" to combine them190 const parseThreeDigitsAsStr = tupleParser.mapP(transformTuple);191 const result1 = run(parseThreeDigitsAsStr, "123A");192 const result2 = run(parseThreeDigitsAsStr, "12AA");193 expect(result1).to.be.instanceOf(Success);194 expect(result1.val[0]).to.eql("123");195 expect(result2).to.be.instanceOf(Failure);196 expect(result2.val.slice(0, 2)).to.eql([197 "any of 1,2,3 andThen any of 1,2,3 andThen any of 1,2,3",198 "Unexpected 'A'",199 ]);200 });201 it("returnP works", () => {202 const testParser = returnP("A");203 expect(testParser).to.be.instanceOf(Parser);204 const parseString = run(testParser, "BC");205 expect(parseString).to.be.instanceOf(Success);206 expect(parseString.val[0]).to.eql("A");207 });208 it("applyP works", () => {209 const add = (a) => a + 1;210 expect(add(1)).to.equal(2);211 const addP = returnP(add);212 const p1 = returnP(1);213 const result1 = applyP(addP, p1);214 expect(result1).to.be.instanceOf(Parser);215 const result2 = run(result1, "BC");216 expect(result2).to.be.instanceOf(Success);217 expect(result2.val[0]).to.eql(2);218 });219 it("infix applyP works", () => {220 const add = (a) => a + 1;221 expect(add(1)).to.equal(2);222 const addP = returnP(add);223 const p1 = returnP(1);224 const result1 = addP.applyP(p1);225 expect(result1).to.be.instanceOf(Parser);226 const result2 = run(result1, "BC");227 expect(result2).to.be.instanceOf(Success);228 expect(result2.val[0]).to.eql(2);229 });230 it("applyP works; take 2", () => {231 const add = (a) => a + 1;232 expect(add(1)).to.equal(2);233 const addP = returnP(add);234 const p1 = returnP(1);235 const addP1 = applyP(addP);236 const result1 = addP1(p1);237 expect(result1).to.be.instanceOf(Parser);238 const result2 = run(result1, "BC");239 expect(result2).to.be.instanceOf(Success);240 expect(result2.val[0]).to.eql(2);241 });242 it("lift2 works", () => {243 const add = curry((a, b) => a + b);244 expect(add(2, 2)).to.equal(4);245 const liftedAdd = lift2(add);246 const result = liftedAdd(returnP(2), returnP(2));247 expect(result).to.be.instanceOf(Parser);248 const result1 = run(result, "BC");249 expect(result1).to.be.instanceOf(Success);250 expect(result1.val[0]).to.eql(4);251 });252 it("sequence works", () => {253 const parsers = [pchar("A"), pchar("B"), pchar("C")];254 let combined = sequence(parsers);255 const result = run(combined, "ABCD");256 expect(result).to.be.instanceOf(Success);257 expect(result.val[0]).to.eql(["A", "B", "C"]);258 });259 it("pstring works", () => {260 const parseABC = pstring("ABC").setLabel("ABC");261 const result1 = run(parseABC, "ABCDE"); // Success ("ABC", "DE")262 const result2 = run(parseABC, "A|CDE"); // Failure "Expecting 'B'. Got '|'"263 const result3 = run(parseABC, "AB|DE"); // Failure "Expecting 'C'. Got '|'"264 expect(result1).to.be.instanceOf(Success);265 expect(result1.val[0]).to.eql("ABC");266 expect(result2).to.be.instanceOf(Failure);267 expect(result2.val.slice(0, 2)).to.eql(["ABC", "Unexpected '|'"]);268 expect(result3).to.be.instanceOf(Failure);269 expect(result3.val.slice(0, 2)).to.eql(["ABC", "Unexpected '|'"]);270 });271 it("many works", () => {272 const manyA = many(pchar("A"));273 // test some success cases274 const result1 = run(manyA, "ABCD"); // Success (['A'], "BCD")275 const result2 = run(manyA, "AACD"); // Success (['A'; 'A'], "CD")276 const result3 = run(manyA, "AAAD"); // Success (['A'; 'A'; 'A'], "D")277 // test a case with no matches278 const result4 = run(manyA, "|BCD"); // Success ([], "|BCD")279 expect(result1).to.be.instanceOf(Success);280 expect(result1.val[0]).to.eql(["A"]);281 expect(result2).to.be.instanceOf(Success);282 expect(result2.val[0]).to.eql(["A", "A"]);283 expect(result3).to.be.instanceOf(Success);284 expect(result3.val[0]).to.eql(["A", "A", "A"]);285 expect(result4).to.be.instanceOf(Success);286 expect(result4.val[0]).to.eql([]);287 });288 it("many works; take 2", () => {289 const manyAB = many(pstring("AB"));290 const result1 = run(manyAB, "ABCD"); // Success (["AB"], "CD")291 const result2 = run(manyAB, "ABABCD"); // Success (["AB"; "AB"], "CD")292 const result3 = run(manyAB, "ZCD"); // Success ([], "ZCD")293 const result4 = run(manyAB, "AZCD"); // Success ([], "AZCD")294 expect(result1).to.be.instanceOf(Success);295 expect(result1.val[0]).to.eql(["AB"]);296 expect(result2).to.be.instanceOf(Success);297 expect(result2.val[0]).to.eql(["AB", "AB"]);298 expect(result3).to.be.instanceOf(Success);299 expect(result3.val[0]).to.eql([]);300 expect(result4).to.be.instanceOf(Success);301 expect(result4.val[0]).to.eql([]);302 });303 it("many1 works", () => {304 // define parser for one digit305 const digit = anyOf(["1", "2", "3", "4"]);306 // define parser for one or more digits307 const digits = many1(digit);308 const result1 = run(digits, "1ABC"); // Success (['1'], "ABC")309 const result2 = run(digits, "12BC"); // Success (['1'; '2'], "BC")310 const result3 = run(digits, "123C"); // Success (['1'; '2'; '3'], "C")311 const result4 = run(digits, "1234"); // Success (['1'; '2'; '3'; '4'], "")312 const result5 = run(digits, "ABC"); // Failure "Expecting '9'. Got 'A'"313 expect(result1).to.be.instanceOf(Success);314 expect(result1.val[0]).to.eql(["1"]);315 expect(result2).to.be.instanceOf(Success);316 expect(result2.val[0]).to.eql(["1", "2"]);317 expect(result3).to.be.instanceOf(Success);318 expect(result3.val[0]).to.eql(["1", "2", "3"]);319 expect(result4).to.be.instanceOf(Success);320 expect(result4.val[0]).to.eql(["1", "2", "3", "4"]);321 expect(result5).to.be.instanceOf(Failure);322 expect(result5.val.slice(0, 2)).to.eql([323 "any of 1,2,3,4",324 "Unexpected 'A'",325 ]);326 });327 it("pint works", () => {328 const result1 = run(pint, "1ABC"); // Success (1, "ABC")329 const result2 = run(pint, "12BC"); // Success (12, "BC")330 const result3 = run(pint, "123C"); // Success (123, "C")331 const result4 = run(pint, "1234"); // Success (1234, "")332 const result5 = run(pint, "ABC"); // Failure "Expecting '9'. Got 'A'"333 const result6 = run(pint, "-123C"); // Success (123, "C")334 expect(result1).to.be.instanceOf(Success);335 expect(result1.val[0]).to.eql(1);336 expect(result2).to.be.instanceOf(Success);337 expect(result2.val[0]).to.eql(12);338 expect(result3).to.be.instanceOf(Success);339 expect(result3.val[0]).to.eql(123);340 expect(result4).to.be.instanceOf(Success);341 expect(result4.val[0]).to.eql(1234);342 expect(result5).to.be.instanceOf(Failure);343 expect(result5.val.slice(0, 2)).to.eql(["integer", "Unexpected 'A'"]);344 expect(result6).to.be.instanceOf(Success);345 expect(result6.val[0]).to.eql(-123);346 });347 it("opt works", () => {348 const digit = anyOf(["1"]);349 const digitThenSemicolon = andThen(digit, opt(pchar(";")));350 const result1 = run(digitThenSemicolon, "1;"); // Success (('1', Some ';'), "")351 const result2 = run(digitThenSemicolon, "1"); // Success (('1', None), "")352 expect(result1).to.be.instanceOf(Success);353 const [[res1, op1], rem1] = result1.val;354 expect(res1).to.equal("1");355 expect(op1).to.be.instanceOf(Some);356 expect(op1.val).to.equal(";");357 expect(result2).to.be.instanceOf(Success);358 const [[res2, op2], rem2] = result2.val;359 expect(res2).to.equal("1");360 expect(op2).to.be.instanceOf(None);361 });362 it("andThen1 works", () => {363 const digit = anyOf(["1"]);364 // use .>> below365 let digitThenSemicolon = andThen1(digit, opt(pchar(";")));366 const result1 = run(digitThenSemicolon, "1;"); // Success ('1', "")367 const result2 = run(digitThenSemicolon, "1"); // Success ('1', "")368 expect(result1).to.be.instanceOf(Success);369 expect(result1.val[0]).to.eql("1");370 expect(result2).to.be.instanceOf(Success);371 expect(result2.val[0]).to.eql("1");372 });373 it("infix andThen1 works", () => {374 const digit = anyOf(["1"]);375 // use .>> below376 let digitThenSemicolon = digit.andThen1(opt(pchar(";")));377 const result1 = run(digitThenSemicolon, "1;"); // Success ('1', "")378 const result2 = run(digitThenSemicolon, "1"); // Success ('1', "")379 expect(result1).to.be.instanceOf(Success);380 expect(result1.val[0]).to.eql("1");381 expect(result2).to.be.instanceOf(Success);382 expect(result2.val[0]).to.eql("1");383 });384 it("between works", () => {385 const pdoublequote = pchar('"');386 const quotedInteger = between(pdoublequote, pint, pdoublequote);387 const result1 = run(quotedInteger, '"1234"'); // Success (1234, "")388 const result2 = run(quotedInteger, "1234"); // Failure "Expecting '"'. Got '1'"389 expect(result1).to.be.instanceOf(Success);390 expect(result1.val[0]).to.eql(1234);391 expect(result2).to.be.instanceOf(Failure);392 expect(result2.val.slice(0, 2)).to.eql(['between ""', "Unexpected '1'"]);393 });394 it("sepBy1, sepBy work", () => {395 const comma = pchar(",");396 const digit = anyOf(["1", "2", "3"]);397 const zeroOrMoreDigitList = sepBy(digit, comma);398 const oneOrMoreDigitList = sepBy1(digit, comma);399 const result1 = run(oneOrMoreDigitList, "1;"); // Success (['1'], ";")400 const result2 = run(oneOrMoreDigitList, "1,2;"); // Success (['1'; '2'], ";")401 const result3 = run(oneOrMoreDigitList, "1,2,3;"); // Success (['1'; '2'; '3'], ";")402 const result4 = run(oneOrMoreDigitList, "Z;"); // Failure "Expecting '9'. Got 'Z'"403 const result5 = run(zeroOrMoreDigitList, "1;"); // Success (['1'], ";")404 const result6 = run(zeroOrMoreDigitList, "1,2;"); // Success (['1'; '2'], ";")405 const result7 = run(zeroOrMoreDigitList, "1,2,3;"); // Success (['1'; '2'; '3'], ";")406 const result8 = run(zeroOrMoreDigitList, "Z;"); // Success ([], "Z;")407 expect(result1).to.be.instanceOf(Success);408 expect(result1.val[0]).to.eql(["1"]);409 expect(result2).to.be.instanceOf(Success);410 expect(result2.val[0]).to.eql(["1", "2"]);411 expect(result3).to.be.instanceOf(Success);412 expect(result3.val[0]).to.eql(["1", "2", "3"]);413 expect(result4).to.be.instanceOf(Failure);414 expect(result4.val.slice(0, 2)).to.eql([415 "any of 1,2,3 andThen zero or many any of 1,2,3",416 "Unexpected 'Z'",417 ]);418 expect(result5).to.be.instanceOf(Success);419 expect(result5.val[0]).to.eql(["1"]);420 expect(result6).to.be.instanceOf(Success);421 expect(result6.val[0]).to.eql(["1", "2"]);422 expect(result7).to.be.instanceOf(Success);423 expect(result7.val[0]).to.eql(["1", "2", "3"]);424 expect(result8).to.be.instanceOf(Success);425 expect(result8.val[0]).to.eql([]);426 });427 it("bindP works", () => {428 const andThenb = curry((p1, p2) => {429 return bindP(430 (p1Result) => bindP((p2Result) => returnP([p1Result, p2Result]), p2),431 p1432 );433 });434 const parseA = pchar("A");435 const parseB = pchar("B");436 const parseAThenB = andThenb(parseA)(parseB);437 const result1 = run(parseAThenB)("ABC");438 const result2 = run(parseAThenB)("ZBC");439 const result3 = run(parseAThenB)("AZC");440 expect(result1).to.be.instanceOf(Success);441 expect(result1.val[0]).to.eql(["A", "B"]);442 expect(result2).to.be.instanceOf(Failure);443 expect(result2.val.slice(0, 2)).to.eql(["A", "Unexpected 'Z'"]);444 expect(result3).to.be.instanceOf(Failure);445 expect(result3.val.slice(0, 2)).to.eql(["B", "Unexpected 'Z'"]);446 });447 it("infix bindP works", () => {448 const andThenb = curry((p1, p2) =>449 p1.bindP((p1Result) =>450 p2.bindP((p2Result) => returnP([p1Result, p2Result]))451 )452 );453 const parseA = pchar("A");454 const parseB = pchar("B");455 const parseAThenB = andThenb(parseA, parseB);456 const result1 = run(parseAThenB)("ABC");457 const result2 = run(parseAThenB)("ZBC");458 const result3 = run(parseAThenB)("AZC");459 expect(result1).to.be.instanceOf(Success);460 expect(result1.val[0]).to.eql(["A", "B"]);461 expect(result2).to.be.instanceOf(Failure);462 expect(result2.val.slice(0, 2)).to.eql(["A", "Unexpected 'Z'"]);463 expect(result3).to.be.instanceOf(Failure);464 expect(result3.val.slice(0, 2)).to.eql(["B", "Unexpected 'Z'"]);465 });466 it("spaces work", () => {467 const result1 = run(spaces, " ABC");468 expect(printResult(result1)[0]).to.eql([" "]);469 // [' ']470 const result2 = run(spaces, "A");471 expect(printResult(result2)[0]).to.eql([]);472 // []473 const result3 = run(spaces1, " ABC");474 expect(printResult(result3)[0]).to.eql([" "]);475 // [' ']476 const result4 = run(spaces1, "A");477 expect(printResult(result4)).to.eql(478 "Line:0 Col:0 Error parsing whitespace\nA\n^Unexpected 'A'"479 );480 });481 it("pfloat works", () => {482 const result1 = run(pfloat, "1.1ABC"); // Success (1, "ABC")483 const result2 = run(pfloat, "12.23BC"); // Success (12, "BC")484 const result3 = run(pfloat, "123.4C"); // Success (123, "C")485 const result4 = run(pfloat, "1234.56"); // Success (1234, "")486 const result5 = run(pfloat, "ABC"); // Failure "Expecting '9'. Got 'A'"487 const result6 = run(pfloat, "-123.4C"); // Success (123, "C")488 expect(result1).to.be.instanceOf(Success);489 expect(result1.val[0]).to.eql(1.1);490 expect(result2).to.be.instanceOf(Success);491 expect(result2.val[0]).to.eql(12.23);492 expect(result3).to.be.instanceOf(Success);493 expect(result3.val[0]).to.eql(123.4);494 expect(result4).to.be.instanceOf(Success);495 expect(result4.val[0]).to.eql(1234.56);496 expect(result5).to.be.instanceOf(Failure);497 expect(result5.val.slice(0, 2)).to.eql(["float", "Unexpected 'A'"]);498 expect(result6).to.be.instanceOf(Success);499 expect(result6.val[0]).to.eql(-123.4);500 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 wpt.getTestResults(data.data.testId, function(err, data) {5 if (err) return console.error(err);6 console.log(data);7 });8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11 if (err) return console.error(err);12 wpt.getTestResults(data.data.testId, {f: 'json'}, function(err, data) {13 if (err) return console.error(err);14 console.log(data);15 });16});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('API_KEY');3 if (err) return console.error(err);4 test.result2(data.data.testId, function(err, data) {5 if (err) return console.error(err);6 console.log(data);7 });8});
Using AI Code Generation
1var wptest = require('./wptest');2var result2 = wptest.result2();3console.log(result2);4exports.result2 = function() {5 return 'result2';6};7var wptest = require('./wptest');8var result3 = wptest.result3();9console.log(result3);10module.exports.result3 = function() {11 return 'result3';12};13var wptest = require('./wptest');14var result4 = wptest.result4();15console.log(result4);16module.exports = {17 result4: function() {18 return 'result4';19 }20};21var wptest = require('./wptest');22var result5 = wptest.result5();23console.log(result5);24module.exports = function() {25 return 'result5';26};27var wptest = require('./wptest');28var result6 = wptest();29console.log(result6);30module.exports = function() {31 return 'result6';32};33var wptest = require('./wptest');34var result7 = wptest();35console.log(result7);36module.exports = function() {37 return 'result7';38};
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org','API_KEY');3wpt.getLocations(function(err, data) {4 if(err) return console.log(err);5 console.log(data);6});7[Ankit Kumar](
Using AI Code Generation
1var wpt = require('./wpt.js');2var wpt = new wpt('API_KEY');3wpt.runTest(url, function(err, data) {4 if (err) throw err;5 var testId = data.data.testId;6 wpt.result2(testId, function(err, data) {7 if (err) throw err;8 console.log(data);9 });10});
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('APIKEY');3wpt.result2('testID', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});
Using AI Code Generation
1var wpt = require('wpt');2var options = {3 videoParams: {4 }5};6wpt.runTest(options, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 console.log(data);11 }12});13var wpt = require('wpt');14var options = {15 videoParams: {16 }17};18wpt.runTest(options, function(err, data) {19 if (err) {20 console.log(err);21 } else {22 var testId = data.data;
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest(options);5var data = {6};7wpt.runTest(url, data, function (err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 wpt.getTestResults(data.data.testId, function (err, data) {13 console.log(data);14 });15 }16});
Using AI Code Generation
1var wpt = require('./wpt.js');2var wpt2 = new wpt("APIKEY");3wpt2.result2(123456789, function(data) {4 console.log(data);5});6var WebPageTest = require('webpagetest');7var wpt = new WebPageTest('www.webpagetest.org', 'APIKEY');8var wpt2 = {9 result2: function(testID, callback) {10 wpt.getTestResults(testID, function(err, data) {11 if (err) {12 console.log("Error: " + err);13 } else {14 callback(data);15 }16 });17 }18};19module.exports = wpt2;20var WebPageTest = require('webpagetest');21var wpt = new WebPageTest('www.webpagetest.org', 'APIKEY');22 if (err) {23 console.log("Error: " + err);24 } else {25 console.log(data);26 }27});28{statusCode: 200, statusText: 'Ok', data: {testId: '160720_4A_1W1
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!