~ubuntuone-pqm-team/yui/stable-min

« back to all changes in this revision

Viewing changes to build/handlebars-compiler/handlebars-compiler.js

  • Committer: Ricardo Kirkner
  • Date: 2014-09-23 20:17:06 UTC
  • Revision ID: ricardo.kirkner@canonical.com-20140923201706-17kwxwckw6orp28k
re-added all .js files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
YUI.add('handlebars-compiler', function (Y, NAME) {
 
2
 
 
3
/*!
 
4
Handlebars.js - Copyright (C) 2011 Yehuda Katz
 
5
https://raw.github.com/wycats/handlebars.js/master/LICENSE
 
6
*/
 
7
// This file contains code that will be inserted at the beginning of the
 
8
// handlebars-compiler module.
 
9
 
 
10
// This is necessary because the Y.Handlebars object is created in the
 
11
// handlebars-base module, but the native Handlebars code expects to use a local
 
12
// Handlebars reference.
 
13
var Handlebars = Y.Handlebars;
 
14
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
 
15
 
 
16
/* Jison generated parser */
 
17
var handlebars = (function(){
 
18
var parser = {trace: function trace() { },
 
19
yy: {},
 
20
symbols_: {"error":2,"root":3,"program":4,"EOF":5,"simpleInverse":6,"statements":7,"statement":8,"openInverse":9,"closeBlock":10,"openBlock":11,"mustache":12,"partial":13,"CONTENT":14,"COMMENT":15,"OPEN_BLOCK":16,"inMustache":17,"CLOSE":18,"OPEN_INVERSE":19,"OPEN_ENDBLOCK":20,"path":21,"OPEN":22,"OPEN_UNESCAPED":23,"CLOSE_UNESCAPED":24,"OPEN_PARTIAL":25,"partialName":26,"params":27,"hash":28,"dataName":29,"param":30,"STRING":31,"INTEGER":32,"BOOLEAN":33,"hashSegments":34,"hashSegment":35,"ID":36,"EQUALS":37,"DATA":38,"pathSegments":39,"SEP":40,"$accept":0,"$end":1},
 
21
terminals_: {2:"error",5:"EOF",14:"CONTENT",15:"COMMENT",16:"OPEN_BLOCK",18:"CLOSE",19:"OPEN_INVERSE",20:"OPEN_ENDBLOCK",22:"OPEN",23:"OPEN_UNESCAPED",24:"CLOSE_UNESCAPED",25:"OPEN_PARTIAL",31:"STRING",32:"INTEGER",33:"BOOLEAN",36:"ID",37:"EQUALS",38:"DATA",40:"SEP"},
 
22
productions_: [0,[3,2],[4,2],[4,3],[4,2],[4,1],[4,1],[4,0],[7,1],[7,2],[8,3],[8,3],[8,1],[8,1],[8,1],[8,1],[11,3],[9,3],[10,3],[12,3],[12,3],[13,3],[13,4],[6,2],[17,3],[17,2],[17,2],[17,1],[17,1],[27,2],[27,1],[30,1],[30,1],[30,1],[30,1],[30,1],[28,1],[34,2],[34,1],[35,3],[35,3],[35,3],[35,3],[35,3],[26,1],[26,1],[26,1],[29,2],[21,1],[39,3],[39,1]],
 
23
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
 
24
 
 
25
var $0 = $$.length - 1;
 
26
switch (yystate) {
 
27
case 1: return $$[$0-1];
 
28
break;
 
29
case 2: this.$ = new yy.ProgramNode([], $$[$0]);
 
30
break;
 
31
case 3: this.$ = new yy.ProgramNode($$[$0-2], $$[$0]);
 
32
break;
 
33
case 4: this.$ = new yy.ProgramNode($$[$0-1], []);
 
34
break;
 
35
case 5: this.$ = new yy.ProgramNode($$[$0]);
 
36
break;
 
37
case 6: this.$ = new yy.ProgramNode([], []);
 
38
break;
 
39
case 7: this.$ = new yy.ProgramNode([]);
 
40
break;
 
41
case 8: this.$ = [$$[$0]];
 
42
break;
 
43
case 9: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
 
44
break;
 
45
case 10: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1].inverse, $$[$0-1], $$[$0]);
 
46
break;
 
47
case 11: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0-1].inverse, $$[$0]);
 
48
break;
 
49
case 12: this.$ = $$[$0];
 
50
break;
 
51
case 13: this.$ = $$[$0];
 
52
break;
 
53
case 14: this.$ = new yy.ContentNode($$[$0]);
 
54
break;
 
55
case 15: this.$ = new yy.CommentNode($$[$0]);
 
56
break;
 
57
case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
 
58
break;
 
59
case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
 
60
break;
 
61
case 18: this.$ = $$[$0-1];
 
62
break;
 
63
case 19:
 
64
    // Parsing out the '&' escape token at this level saves ~500 bytes after min due to the removal of one parser node.
 
65
    this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], $$[$0-2][2] === '&');
 
66
 
 
67
break;
 
68
case 20: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true);
 
69
break;
 
70
case 21: this.$ = new yy.PartialNode($$[$0-1]);
 
71
break;
 
72
case 22: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1]);
 
73
break;
 
74
case 23:
 
75
break;
 
76
case 24: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]];
 
77
break;
 
78
case 25: this.$ = [[$$[$0-1]].concat($$[$0]), null];
 
79
break;
 
80
case 26: this.$ = [[$$[$0-1]], $$[$0]];
 
81
break;
 
82
case 27: this.$ = [[$$[$0]], null];
 
83
break;
 
84
case 28: this.$ = [[$$[$0]], null];
 
85
break;
 
86
case 29: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
 
87
break;
 
88
case 30: this.$ = [$$[$0]];
 
89
break;
 
90
case 31: this.$ = $$[$0];
 
91
break;
 
92
case 32: this.$ = new yy.StringNode($$[$0]);
 
93
break;
 
94
case 33: this.$ = new yy.IntegerNode($$[$0]);
 
95
break;
 
96
case 34: this.$ = new yy.BooleanNode($$[$0]);
 
97
break;
 
98
case 35: this.$ = $$[$0];
 
99
break;
 
100
case 36: this.$ = new yy.HashNode($$[$0]);
 
101
break;
 
102
case 37: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
 
103
break;
 
104
case 38: this.$ = [$$[$0]];
 
105
break;
 
106
case 39: this.$ = [$$[$0-2], $$[$0]];
 
107
break;
 
108
case 40: this.$ = [$$[$0-2], new yy.StringNode($$[$0])];
 
109
break;
 
110
case 41: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])];
 
111
break;
 
112
case 42: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])];
 
113
break;
 
114
case 43: this.$ = [$$[$0-2], $$[$0]];
 
115
break;
 
116
case 44: this.$ = new yy.PartialNameNode($$[$0]);
 
117
break;
 
118
case 45: this.$ = new yy.PartialNameNode(new yy.StringNode($$[$0]));
 
119
break;
 
120
case 46: this.$ = new yy.PartialNameNode(new yy.IntegerNode($$[$0]));
 
121
break;
 
122
case 47: this.$ = new yy.DataNode($$[$0]);
 
123
break;
 
124
case 48: this.$ = new yy.IdNode($$[$0]);
 
125
break;
 
126
case 49: $$[$0-2].push({part: $$[$0], separator: $$[$0-1]}); this.$ = $$[$0-2];
 
127
break;
 
128
case 50: this.$ = [{part: $$[$0]}];
 
129
break;
 
130
}
 
131
},
 
132
table: [{3:1,4:2,5:[2,7],6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],22:[1,14],23:[1,15],25:[1,16]},{1:[3]},{5:[1,17]},{5:[2,6],7:18,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,6],22:[1,14],23:[1,15],25:[1,16]},{5:[2,5],6:20,8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,5],22:[1,14],23:[1,15],25:[1,16]},{17:23,18:[1,22],21:24,29:25,36:[1,28],38:[1,27],39:26},{5:[2,8],14:[2,8],15:[2,8],16:[2,8],19:[2,8],20:[2,8],22:[2,8],23:[2,8],25:[2,8]},{4:29,6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,7],22:[1,14],23:[1,15],25:[1,16]},{4:30,6:3,7:4,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,5],20:[2,7],22:[1,14],23:[1,15],25:[1,16]},{5:[2,12],14:[2,12],15:[2,12],16:[2,12],19:[2,12],20:[2,12],22:[2,12],23:[2,12],25:[2,12]},{5:[2,13],14:[2,13],15:[2,13],16:[2,13],19:[2,13],20:[2,13],22:[2,13],23:[2,13],25:[2,13]},{5:[2,14],14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],25:[2,14]},{5:[2,15],14:[2,15],15:[2,15],16:[2,15],19:[2,15],20:[2,15],22:[2,15],23:[2,15],25:[2,15]},{17:31,21:24,29:25,36:[1,28],38:[1,27],39:26},{17:32,21:24,29:25,36:[1,28],38:[1,27],39:26},{17:33,21:24,29:25,36:[1,28],38:[1,27],39:26},{21:35,26:34,31:[1,36],32:[1,37],36:[1,28],39:26},{1:[2,1]},{5:[2,2],8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,2],22:[1,14],23:[1,15],25:[1,16]},{17:23,21:24,29:25,36:[1,28],38:[1,27],39:26},{5:[2,4],7:38,8:6,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,4],22:[1,14],23:[1,15],25:[1,16]},{5:[2,9],14:[2,9],15:[2,9],16:[2,9],19:[2,9],20:[2,9],22:[2,9],23:[2,9],25:[2,9]},{5:[2,23],14:[2,23],15:[2,23],16:[2,23],19:[2,23],20:[2,23],22:[2,23],23:[2,23],25:[2,23]},{18:[1,39]},{18:[2,27],21:44,24:[2,27],27:40,28:41,29:48,30:42,31:[1,45],32:[1,46],33:[1,47],34:43,35:49,36:[1,50],38:[1,27],39:26},{18:[2,28],24:[2,28]},{18:[2,48],24:[2,48],31:[2,48],32:[2,48],33:[2,48],36:[2,48],38:[2,48],40:[1,51]},{21:52,36:[1,28],39:26},{18:[2,50],24:[2,50],31:[2,50],32:[2,50],33:[2,50],36:[2,50],38:[2,50],40:[2,50]},{10:53,20:[1,54]},{10:55,20:[1,54]},{18:[1,56]},{18:[1,57]},{24:[1,58]},{18:[1,59],21:60,36:[1,28],39:26},{18:[2,44],36:[2,44]},{18:[2,45],36:[2,45]},{18:[2,46],36:[2,46]},{5:[2,3],8:21,9:7,11:8,12:9,13:10,14:[1,11],15:[1,12],16:[1,13],19:[1,19],20:[2,3],22:[1,14],23:[1,15],25:[1,16]},{14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],25:[2,17]},{18:[2,25],21:44,24:[2,25],28:61,29:48,30:62,31:[1,45],32:[1,46],33:[1,47],34:43,35:49,36:[1,50],38:[1,27],39:26},{18:[2,26],24:[2,26]},{18:[2,30],24:[2,30],31:[2,30],32:[2,30],33:[2,30],36:[2,30],38:[2,30]},{18:[2,36],24:[2,36],35:63,36:[1,64]},{18:[2,31],24:[2,31],31:[2,31],32:[2,31],33:[2,31],36:[2,31],38:[2,31]},{18:[2,32],24:[2,32],31:[2,32],32:[2,32],33:[2,32],36:[2,32],38:[2,32]},{18:[2,33],24:[2,33],31:[2,33],32:[2,33],33:[2,33],36:[2,33],38:[2,33]},{18:[2,34],24:[2,34],31:[2,34],32:[2,34],33:[2,34],36:[2,34],38:[2,34]},{18:[2,35],24:[2,35],31:[2,35],32:[2,35],33:[2,35],36:[2,35],38:[2,35]},{18:[2,38],24:[2,38],36:[2,38]},{18:[2,50],24:[2,50],31:[2,50],32:[2,50],33:[2,50],36:[2,50],37:[1,65],38:[2,50],40:[2,50]},{36:[1,66]},{18:[2,47],24:[2,47],31:[2,47],32:[2,47],33:[2,47],36:[2,47],38:[2,47]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],25:[2,10]},{21:67,36:[1,28],39:26},{5:[2,11],14:[2,11],15:[2,11],16:[2,11],19:[2,11],20:[2,11],22:[2,11],23:[2,11],25:[2,11]},{14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],25:[2,16]},{5:[2,19],14:[2,19],15:[2,19],16:[2,19],19:[2,19],20:[2,19],22:[2,19],23:[2,19],25:[2,19]},{5:[2,20],14:[2,20],15:[2,20],16:[2,20],19:[2,20],20:[2,20],22:[2,20],23:[2,20],25:[2,20]},{5:[2,21],14:[2,21],15:[2,21],16:[2,21],19:[2,21],20:[2,21],22:[2,21],23:[2,21],25:[2,21]},{18:[1,68]},{18:[2,24],24:[2,24]},{18:[2,29],24:[2,29],31:[2,29],32:[2,29],33:[2,29],36:[2,29],38:[2,29]},{18:[2,37],24:[2,37],36:[2,37]},{37:[1,65]},{21:69,29:73,31:[1,70],32:[1,71],33:[1,72],36:[1,28],38:[1,27],39:26},{18:[2,49],24:[2,49],31:[2,49],32:[2,49],33:[2,49],36:[2,49],38:[2,49],40:[2,49]},{18:[1,74]},{5:[2,22],14:[2,22],15:[2,22],16:[2,22],19:[2,22],20:[2,22],22:[2,22],23:[2,22],25:[2,22]},{18:[2,39],24:[2,39],36:[2,39]},{18:[2,40],24:[2,40],36:[2,40]},{18:[2,41],24:[2,41],36:[2,41]},{18:[2,42],24:[2,42],36:[2,42]},{18:[2,43],24:[2,43],36:[2,43]},{5:[2,18],14:[2,18],15:[2,18],16:[2,18],19:[2,18],20:[2,18],22:[2,18],23:[2,18],25:[2,18]}],
 
133
defaultActions: {17:[2,1]},
 
134
parseError: function parseError(str, hash) {
 
135
    throw new Error(str);
 
136
},
 
137
parse: function parse(input) {
 
138
    var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
 
139
    this.lexer.setInput(input);
 
140
    this.lexer.yy = this.yy;
 
141
    this.yy.lexer = this.lexer;
 
142
    this.yy.parser = this;
 
143
    if (typeof this.lexer.yylloc == "undefined")
 
144
        this.lexer.yylloc = {};
 
145
    var yyloc = this.lexer.yylloc;
 
146
    lstack.push(yyloc);
 
147
    var ranges = this.lexer.options && this.lexer.options.ranges;
 
148
    if (typeof this.yy.parseError === "function")
 
149
        this.parseError = this.yy.parseError;
 
150
    function popStack(n) {
 
151
        stack.length = stack.length - 2 * n;
 
152
        vstack.length = vstack.length - n;
 
153
        lstack.length = lstack.length - n;
 
154
    }
 
155
    function lex() {
 
156
        var token;
 
157
        token = self.lexer.lex() || 1;
 
158
        if (typeof token !== "number") {
 
159
            token = self.symbols_[token] || token;
 
160
        }
 
161
        return token;
 
162
    }
 
163
    var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
 
164
    while (true) {
 
165
        state = stack[stack.length - 1];
 
166
        if (this.defaultActions[state]) {
 
167
            action = this.defaultActions[state];
 
168
        } else {
 
169
            if (symbol === null || typeof symbol == "undefined") {
 
170
                symbol = lex();
 
171
            }
 
172
            action = table[state] && table[state][symbol];
 
173
        }
 
174
        if (typeof action === "undefined" || !action.length || !action[0]) {
 
175
            var errStr = "";
 
176
            if (!recovering) {
 
177
                expected = [];
 
178
                for (p in table[state])
 
179
                    if (this.terminals_[p] && p > 2) {
 
180
                        expected.push("'" + this.terminals_[p] + "'");
 
181
                    }
 
182
                if (this.lexer.showPosition) {
 
183
                    errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
 
184
                } else {
 
185
                    errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
 
186
                }
 
187
                this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
 
188
            }
 
189
        }
 
190
        if (action[0] instanceof Array && action.length > 1) {
 
191
            throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
 
192
        }
 
193
        switch (action[0]) {
 
194
        case 1:
 
195
            stack.push(symbol);
 
196
            vstack.push(this.lexer.yytext);
 
197
            lstack.push(this.lexer.yylloc);
 
198
            stack.push(action[1]);
 
199
            symbol = null;
 
200
            if (!preErrorSymbol) {
 
201
                yyleng = this.lexer.yyleng;
 
202
                yytext = this.lexer.yytext;
 
203
                yylineno = this.lexer.yylineno;
 
204
                yyloc = this.lexer.yylloc;
 
205
                if (recovering > 0)
 
206
                    recovering--;
 
207
            } else {
 
208
                symbol = preErrorSymbol;
 
209
                preErrorSymbol = null;
 
210
            }
 
211
            break;
 
212
        case 2:
 
213
            len = this.productions_[action[1]][1];
 
214
            yyval.$ = vstack[vstack.length - len];
 
215
            yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
 
216
            if (ranges) {
 
217
                yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
 
218
            }
 
219
            r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
 
220
            if (typeof r !== "undefined") {
 
221
                return r;
 
222
            }
 
223
            if (len) {
 
224
                stack = stack.slice(0, -1 * len * 2);
 
225
                vstack = vstack.slice(0, -1 * len);
 
226
                lstack = lstack.slice(0, -1 * len);
 
227
            }
 
228
            stack.push(this.productions_[action[1]][0]);
 
229
            vstack.push(yyval.$);
 
230
            lstack.push(yyval._$);
 
231
            newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
 
232
            stack.push(newState);
 
233
            break;
 
234
        case 3:
 
235
            return true;
 
236
        }
 
237
    }
 
238
    return true;
 
239
}
 
240
};
 
241
/* Jison generated lexer */
 
242
var lexer = (function(){
 
243
var lexer = ({EOF:1,
 
244
parseError:function parseError(str, hash) {
 
245
        if (this.yy.parser) {
 
246
            this.yy.parser.parseError(str, hash);
 
247
        } else {
 
248
            throw new Error(str);
 
249
        }
 
250
    },
 
251
setInput:function (input) {
 
252
        this._input = input;
 
253
        this._more = this._less = this.done = false;
 
254
        this.yylineno = this.yyleng = 0;
 
255
        this.yytext = this.matched = this.match = '';
 
256
        this.conditionStack = ['INITIAL'];
 
257
        this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
 
258
        if (this.options.ranges) this.yylloc.range = [0,0];
 
259
        this.offset = 0;
 
260
        return this;
 
261
    },
 
262
input:function () {
 
263
        var ch = this._input[0];
 
264
        this.yytext += ch;
 
265
        this.yyleng++;
 
266
        this.offset++;
 
267
        this.match += ch;
 
268
        this.matched += ch;
 
269
        var lines = ch.match(/(?:\r\n?|\n).*/g);
 
270
        if (lines) {
 
271
            this.yylineno++;
 
272
            this.yylloc.last_line++;
 
273
        } else {
 
274
            this.yylloc.last_column++;
 
275
        }
 
276
        if (this.options.ranges) this.yylloc.range[1]++;
 
277
 
 
278
        this._input = this._input.slice(1);
 
279
        return ch;
 
280
    },
 
281
unput:function (ch) {
 
282
        var len = ch.length;
 
283
        var lines = ch.split(/(?:\r\n?|\n)/g);
 
284
 
 
285
        this._input = ch + this._input;
 
286
        this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
 
287
        //this.yyleng -= len;
 
288
        this.offset -= len;
 
289
        var oldLines = this.match.split(/(?:\r\n?|\n)/g);
 
290
        this.match = this.match.substr(0, this.match.length-1);
 
291
        this.matched = this.matched.substr(0, this.matched.length-1);
 
292
 
 
293
        if (lines.length-1) this.yylineno -= lines.length-1;
 
294
        var r = this.yylloc.range;
 
295
 
 
296
        this.yylloc = {first_line: this.yylloc.first_line,
 
297
          last_line: this.yylineno+1,
 
298
          first_column: this.yylloc.first_column,
 
299
          last_column: lines ?
 
300
              (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
 
301
              this.yylloc.first_column - len
 
302
          };
 
303
 
 
304
        if (this.options.ranges) {
 
305
            this.yylloc.range = [r[0], r[0] + this.yyleng - len];
 
306
        }
 
307
        return this;
 
308
    },
 
309
more:function () {
 
310
        this._more = true;
 
311
        return this;
 
312
    },
 
313
less:function (n) {
 
314
        this.unput(this.match.slice(n));
 
315
    },
 
316
pastInput:function () {
 
317
        var past = this.matched.substr(0, this.matched.length - this.match.length);
 
318
        return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
 
319
    },
 
320
upcomingInput:function () {
 
321
        var next = this.match;
 
322
        if (next.length < 20) {
 
323
            next += this._input.substr(0, 20-next.length);
 
324
        }
 
325
        return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
 
326
    },
 
327
showPosition:function () {
 
328
        var pre = this.pastInput();
 
329
        var c = new Array(pre.length + 1).join("-");
 
330
        return pre + this.upcomingInput() + "\n" + c+"^";
 
331
    },
 
332
next:function () {
 
333
        if (this.done) {
 
334
            return this.EOF;
 
335
        }
 
336
        if (!this._input) this.done = true;
 
337
 
 
338
        var token,
 
339
            match,
 
340
            tempMatch,
 
341
            index,
 
342
            col,
 
343
            lines;
 
344
        if (!this._more) {
 
345
            this.yytext = '';
 
346
            this.match = '';
 
347
        }
 
348
        var rules = this._currentRules();
 
349
        for (var i=0;i < rules.length; i++) {
 
350
            tempMatch = this._input.match(this.rules[rules[i]]);
 
351
            if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
 
352
                match = tempMatch;
 
353
                index = i;
 
354
                if (!this.options.flex) break;
 
355
            }
 
356
        }
 
357
        if (match) {
 
358
            lines = match[0].match(/(?:\r\n?|\n).*/g);
 
359
            if (lines) this.yylineno += lines.length;
 
360
            this.yylloc = {first_line: this.yylloc.last_line,
 
361
                           last_line: this.yylineno+1,
 
362
                           first_column: this.yylloc.last_column,
 
363
                           last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
 
364
            this.yytext += match[0];
 
365
            this.match += match[0];
 
366
            this.matches = match;
 
367
            this.yyleng = this.yytext.length;
 
368
            if (this.options.ranges) {
 
369
                this.yylloc.range = [this.offset, this.offset += this.yyleng];
 
370
            }
 
371
            this._more = false;
 
372
            this._input = this._input.slice(match[0].length);
 
373
            this.matched += match[0];
 
374
            token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
 
375
            if (this.done && this._input) this.done = false;
 
376
            if (token) return token;
 
377
            else return;
 
378
        }
 
379
        if (this._input === "") {
 
380
            return this.EOF;
 
381
        } else {
 
382
            return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
 
383
                    {text: "", token: null, line: this.yylineno});
 
384
        }
 
385
    },
 
386
lex:function lex() {
 
387
        var r = this.next();
 
388
        if (typeof r !== 'undefined') {
 
389
            return r;
 
390
        } else {
 
391
            return this.lex();
 
392
        }
 
393
    },
 
394
begin:function begin(condition) {
 
395
        this.conditionStack.push(condition);
 
396
    },
 
397
popState:function popState() {
 
398
        return this.conditionStack.pop();
 
399
    },
 
400
_currentRules:function _currentRules() {
 
401
        return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
 
402
    },
 
403
topState:function () {
 
404
        return this.conditionStack[this.conditionStack.length-2];
 
405
    },
 
406
pushState:function begin(condition) {
 
407
        this.begin(condition);
 
408
    }});
 
409
lexer.options = {};
 
410
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
 
411
 
 
412
var YYSTATE=YY_START
 
413
switch($avoiding_name_collisions) {
 
414
case 0: yy_.yytext = "\\"; return 14;
 
415
break;
 
416
case 1:
 
417
                                   if(yy_.yytext.slice(-1) !== "\\") this.begin("mu");
 
418
                                   if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1), this.begin("emu");
 
419
                                   if(yy_.yytext) return 14;
 
420
 
 
421
break;
 
422
case 2: return 14;
 
423
break;
 
424
case 3:
 
425
                                   if(yy_.yytext.slice(-1) !== "\\") this.popState();
 
426
                                   if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1);
 
427
                                   return 14;
 
428
 
 
429
break;
 
430
case 4: yy_.yytext = yy_.yytext.substr(0, yy_.yyleng-4); this.popState(); return 15;
 
431
break;
 
432
case 5: return 25;
 
433
break;
 
434
case 6: return 16;
 
435
break;
 
436
case 7: return 20;
 
437
break;
 
438
case 8: return 19;
 
439
break;
 
440
case 9: return 19;
 
441
break;
 
442
case 10: return 23;
 
443
break;
 
444
case 11: return 22;
 
445
break;
 
446
case 12: this.popState(); this.begin('com');
 
447
break;
 
448
case 13: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
 
449
break;
 
450
case 14: return 22;
 
451
break;
 
452
case 15: return 37;
 
453
break;
 
454
case 16: return 36;
 
455
break;
 
456
case 17: return 36;
 
457
break;
 
458
case 18: return 40;
 
459
break;
 
460
case 19: /*ignore whitespace*/
 
461
break;
 
462
case 20: this.popState(); return 24;
 
463
break;
 
464
case 21: this.popState(); return 18;
 
465
break;
 
466
case 22: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 31;
 
467
break;
 
468
case 23: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\'/g,"'"); return 31;
 
469
break;
 
470
case 24: return 38;
 
471
break;
 
472
case 25: return 33;
 
473
break;
 
474
case 26: return 33;
 
475
break;
 
476
case 27: return 32;
 
477
break;
 
478
case 28: return 36;
 
479
break;
 
480
case 29: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 36;
 
481
break;
 
482
case 30: return 'INVALID';
 
483
break;
 
484
case 31: return 5;
 
485
break;
 
486
}
 
487
};
 
488
lexer.rules = [/^(?:\\\\(?=(\{\{)))/,/^(?:[^\x00]*?(?=(\{\{)))/,/^(?:[^\x00]+)/,/^(?:[^\x00]{2,}?(?=(\{\{|$)))/,/^(?:[\s\S]*?--\}\})/,/^(?:\{\{>)/,/^(?:\{\{#)/,/^(?:\{\{\/)/,/^(?:\{\{\^)/,/^(?:\{\{\s*else\b)/,/^(?:\{\{\{)/,/^(?:\{\{&)/,/^(?:\{\{!--)/,/^(?:\{\{![\s\S]*?\}\})/,/^(?:\{\{)/,/^(?:=)/,/^(?:\.(?=[}\/ ]))/,/^(?:\.\.)/,/^(?:[\/.])/,/^(?:\s+)/,/^(?:\}\}\})/,/^(?:\}\})/,/^(?:"(\\["]|[^"])*")/,/^(?:'(\\[']|[^'])*')/,/^(?:@)/,/^(?:true(?=[}\s]))/,/^(?:false(?=[}\s]))/,/^(?:-?[0-9]+(?=[}\s]))/,/^(?:[^\s!"#%-,\.\/;->@\[-\^`\{-~]+(?=[=}\s\/.]))/,/^(?:\[[^\]]*\])/,/^(?:.)/,/^(?:$)/];
 
489
lexer.conditions = {"mu":{"rules":[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],"inclusive":false},"emu":{"rules":[3],"inclusive":false},"com":{"rules":[4],"inclusive":false},"INITIAL":{"rules":[0,1,2,31],"inclusive":true}};
 
490
return lexer;})()
 
491
parser.lexer = lexer;
 
492
function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
 
493
return new Parser;
 
494
})();
 
495
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
 
496
 
 
497
Handlebars.Parser = handlebars;
 
498
 
 
499
Handlebars.parse = function(input) {
 
500
 
 
501
  // Just return if an already-compile AST was passed in.
 
502
  if(input.constructor === Handlebars.AST.ProgramNode) { return input; }
 
503
 
 
504
  Handlebars.Parser.yy = Handlebars.AST;
 
505
  return Handlebars.Parser.parse(input);
 
506
};
 
507
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
 
508
 
 
509
Handlebars.AST = {};
 
510
 
 
511
Handlebars.AST.ProgramNode = function(statements, inverse) {
 
512
  this.type = "program";
 
513
  this.statements = statements;
 
514
  if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
 
515
};
 
516
 
 
517
Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) {
 
518
  this.type = "mustache";
 
519
  this.escaped = !unescaped;
 
520
  this.hash = hash;
 
521
 
 
522
  var id = this.id = rawParams[0];
 
523
  var params = this.params = rawParams.slice(1);
 
524
 
 
525
  // a mustache is an eligible helper if:
 
526
  // * its id is simple (a single part, not `this` or `..`)
 
527
  var eligibleHelper = this.eligibleHelper = id.isSimple;
 
528
 
 
529
  // a mustache is definitely a helper if:
 
530
  // * it is an eligible helper, and
 
531
  // * it has at least one parameter or hash segment
 
532
  this.isHelper = eligibleHelper && (params.length || hash);
 
533
 
 
534
  // if a mustache is an eligible helper but not a definite
 
535
  // helper, it is ambiguous, and will be resolved in a later
 
536
  // pass or at runtime.
 
537
};
 
538
 
 
539
Handlebars.AST.PartialNode = function(partialName, context) {
 
540
  this.type         = "partial";
 
541
  this.partialName  = partialName;
 
542
  this.context      = context;
 
543
};
 
544
 
 
545
Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
 
546
  var verifyMatch = function(open, close) {
 
547
    if(open.original !== close.original) {
 
548
      throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
 
549
    }
 
550
  };
 
551
 
 
552
  verifyMatch(mustache.id, close);
 
553
  this.type = "block";
 
554
  this.mustache = mustache;
 
555
  this.program  = program;
 
556
  this.inverse  = inverse;
 
557
 
 
558
  if (this.inverse && !this.program) {
 
559
    this.isInverse = true;
 
560
  }
 
561
};
 
562
 
 
563
Handlebars.AST.ContentNode = function(string) {
 
564
  this.type = "content";
 
565
  this.string = string;
 
566
};
 
567
 
 
568
Handlebars.AST.HashNode = function(pairs) {
 
569
  this.type = "hash";
 
570
  this.pairs = pairs;
 
571
};
 
572
 
 
573
Handlebars.AST.IdNode = function(parts) {
 
574
  this.type = "ID";
 
575
 
 
576
  var original = "",
 
577
      dig = [],
 
578
      depth = 0;
 
579
 
 
580
  for(var i=0,l=parts.length; i<l; i++) {
 
581
    var part = parts[i].part;
 
582
    original += (parts[i].separator || '') + part;
 
583
 
 
584
    if (part === ".." || part === "." || part === "this") {
 
585
      if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + original); }
 
586
      else if (part === "..") { depth++; }
 
587
      else { this.isScoped = true; }
 
588
    }
 
589
    else { dig.push(part); }
 
590
  }
 
591
 
 
592
  this.original = original;
 
593
  this.parts    = dig;
 
594
  this.string   = dig.join('.');
 
595
  this.depth    = depth;
 
596
 
 
597
  // an ID is simple if it only has one part, and that part is not
 
598
  // `..` or `this`.
 
599
  this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
 
600
 
 
601
  this.stringModeValue = this.string;
 
602
};
 
603
 
 
604
Handlebars.AST.PartialNameNode = function(name) {
 
605
  this.type = "PARTIAL_NAME";
 
606
  this.name = name.original;
 
607
};
 
608
 
 
609
Handlebars.AST.DataNode = function(id) {
 
610
  this.type = "DATA";
 
611
  this.id = id;
 
612
};
 
613
 
 
614
Handlebars.AST.StringNode = function(string) {
 
615
  this.type = "STRING";
 
616
  this.original =
 
617
    this.string =
 
618
    this.stringModeValue = string;
 
619
};
 
620
 
 
621
Handlebars.AST.IntegerNode = function(integer) {
 
622
  this.type = "INTEGER";
 
623
  this.original =
 
624
    this.integer = integer;
 
625
  this.stringModeValue = Number(integer);
 
626
};
 
627
 
 
628
Handlebars.AST.BooleanNode = function(bool) {
 
629
  this.type = "BOOLEAN";
 
630
  this.bool = bool;
 
631
  this.stringModeValue = bool === "true";
 
632
};
 
633
 
 
634
Handlebars.AST.CommentNode = function(comment) {
 
635
  this.type = "comment";
 
636
  this.comment = comment;
 
637
};
 
638
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
 
639
 
 
640
/*jshint eqnull:true*/
 
641
var Compiler = Handlebars.Compiler = function() {};
 
642
var JavaScriptCompiler = Handlebars.JavaScriptCompiler = function() {};
 
643
 
 
644
// the foundHelper register will disambiguate helper lookup from finding a
 
645
// function in a context. This is necessary for mustache compatibility, which
 
646
// requires that context functions in blocks are evaluated by blockHelperMissing,
 
647
// and then proceed as if the resulting value was provided to blockHelperMissing.
 
648
 
 
649
Compiler.prototype = {
 
650
  compiler: Compiler,
 
651
 
 
652
  disassemble: function() {
 
653
    var opcodes = this.opcodes, opcode, out = [], params, param;
 
654
 
 
655
    for (var i=0, l=opcodes.length; i<l; i++) {
 
656
      opcode = opcodes[i];
 
657
 
 
658
      if (opcode.opcode === 'DECLARE') {
 
659
        out.push("DECLARE " + opcode.name + "=" + opcode.value);
 
660
      } else {
 
661
        params = [];
 
662
        for (var j=0; j<opcode.args.length; j++) {
 
663
          param = opcode.args[j];
 
664
          if (typeof param === "string") {
 
665
            param = "\"" + param.replace("\n", "\\n") + "\"";
 
666
          }
 
667
          params.push(param);
 
668
        }
 
669
        out.push(opcode.opcode + " " + params.join(" "));
 
670
      }
 
671
    }
 
672
 
 
673
    return out.join("\n");
 
674
  },
 
675
  equals: function(other) {
 
676
    var len = this.opcodes.length;
 
677
    if (other.opcodes.length !== len) {
 
678
      return false;
 
679
    }
 
680
 
 
681
    for (var i = 0; i < len; i++) {
 
682
      var opcode = this.opcodes[i],
 
683
          otherOpcode = other.opcodes[i];
 
684
      if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) {
 
685
        return false;
 
686
      }
 
687
      for (var j = 0; j < opcode.args.length; j++) {
 
688
        if (opcode.args[j] !== otherOpcode.args[j]) {
 
689
          return false;
 
690
        }
 
691
      }
 
692
    }
 
693
 
 
694
    len = this.children.length;
 
695
    if (other.children.length !== len) {
 
696
      return false;
 
697
    }
 
698
    for (i = 0; i < len; i++) {
 
699
      if (!this.children[i].equals(other.children[i])) {
 
700
        return false;
 
701
      }
 
702
    }
 
703
 
 
704
    return true;
 
705
  },
 
706
 
 
707
  guid: 0,
 
708
 
 
709
  compile: function(program, options) {
 
710
    this.children = [];
 
711
    this.depths = {list: []};
 
712
    this.options = options;
 
713
 
 
714
    // These changes will propagate to the other compiler components
 
715
    var knownHelpers = this.options.knownHelpers;
 
716
    this.options.knownHelpers = {
 
717
      'helperMissing': true,
 
718
      'blockHelperMissing': true,
 
719
      'each': true,
 
720
      'if': true,
 
721
      'unless': true,
 
722
      'with': true,
 
723
      'log': true
 
724
    };
 
725
    if (knownHelpers) {
 
726
      for (var name in knownHelpers) {
 
727
        this.options.knownHelpers[name] = knownHelpers[name];
 
728
      }
 
729
    }
 
730
 
 
731
    return this.program(program);
 
732
  },
 
733
 
 
734
  accept: function(node) {
 
735
    return this[node.type](node);
 
736
  },
 
737
 
 
738
  program: function(program) {
 
739
    var statements = program.statements, statement;
 
740
    this.opcodes = [];
 
741
 
 
742
    for(var i=0, l=statements.length; i<l; i++) {
 
743
      statement = statements[i];
 
744
      this[statement.type](statement);
 
745
    }
 
746
    this.isSimple = l === 1;
 
747
 
 
748
    this.depths.list = this.depths.list.sort(function(a, b) {
 
749
      return a - b;
 
750
    });
 
751
 
 
752
    return this;
 
753
  },
 
754
 
 
755
  compileProgram: function(program) {
 
756
    var result = new this.compiler().compile(program, this.options);
 
757
    var guid = this.guid++, depth;
 
758
 
 
759
    this.usePartial = this.usePartial || result.usePartial;
 
760
 
 
761
    this.children[guid] = result;
 
762
 
 
763
    for(var i=0, l=result.depths.list.length; i<l; i++) {
 
764
      depth = result.depths.list[i];
 
765
 
 
766
      if(depth < 2) { continue; }
 
767
      else { this.addDepth(depth - 1); }
 
768
    }
 
769
 
 
770
    return guid;
 
771
  },
 
772
 
 
773
  block: function(block) {
 
774
    var mustache = block.mustache,
 
775
        program = block.program,
 
776
        inverse = block.inverse;
 
777
 
 
778
    if (program) {
 
779
      program = this.compileProgram(program);
 
780
    }
 
781
 
 
782
    if (inverse) {
 
783
      inverse = this.compileProgram(inverse);
 
784
    }
 
785
 
 
786
    var type = this.classifyMustache(mustache);
 
787
 
 
788
    if (type === "helper") {
 
789
      this.helperMustache(mustache, program, inverse);
 
790
    } else if (type === "simple") {
 
791
      this.simpleMustache(mustache);
 
792
 
 
793
      // now that the simple mustache is resolved, we need to
 
794
      // evaluate it by executing `blockHelperMissing`
 
795
      this.opcode('pushProgram', program);
 
796
      this.opcode('pushProgram', inverse);
 
797
      this.opcode('emptyHash');
 
798
      this.opcode('blockValue');
 
799
    } else {
 
800
      this.ambiguousMustache(mustache, program, inverse);
 
801
 
 
802
      // now that the simple mustache is resolved, we need to
 
803
      // evaluate it by executing `blockHelperMissing`
 
804
      this.opcode('pushProgram', program);
 
805
      this.opcode('pushProgram', inverse);
 
806
      this.opcode('emptyHash');
 
807
      this.opcode('ambiguousBlockValue');
 
808
    }
 
809
 
 
810
    this.opcode('append');
 
811
  },
 
812
 
 
813
  hash: function(hash) {
 
814
    var pairs = hash.pairs, pair, val;
 
815
 
 
816
    this.opcode('pushHash');
 
817
 
 
818
    for(var i=0, l=pairs.length; i<l; i++) {
 
819
      pair = pairs[i];
 
820
      val  = pair[1];
 
821
 
 
822
      if (this.options.stringParams) {
 
823
        if(val.depth) {
 
824
          this.addDepth(val.depth);
 
825
        }
 
826
        this.opcode('getContext', val.depth || 0);
 
827
        this.opcode('pushStringParam', val.stringModeValue, val.type);
 
828
      } else {
 
829
        this.accept(val);
 
830
      }
 
831
 
 
832
      this.opcode('assignToHash', pair[0]);
 
833
    }
 
834
    this.opcode('popHash');
 
835
  },
 
836
 
 
837
  partial: function(partial) {
 
838
    var partialName = partial.partialName;
 
839
    this.usePartial = true;
 
840
 
 
841
    if(partial.context) {
 
842
      this.ID(partial.context);
 
843
    } else {
 
844
      this.opcode('push', 'depth0');
 
845
    }
 
846
 
 
847
    this.opcode('invokePartial', partialName.name);
 
848
    this.opcode('append');
 
849
  },
 
850
 
 
851
  content: function(content) {
 
852
    this.opcode('appendContent', content.string);
 
853
  },
 
854
 
 
855
  mustache: function(mustache) {
 
856
    var options = this.options;
 
857
    var type = this.classifyMustache(mustache);
 
858
 
 
859
    if (type === "simple") {
 
860
      this.simpleMustache(mustache);
 
861
    } else if (type === "helper") {
 
862
      this.helperMustache(mustache);
 
863
    } else {
 
864
      this.ambiguousMustache(mustache);
 
865
    }
 
866
 
 
867
    if(mustache.escaped && !options.noEscape) {
 
868
      this.opcode('appendEscaped');
 
869
    } else {
 
870
      this.opcode('append');
 
871
    }
 
872
  },
 
873
 
 
874
  ambiguousMustache: function(mustache, program, inverse) {
 
875
    var id = mustache.id,
 
876
        name = id.parts[0],
 
877
        isBlock = program != null || inverse != null;
 
878
 
 
879
    this.opcode('getContext', id.depth);
 
880
 
 
881
    this.opcode('pushProgram', program);
 
882
    this.opcode('pushProgram', inverse);
 
883
 
 
884
    this.opcode('invokeAmbiguous', name, isBlock);
 
885
  },
 
886
 
 
887
  simpleMustache: function(mustache) {
 
888
    var id = mustache.id;
 
889
 
 
890
    if (id.type === 'DATA') {
 
891
      this.DATA(id);
 
892
    } else if (id.parts.length) {
 
893
      this.ID(id);
 
894
    } else {
 
895
      // Simplified ID for `this`
 
896
      this.addDepth(id.depth);
 
897
      this.opcode('getContext', id.depth);
 
898
      this.opcode('pushContext');
 
899
    }
 
900
 
 
901
    this.opcode('resolvePossibleLambda');
 
902
  },
 
903
 
 
904
  helperMustache: function(mustache, program, inverse) {
 
905
    var params = this.setupFullMustacheParams(mustache, program, inverse),
 
906
        name = mustache.id.parts[0];
 
907
 
 
908
    if (this.options.knownHelpers[name]) {
 
909
      this.opcode('invokeKnownHelper', params.length, name);
 
910
    } else if (this.options.knownHelpersOnly) {
 
911
      throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name);
 
912
    } else {
 
913
      this.opcode('invokeHelper', params.length, name);
 
914
    }
 
915
  },
 
916
 
 
917
  ID: function(id) {
 
918
    this.addDepth(id.depth);
 
919
    this.opcode('getContext', id.depth);
 
920
 
 
921
    var name = id.parts[0];
 
922
    if (!name) {
 
923
      this.opcode('pushContext');
 
924
    } else {
 
925
      this.opcode('lookupOnContext', id.parts[0]);
 
926
    }
 
927
 
 
928
    for(var i=1, l=id.parts.length; i<l; i++) {
 
929
      this.opcode('lookup', id.parts[i]);
 
930
    }
 
931
  },
 
932
 
 
933
  DATA: function(data) {
 
934
    this.options.data = true;
 
935
    if (data.id.isScoped || data.id.depth) {
 
936
      throw new Handlebars.Exception('Scoped data references are not supported: ' + data.original);
 
937
    }
 
938
 
 
939
    this.opcode('lookupData');
 
940
    var parts = data.id.parts;
 
941
    for(var i=0, l=parts.length; i<l; i++) {
 
942
      this.opcode('lookup', parts[i]);
 
943
    }
 
944
  },
 
945
 
 
946
  STRING: function(string) {
 
947
    this.opcode('pushString', string.string);
 
948
  },
 
949
 
 
950
  INTEGER: function(integer) {
 
951
    this.opcode('pushLiteral', integer.integer);
 
952
  },
 
953
 
 
954
  BOOLEAN: function(bool) {
 
955
    this.opcode('pushLiteral', bool.bool);
 
956
  },
 
957
 
 
958
  comment: function() {},
 
959
 
 
960
  // HELPERS
 
961
  opcode: function(name) {
 
962
    this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) });
 
963
  },
 
964
 
 
965
  declare: function(name, value) {
 
966
    this.opcodes.push({ opcode: 'DECLARE', name: name, value: value });
 
967
  },
 
968
 
 
969
  addDepth: function(depth) {
 
970
    if(isNaN(depth)) { throw new Error("EWOT"); }
 
971
    if(depth === 0) { return; }
 
972
 
 
973
    if(!this.depths[depth]) {
 
974
      this.depths[depth] = true;
 
975
      this.depths.list.push(depth);
 
976
    }
 
977
  },
 
978
 
 
979
  classifyMustache: function(mustache) {
 
980
    var isHelper   = mustache.isHelper;
 
981
    var isEligible = mustache.eligibleHelper;
 
982
    var options    = this.options;
 
983
 
 
984
    // if ambiguous, we can possibly resolve the ambiguity now
 
985
    if (isEligible && !isHelper) {
 
986
      var name = mustache.id.parts[0];
 
987
 
 
988
      if (options.knownHelpers[name]) {
 
989
        isHelper = true;
 
990
      } else if (options.knownHelpersOnly) {
 
991
        isEligible = false;
 
992
      }
 
993
    }
 
994
 
 
995
    if (isHelper) { return "helper"; }
 
996
    else if (isEligible) { return "ambiguous"; }
 
997
    else { return "simple"; }
 
998
  },
 
999
 
 
1000
  pushParams: function(params) {
 
1001
    var i = params.length, param;
 
1002
 
 
1003
    while(i--) {
 
1004
      param = params[i];
 
1005
 
 
1006
      if(this.options.stringParams) {
 
1007
        if(param.depth) {
 
1008
          this.addDepth(param.depth);
 
1009
        }
 
1010
 
 
1011
        this.opcode('getContext', param.depth || 0);
 
1012
        this.opcode('pushStringParam', param.stringModeValue, param.type);
 
1013
      } else {
 
1014
        this[param.type](param);
 
1015
      }
 
1016
    }
 
1017
  },
 
1018
 
 
1019
  setupMustacheParams: function(mustache) {
 
1020
    var params = mustache.params;
 
1021
    this.pushParams(params);
 
1022
 
 
1023
    if(mustache.hash) {
 
1024
      this.hash(mustache.hash);
 
1025
    } else {
 
1026
      this.opcode('emptyHash');
 
1027
    }
 
1028
 
 
1029
    return params;
 
1030
  },
 
1031
 
 
1032
  // this will replace setupMustacheParams when we're done
 
1033
  setupFullMustacheParams: function(mustache, program, inverse) {
 
1034
    var params = mustache.params;
 
1035
    this.pushParams(params);
 
1036
 
 
1037
    this.opcode('pushProgram', program);
 
1038
    this.opcode('pushProgram', inverse);
 
1039
 
 
1040
    if(mustache.hash) {
 
1041
      this.hash(mustache.hash);
 
1042
    } else {
 
1043
      this.opcode('emptyHash');
 
1044
    }
 
1045
 
 
1046
    return params;
 
1047
  }
 
1048
};
 
1049
 
 
1050
var Literal = function(value) {
 
1051
  this.value = value;
 
1052
};
 
1053
 
 
1054
JavaScriptCompiler.prototype = {
 
1055
  // PUBLIC API: You can override these methods in a subclass to provide
 
1056
  // alternative compiled forms for name lookup and buffering semantics
 
1057
  nameLookup: function(parent, name /* , type*/) {
 
1058
    if (/^[0-9]+$/.test(name)) {
 
1059
      return parent + "[" + name + "]";
 
1060
    } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
 
1061
      return parent + "." + name;
 
1062
    }
 
1063
    else {
 
1064
      return parent + "['" + name + "']";
 
1065
    }
 
1066
  },
 
1067
 
 
1068
  appendToBuffer: function(string) {
 
1069
    if (this.environment.isSimple) {
 
1070
      return "return " + string + ";";
 
1071
    } else {
 
1072
      return {
 
1073
        appendToBuffer: true,
 
1074
        content: string,
 
1075
        toString: function() { return "buffer += " + string + ";"; }
 
1076
      };
 
1077
    }
 
1078
  },
 
1079
 
 
1080
  initializeBuffer: function() {
 
1081
    return this.quotedString("");
 
1082
  },
 
1083
 
 
1084
  namespace: "Handlebars",
 
1085
  // END PUBLIC API
 
1086
 
 
1087
  compile: function(environment, options, context, asObject) {
 
1088
    this.environment = environment;
 
1089
    this.options = options || {};
 
1090
 
 
1091
    Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
 
1092
 
 
1093
    this.name = this.environment.name;
 
1094
    this.isChild = !!context;
 
1095
    this.context = context || {
 
1096
      programs: [],
 
1097
      environments: [],
 
1098
      aliases: { }
 
1099
    };
 
1100
 
 
1101
    this.preamble();
 
1102
 
 
1103
    this.stackSlot = 0;
 
1104
    this.stackVars = [];
 
1105
    this.registers = { list: [] };
 
1106
    this.compileStack = [];
 
1107
    this.inlineStack = [];
 
1108
 
 
1109
    this.compileChildren(environment, options);
 
1110
 
 
1111
    var opcodes = environment.opcodes, opcode;
 
1112
 
 
1113
    this.i = 0;
 
1114
 
 
1115
    for(l=opcodes.length; this.i<l; this.i++) {
 
1116
      opcode = opcodes[this.i];
 
1117
 
 
1118
      if(opcode.opcode === 'DECLARE') {
 
1119
        this[opcode.name] = opcode.value;
 
1120
      } else {
 
1121
        this[opcode.opcode].apply(this, opcode.args);
 
1122
      }
 
1123
    }
 
1124
 
 
1125
    return this.createFunctionContext(asObject);
 
1126
  },
 
1127
 
 
1128
  nextOpcode: function() {
 
1129
    var opcodes = this.environment.opcodes;
 
1130
    return opcodes[this.i + 1];
 
1131
  },
 
1132
 
 
1133
  eat: function() {
 
1134
    this.i = this.i + 1;
 
1135
  },
 
1136
 
 
1137
  preamble: function() {
 
1138
    var out = [];
 
1139
 
 
1140
    if (!this.isChild) {
 
1141
      var namespace = this.namespace;
 
1142
 
 
1143
      var copies = "helpers = this.merge(helpers, " + namespace + ".helpers);";
 
1144
      if (this.environment.usePartial) { copies = copies + " partials = this.merge(partials, " + namespace + ".partials);"; }
 
1145
      if (this.options.data) { copies = copies + " data = data || {};"; }
 
1146
      out.push(copies);
 
1147
    } else {
 
1148
      out.push('');
 
1149
    }
 
1150
 
 
1151
    if (!this.environment.isSimple) {
 
1152
      out.push(", buffer = " + this.initializeBuffer());
 
1153
    } else {
 
1154
      out.push("");
 
1155
    }
 
1156
 
 
1157
    // track the last context pushed into place to allow skipping the
 
1158
    // getContext opcode when it would be a noop
 
1159
    this.lastContext = 0;
 
1160
    this.source = out;
 
1161
  },
 
1162
 
 
1163
  createFunctionContext: function(asObject) {
 
1164
    var locals = this.stackVars.concat(this.registers.list);
 
1165
 
 
1166
    if(locals.length > 0) {
 
1167
      this.source[1] = this.source[1] + ", " + locals.join(", ");
 
1168
    }
 
1169
 
 
1170
    // Generate minimizer alias mappings
 
1171
    if (!this.isChild) {
 
1172
      for (var alias in this.context.aliases) {
 
1173
        if (this.context.aliases.hasOwnProperty(alias)) {
 
1174
          this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
 
1175
        }
 
1176
      }
 
1177
    }
 
1178
 
 
1179
    if (this.source[1]) {
 
1180
      this.source[1] = "var " + this.source[1].substring(2) + ";";
 
1181
    }
 
1182
 
 
1183
    // Merge children
 
1184
    if (!this.isChild) {
 
1185
      this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
 
1186
    }
 
1187
 
 
1188
    if (!this.environment.isSimple) {
 
1189
      this.source.push("return buffer;");
 
1190
    }
 
1191
 
 
1192
    var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
 
1193
 
 
1194
    for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
 
1195
      params.push("depth" + this.environment.depths.list[i]);
 
1196
    }
 
1197
 
 
1198
    // Perform a second pass over the output to merge content when possible
 
1199
    var source = this.mergeSource();
 
1200
 
 
1201
    if (!this.isChild) {
 
1202
      var revision = Handlebars.COMPILER_REVISION,
 
1203
          versions = Handlebars.REVISION_CHANGES[revision];
 
1204
      source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source;
 
1205
    }
 
1206
 
 
1207
    if (asObject) {
 
1208
      params.push(source);
 
1209
 
 
1210
      return Function.apply(this, params);
 
1211
    } else {
 
1212
      var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n  ' + source + '}';
 
1213
      Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
 
1214
      return functionSource;
 
1215
    }
 
1216
  },
 
1217
  mergeSource: function() {
 
1218
    // WARN: We are not handling the case where buffer is still populated as the source should
 
1219
    // not have buffer append operations as their final action.
 
1220
    var source = '',
 
1221
        buffer;
 
1222
    for (var i = 0, len = this.source.length; i < len; i++) {
 
1223
      var line = this.source[i];
 
1224
      if (line.appendToBuffer) {
 
1225
        if (buffer) {
 
1226
          buffer = buffer + '\n    + ' + line.content;
 
1227
        } else {
 
1228
          buffer = line.content;
 
1229
        }
 
1230
      } else {
 
1231
        if (buffer) {
 
1232
          source += 'buffer += ' + buffer + ';\n  ';
 
1233
          buffer = undefined;
 
1234
        }
 
1235
        source += line + '\n  ';
 
1236
      }
 
1237
    }
 
1238
    return source;
 
1239
  },
 
1240
 
 
1241
  // [blockValue]
 
1242
  //
 
1243
  // On stack, before: hash, inverse, program, value
 
1244
  // On stack, after: return value of blockHelperMissing
 
1245
  //
 
1246
  // The purpose of this opcode is to take a block of the form
 
1247
  // `{{#foo}}...{{/foo}}`, resolve the value of `foo`, and
 
1248
  // replace it on the stack with the result of properly
 
1249
  // invoking blockHelperMissing.
 
1250
  blockValue: function() {
 
1251
    this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
 
1252
 
 
1253
    var params = ["depth0"];
 
1254
    this.setupParams(0, params);
 
1255
 
 
1256
    this.replaceStack(function(current) {
 
1257
      params.splice(1, 0, current);
 
1258
      return "blockHelperMissing.call(" + params.join(", ") + ")";
 
1259
    });
 
1260
  },
 
1261
 
 
1262
  // [ambiguousBlockValue]
 
1263
  //
 
1264
  // On stack, before: hash, inverse, program, value
 
1265
  // Compiler value, before: lastHelper=value of last found helper, if any
 
1266
  // On stack, after, if no lastHelper: same as [blockValue]
 
1267
  // On stack, after, if lastHelper: value
 
1268
  ambiguousBlockValue: function() {
 
1269
    this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
 
1270
 
 
1271
    var params = ["depth0"];
 
1272
    this.setupParams(0, params);
 
1273
 
 
1274
    var current = this.topStack();
 
1275
    params.splice(1, 0, current);
 
1276
 
 
1277
    // Use the options value generated from the invocation
 
1278
    params[params.length-1] = 'options';
 
1279
 
 
1280
    this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
 
1281
  },
 
1282
 
 
1283
  // [appendContent]
 
1284
  //
 
1285
  // On stack, before: ...
 
1286
  // On stack, after: ...
 
1287
  //
 
1288
  // Appends the string value of `content` to the current buffer
 
1289
  appendContent: function(content) {
 
1290
    this.source.push(this.appendToBuffer(this.quotedString(content)));
 
1291
  },
 
1292
 
 
1293
  // [append]
 
1294
  //
 
1295
  // On stack, before: value, ...
 
1296
  // On stack, after: ...
 
1297
  //
 
1298
  // Coerces `value` to a String and appends it to the current buffer.
 
1299
  //
 
1300
  // If `value` is truthy, or 0, it is coerced into a string and appended
 
1301
  // Otherwise, the empty string is appended
 
1302
  append: function() {
 
1303
    // Force anything that is inlined onto the stack so we don't have duplication
 
1304
    // when we examine local
 
1305
    this.flushInline();
 
1306
    var local = this.popStack();
 
1307
    this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
 
1308
    if (this.environment.isSimple) {
 
1309
      this.source.push("else { " + this.appendToBuffer("''") + " }");
 
1310
    }
 
1311
  },
 
1312
 
 
1313
  // [appendEscaped]
 
1314
  //
 
1315
  // On stack, before: value, ...
 
1316
  // On stack, after: ...
 
1317
  //
 
1318
  // Escape `value` and append it to the buffer
 
1319
  appendEscaped: function() {
 
1320
    this.context.aliases.escapeExpression = 'this.escapeExpression';
 
1321
 
 
1322
    this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
 
1323
  },
 
1324
 
 
1325
  // [getContext]
 
1326
  //
 
1327
  // On stack, before: ...
 
1328
  // On stack, after: ...
 
1329
  // Compiler value, after: lastContext=depth
 
1330
  //
 
1331
  // Set the value of the `lastContext` compiler value to the depth
 
1332
  getContext: function(depth) {
 
1333
    if(this.lastContext !== depth) {
 
1334
      this.lastContext = depth;
 
1335
    }
 
1336
  },
 
1337
 
 
1338
  // [lookupOnContext]
 
1339
  //
 
1340
  // On stack, before: ...
 
1341
  // On stack, after: currentContext[name], ...
 
1342
  //
 
1343
  // Looks up the value of `name` on the current context and pushes
 
1344
  // it onto the stack.
 
1345
  lookupOnContext: function(name) {
 
1346
    this.push(this.nameLookup('depth' + this.lastContext, name, 'context'));
 
1347
  },
 
1348
 
 
1349
  // [pushContext]
 
1350
  //
 
1351
  // On stack, before: ...
 
1352
  // On stack, after: currentContext, ...
 
1353
  //
 
1354
  // Pushes the value of the current context onto the stack.
 
1355
  pushContext: function() {
 
1356
    this.pushStackLiteral('depth' + this.lastContext);
 
1357
  },
 
1358
 
 
1359
  // [resolvePossibleLambda]
 
1360
  //
 
1361
  // On stack, before: value, ...
 
1362
  // On stack, after: resolved value, ...
 
1363
  //
 
1364
  // If the `value` is a lambda, replace it on the stack by
 
1365
  // the return value of the lambda
 
1366
  resolvePossibleLambda: function() {
 
1367
    this.context.aliases.functionType = '"function"';
 
1368
 
 
1369
    this.replaceStack(function(current) {
 
1370
      return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
 
1371
    });
 
1372
  },
 
1373
 
 
1374
  // [lookup]
 
1375
  //
 
1376
  // On stack, before: value, ...
 
1377
  // On stack, after: value[name], ...
 
1378
  //
 
1379
  // Replace the value on the stack with the result of looking
 
1380
  // up `name` on `value`
 
1381
  lookup: function(name) {
 
1382
    this.replaceStack(function(current) {
 
1383
      return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
 
1384
    });
 
1385
  },
 
1386
 
 
1387
  // [lookupData]
 
1388
  //
 
1389
  // On stack, before: ...
 
1390
  // On stack, after: data[id], ...
 
1391
  //
 
1392
  // Push the result of looking up `id` on the current data
 
1393
  lookupData: function(id) {
 
1394
    this.push('data');
 
1395
  },
 
1396
 
 
1397
  // [pushStringParam]
 
1398
  //
 
1399
  // On stack, before: ...
 
1400
  // On stack, after: string, currentContext, ...
 
1401
  //
 
1402
  // This opcode is designed for use in string mode, which
 
1403
  // provides the string value of a parameter along with its
 
1404
  // depth rather than resolving it immediately.
 
1405
  pushStringParam: function(string, type) {
 
1406
    this.pushStackLiteral('depth' + this.lastContext);
 
1407
 
 
1408
    this.pushString(type);
 
1409
 
 
1410
    if (typeof string === 'string') {
 
1411
      this.pushString(string);
 
1412
    } else {
 
1413
      this.pushStackLiteral(string);
 
1414
    }
 
1415
  },
 
1416
 
 
1417
  emptyHash: function() {
 
1418
    this.pushStackLiteral('{}');
 
1419
 
 
1420
    if (this.options.stringParams) {
 
1421
      this.register('hashTypes', '{}');
 
1422
      this.register('hashContexts', '{}');
 
1423
    }
 
1424
  },
 
1425
  pushHash: function() {
 
1426
    this.hash = {values: [], types: [], contexts: []};
 
1427
  },
 
1428
  popHash: function() {
 
1429
    var hash = this.hash;
 
1430
    this.hash = undefined;
 
1431
 
 
1432
    if (this.options.stringParams) {
 
1433
      this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
 
1434
      this.register('hashTypes', '{' + hash.types.join(',') + '}');
 
1435
    }
 
1436
    this.push('{\n    ' + hash.values.join(',\n    ') + '\n  }');
 
1437
  },
 
1438
 
 
1439
  // [pushString]
 
1440
  //
 
1441
  // On stack, before: ...
 
1442
  // On stack, after: quotedString(string), ...
 
1443
  //
 
1444
  // Push a quoted version of `string` onto the stack
 
1445
  pushString: function(string) {
 
1446
    this.pushStackLiteral(this.quotedString(string));
 
1447
  },
 
1448
 
 
1449
  // [push]
 
1450
  //
 
1451
  // On stack, before: ...
 
1452
  // On stack, after: expr, ...
 
1453
  //
 
1454
  // Push an expression onto the stack
 
1455
  push: function(expr) {
 
1456
    this.inlineStack.push(expr);
 
1457
    return expr;
 
1458
  },
 
1459
 
 
1460
  // [pushLiteral]
 
1461
  //
 
1462
  // On stack, before: ...
 
1463
  // On stack, after: value, ...
 
1464
  //
 
1465
  // Pushes a value onto the stack. This operation prevents
 
1466
  // the compiler from creating a temporary variable to hold
 
1467
  // it.
 
1468
  pushLiteral: function(value) {
 
1469
    this.pushStackLiteral(value);
 
1470
  },
 
1471
 
 
1472
  // [pushProgram]
 
1473
  //
 
1474
  // On stack, before: ...
 
1475
  // On stack, after: program(guid), ...
 
1476
  //
 
1477
  // Push a program expression onto the stack. This takes
 
1478
  // a compile-time guid and converts it into a runtime-accessible
 
1479
  // expression.
 
1480
  pushProgram: function(guid) {
 
1481
    if (guid != null) {
 
1482
      this.pushStackLiteral(this.programExpression(guid));
 
1483
    } else {
 
1484
      this.pushStackLiteral(null);
 
1485
    }
 
1486
  },
 
1487
 
 
1488
  // [invokeHelper]
 
1489
  //
 
1490
  // On stack, before: hash, inverse, program, params..., ...
 
1491
  // On stack, after: result of helper invocation
 
1492
  //
 
1493
  // Pops off the helper's parameters, invokes the helper,
 
1494
  // and pushes the helper's return value onto the stack.
 
1495
  //
 
1496
  // If the helper is not found, `helperMissing` is called.
 
1497
  invokeHelper: function(paramSize, name) {
 
1498
    this.context.aliases.helperMissing = 'helpers.helperMissing';
 
1499
 
 
1500
    var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
 
1501
    var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
 
1502
 
 
1503
    this.push(helper.name + ' || ' + nonHelper);
 
1504
    this.replaceStack(function(name) {
 
1505
      return name + ' ? ' + name + '.call(' +
 
1506
          helper.callParams + ") " + ": helperMissing.call(" +
 
1507
          helper.helperMissingParams + ")";
 
1508
    });
 
1509
  },
 
1510
 
 
1511
  // [invokeKnownHelper]
 
1512
  //
 
1513
  // On stack, before: hash, inverse, program, params..., ...
 
1514
  // On stack, after: result of helper invocation
 
1515
  //
 
1516
  // This operation is used when the helper is known to exist,
 
1517
  // so a `helperMissing` fallback is not required.
 
1518
  invokeKnownHelper: function(paramSize, name) {
 
1519
    var helper = this.setupHelper(paramSize, name);
 
1520
    this.push(helper.name + ".call(" + helper.callParams + ")");
 
1521
  },
 
1522
 
 
1523
  // [invokeAmbiguous]
 
1524
  //
 
1525
  // On stack, before: hash, inverse, program, params..., ...
 
1526
  // On stack, after: result of disambiguation
 
1527
  //
 
1528
  // This operation is used when an expression like `{{foo}}`
 
1529
  // is provided, but we don't know at compile-time whether it
 
1530
  // is a helper or a path.
 
1531
  //
 
1532
  // This operation emits more code than the other options,
 
1533
  // and can be avoided by passing the `knownHelpers` and
 
1534
  // `knownHelpersOnly` flags at compile-time.
 
1535
  invokeAmbiguous: function(name, helperCall) {
 
1536
    this.context.aliases.functionType = '"function"';
 
1537
 
 
1538
    this.pushStackLiteral('{}');    // Hash value
 
1539
    var helper = this.setupHelper(0, name, helperCall);
 
1540
 
 
1541
    var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
 
1542
 
 
1543
    var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
 
1544
    var nextStack = this.nextStack();
 
1545
 
 
1546
    this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
 
1547
    this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
 
1548
  },
 
1549
 
 
1550
  // [invokePartial]
 
1551
  //
 
1552
  // On stack, before: context, ...
 
1553
  // On stack after: result of partial invocation
 
1554
  //
 
1555
  // This operation pops off a context, invokes a partial with that context,
 
1556
  // and pushes the result of the invocation back.
 
1557
  invokePartial: function(name) {
 
1558
    var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
 
1559
 
 
1560
    if (this.options.data) {
 
1561
      params.push("data");
 
1562
    }
 
1563
 
 
1564
    this.context.aliases.self = "this";
 
1565
    this.push("self.invokePartial(" + params.join(", ") + ")");
 
1566
  },
 
1567
 
 
1568
  // [assignToHash]
 
1569
  //
 
1570
  // On stack, before: value, hash, ...
 
1571
  // On stack, after: hash, ...
 
1572
  //
 
1573
  // Pops a value and hash off the stack, assigns `hash[key] = value`
 
1574
  // and pushes the hash back onto the stack.
 
1575
  assignToHash: function(key) {
 
1576
    var value = this.popStack(),
 
1577
        context,
 
1578
        type;
 
1579
 
 
1580
    if (this.options.stringParams) {
 
1581
      type = this.popStack();
 
1582
      context = this.popStack();
 
1583
    }
 
1584
 
 
1585
    var hash = this.hash;
 
1586
    if (context) {
 
1587
      hash.contexts.push("'" + key + "': " + context);
 
1588
    }
 
1589
    if (type) {
 
1590
      hash.types.push("'" + key + "': " + type);
 
1591
    }
 
1592
    hash.values.push("'" + key + "': (" + value + ")");
 
1593
  },
 
1594
 
 
1595
  // HELPERS
 
1596
 
 
1597
  compiler: JavaScriptCompiler,
 
1598
 
 
1599
  compileChildren: function(environment, options) {
 
1600
    var children = environment.children, child, compiler;
 
1601
 
 
1602
    for(var i=0, l=children.length; i<l; i++) {
 
1603
      child = children[i];
 
1604
      compiler = new this.compiler();
 
1605
 
 
1606
      var index = this.matchExistingProgram(child);
 
1607
 
 
1608
      if (index == null) {
 
1609
        this.context.programs.push('');     // Placeholder to prevent name conflicts for nested children
 
1610
        index = this.context.programs.length;
 
1611
        child.index = index;
 
1612
        child.name = 'program' + index;
 
1613
        this.context.programs[index] = compiler.compile(child, options, this.context);
 
1614
        this.context.environments[index] = child;
 
1615
      } else {
 
1616
        child.index = index;
 
1617
        child.name = 'program' + index;
 
1618
      }
 
1619
    }
 
1620
  },
 
1621
  matchExistingProgram: function(child) {
 
1622
    for (var i = 0, len = this.context.environments.length; i < len; i++) {
 
1623
      var environment = this.context.environments[i];
 
1624
      if (environment && environment.equals(child)) {
 
1625
        return i;
 
1626
      }
 
1627
    }
 
1628
  },
 
1629
 
 
1630
  programExpression: function(guid) {
 
1631
    this.context.aliases.self = "this";
 
1632
 
 
1633
    if(guid == null) {
 
1634
      return "self.noop";
 
1635
    }
 
1636
 
 
1637
    var child = this.environment.children[guid],
 
1638
        depths = child.depths.list, depth;
 
1639
 
 
1640
    var programParams = [child.index, child.name, "data"];
 
1641
 
 
1642
    for(var i=0, l = depths.length; i<l; i++) {
 
1643
      depth = depths[i];
 
1644
 
 
1645
      if(depth === 1) { programParams.push("depth0"); }
 
1646
      else { programParams.push("depth" + (depth - 1)); }
 
1647
    }
 
1648
 
 
1649
    return (depths.length === 0 ? "self.program(" : "self.programWithDepth(") + programParams.join(", ") + ")";
 
1650
  },
 
1651
 
 
1652
  register: function(name, val) {
 
1653
    this.useRegister(name);
 
1654
    this.source.push(name + " = " + val + ";");
 
1655
  },
 
1656
 
 
1657
  useRegister: function(name) {
 
1658
    if(!this.registers[name]) {
 
1659
      this.registers[name] = true;
 
1660
      this.registers.list.push(name);
 
1661
    }
 
1662
  },
 
1663
 
 
1664
  pushStackLiteral: function(item) {
 
1665
    return this.push(new Literal(item));
 
1666
  },
 
1667
 
 
1668
  pushStack: function(item) {
 
1669
    this.flushInline();
 
1670
 
 
1671
    var stack = this.incrStack();
 
1672
    if (item) {
 
1673
      this.source.push(stack + " = " + item + ";");
 
1674
    }
 
1675
    this.compileStack.push(stack);
 
1676
    return stack;
 
1677
  },
 
1678
 
 
1679
  replaceStack: function(callback) {
 
1680
    var prefix = '',
 
1681
        inline = this.isInline(),
 
1682
        stack;
 
1683
 
 
1684
    // If we are currently inline then we want to merge the inline statement into the
 
1685
    // replacement statement via ','
 
1686
    if (inline) {
 
1687
      var top = this.popStack(true);
 
1688
 
 
1689
      if (top instanceof Literal) {
 
1690
        // Literals do not need to be inlined
 
1691
        stack = top.value;
 
1692
      } else {
 
1693
        // Get or create the current stack name for use by the inline
 
1694
        var name = this.stackSlot ? this.topStackName() : this.incrStack();
 
1695
 
 
1696
        prefix = '(' + this.push(name) + ' = ' + top + '),';
 
1697
        stack = this.topStack();
 
1698
      }
 
1699
    } else {
 
1700
      stack = this.topStack();
 
1701
    }
 
1702
 
 
1703
    var item = callback.call(this, stack);
 
1704
 
 
1705
    if (inline) {
 
1706
      if (this.inlineStack.length || this.compileStack.length) {
 
1707
        this.popStack();
 
1708
      }
 
1709
      this.push('(' + prefix + item + ')');
 
1710
    } else {
 
1711
      // Prevent modification of the context depth variable. Through replaceStack
 
1712
      if (!/^stack/.test(stack)) {
 
1713
        stack = this.nextStack();
 
1714
      }
 
1715
 
 
1716
      this.source.push(stack + " = (" + prefix + item + ");");
 
1717
    }
 
1718
    return stack;
 
1719
  },
 
1720
 
 
1721
  nextStack: function() {
 
1722
    return this.pushStack();
 
1723
  },
 
1724
 
 
1725
  incrStack: function() {
 
1726
    this.stackSlot++;
 
1727
    if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
 
1728
    return this.topStackName();
 
1729
  },
 
1730
  topStackName: function() {
 
1731
    return "stack" + this.stackSlot;
 
1732
  },
 
1733
  flushInline: function() {
 
1734
    var inlineStack = this.inlineStack;
 
1735
    if (inlineStack.length) {
 
1736
      this.inlineStack = [];
 
1737
      for (var i = 0, len = inlineStack.length; i < len; i++) {
 
1738
        var entry = inlineStack[i];
 
1739
        if (entry instanceof Literal) {
 
1740
          this.compileStack.push(entry);
 
1741
        } else {
 
1742
          this.pushStack(entry);
 
1743
        }
 
1744
      }
 
1745
    }
 
1746
  },
 
1747
  isInline: function() {
 
1748
    return this.inlineStack.length;
 
1749
  },
 
1750
 
 
1751
  popStack: function(wrapped) {
 
1752
    var inline = this.isInline(),
 
1753
        item = (inline ? this.inlineStack : this.compileStack).pop();
 
1754
 
 
1755
    if (!wrapped && (item instanceof Literal)) {
 
1756
      return item.value;
 
1757
    } else {
 
1758
      if (!inline) {
 
1759
        this.stackSlot--;
 
1760
      }
 
1761
      return item;
 
1762
    }
 
1763
  },
 
1764
 
 
1765
  topStack: function(wrapped) {
 
1766
    var stack = (this.isInline() ? this.inlineStack : this.compileStack),
 
1767
        item = stack[stack.length - 1];
 
1768
 
 
1769
    if (!wrapped && (item instanceof Literal)) {
 
1770
      return item.value;
 
1771
    } else {
 
1772
      return item;
 
1773
    }
 
1774
  },
 
1775
 
 
1776
  quotedString: function(str) {
 
1777
    return '"' + str
 
1778
      .replace(/\\/g, '\\\\')
 
1779
      .replace(/"/g, '\\"')
 
1780
      .replace(/\n/g, '\\n')
 
1781
      .replace(/\r/g, '\\r')
 
1782
      .replace(/\u2028/g, '\\u2028')   // Per Ecma-262 7.3 + 7.8.4
 
1783
      .replace(/\u2029/g, '\\u2029') + '"';
 
1784
  },
 
1785
 
 
1786
  setupHelper: function(paramSize, name, missingParams) {
 
1787
    var params = [];
 
1788
    this.setupParams(paramSize, params, missingParams);
 
1789
    var foundHelper = this.nameLookup('helpers', name, 'helper');
 
1790
 
 
1791
    return {
 
1792
      params: params,
 
1793
      name: foundHelper,
 
1794
      callParams: ["depth0"].concat(params).join(", "),
 
1795
      helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
 
1796
    };
 
1797
  },
 
1798
 
 
1799
  // the params and contexts arguments are passed in arrays
 
1800
  // to fill in
 
1801
  setupParams: function(paramSize, params, useRegister) {
 
1802
    var options = [], contexts = [], types = [], param, inverse, program;
 
1803
 
 
1804
    options.push("hash:" + this.popStack());
 
1805
 
 
1806
    inverse = this.popStack();
 
1807
    program = this.popStack();
 
1808
 
 
1809
    // Avoid setting fn and inverse if neither are set. This allows
 
1810
    // helpers to do a check for `if (options.fn)`
 
1811
    if (program || inverse) {
 
1812
      if (!program) {
 
1813
        this.context.aliases.self = "this";
 
1814
        program = "self.noop";
 
1815
      }
 
1816
 
 
1817
      if (!inverse) {
 
1818
       this.context.aliases.self = "this";
 
1819
        inverse = "self.noop";
 
1820
      }
 
1821
 
 
1822
      options.push("inverse:" + inverse);
 
1823
      options.push("fn:" + program);
 
1824
    }
 
1825
 
 
1826
    for(var i=0; i<paramSize; i++) {
 
1827
      param = this.popStack();
 
1828
      params.push(param);
 
1829
 
 
1830
      if(this.options.stringParams) {
 
1831
        types.push(this.popStack());
 
1832
        contexts.push(this.popStack());
 
1833
      }
 
1834
    }
 
1835
 
 
1836
    if (this.options.stringParams) {
 
1837
      options.push("contexts:[" + contexts.join(",") + "]");
 
1838
      options.push("types:[" + types.join(",") + "]");
 
1839
      options.push("hashContexts:hashContexts");
 
1840
      options.push("hashTypes:hashTypes");
 
1841
    }
 
1842
 
 
1843
    if(this.options.data) {
 
1844
      options.push("data:data");
 
1845
    }
 
1846
 
 
1847
    options = "{" + options.join(",") + "}";
 
1848
    if (useRegister) {
 
1849
      this.register('options', options);
 
1850
      params.push('options');
 
1851
    } else {
 
1852
      params.push(options);
 
1853
    }
 
1854
    return params.join(", ");
 
1855
  }
 
1856
};
 
1857
 
 
1858
var reservedWords = (
 
1859
  "break else new var" +
 
1860
  " case finally return void" +
 
1861
  " catch for switch while" +
 
1862
  " continue function this with" +
 
1863
  " default if throw" +
 
1864
  " delete in try" +
 
1865
  " do instanceof typeof" +
 
1866
  " abstract enum int short" +
 
1867
  " boolean export interface static" +
 
1868
  " byte extends long super" +
 
1869
  " char final native synchronized" +
 
1870
  " class float package throws" +
 
1871
  " const goto private transient" +
 
1872
  " debugger implements protected volatile" +
 
1873
  " double import public let yield"
 
1874
).split(" ");
 
1875
 
 
1876
var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
 
1877
 
 
1878
for(var i=0, l=reservedWords.length; i<l; i++) {
 
1879
  compilerWords[reservedWords[i]] = true;
 
1880
}
 
1881
 
 
1882
JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
 
1883
  if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
 
1884
    return true;
 
1885
  }
 
1886
  return false;
 
1887
};
 
1888
 
 
1889
Handlebars.precompile = function(input, options) {
 
1890
  if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
 
1891
    throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input);
 
1892
  }
 
1893
 
 
1894
  options = options || {};
 
1895
  if (!('data' in options)) {
 
1896
    options.data = true;
 
1897
  }
 
1898
  var ast = Handlebars.parse(input);
 
1899
  var environment = new Compiler().compile(ast, options);
 
1900
  return new JavaScriptCompiler().compile(environment, options);
 
1901
};
 
1902
 
 
1903
Handlebars.compile = function(input, options) {
 
1904
  if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
 
1905
    throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
 
1906
  }
 
1907
 
 
1908
  options = options || {};
 
1909
  if (!('data' in options)) {
 
1910
    options.data = true;
 
1911
  }
 
1912
  var compiled;
 
1913
  function compile() {
 
1914
    var ast = Handlebars.parse(input);
 
1915
    var environment = new Compiler().compile(ast, options);
 
1916
    var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true);
 
1917
    return Handlebars.template(templateSpec);
 
1918
  }
 
1919
 
 
1920
  // Template is only compiled on first use and cached after that point.
 
1921
  return function(context, options) {
 
1922
    if (!compiled) {
 
1923
      compiled = compile();
 
1924
    }
 
1925
    return compiled.call(this, context, options);
 
1926
  };
 
1927
};
 
1928
// This file contains YUI-specific wrapper code and overrides for the
 
1929
// handlebars-compiler module.
 
1930
 
 
1931
/**
 
1932
Handlebars parser and compiler. Use this module when you need to compile
 
1933
Handlebars templates.
 
1934
 
 
1935
@module handlebars
 
1936
@submodule handlebars-compiler
 
1937
*/
 
1938
 
 
1939
var levels = ['debug', 'info', 'warn', 'error'];
 
1940
 
 
1941
/**
 
1942
Logs a debugging message. Note that messages will only be logged when the
 
1943
handlebars module is loaded in "debug" mode.
 
1944
 
 
1945
@method log
 
1946
@param {String} level Log level for this message. Supported levels are "debug",
 
1947
    "info", "warn", and "error".
 
1948
@param {String} message Message to log.
 
1949
@for Handlebars
 
1950
*/
 
1951
Handlebars.logger.log = function (level, message) {
 
1952
 
 
1953
};
 
1954
 
 
1955
/**
 
1956
Compiles and renders a Handlebars template string in a single step.
 
1957
 
 
1958
If you'll be using a template more than once, it's more efficient to compile it
 
1959
into a function once using `compile()`, and then render it whenever you need to
 
1960
by simply executing the compiled function. However, if you only need to compile
 
1961
and render a template once, `render()` is a handy shortcut for doing both in a
 
1962
single step.
 
1963
 
 
1964
@example
 
1965
 
 
1966
    Y.Handlebars.render('The pie of the day is {{pie}}!', {
 
1967
        pie: 'Maple Custard'
 
1968
    });
 
1969
    // => "The pie of the day is Maple Custard!"
 
1970
 
 
1971
@method render
 
1972
@param {String} string Handlebars template string to render.
 
1973
@param {Object} context Context object to pass to the template.
 
1974
@param {Object} [options] Compile/render options.
 
1975
    @param {Object} [options.helpers] Helper functions.
 
1976
    @param {Object} [options.partials] Partials.
 
1977
    @param {Object} [options.data] Data.
 
1978
@return {String} Rendered template string.
 
1979
*/
 
1980
Handlebars.render = function (string, context, options) {
 
1981
    return Handlebars.compile(string)(context, options);
 
1982
};
 
1983
 
 
1984
// The rest of this file is just API docs for methods defined in Handlebars
 
1985
// itself.
 
1986
 
 
1987
/**
 
1988
Compiles a Handlebars template string into a function. To render the template,
 
1989
call the function and pass in a context object.
 
1990
 
 
1991
@example
 
1992
 
 
1993
    var template = Y.Handlebars.compile('The pie of the day is {{pie}}!.');
 
1994
    template({pie: 'Pecan'});
 
1995
    // => "The pie of the day is Pecan!"
 
1996
 
 
1997
@method compile
 
1998
@param {String} string Handlebars template string to compile.
 
1999
@param {Object} [options] Compiler options.
 
2000
@return {Function} Compiled template function.
 
2001
*/
 
2002
 
 
2003
/**
 
2004
Precompiles a Handlebars template string into a string of JavaScript code. This
 
2005
can be used to precompile a template at build time or on the server, and the
 
2006
resulting template can then be rendered at runtime or on the client without
 
2007
needing to go through a compile step.
 
2008
 
 
2009
To render a precompiled template, evaluate the code and then pass the resulting
 
2010
function to `Y.Handlebars.template()` to get back an executable template
 
2011
function.
 
2012
 
 
2013
@method precompile
 
2014
@param {String} string Handlebars template string to compile.
 
2015
@param {Object} [options] Compiler options.
 
2016
@return {String} Precompiled template code.
 
2017
*/
 
2018
 
 
2019
 
 
2020
}, '@VERSION@', {"requires": ["handlebars-base"]});