1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | 11 11 11 11 11 11 11 11 11 903 903 903 903 903 903 903 903 903 903 11 387 181 206 11098 40169 6279 40177 903 903 903 903 2542 605 1937 1937 1915 1915 1915 872 110 110 108 108 4 108 762 67 67 1313 134 67 134 1 762 5423 5423 34 8 4240 2446 121 300 59 13 110 105 44 34 81 157 76 340 168 16 36 36 36 36 132 132 132 132 2 919 919 919 8 8 24 8 32 12 12 20 13 13 13 13 13 13 13 13 41 4 4 24 24 12 12 1 12 12 12 24 24 20 4 12 12 477 477 477 477 477 1 476 476 123 476 8 476 20 455 4 475 81 81 81 81 24 57 81 81 44 44 44 44 34 34 34 34 34 34 8 8 34 34 34 76 76 76 76 76 24 24 76 215 215 215 215 215 1 214 1 213 213 213 110 110 110 110 110 110 110 110 110 110 110 110 300 300 300 300 300 300 300 300 300 300 300 207 207 28 28 28 28 207 207 207 207 207 207 207 59 59 1 58 105 105 105 103 103 20 20 20 8 8 20 20 83 12 12 71 71 71 71 71 71 71 71 69 69 69 25 69 168 168 168 168 168 168 4 4 168 168 121 121 121 121 121 111 111 110 110 110 10 1295 1295 1295 24 8 8 8 8 8 8 8 16 16 16 16 16 16 16 16 1271 1271 1271 186 186 186 186 121 121 352 121 1430 1430 1430 1430 1430 2284 152 2132 2127 2127 1425 1425 16 16 16 16 2446 2446 2446 2446 2630 2630 2630 2630 3880 376 376 376 803 1 803 803 803 803 803 1022 802 72 72 72 2621 2621 71 71 2621 762 762 137 137 27 27 27 27 1692 3 2618 2618 71 2547 865 861 1430 2614 | 'use strict'; var Lexer = require('./lexer'); var nodes = require('./nodes'); var utils = require('./utils'); var filters = require('./filters'); var path = require('path'); var constantinople = require('constantinople'); var parseJSExpression = require('character-parser').parseMax; var extname = path.extname; /** * Initialize `Parser` with the given input `str` and `filename`. * * @param {String} str * @param {String} filename * @param {Object} options * @api public */ var Parser = exports = module.exports = function Parser(str, filename, options){ //Strip any UTF-8 BOM off of the start of `str`, if it exists. this.input = str.replace(/^\uFEFF/, ''); this.lexer = new Lexer(this.input, filename); this.filename = filename; this.blocks = {}; this.mixins = {}; this.options = options; this.contexts = [this]; this.inMixin = false; this.dependencies = []; this.inBlock = 0; }; /** * Parser prototype. */ Parser.prototype = { /** * Save original constructor */ constructor: Parser, /** * Push `parser` onto the context stack, * or pop and return a `Parser`. */ context: function(parser){ if (parser) { this.contexts.push(parser); } else { return this.contexts.pop(); } }, /** * Return the next token object. * * @return {Object} * @api private */ advance: function(){ return this.lexer.advance(); }, /** * Single token lookahead. * * @return {Object} * @api private */ peek: function() { return this.lookahead(1); }, /** * Return lexer lineno. * * @return {Number} * @api private */ line: function() { return this.lexer.lineno; }, /** * `n` token lookahead. * * @param {Number} n * @return {Object} * @api private */ lookahead: function(n){ return this.lexer.lookahead(n); }, /** * Parse input returning a string of js for evaluation. * * @return {String} * @api public */ parse: function(){ var block = new nodes.Block, parser; block.line = 0; block.filename = this.filename; while ('eos' != this.peek().type) { if ('newline' == this.peek().type) { this.advance(); } else { var next = this.peek(); var expr = this.parseExpr(); expr.filename = expr.filename || this.filename; expr.line = next.line; block.push(expr); } } if (parser = this.extending) { this.context(parser); var ast = parser.parse(); this.context(); // hoist mixins for (var name in this.mixins) ast.unshift(this.mixins[name]); return ast; } if (!this.extending && !this.included && Object.keys(this.blocks).length){ var blocks = []; utils.walkAST(block, function (node) { if (node.type === 'Block' && node.name) { blocks.push(node.name); } }); Object.keys(this.blocks).forEach(function (name) { if (blocks.indexOf(name) === -1 && !this.blocks[name].isSubBlock) { console.warn('Warning: Unexpected block "' + name + '" ' + ' on line ' + this.blocks[name].line + ' of ' + (this.blocks[name].filename) + '. This block is never used. This warning will be an error in v2.0.0'); } }.bind(this)); } return block; }, /** * Expect the given type, or throw an exception. * * @param {String} type * @api private */ expect: function(type){ Eif (this.peek().type === type) { return this.advance(); } else { throw new Error('expected "' + type + '", but got "' + this.peek().type + '"'); } }, /** * Accept the given `type`. * * @param {String} type * @api private */ accept: function(type){ if (this.peek().type === type) { return this.advance(); } }, /** * tag * | doctype * | mixin * | include * | filter * | comment * | text * | each * | code * | yield * | id * | class * | interpolation */ parseExpr: function(){ switch (this.peek().type) { case 'tag': return this.parseTag(); case 'mixin': return this.parseMixin(); case 'block': return this.parseBlock(); case 'mixin-block': return this.parseMixinBlock(); case 'case': return this.parseCase(); case 'extends': return this.parseExtends(); case 'include': return this.parseInclude(); case 'doctype': return this.parseDoctype(); case 'filter': return this.parseFilter(); case 'comment': return this.parseComment(); case 'text': return this.parseText(); case 'each': return this.parseEach(); case 'code': return this.parseCode(); case 'call': return this.parseCall(); case 'interpolation': return this.parseInterpolation(); case 'yield': this.advance(); var block = new nodes.Block; block.yield = true; return block; case 'id': case 'class': var tok = this.advance(); this.lexer.defer(this.lexer.tok('tag', 'div')); this.lexer.defer(tok); return this.parseExpr(); default: throw new Error('unexpected token "' + this.peek().type + '"'); } }, /** * Text */ parseText: function(){ var tok = this.expect('text'); var tokens = this.parseInlineTagsInText(tok.val); if (tokens.length === 1) return tokens[0]; var node = new nodes.Block; for (var i = 0; i < tokens.length; i++) { node.push(tokens[i]); }; return node; }, /** * ':' expr * | block */ parseBlockExpansion: function(){ if (':' == this.peek().type) { this.advance(); return new nodes.Block(this.parseExpr()); } else { return this.block(); } }, /** * case */ parseCase: function(){ var val = this.expect('case').val; var node = new nodes.Case(val); node.line = this.line(); var block = new nodes.Block; block.line = this.line(); block.filename = this.filename; this.expect('indent'); while ('outdent' != this.peek().type) { switch (this.peek().type) { case 'newline': this.advance(); break; case 'when': block.push(this.parseWhen()); break; case 'default': block.push(this.parseDefault()); break; default: throw new Error('Unexpected token "' + this.peek().type + '", expected "when", "default" or "newline"'); } } this.expect('outdent'); node.block = block; return node; }, /** * when */ parseWhen: function(){ var val = this.expect('when').val; if (this.peek().type !== 'newline') return new nodes.Case.When(val, this.parseBlockExpansion()); else return new nodes.Case.When(val); }, /** * default */ parseDefault: function(){ this.expect('default'); return new nodes.Case.When('default', this.parseBlockExpansion()); }, /** * code */ parseCode: function(afterIf){ var tok = this.expect('code'); var node = new nodes.Code(tok.val, tok.buffer, tok.escape); var block; node.line = this.line(); // throw an error if an else does not have an if if (tok.isElse && !tok.hasIf) { throw new Error('Unexpected else without if'); } // handle block block = 'indent' == this.peek().type; if (block) { node.block = this.block(); } // handle missing block if (tok.requiresBlock && !block) { node.block = new nodes.Block(); } // mark presense of if for future elses if (tok.isIf && this.peek().isElse) { this.peek().hasIf = true; } else if (tok.isIf && this.peek().type === 'newline' && this.lookahead(2).isElse) { this.lookahead(2).hasIf = true; } return node; }, /** * comment */ parseComment: function(){ var tok = this.expect('comment'); var node; var block; if (block = this.parseTextBlock()) { node = new nodes.BlockComment(tok.val, block, tok.buffer); } else { node = new nodes.Comment(tok.val, tok.buffer); } node.line = this.line(); return node; }, /** * doctype */ parseDoctype: function(){ var tok = this.expect('doctype'); var node = new nodes.Doctype(tok.val); node.line = this.line(); return node; }, /** * filter attrs? text-block */ parseFilter: function(){ var tok = this.expect('filter'); var attrs = this.accept('attrs'); var block; block = this.parseTextBlock() || new nodes.Block(); var options = {}; if (attrs) { attrs.attrs.forEach(function (attribute) { options[attribute.name] = constantinople.toConstant(attribute.val); }); } var node = new nodes.Filter(tok.val, block, options); node.line = this.line(); return node; }, /** * each block */ parseEach: function(){ var tok = this.expect('each'); var node = new nodes.Each(tok.code, tok.val, tok.key); node.line = this.line(); node.block = this.block(); if (this.peek().type == 'code' && this.peek().val == 'else') { this.advance(); node.alternative = this.block(); } return node; }, /** * Resolves a path relative to the template for use in * includes and extends * * @param {String} path * @param {String} purpose Used in error messages. * @return {String} * @api private */ resolvePath: function (path, purpose) { var p = require('path'); var dirname = p.dirname; var basename = p.basename; var join = p.join; if (path[0] !== '/' && !this.filename) throw new Error('the "filename" option is required to use "' + purpose + '" with "relative" paths'); if (path[0] === '/' && !this.options.basedir) throw new Error('the "basedir" option is required to use "' + purpose + '" with "absolute" paths'); path = join(path[0] === '/' ? this.options.basedir : dirname(this.filename), path); if (basename(path).indexOf('.') === -1) path += '.jade'; return path; }, /** * 'extends' name */ parseExtends: function(){ var fs = require('fs'); var path = this.resolvePath(this.expect('extends').val.trim(), 'extends'); if ('.jade' != path.substr(-5)) path += '.jade'; this.dependencies.push(path); var str = fs.readFileSync(path, 'utf8'); var parser = new this.constructor(str, path, this.options); parser.dependencies = this.dependencies; parser.blocks = this.blocks; parser.included = this.included; parser.contexts = this.contexts; this.extending = parser; // TODO: null node return new nodes.Literal(''); }, /** * 'block' name block */ parseBlock: function(){ var block = this.expect('block'); var mode = block.mode; var name = block.val.trim(); var line = block.line; this.inBlock++; block = 'indent' == this.peek().type ? this.block() : new nodes.Block(new nodes.Literal('')); this.inBlock--; block.name = name; block.line = line; var prev = this.blocks[name] || {prepended: [], appended: []} if (prev.mode === 'replace') return this.blocks[name] = prev; var allNodes = prev.prepended.concat(block.nodes).concat(prev.appended); switch (mode) { case 'append': prev.appended = prev.parser === this ? prev.appended.concat(block.nodes) : block.nodes.concat(prev.appended); break; case 'prepend': prev.prepended = prev.parser === this ? block.nodes.concat(prev.prepended) : prev.prepended.concat(block.nodes); break; } block.nodes = allNodes; block.appended = prev.appended; block.prepended = prev.prepended; block.mode = mode; block.parser = this; block.isSubBlock = this.inBlock > 0; return this.blocks[name] = block; }, parseMixinBlock: function () { var block = this.expect('mixin-block'); if (!this.inMixin) { throw new Error('Anonymous blocks are not allowed unless they are part of a mixin.'); } return new nodes.MixinBlock(); }, /** * include block? */ parseInclude: function(){ var fs = require('fs'); var tok = this.expect('include'); var path = this.resolvePath(tok.val.trim(), 'include'); this.dependencies.push(path); // has-filter if (tok.filter) { var str = fs.readFileSync(path, 'utf8').replace(/\r/g, ''); var options = {filename: path}; if (tok.attrs) { tok.attrs.attrs.forEach(function (attribute) { options[attribute.name] = constantinople.toConstant(attribute.val); }); } str = filters(tok.filter, str, options); return new nodes.Literal(str); } // non-jade if ('.jade' != path.substr(-5)) { var str = fs.readFileSync(path, 'utf8').replace(/\r/g, ''); return new nodes.Literal(str); } var str = fs.readFileSync(path, 'utf8'); var parser = new this.constructor(str, path, this.options); parser.dependencies = this.dependencies; parser.blocks = utils.merge({}, this.blocks); parser.included = true; parser.mixins = this.mixins; this.context(parser); var ast = parser.parse(); this.context(); ast.filename = path; if ('indent' == this.peek().type) { ast.includeBlock().push(this.block()); } return ast; }, /** * call ident block */ parseCall: function(){ var tok = this.expect('call'); var name = tok.val; var args = tok.args; var mixin = new nodes.Mixin(name, args, new nodes.Block, true); this.tag(mixin); if (mixin.code) { mixin.block.push(mixin.code); mixin.code = null; } if (mixin.block.isEmpty()) mixin.block = null; return mixin; }, /** * mixin block */ parseMixin: function(){ var tok = this.expect('mixin'); var name = tok.val; var args = tok.args; var mixin; // definition if ('indent' == this.peek().type) { this.inMixin = true; mixin = new nodes.Mixin(name, args, this.block(), false); this.mixins[name] = mixin; this.inMixin = false; return mixin; // call } else { return new nodes.Mixin(name, args, null, true); } }, parseInlineTagsInText: function (str) { var line = this.line(); var match = /(\\)?#\[((?:.|\n)*)$/.exec(str); if (match) { if (match[1]) { // escape var text = new nodes.Text(str.substr(0, match.index) + '#['); text.line = line; var rest = this.parseInlineTagsInText(match[2]); Eif (rest[0].type === 'Text') { text.val += rest[0].val; rest.shift(); } return [text].concat(rest); } else { var text = new nodes.Text(str.substr(0, match.index)); text.line = line; var buffer = [text]; var rest = match[2]; var range = parseJSExpression(rest); var inner = new Parser(range.src, this.filename, this.options); buffer.push(inner.parse()); return buffer.concat(this.parseInlineTagsInText(rest.substr(range.end + 1))); } } else { var text = new nodes.Text(str); text.line = line; return [text]; } }, /** * indent (text | newline)* outdent */ parseTextBlock: function(){ var block = new nodes.Block; block.line = this.line(); var body = this.peek(); if (body.type !== 'pipeless-text') return; this.advance(); block.nodes = body.val.reduce(function (accumulator, text) { return accumulator.concat(this.parseInlineTagsInText(text)); }.bind(this), []); return block; }, /** * indent expr* outdent */ block: function(){ var block = new nodes.Block; block.line = this.line(); block.filename = this.filename; this.expect('indent'); while ('outdent' != this.peek().type) { if ('newline' == this.peek().type) { this.advance(); } else { var expr = this.parseExpr(); expr.filename = this.filename; block.push(expr); } } this.expect('outdent'); return block; }, /** * interpolation (attrs | class | id)* (text | code | ':')? newline* block? */ parseInterpolation: function(){ var tok = this.advance(); var tag = new nodes.Tag(tok.val); tag.buffer = true; return this.tag(tag); }, /** * tag (attrs | class | id)* (text | code | ':')? newline* block? */ parseTag: function(){ var tok = this.advance(); var tag = new nodes.Tag(tok.val); tag.selfClosing = tok.selfClosing; return this.tag(tag); }, /** * Parse tag. */ tag: function(tag){ tag.line = this.line(); var seenAttrs = false; // (attrs | class | id)* out: while (true) { switch (this.peek().type) { case 'id': case 'class': var tok = this.advance(); tag.setAttribute(tok.type, "'" + tok.val + "'"); continue; case 'attrs': if (seenAttrs) { console.warn(this.filename + ', line ' + this.peek().line + ':\nYou should not have jade tags with multiple attributes.'); } seenAttrs = true; var tok = this.advance(); var attrs = tok.attrs; if (tok.selfClosing) tag.selfClosing = true; for (var i = 0; i < attrs.length; i++) { tag.setAttribute(attrs[i].name, attrs[i].val, attrs[i].escaped); } continue; case '&attributes': var tok = this.advance(); tag.addAttributes(tok.val); break; default: break out; } } // check immediate '.' if ('dot' == this.peek().type) { tag.textOnly = true; this.advance(); } // (text | code | ':')? switch (this.peek().type) { case 'text': tag.block.push(this.parseText()); break; case 'code': tag.code = this.parseCode(); break; case ':': this.advance(); tag.block = new nodes.Block; tag.block.push(this.parseExpr()); break; case 'newline': case 'indent': case 'outdent': case 'eos': case 'pipeless-text': break; default: throw new Error('Unexpected token `' + this.peek().type + '` expected `text`, `code`, `:`, `newline` or `eos`') } // newline* while ('newline' == this.peek().type) this.advance(); // block? if (tag.textOnly) { tag.block = this.parseTextBlock() || new nodes.Block(); } else if ('indent' == this.peek().type) { var block = this.block(); for (var i = 0, len = block.nodes.length; i < len; ++i) { tag.block.push(block.nodes[i]); } } return tag; } }; |