sht_rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 2.0.0
data/README.md CHANGED
@@ -47,7 +47,7 @@ The same template you can render in JavaScript:
47
47
 
48
48
  ## Demo
49
49
 
50
- Site: [http://st-rails-example.herokuapp.com/sht](http://st-rails-example.herokuapp.com/sht)
50
+ Site: [http://st-rails-example.herokuapp.com](http://st-rails-example.herokuapp.com)
51
51
 
52
52
  Source code: [https://github.com/le0pard/st_rails_example](https://github.com/le0pard/st_rails_example)
53
53
 
@@ -4,10 +4,10 @@ module ShtRails
4
4
  app.paths['app/views'] << ShtRails.template_base_path
5
5
  end
6
6
 
7
- initializer "sprockets.smt_rails", :after => "sprockets.environment", :group => :all do |app|
7
+ initializer "sprockets.sht_rails", :after => "sprockets.environment", :group => :all do |app|
8
8
  next unless app.assets
9
9
  app.assets.register_engine(".#{ShtRails.template_extension}", Tilt)
10
10
  app.config.assets.paths << ShtRails.template_base_path
11
11
  end
12
12
  end
13
- end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module ShtRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,7 +1,41 @@
1
+ /*
2
+
3
+ Copyright (C) 2011 by Yehuda Katz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ */
24
+
1
25
  // lib/handlebars/base.js
2
- var Handlebars = {};
3
26
 
4
- Handlebars.VERSION = "1.0.beta.6";
27
+ /*jshint eqnull:true*/
28
+ this.Handlebars = {};
29
+
30
+ (function(Handlebars) {
31
+
32
+ Handlebars.VERSION = "1.0.0-rc.3";
33
+ Handlebars.COMPILER_REVISION = 2;
34
+
35
+ Handlebars.REVISION_CHANGES = {
36
+ 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
37
+ 2: '>= 1.0.0-rc.3'
38
+ };
5
39
 
6
40
  Handlebars.helpers = {};
7
41
  Handlebars.partials = {};
@@ -40,29 +74,71 @@ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
40
74
  return inverse(this);
41
75
  } else if(type === "[object Array]") {
42
76
  if(context.length > 0) {
43
- for(var i=0, j=context.length; i<j; i++) {
44
- ret = ret + fn(context[i]);
45
- }
77
+ return Handlebars.helpers.each(context, options);
46
78
  } else {
47
- ret = inverse(this);
79
+ return inverse(this);
48
80
  }
49
- return ret;
50
81
  } else {
51
82
  return fn(context);
52
83
  }
53
84
  });
54
85
 
86
+ Handlebars.K = function() {};
87
+
88
+ Handlebars.createFrame = Object.create || function(object) {
89
+ Handlebars.K.prototype = object;
90
+ var obj = new Handlebars.K();
91
+ Handlebars.K.prototype = null;
92
+ return obj;
93
+ };
94
+
95
+ Handlebars.logger = {
96
+ DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
97
+
98
+ methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
99
+
100
+ // can be overridden in the host environment
101
+ log: function(level, obj) {
102
+ if (Handlebars.logger.level <= level) {
103
+ var method = Handlebars.logger.methodMap[level];
104
+ if (typeof console !== 'undefined' && console[method]) {
105
+ console[method].call(console, obj);
106
+ }
107
+ }
108
+ }
109
+ };
110
+
111
+ Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
112
+
55
113
  Handlebars.registerHelper('each', function(context, options) {
56
114
  var fn = options.fn, inverse = options.inverse;
57
- var ret = "";
115
+ var i = 0, ret = "", data;
116
+
117
+ if (options.data) {
118
+ data = Handlebars.createFrame(options.data);
119
+ }
58
120
 
59
- if(context && context.length > 0) {
60
- for(var i=0, j=context.length; i<j; i++) {
61
- ret = ret + fn(context[i]);
121
+ if(context && typeof context === 'object') {
122
+ if(context instanceof Array){
123
+ for(var j = context.length; i<j; i++) {
124
+ if (data) { data.index = i; }
125
+ ret = ret + fn(context[i], { data: data });
126
+ }
127
+ } else {
128
+ for(var key in context) {
129
+ if(context.hasOwnProperty(key)) {
130
+ if(data) { data.key = key; }
131
+ ret = ret + fn(context[key], {data: data});
132
+ i++;
133
+ }
134
+ }
62
135
  }
63
- } else {
136
+ }
137
+
138
+ if(i === 0){
64
139
  ret = inverse(this);
65
140
  }
141
+
66
142
  return ret;
67
143
  });
68
144
 
@@ -89,107 +165,123 @@ Handlebars.registerHelper('with', function(context, options) {
89
165
  return options.fn(context);
90
166
  });
91
167
 
92
- Handlebars.registerHelper('log', function(context) {
93
- Handlebars.log(context);
168
+ Handlebars.registerHelper('log', function(context, options) {
169
+ var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
170
+ Handlebars.log(level, context);
94
171
  });
172
+
173
+ }(this.Handlebars));
95
174
  ;
96
175
  // lib/handlebars/compiler/parser.js
97
176
  /* Jison generated parser */
98
177
  var handlebars = (function(){
99
-
100
178
  var parser = {trace: function trace() { },
101
179
  yy: {},
102
- symbols_: {"error":2,"root":3,"program":4,"EOF":5,"statements":6,"simpleInverse":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,"OPEN_PARTIAL":24,"params":25,"hash":26,"param":27,"STRING":28,"INTEGER":29,"BOOLEAN":30,"hashSegments":31,"hashSegment":32,"ID":33,"EQUALS":34,"pathSegments":35,"SEP":36,"$accept":0,"$end":1},
103
- 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:"OPEN_PARTIAL",28:"STRING",29:"INTEGER",30:"BOOLEAN",33:"ID",34:"EQUALS",36:"SEP"},
104
- productions_: [0,[3,2],[4,3],[4,1],[4,0],[6,1],[6,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],[7,2],[17,3],[17,2],[17,2],[17,1],[25,2],[25,1],[27,1],[27,1],[27,1],[27,1],[26,1],[31,2],[31,1],[32,3],[32,3],[32,3],[32,3],[21,1],[35,3],[35,1]],
180
+ 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,"OPEN_PARTIAL":24,"partialName":25,"params":26,"hash":27,"DATA":28,"param":29,"STRING":30,"INTEGER":31,"BOOLEAN":32,"hashSegments":33,"hashSegment":34,"ID":35,"EQUALS":36,"PARTIAL_NAME":37,"pathSegments":38,"SEP":39,"$accept":0,"$end":1},
181
+ 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:"OPEN_PARTIAL",28:"DATA",30:"STRING",31:"INTEGER",32:"BOOLEAN",35:"ID",36:"EQUALS",37:"PARTIAL_NAME",39:"SEP"},
182
+ 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],[26,2],[26,1],[29,1],[29,1],[29,1],[29,1],[29,1],[27,1],[33,2],[33,1],[34,3],[34,3],[34,3],[34,3],[34,3],[25,1],[21,1],[38,3],[38,1]],
105
183
  performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
106
184
 
107
185
  var $0 = $$.length - 1;
108
186
  switch (yystate) {
109
- case 1: return $$[$0-1]
187
+ case 1: return $$[$0-1];
188
+ break;
189
+ case 2: this.$ = new yy.ProgramNode([], $$[$0]);
190
+ break;
191
+ case 3: this.$ = new yy.ProgramNode($$[$0-2], $$[$0]);
192
+ break;
193
+ case 4: this.$ = new yy.ProgramNode($$[$0-1], []);
194
+ break;
195
+ case 5: this.$ = new yy.ProgramNode($$[$0]);
196
+ break;
197
+ case 6: this.$ = new yy.ProgramNode([], []);
110
198
  break;
111
- case 2: this.$ = new yy.ProgramNode($$[$0-2], $$[$0])
199
+ case 7: this.$ = new yy.ProgramNode([]);
112
200
  break;
113
- case 3: this.$ = new yy.ProgramNode($$[$0])
201
+ case 8: this.$ = [$$[$0]];
114
202
  break;
115
- case 4: this.$ = new yy.ProgramNode([])
203
+ case 9: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
116
204
  break;
117
- case 5: this.$ = [$$[$0]]
205
+ case 10: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1].inverse, $$[$0-1], $$[$0]);
118
206
  break;
119
- case 6: $$[$0-1].push($$[$0]); this.$ = $$[$0-1]
207
+ case 11: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0-1].inverse, $$[$0]);
120
208
  break;
121
- case 7: this.$ = new yy.InverseNode($$[$0-2], $$[$0-1], $$[$0])
209
+ case 12: this.$ = $$[$0];
122
210
  break;
123
- case 8: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0])
211
+ case 13: this.$ = $$[$0];
124
212
  break;
125
- case 9: this.$ = $$[$0]
213
+ case 14: this.$ = new yy.ContentNode($$[$0]);
126
214
  break;
127
- case 10: this.$ = $$[$0]
215
+ case 15: this.$ = new yy.CommentNode($$[$0]);
128
216
  break;
129
- case 11: this.$ = new yy.ContentNode($$[$0])
217
+ case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
130
218
  break;
131
- case 12: this.$ = new yy.CommentNode($$[$0])
219
+ case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
132
220
  break;
133
- case 13: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
221
+ case 18: this.$ = $$[$0-1];
134
222
  break;
135
- case 14: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
223
+ case 19: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
136
224
  break;
137
- case 15: this.$ = $$[$0-1]
225
+ case 20: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true);
138
226
  break;
139
- case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
227
+ case 21: this.$ = new yy.PartialNode($$[$0-1]);
140
228
  break;
141
- case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true)
229
+ case 22: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1]);
142
230
  break;
143
- case 18: this.$ = new yy.PartialNode($$[$0-1])
231
+ case 23:
144
232
  break;
145
- case 19: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1])
233
+ case 24: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]];
146
234
  break;
147
- case 20:
235
+ case 25: this.$ = [[$$[$0-1]].concat($$[$0]), null];
148
236
  break;
149
- case 21: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]]
237
+ case 26: this.$ = [[$$[$0-1]], $$[$0]];
150
238
  break;
151
- case 22: this.$ = [[$$[$0-1]].concat($$[$0]), null]
239
+ case 27: this.$ = [[$$[$0]], null];
152
240
  break;
153
- case 23: this.$ = [[$$[$0-1]], $$[$0]]
241
+ case 28: this.$ = [[new yy.DataNode($$[$0])], null];
154
242
  break;
155
- case 24: this.$ = [[$$[$0]], null]
243
+ case 29: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
156
244
  break;
157
- case 25: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
245
+ case 30: this.$ = [$$[$0]];
158
246
  break;
159
- case 26: this.$ = [$$[$0]]
247
+ case 31: this.$ = $$[$0];
160
248
  break;
161
- case 27: this.$ = $$[$0]
249
+ case 32: this.$ = new yy.StringNode($$[$0]);
162
250
  break;
163
- case 28: this.$ = new yy.StringNode($$[$0])
251
+ case 33: this.$ = new yy.IntegerNode($$[$0]);
164
252
  break;
165
- case 29: this.$ = new yy.IntegerNode($$[$0])
253
+ case 34: this.$ = new yy.BooleanNode($$[$0]);
166
254
  break;
167
- case 30: this.$ = new yy.BooleanNode($$[$0])
255
+ case 35: this.$ = new yy.DataNode($$[$0]);
168
256
  break;
169
- case 31: this.$ = new yy.HashNode($$[$0])
257
+ case 36: this.$ = new yy.HashNode($$[$0]);
170
258
  break;
171
- case 32: $$[$0-1].push($$[$0]); this.$ = $$[$0-1]
259
+ case 37: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
172
260
  break;
173
- case 33: this.$ = [$$[$0]]
261
+ case 38: this.$ = [$$[$0]];
174
262
  break;
175
- case 34: this.$ = [$$[$0-2], $$[$0]]
263
+ case 39: this.$ = [$$[$0-2], $$[$0]];
176
264
  break;
177
- case 35: this.$ = [$$[$0-2], new yy.StringNode($$[$0])]
265
+ case 40: this.$ = [$$[$0-2], new yy.StringNode($$[$0])];
178
266
  break;
179
- case 36: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])]
267
+ case 41: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])];
180
268
  break;
181
- case 37: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])]
269
+ case 42: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])];
182
270
  break;
183
- case 38: this.$ = new yy.IdNode($$[$0])
271
+ case 43: this.$ = [$$[$0-2], new yy.DataNode($$[$0])];
184
272
  break;
185
- case 39: $$[$0-2].push($$[$0]); this.$ = $$[$0-2];
273
+ case 44: this.$ = new yy.PartialNameNode($$[$0]);
186
274
  break;
187
- case 40: this.$ = [$$[$0]]
275
+ case 45: this.$ = new yy.IdNode($$[$0]);
276
+ break;
277
+ case 46: $$[$0-2].push($$[$0]); this.$ = $$[$0-2];
278
+ break;
279
+ case 47: this.$ = [$$[$0]];
188
280
  break;
189
281
  }
190
282
  },
191
- table: [{3:1,4:2,5:[2,4],6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{1:[3]},{5:[1,16]},{5:[2,3],7:17,8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,19],20:[2,3],22:[1,13],23:[1,14],24:[1,15]},{5:[2,5],14:[2,5],15:[2,5],16:[2,5],19:[2,5],20:[2,5],22:[2,5],23:[2,5],24:[2,5]},{4:20,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{4:21,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{5:[2,9],14:[2,9],15:[2,9],16:[2,9],19:[2,9],20:[2,9],22:[2,9],23:[2,9],24:[2,9]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],24:[2,10]},{5:[2,11],14:[2,11],15:[2,11],16:[2,11],19:[2,11],20:[2,11],22:[2,11],23:[2,11],24:[2,11]},{5:[2,12],14:[2,12],15:[2,12],16:[2,12],19:[2,12],20:[2,12],22:[2,12],23:[2,12],24:[2,12]},{17:22,21:23,33:[1,25],35:24},{17:26,21:23,33:[1,25],35:24},{17:27,21:23,33:[1,25],35:24},{17:28,21:23,33:[1,25],35:24},{21:29,33:[1,25],35:24},{1:[2,1]},{6:30,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{5:[2,6],14:[2,6],15:[2,6],16:[2,6],19:[2,6],20:[2,6],22:[2,6],23:[2,6],24:[2,6]},{17:22,18:[1,31],21:23,33:[1,25],35:24},{10:32,20:[1,33]},{10:34,20:[1,33]},{18:[1,35]},{18:[2,24],21:40,25:36,26:37,27:38,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,38],28:[2,38],29:[2,38],30:[2,38],33:[2,38],36:[1,46]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],36:[2,40]},{18:[1,47]},{18:[1,48]},{18:[1,49]},{18:[1,50],21:51,33:[1,25],35:24},{5:[2,2],8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,2],22:[1,13],23:[1,14],24:[1,15]},{14:[2,20],15:[2,20],16:[2,20],19:[2,20],22:[2,20],23:[2,20],24:[2,20]},{5:[2,7],14:[2,7],15:[2,7],16:[2,7],19:[2,7],20:[2,7],22:[2,7],23:[2,7],24:[2,7]},{21:52,33:[1,25],35:24},{5:[2,8],14:[2,8],15:[2,8],16:[2,8],19:[2,8],20:[2,8],22:[2,8],23:[2,8],24:[2,8]},{14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],24:[2,14]},{18:[2,22],21:40,26:53,27:54,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,23]},{18:[2,26],28:[2,26],29:[2,26],30:[2,26],33:[2,26]},{18:[2,31],32:55,33:[1,56]},{18:[2,27],28:[2,27],29:[2,27],30:[2,27],33:[2,27]},{18:[2,28],28:[2,28],29:[2,28],30:[2,28],33:[2,28]},{18:[2,29],28:[2,29],29:[2,29],30:[2,29],33:[2,29]},{18:[2,30],28:[2,30],29:[2,30],30:[2,30],33:[2,30]},{18:[2,33],33:[2,33]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],34:[1,57],36:[2,40]},{33:[1,58]},{14:[2,13],15:[2,13],16:[2,13],19:[2,13],20:[2,13],22:[2,13],23:[2,13],24:[2,13]},{5:[2,16],14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],24:[2,16]},{5:[2,17],14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{5:[2,18],14:[2,18],15:[2,18],16:[2,18],19:[2,18],20:[2,18],22:[2,18],23:[2,18],24:[2,18]},{18:[1,59]},{18:[1,60]},{18:[2,21]},{18:[2,25],28:[2,25],29:[2,25],30:[2,25],33:[2,25]},{18:[2,32],33:[2,32]},{34:[1,57]},{21:61,28:[1,62],29:[1,63],30:[1,64],33:[1,25],35:24},{18:[2,39],28:[2,39],29:[2,39],30:[2,39],33:[2,39],36:[2,39]},{5:[2,19],14:[2,19],15:[2,19],16:[2,19],19:[2,19],20:[2,19],22:[2,19],23:[2,19],24:[2,19]},{5:[2,15],14:[2,15],15:[2,15],16:[2,15],19:[2,15],20:[2,15],22:[2,15],23:[2,15],24:[2,15]},{18:[2,34],33:[2,34]},{18:[2,35],33:[2,35]},{18:[2,36],33:[2,36]},{18:[2,37],33:[2,37]}],
192
- defaultActions: {16:[2,1],37:[2,23],53:[2,21]},
283
+ 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],24:[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],24:[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],24:[1,16]},{17:23,18:[1,22],21:24,28:[1,25],35:[1,27],38: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],24:[2,8]},{4:28,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],24:[1,16]},{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],24:[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],24:[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],24:[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],24:[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],24:[2,15]},{17:30,21:24,28:[1,25],35:[1,27],38:26},{17:31,21:24,28:[1,25],35:[1,27],38:26},{17:32,21:24,28:[1,25],35:[1,27],38:26},{25:33,37:[1,34]},{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],24:[1,16]},{17:23,21:24,28:[1,25],35:[1,27],38:26},{5:[2,4],7:35,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],24:[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],24:[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],24:[2,23]},{18:[1,36]},{18:[2,27],21:41,26:37,27:38,28:[1,45],29:39,30:[1,42],31:[1,43],32:[1,44],33:40,34:46,35:[1,47],38:26},{18:[2,28]},{18:[2,45],28:[2,45],30:[2,45],31:[2,45],32:[2,45],35:[2,45],39:[1,48]},{18:[2,47],28:[2,47],30:[2,47],31:[2,47],32:[2,47],35:[2,47],39:[2,47]},{10:49,20:[1,50]},{10:51,20:[1,50]},{18:[1,52]},{18:[1,53]},{18:[1,54]},{18:[1,55],21:56,35:[1,27],38:26},{18:[2,44],35:[2,44]},{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],24:[1,16]},{14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{18:[2,25],21:41,27:57,28:[1,45],29:58,30:[1,42],31:[1,43],32:[1,44],33:40,34:46,35:[1,47],38:26},{18:[2,26]},{18:[2,30],28:[2,30],30:[2,30],31:[2,30],32:[2,30],35:[2,30]},{18:[2,36],34:59,35:[1,60]},{18:[2,31],28:[2,31],30:[2,31],31:[2,31],32:[2,31],35:[2,31]},{18:[2,32],28:[2,32],30:[2,32],31:[2,32],32:[2,32],35:[2,32]},{18:[2,33],28:[2,33],30:[2,33],31:[2,33],32:[2,33],35:[2,33]},{18:[2,34],28:[2,34],30:[2,34],31:[2,34],32:[2,34],35:[2,34]},{18:[2,35],28:[2,35],30:[2,35],31:[2,35],32:[2,35],35:[2,35]},{18:[2,38],35:[2,38]},{18:[2,47],28:[2,47],30:[2,47],31:[2,47],32:[2,47],35:[2,47],36:[1,61],39:[2,47]},{35:[1,62]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],24:[2,10]},{21:63,35:[1,27],38: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],24:[2,11]},{14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],24:[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],24:[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],24:[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],24:[2,21]},{18:[1,64]},{18:[2,24]},{18:[2,29],28:[2,29],30:[2,29],31:[2,29],32:[2,29],35:[2,29]},{18:[2,37],35:[2,37]},{36:[1,61]},{21:65,28:[1,69],30:[1,66],31:[1,67],32:[1,68],35:[1,27],38:26},{18:[2,46],28:[2,46],30:[2,46],31:[2,46],32:[2,46],35:[2,46],39:[2,46]},{18:[1,70]},{5:[2,22],14:[2,22],15:[2,22],16:[2,22],19:[2,22],20:[2,22],22:[2,22],23:[2,22],24:[2,22]},{18:[2,39],35:[2,39]},{18:[2,40],35:[2,40]},{18:[2,41],35:[2,41]},{18:[2,42],35:[2,42]},{18:[2,43],35:[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],24:[2,18]}],
284
+ defaultActions: {17:[2,1],25:[2,28],38:[2,26],57:[2,24]},
193
285
  parseError: function parseError(str, hash) {
194
286
  throw new Error(str);
195
287
  },
@@ -198,10 +290,12 @@ parse: function parse(input) {
198
290
  this.lexer.setInput(input);
199
291
  this.lexer.yy = this.yy;
200
292
  this.yy.lexer = this.lexer;
293
+ this.yy.parser = this;
201
294
  if (typeof this.lexer.yylloc == "undefined")
202
295
  this.lexer.yylloc = {};
203
296
  var yyloc = this.lexer.yylloc;
204
297
  lstack.push(yyloc);
298
+ var ranges = this.lexer.options && this.lexer.options.ranges;
205
299
  if (typeof this.yy.parseError === "function")
206
300
  this.parseError = this.yy.parseError;
207
301
  function popStack(n) {
@@ -223,20 +317,21 @@ parse: function parse(input) {
223
317
  if (this.defaultActions[state]) {
224
318
  action = this.defaultActions[state];
225
319
  } else {
226
- if (symbol == null)
320
+ if (symbol === null || typeof symbol == "undefined") {
227
321
  symbol = lex();
322
+ }
228
323
  action = table[state] && table[state][symbol];
229
324
  }
230
325
  if (typeof action === "undefined" || !action.length || !action[0]) {
326
+ var errStr = "";
231
327
  if (!recovering) {
232
328
  expected = [];
233
329
  for (p in table[state])
234
330
  if (this.terminals_[p] && p > 2) {
235
331
  expected.push("'" + this.terminals_[p] + "'");
236
332
  }
237
- var errStr = "";
238
333
  if (this.lexer.showPosition) {
239
- errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + this.terminals_[symbol] + "'";
334
+ errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
240
335
  } else {
241
336
  errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
242
337
  }
@@ -269,6 +364,9 @@ parse: function parse(input) {
269
364
  len = this.productions_[action[1]][1];
270
365
  yyval.$ = vstack[vstack.length - len];
271
366
  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};
367
+ if (ranges) {
368
+ yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
369
+ }
272
370
  r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
273
371
  if (typeof r !== "undefined") {
274
372
  return r;
@@ -290,13 +388,13 @@ parse: function parse(input) {
290
388
  }
291
389
  return true;
292
390
  }
293
- };/* Jison generated lexer */
391
+ };
392
+ /* Jison generated lexer */
294
393
  var lexer = (function(){
295
-
296
394
  var lexer = ({EOF:1,
297
395
  parseError:function parseError(str, hash) {
298
- if (this.yy.parseError) {
299
- this.yy.parseError(str, hash);
396
+ if (this.yy.parser) {
397
+ this.yy.parser.parseError(str, hash);
300
398
  } else {
301
399
  throw new Error(str);
302
400
  }
@@ -308,27 +406,64 @@ setInput:function (input) {
308
406
  this.yytext = this.matched = this.match = '';
309
407
  this.conditionStack = ['INITIAL'];
310
408
  this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
409
+ if (this.options.ranges) this.yylloc.range = [0,0];
410
+ this.offset = 0;
311
411
  return this;
312
412
  },
313
413
  input:function () {
314
414
  var ch = this._input[0];
315
- this.yytext+=ch;
415
+ this.yytext += ch;
316
416
  this.yyleng++;
317
- this.match+=ch;
318
- this.matched+=ch;
319
- var lines = ch.match(/\n/);
320
- if (lines) this.yylineno++;
417
+ this.offset++;
418
+ this.match += ch;
419
+ this.matched += ch;
420
+ var lines = ch.match(/(?:\r\n?|\n).*/g);
421
+ if (lines) {
422
+ this.yylineno++;
423
+ this.yylloc.last_line++;
424
+ } else {
425
+ this.yylloc.last_column++;
426
+ }
427
+ if (this.options.ranges) this.yylloc.range[1]++;
428
+
321
429
  this._input = this._input.slice(1);
322
430
  return ch;
323
431
  },
324
432
  unput:function (ch) {
433
+ var len = ch.length;
434
+ var lines = ch.split(/(?:\r\n?|\n)/g);
435
+
325
436
  this._input = ch + this._input;
437
+ this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
438
+ //this.yyleng -= len;
439
+ this.offset -= len;
440
+ var oldLines = this.match.split(/(?:\r\n?|\n)/g);
441
+ this.match = this.match.substr(0, this.match.length-1);
442
+ this.matched = this.matched.substr(0, this.matched.length-1);
443
+
444
+ if (lines.length-1) this.yylineno -= lines.length-1;
445
+ var r = this.yylloc.range;
446
+
447
+ this.yylloc = {first_line: this.yylloc.first_line,
448
+ last_line: this.yylineno+1,
449
+ first_column: this.yylloc.first_column,
450
+ last_column: lines ?
451
+ (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
452
+ this.yylloc.first_column - len
453
+ };
454
+
455
+ if (this.options.ranges) {
456
+ this.yylloc.range = [r[0], r[0] + this.yyleng - len];
457
+ }
326
458
  return this;
327
459
  },
328
460
  more:function () {
329
461
  this._more = true;
330
462
  return this;
331
463
  },
464
+ less:function (n) {
465
+ this.unput(this.match.slice(n));
466
+ },
332
467
  pastInput:function () {
333
468
  var past = this.matched.substr(0, this.matched.length - this.match.length);
334
469
  return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
@@ -353,6 +488,8 @@ next:function () {
353
488
 
354
489
  var token,
355
490
  match,
491
+ tempMatch,
492
+ index,
356
493
  col,
357
494
  lines;
358
495
  if (!this._more) {
@@ -361,30 +498,39 @@ next:function () {
361
498
  }
362
499
  var rules = this._currentRules();
363
500
  for (var i=0;i < rules.length; i++) {
364
- match = this._input.match(this.rules[rules[i]]);
365
- if (match) {
366
- lines = match[0].match(/\n.*/g);
367
- if (lines) this.yylineno += lines.length;
368
- this.yylloc = {first_line: this.yylloc.last_line,
369
- last_line: this.yylineno+1,
370
- first_column: this.yylloc.last_column,
371
- last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
372
- this.yytext += match[0];
373
- this.match += match[0];
374
- this.matches = match;
375
- this.yyleng = this.yytext.length;
376
- this._more = false;
377
- this._input = this._input.slice(match[0].length);
378
- this.matched += match[0];
379
- token = this.performAction.call(this, this.yy, this, rules[i],this.conditionStack[this.conditionStack.length-1]);
380
- if (token) return token;
381
- else return;
501
+ tempMatch = this._input.match(this.rules[rules[i]]);
502
+ if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
503
+ match = tempMatch;
504
+ index = i;
505
+ if (!this.options.flex) break;
506
+ }
507
+ }
508
+ if (match) {
509
+ lines = match[0].match(/(?:\r\n?|\n).*/g);
510
+ if (lines) this.yylineno += lines.length;
511
+ this.yylloc = {first_line: this.yylloc.last_line,
512
+ last_line: this.yylineno+1,
513
+ first_column: this.yylloc.last_column,
514
+ last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
515
+ this.yytext += match[0];
516
+ this.match += match[0];
517
+ this.matches = match;
518
+ this.yyleng = this.yytext.length;
519
+ if (this.options.ranges) {
520
+ this.yylloc.range = [this.offset, this.offset += this.yyleng];
382
521
  }
522
+ this._more = false;
523
+ this._input = this._input.slice(match[0].length);
524
+ this.matched += match[0];
525
+ token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
526
+ if (this.done && this._input) this.done = false;
527
+ if (token) return token;
528
+ else return;
383
529
  }
384
530
  if (this._input === "") {
385
531
  return this.EOF;
386
532
  } else {
387
- this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
533
+ return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
388
534
  {text: "", token: null, line: this.yylineno});
389
535
  }
390
536
  },
@@ -411,6 +557,7 @@ topState:function () {
411
557
  pushState:function begin(condition) {
412
558
  this.begin(condition);
413
559
  }});
560
+ lexer.options = {};
414
561
  lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
415
562
 
416
563
  var YYSTATE=YY_START
@@ -419,107 +566,100 @@ case 0:
419
566
  if(yy_.yytext.slice(-1) !== "\\") this.begin("mu");
420
567
  if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1), this.begin("emu");
421
568
  if(yy_.yytext) return 14;
422
-
569
+
570
+ break;
571
+ case 1: return 14;
572
+ break;
573
+ case 2:
574
+ if(yy_.yytext.slice(-1) !== "\\") this.popState();
575
+ if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1);
576
+ return 14;
577
+
578
+ break;
579
+ case 3: yy_.yytext = yy_.yytext.substr(0, yy_.yyleng-4); this.popState(); return 15;
580
+ break;
581
+ case 4: this.begin("par"); return 24;
423
582
  break;
424
- case 1: return 14;
583
+ case 5: return 16;
425
584
  break;
426
- case 2: this.popState(); return 14;
585
+ case 6: return 20;
427
586
  break;
428
- case 3: return 24;
587
+ case 7: return 19;
429
588
  break;
430
- case 4: return 16;
589
+ case 8: return 19;
431
590
  break;
432
- case 5: return 20;
591
+ case 9: return 23;
433
592
  break;
434
- case 6: return 19;
593
+ case 10: return 23;
435
594
  break;
436
- case 7: return 19;
595
+ case 11: this.popState(); this.begin('com');
437
596
  break;
438
- case 8: return 23;
597
+ case 12: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
439
598
  break;
440
- case 9: return 23;
599
+ case 13: return 22;
441
600
  break;
442
- case 10: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
601
+ case 14: return 36;
443
602
  break;
444
- case 11: return 22;
603
+ case 15: return 35;
445
604
  break;
446
- case 12: return 34;
605
+ case 16: return 35;
447
606
  break;
448
- case 13: return 33;
607
+ case 17: return 39;
449
608
  break;
450
- case 14: return 33;
609
+ case 18: /*ignore whitespace*/
451
610
  break;
452
- case 15: return 36;
611
+ case 19: this.popState(); return 18;
453
612
  break;
454
- case 16: /*ignore whitespace*/
613
+ case 20: this.popState(); return 18;
455
614
  break;
456
- case 17: this.popState(); return 18;
615
+ case 21: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 30;
457
616
  break;
458
- case 18: this.popState(); return 18;
617
+ case 22: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\'/g,"'"); return 30;
459
618
  break;
460
- case 19: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 28;
619
+ case 23: yy_.yytext = yy_.yytext.substr(1); return 28;
461
620
  break;
462
- case 20: return 30;
621
+ case 24: return 32;
463
622
  break;
464
- case 21: return 30;
623
+ case 25: return 32;
465
624
  break;
466
- case 22: return 29;
625
+ case 26: return 31;
467
626
  break;
468
- case 23: return 33;
627
+ case 27: return 35;
469
628
  break;
470
- case 24: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 33;
629
+ case 28: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 35;
471
630
  break;
472
- case 25: return 'INVALID';
631
+ case 29: return 'INVALID';
473
632
  break;
474
- case 26: return 5;
633
+ case 30: /*ignore whitespace*/
634
+ break;
635
+ case 31: this.popState(); return 37;
636
+ break;
637
+ case 32: return 5;
475
638
  break;
476
639
  }
477
640
  };
478
- lexer.rules = [/^[^\x00]*?(?=(\{\{))/,/^[^\x00]+/,/^[^\x00]{2,}?(?=(\{\{))/,/^\{\{>/,/^\{\{#/,/^\{\{\//,/^\{\{\^/,/^\{\{\s*else\b/,/^\{\{\{/,/^\{\{&/,/^\{\{![\s\S]*?\}\}/,/^\{\{/,/^=/,/^\.(?=[} ])/,/^\.\./,/^[\/.]/,/^\s+/,/^\}\}\}/,/^\}\}/,/^"(\\["]|[^"])*"/,/^true(?=[}\s])/,/^false(?=[}\s])/,/^[0-9]+(?=[}\s])/,/^[a-zA-Z0-9_$-]+(?=[=}\s\/.])/,/^\[[^\]]*\]/,/^./,/^$/];
479
- lexer.conditions = {"mu":{"rules":[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"inclusive":false},"emu":{"rules":[2],"inclusive":false},"INITIAL":{"rules":[0,1,26],"inclusive":true}};return lexer;})()
641
+ lexer.rules = [/^(?:[^\x00]*?(?=(\{\{)))/,/^(?:[^\x00]+)/,/^(?:[^\x00]{2,}?(?=(\{\{|$)))/,/^(?:[\s\S]*?--\}\})/,/^(?:\{\{>)/,/^(?:\{\{#)/,/^(?:\{\{\/)/,/^(?:\{\{\^)/,/^(?:\{\{\s*else\b)/,/^(?:\{\{\{)/,/^(?:\{\{&)/,/^(?:\{\{!--)/,/^(?:\{\{![\s\S]*?\}\})/,/^(?:\{\{)/,/^(?:=)/,/^(?:\.(?=[} ]))/,/^(?:\.\.)/,/^(?:[\/.])/,/^(?:\s+)/,/^(?:\}\}\})/,/^(?:\}\})/,/^(?:"(\\["]|[^"])*")/,/^(?:'(\\[']|[^'])*')/,/^(?:@[a-zA-Z]+)/,/^(?:true(?=[}\s]))/,/^(?:false(?=[}\s]))/,/^(?:[0-9]+(?=[}\s]))/,/^(?:[a-zA-Z0-9_$-]+(?=[=}\s\/.]))/,/^(?:\[[^\]]*\])/,/^(?:.)/,/^(?:\s+)/,/^(?:[a-zA-Z0-9_$-/]+)/,/^(?:$)/];
642
+ lexer.conditions = {"mu":{"rules":[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,32],"inclusive":false},"emu":{"rules":[2],"inclusive":false},"com":{"rules":[3],"inclusive":false},"par":{"rules":[30,31],"inclusive":false},"INITIAL":{"rules":[0,1,32],"inclusive":true}};
643
+ return lexer;})()
480
644
  parser.lexer = lexer;
481
- return parser;
482
- })();
483
- if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
484
- exports.parser = handlebars;
485
- exports.parse = function () { return handlebars.parse.apply(handlebars, arguments); }
486
- exports.main = function commonjsMain(args) {
487
- if (!args[1])
488
- throw new Error('Usage: '+args[0]+' FILE');
489
- if (typeof process !== 'undefined') {
490
- var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
491
- } else {
492
- var cwd = require("file").path(require("file").cwd());
493
- var source = cwd.join(args[1]).read({charset: "utf-8"});
494
- }
495
- return exports.parser.parse(source);
496
- }
497
- if (typeof module !== 'undefined' && require.main === module) {
498
- exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
499
- }
500
- };
501
- ;
645
+ function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
646
+ return new Parser;
647
+ })();;
502
648
  // lib/handlebars/compiler/base.js
503
649
  Handlebars.Parser = handlebars;
504
650
 
505
- Handlebars.parse = function(string) {
651
+ Handlebars.parse = function(input) {
652
+
653
+ // Just return if an already-compile AST was passed in.
654
+ if(input.constructor === Handlebars.AST.ProgramNode) { return input; }
655
+
506
656
  Handlebars.Parser.yy = Handlebars.AST;
507
- return Handlebars.Parser.parse(string);
657
+ return Handlebars.Parser.parse(input);
508
658
  };
509
659
 
510
660
  Handlebars.print = function(ast) {
511
661
  return new Handlebars.PrintVisitor().accept(ast);
512
- };
513
-
514
- Handlebars.logger = {
515
- DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
516
-
517
- // override in the host environment
518
- log: function(level, str) {}
519
- };
520
-
521
- Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
522
- ;
662
+ };;
523
663
  // lib/handlebars/compiler/ast.js
524
664
  (function() {
525
665
 
@@ -531,21 +671,32 @@ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
531
671
  if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
532
672
  };
533
673
 
534
- Handlebars.AST.MustacheNode = function(params, hash, unescaped) {
674
+ Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) {
535
675
  this.type = "mustache";
536
- this.id = params[0];
537
- this.params = params.slice(1);
538
- this.hash = hash;
539
676
  this.escaped = !unescaped;
540
- };
677
+ this.hash = hash;
678
+
679
+ var id = this.id = rawParams[0];
680
+ var params = this.params = rawParams.slice(1);
681
+
682
+ // a mustache is an eligible helper if:
683
+ // * its id is simple (a single part, not `this` or `..`)
684
+ var eligibleHelper = this.eligibleHelper = id.isSimple;
541
685
 
542
- Handlebars.AST.PartialNode = function(id, context) {
543
- this.type = "partial";
686
+ // a mustache is definitely a helper if:
687
+ // * it is an eligible helper, and
688
+ // * it has at least one parameter or hash segment
689
+ this.isHelper = eligibleHelper && (params.length || hash);
544
690
 
545
- // TODO: disallow complex IDs
691
+ // if a mustache is an eligible helper but not a definite
692
+ // helper, it is ambiguous, and will be resolved in a later
693
+ // pass or at runtime.
694
+ };
546
695
 
547
- this.id = id;
548
- this.context = context;
696
+ Handlebars.AST.PartialNode = function(partialName, context) {
697
+ this.type = "partial";
698
+ this.partialName = partialName;
699
+ this.context = context;
549
700
  };
550
701
 
551
702
  var verifyMatch = function(open, close) {
@@ -554,18 +705,16 @@ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
554
705
  }
555
706
  };
556
707
 
557
- Handlebars.AST.BlockNode = function(mustache, program, close) {
708
+ Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
558
709
  verifyMatch(mustache.id, close);
559
710
  this.type = "block";
560
711
  this.mustache = mustache;
561
712
  this.program = program;
562
- };
713
+ this.inverse = inverse;
563
714
 
564
- Handlebars.AST.InverseNode = function(mustache, program, close) {
565
- verifyMatch(mustache.id, close);
566
- this.type = "inverse";
567
- this.mustache = mustache;
568
- this.program = program;
715
+ if (this.inverse && !this.program) {
716
+ this.isInverse = true;
717
+ }
569
718
  };
570
719
 
571
720
  Handlebars.AST.ContentNode = function(string) {
@@ -587,30 +736,51 @@ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
587
736
  for(var i=0,l=parts.length; i<l; i++) {
588
737
  var part = parts[i];
589
738
 
590
- if(part === "..") { depth++; }
591
- else if(part === "." || part === "this") { this.isScoped = true; }
739
+ if (part === ".." || part === "." || part === "this") {
740
+ if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + this.original); }
741
+ else if (part === "..") { depth++; }
742
+ else { this.isScoped = true; }
743
+ }
592
744
  else { dig.push(part); }
593
745
  }
594
746
 
595
747
  this.parts = dig;
596
748
  this.string = dig.join('.');
597
749
  this.depth = depth;
598
- this.isSimple = (dig.length === 1) && (depth === 0);
750
+
751
+ // an ID is simple if it only has one part, and that part is not
752
+ // `..` or `this`.
753
+ this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
754
+
755
+ this.stringModeValue = this.string;
756
+ };
757
+
758
+ Handlebars.AST.PartialNameNode = function(name) {
759
+ this.type = "PARTIAL_NAME";
760
+ this.name = name;
761
+ };
762
+
763
+ Handlebars.AST.DataNode = function(id) {
764
+ this.type = "DATA";
765
+ this.id = id;
599
766
  };
600
767
 
601
768
  Handlebars.AST.StringNode = function(string) {
602
769
  this.type = "STRING";
603
770
  this.string = string;
771
+ this.stringModeValue = string;
604
772
  };
605
773
 
606
774
  Handlebars.AST.IntegerNode = function(integer) {
607
775
  this.type = "INTEGER";
608
776
  this.integer = integer;
777
+ this.stringModeValue = Number(integer);
609
778
  };
610
779
 
611
780
  Handlebars.AST.BooleanNode = function(bool) {
612
781
  this.type = "BOOLEAN";
613
782
  this.bool = bool;
783
+ this.stringModeValue = bool === "true";
614
784
  };
615
785
 
616
786
  Handlebars.AST.CommentNode = function(comment) {
@@ -620,16 +790,18 @@ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
620
790
 
621
791
  })();;
622
792
  // lib/handlebars/utils.js
793
+
794
+ var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
795
+
623
796
  Handlebars.Exception = function(message) {
624
797
  var tmp = Error.prototype.constructor.apply(this, arguments);
625
798
 
626
- for (var p in tmp) {
627
- if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
799
+ // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
800
+ for (var idx = 0; idx < errorProps.length; idx++) {
801
+ this[errorProps[idx]] = tmp[errorProps[idx]];
628
802
  }
629
-
630
- this.message = tmp.message;
631
803
  };
632
- Handlebars.Exception.prototype = new Error;
804
+ Handlebars.Exception.prototype = new Error();
633
805
 
634
806
  // Build out our basic SafeString type
635
807
  Handlebars.SafeString = function(string) {
@@ -641,6 +813,7 @@ Handlebars.SafeString.prototype.toString = function() {
641
813
 
642
814
  (function() {
643
815
  var escape = {
816
+ "&": "&amp;",
644
817
  "<": "&lt;",
645
818
  ">": "&gt;",
646
819
  '"': "&quot;",
@@ -648,7 +821,7 @@ Handlebars.SafeString.prototype.toString = function() {
648
821
  "`": "&#x60;"
649
822
  };
650
823
 
651
- var badChars = /&(?!\w+;)|[<>"'`]/g;
824
+ var badChars = /[&<>"'`]/g;
652
825
  var possible = /[&<>"'`]/;
653
826
 
654
827
  var escapeChar = function(chr) {
@@ -669,11 +842,7 @@ Handlebars.SafeString.prototype.toString = function() {
669
842
  },
670
843
 
671
844
  isEmpty: function(value) {
672
- if (typeof value === "undefined") {
673
- return true;
674
- } else if (value === null) {
675
- return true;
676
- } else if (value === false) {
845
+ if (!value && value !== 0) {
677
846
  return true;
678
847
  } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
679
848
  return true;
@@ -684,93 +853,63 @@ Handlebars.SafeString.prototype.toString = function() {
684
853
  };
685
854
  })();;
686
855
  // lib/handlebars/compiler/compiler.js
856
+
857
+ /*jshint eqnull:true*/
687
858
  Handlebars.Compiler = function() {};
688
859
  Handlebars.JavaScriptCompiler = function() {};
689
860
 
690
861
  (function(Compiler, JavaScriptCompiler) {
691
- Compiler.OPCODE_MAP = {
692
- appendContent: 1,
693
- getContext: 2,
694
- lookupWithHelpers: 3,
695
- lookup: 4,
696
- append: 5,
697
- invokeMustache: 6,
698
- appendEscaped: 7,
699
- pushString: 8,
700
- truthyOrFallback: 9,
701
- functionOrFallback: 10,
702
- invokeProgram: 11,
703
- invokePartial: 12,
704
- push: 13,
705
- assignToHash: 15,
706
- pushStringParam: 16
707
- };
708
-
709
- Compiler.MULTI_PARAM_OPCODES = {
710
- appendContent: 1,
711
- getContext: 1,
712
- lookupWithHelpers: 2,
713
- lookup: 1,
714
- invokeMustache: 3,
715
- pushString: 1,
716
- truthyOrFallback: 1,
717
- functionOrFallback: 1,
718
- invokeProgram: 3,
719
- invokePartial: 1,
720
- push: 1,
721
- assignToHash: 1,
722
- pushStringParam: 1
723
- };
724
-
725
- Compiler.DISASSEMBLE_MAP = {};
726
-
727
- for(var prop in Compiler.OPCODE_MAP) {
728
- var value = Compiler.OPCODE_MAP[prop];
729
- Compiler.DISASSEMBLE_MAP[value] = prop;
730
- }
731
-
732
- Compiler.multiParamSize = function(code) {
733
- return Compiler.MULTI_PARAM_OPCODES[Compiler.DISASSEMBLE_MAP[code]];
734
- };
862
+ // the foundHelper register will disambiguate helper lookup from finding a
863
+ // function in a context. This is necessary for mustache compatibility, which
864
+ // requires that context functions in blocks are evaluated by blockHelperMissing,
865
+ // and then proceed as if the resulting value was provided to blockHelperMissing.
735
866
 
736
867
  Compiler.prototype = {
737
868
  compiler: Compiler,
738
869
 
739
870
  disassemble: function() {
740
- var opcodes = this.opcodes, opcode, nextCode;
741
- var out = [], str, name, value;
871
+ var opcodes = this.opcodes, opcode, out = [], params, param;
742
872
 
743
- for(var i=0, l=opcodes.length; i<l; i++) {
873
+ for (var i=0, l=opcodes.length; i<l; i++) {
744
874
  opcode = opcodes[i];
745
875
 
746
- if(opcode === 'DECLARE') {
747
- name = opcodes[++i];
748
- value = opcodes[++i];
749
- out.push("DECLARE " + name + " = " + value);
876
+ if (opcode.opcode === 'DECLARE') {
877
+ out.push("DECLARE " + opcode.name + "=" + opcode.value);
750
878
  } else {
751
- str = Compiler.DISASSEMBLE_MAP[opcode];
752
-
753
- var extraParams = Compiler.multiParamSize(opcode);
754
- var codes = [];
755
-
756
- for(var j=0; j<extraParams; j++) {
757
- nextCode = opcodes[++i];
758
-
759
- if(typeof nextCode === "string") {
760
- nextCode = "\"" + nextCode.replace("\n", "\\n") + "\"";
879
+ params = [];
880
+ for (var j=0; j<opcode.args.length; j++) {
881
+ param = opcode.args[j];
882
+ if (typeof param === "string") {
883
+ param = "\"" + param.replace("\n", "\\n") + "\"";
761
884
  }
762
-
763
- codes.push(nextCode);
885
+ params.push(param);
764
886
  }
765
-
766
- str = str + " " + codes.join(" ");
767
-
768
- out.push(str);
887
+ out.push(opcode.opcode + " " + params.join(" "));
769
888
  }
770
889
  }
771
890
 
772
891
  return out.join("\n");
773
892
  },
893
+ equals: function(other) {
894
+ var len = this.opcodes.length;
895
+ if (other.opcodes.length !== len) {
896
+ return false;
897
+ }
898
+
899
+ for (var i = 0; i < len; i++) {
900
+ var opcode = this.opcodes[i],
901
+ otherOpcode = other.opcodes[i];
902
+ if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) {
903
+ return false;
904
+ }
905
+ for (var j = 0; j < opcode.args.length; j++) {
906
+ if (opcode.args[j] !== otherOpcode.args[j]) {
907
+ return false;
908
+ }
909
+ }
910
+ }
911
+ return true;
912
+ },
774
913
 
775
914
  guid: 0,
776
915
 
@@ -822,7 +961,7 @@ Handlebars.JavaScriptCompiler = function() {};
822
961
 
823
962
  compileProgram: function(program) {
824
963
  var result = new this.compiler().compile(program, this.options);
825
- var guid = this.guid++;
964
+ var guid = this.guid++, depth;
826
965
 
827
966
  this.usePartial = this.usePartial || result.usePartial;
828
967
 
@@ -839,51 +978,67 @@ Handlebars.JavaScriptCompiler = function() {};
839
978
  },
840
979
 
841
980
  block: function(block) {
842
- var mustache = block.mustache;
843
- var depth, child, inverse, inverseGuid;
981
+ var mustache = block.mustache,
982
+ program = block.program,
983
+ inverse = block.inverse;
844
984
 
845
- var params = this.setupStackForMustache(mustache);
846
-
847
- var programGuid = this.compileProgram(block.program);
848
-
849
- if(block.program.inverse) {
850
- inverseGuid = this.compileProgram(block.program.inverse);
851
- this.declare('inverse', inverseGuid);
985
+ if (program) {
986
+ program = this.compileProgram(program);
852
987
  }
853
988
 
854
- this.opcode('invokeProgram', programGuid, params.length, !!mustache.hash);
855
- this.declare('inverse', null);
856
- this.opcode('append');
857
- },
989
+ if (inverse) {
990
+ inverse = this.compileProgram(inverse);
991
+ }
858
992
 
859
- inverse: function(block) {
860
- var params = this.setupStackForMustache(block.mustache);
993
+ var type = this.classifyMustache(mustache);
861
994
 
862
- var programGuid = this.compileProgram(block.program);
995
+ if (type === "helper") {
996
+ this.helperMustache(mustache, program, inverse);
997
+ } else if (type === "simple") {
998
+ this.simpleMustache(mustache);
863
999
 
864
- this.declare('inverse', programGuid);
1000
+ // now that the simple mustache is resolved, we need to
1001
+ // evaluate it by executing `blockHelperMissing`
1002
+ this.opcode('pushProgram', program);
1003
+ this.opcode('pushProgram', inverse);
1004
+ this.opcode('emptyHash');
1005
+ this.opcode('blockValue');
1006
+ } else {
1007
+ this.ambiguousMustache(mustache, program, inverse);
1008
+
1009
+ // now that the simple mustache is resolved, we need to
1010
+ // evaluate it by executing `blockHelperMissing`
1011
+ this.opcode('pushProgram', program);
1012
+ this.opcode('pushProgram', inverse);
1013
+ this.opcode('emptyHash');
1014
+ this.opcode('ambiguousBlockValue');
1015
+ }
865
1016
 
866
- this.opcode('invokeProgram', null, params.length, !!block.mustache.hash);
867
- this.declare('inverse', null);
868
1017
  this.opcode('append');
869
1018
  },
870
1019
 
871
1020
  hash: function(hash) {
872
1021
  var pairs = hash.pairs, pair, val;
873
1022
 
874
- this.opcode('push', '{}');
1023
+ this.opcode('pushHash');
875
1024
 
876
1025
  for(var i=0, l=pairs.length; i<l; i++) {
877
1026
  pair = pairs[i];
878
1027
  val = pair[1];
879
1028
 
880
- this.accept(val);
1029
+ if (this.options.stringParams) {
1030
+ this.opcode('pushStringParam', val.stringModeValue, val.type);
1031
+ } else {
1032
+ this.accept(val);
1033
+ }
1034
+
881
1035
  this.opcode('assignToHash', pair[0]);
882
1036
  }
1037
+ this.opcode('popHash');
883
1038
  },
884
1039
 
885
1040
  partial: function(partial) {
886
- var id = partial.id;
1041
+ var partialName = partial.partialName;
887
1042
  this.usePartial = true;
888
1043
 
889
1044
  if(partial.context) {
@@ -892,7 +1047,7 @@ Handlebars.JavaScriptCompiler = function() {};
892
1047
  this.opcode('push', 'depth0');
893
1048
  }
894
1049
 
895
- this.opcode('invokePartial', id.original);
1050
+ this.opcode('invokePartial', partialName.name);
896
1051
  this.opcode('append');
897
1052
  },
898
1053
 
@@ -901,44 +1056,142 @@ Handlebars.JavaScriptCompiler = function() {};
901
1056
  },
902
1057
 
903
1058
  mustache: function(mustache) {
904
- var params = this.setupStackForMustache(mustache);
1059
+ var options = this.options;
1060
+ var type = this.classifyMustache(mustache);
905
1061
 
906
- this.opcode('invokeMustache', params.length, mustache.id.original, !!mustache.hash);
1062
+ if (type === "simple") {
1063
+ this.simpleMustache(mustache);
1064
+ } else if (type === "helper") {
1065
+ this.helperMustache(mustache);
1066
+ } else {
1067
+ this.ambiguousMustache(mustache);
1068
+ }
907
1069
 
908
- if(mustache.escaped && !this.options.noEscape) {
1070
+ if(mustache.escaped && !options.noEscape) {
909
1071
  this.opcode('appendEscaped');
910
1072
  } else {
911
1073
  this.opcode('append');
912
1074
  }
913
1075
  },
914
1076
 
1077
+ ambiguousMustache: function(mustache, program, inverse) {
1078
+ var id = mustache.id,
1079
+ name = id.parts[0],
1080
+ isBlock = program != null || inverse != null;
1081
+
1082
+ this.opcode('getContext', id.depth);
1083
+
1084
+ this.opcode('pushProgram', program);
1085
+ this.opcode('pushProgram', inverse);
1086
+
1087
+ this.opcode('invokeAmbiguous', name, isBlock);
1088
+ },
1089
+
1090
+ simpleMustache: function(mustache) {
1091
+ var id = mustache.id;
1092
+
1093
+ if (id.type === 'DATA') {
1094
+ this.DATA(id);
1095
+ } else if (id.parts.length) {
1096
+ this.ID(id);
1097
+ } else {
1098
+ // Simplified ID for `this`
1099
+ this.addDepth(id.depth);
1100
+ this.opcode('getContext', id.depth);
1101
+ this.opcode('pushContext');
1102
+ }
1103
+
1104
+ this.opcode('resolvePossibleLambda');
1105
+ },
1106
+
1107
+ helperMustache: function(mustache, program, inverse) {
1108
+ var params = this.setupFullMustacheParams(mustache, program, inverse),
1109
+ name = mustache.id.parts[0];
1110
+
1111
+ if (this.options.knownHelpers[name]) {
1112
+ this.opcode('invokeKnownHelper', params.length, name);
1113
+ } else if (this.knownHelpersOnly) {
1114
+ throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name);
1115
+ } else {
1116
+ this.opcode('invokeHelper', params.length, name);
1117
+ }
1118
+ },
1119
+
915
1120
  ID: function(id) {
916
1121
  this.addDepth(id.depth);
917
-
918
1122
  this.opcode('getContext', id.depth);
919
1123
 
920
- this.opcode('lookupWithHelpers', id.parts[0] || null, id.isScoped || false);
1124
+ var name = id.parts[0];
1125
+ if (!name) {
1126
+ this.opcode('pushContext');
1127
+ } else {
1128
+ this.opcode('lookupOnContext', id.parts[0]);
1129
+ }
921
1130
 
922
1131
  for(var i=1, l=id.parts.length; i<l; i++) {
923
1132
  this.opcode('lookup', id.parts[i]);
924
1133
  }
925
1134
  },
926
1135
 
1136
+ DATA: function(data) {
1137
+ this.options.data = true;
1138
+ this.opcode('lookupData', data.id);
1139
+ },
1140
+
927
1141
  STRING: function(string) {
928
1142
  this.opcode('pushString', string.string);
929
1143
  },
930
1144
 
931
1145
  INTEGER: function(integer) {
932
- this.opcode('push', integer.integer);
1146
+ this.opcode('pushLiteral', integer.integer);
933
1147
  },
934
1148
 
935
1149
  BOOLEAN: function(bool) {
936
- this.opcode('push', bool.bool);
1150
+ this.opcode('pushLiteral', bool.bool);
937
1151
  },
938
1152
 
939
1153
  comment: function() {},
940
1154
 
941
1155
  // HELPERS
1156
+ opcode: function(name) {
1157
+ this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) });
1158
+ },
1159
+
1160
+ declare: function(name, value) {
1161
+ this.opcodes.push({ opcode: 'DECLARE', name: name, value: value });
1162
+ },
1163
+
1164
+ addDepth: function(depth) {
1165
+ if(isNaN(depth)) { throw new Error("EWOT"); }
1166
+ if(depth === 0) { return; }
1167
+
1168
+ if(!this.depths[depth]) {
1169
+ this.depths[depth] = true;
1170
+ this.depths.list.push(depth);
1171
+ }
1172
+ },
1173
+
1174
+ classifyMustache: function(mustache) {
1175
+ var isHelper = mustache.isHelper;
1176
+ var isEligible = mustache.eligibleHelper;
1177
+ var options = this.options;
1178
+
1179
+ // if ambiguous, we can possibly resolve the ambiguity now
1180
+ if (isEligible && !isHelper) {
1181
+ var name = mustache.id.parts[0];
1182
+
1183
+ if (options.knownHelpers[name]) {
1184
+ isHelper = true;
1185
+ } else if (options.knownHelpersOnly) {
1186
+ isEligible = false;
1187
+ }
1188
+ }
1189
+
1190
+ if (isHelper) { return "helper"; }
1191
+ else if (isEligible) { return "ambiguous"; }
1192
+ else { return "simple"; }
1193
+ },
1194
+
942
1195
  pushParams: function(params) {
943
1196
  var i = params.length, param;
944
1197
 
@@ -951,61 +1204,59 @@ Handlebars.JavaScriptCompiler = function() {};
951
1204
  }
952
1205
 
953
1206
  this.opcode('getContext', param.depth || 0);
954
- this.opcode('pushStringParam', param.string);
1207
+ this.opcode('pushStringParam', param.stringModeValue, param.type);
955
1208
  } else {
956
1209
  this[param.type](param);
957
1210
  }
958
1211
  }
959
1212
  },
960
1213
 
961
- opcode: function(name, val1, val2, val3) {
962
- this.opcodes.push(Compiler.OPCODE_MAP[name]);
963
- if(val1 !== undefined) { this.opcodes.push(val1); }
964
- if(val2 !== undefined) { this.opcodes.push(val2); }
965
- if(val3 !== undefined) { this.opcodes.push(val3); }
966
- },
967
-
968
- declare: function(name, value) {
969
- this.opcodes.push('DECLARE');
970
- this.opcodes.push(name);
971
- this.opcodes.push(value);
972
- },
973
-
974
- addDepth: function(depth) {
975
- if(depth === 0) { return; }
1214
+ setupMustacheParams: function(mustache) {
1215
+ var params = mustache.params;
1216
+ this.pushParams(params);
976
1217
 
977
- if(!this.depths[depth]) {
978
- this.depths[depth] = true;
979
- this.depths.list.push(depth);
1218
+ if(mustache.hash) {
1219
+ this.hash(mustache.hash);
1220
+ } else {
1221
+ this.opcode('emptyHash');
980
1222
  }
1223
+
1224
+ return params;
981
1225
  },
982
1226
 
983
- setupStackForMustache: function(mustache) {
1227
+ // this will replace setupMustacheParams when we're done
1228
+ setupFullMustacheParams: function(mustache, program, inverse) {
984
1229
  var params = mustache.params;
985
-
986
1230
  this.pushParams(params);
987
1231
 
1232
+ this.opcode('pushProgram', program);
1233
+ this.opcode('pushProgram', inverse);
1234
+
988
1235
  if(mustache.hash) {
989
1236
  this.hash(mustache.hash);
1237
+ } else {
1238
+ this.opcode('emptyHash');
990
1239
  }
991
1240
 
992
- this.ID(mustache.id);
993
-
994
1241
  return params;
995
1242
  }
996
1243
  };
997
1244
 
1245
+ var Literal = function(value) {
1246
+ this.value = value;
1247
+ };
1248
+
998
1249
  JavaScriptCompiler.prototype = {
999
1250
  // PUBLIC API: You can override these methods in a subclass to provide
1000
1251
  // alternative compiled forms for name lookup and buffering semantics
1001
- nameLookup: function(parent, name, type) {
1002
- if (/^[0-9]+$/.test(name)) {
1252
+ nameLookup: function(parent, name /* , type*/) {
1253
+ if (/^[0-9]+$/.test(name)) {
1003
1254
  return parent + "[" + name + "]";
1004
1255
  } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
1005
- return parent + "." + name;
1006
- }
1007
- else {
1008
- return parent + "['" + name + "']";
1256
+ return parent + "." + name;
1257
+ }
1258
+ else {
1259
+ return parent + "['" + name + "']";
1009
1260
  }
1010
1261
  },
1011
1262
 
@@ -1013,7 +1264,11 @@ Handlebars.JavaScriptCompiler = function() {};
1013
1264
  if (this.environment.isSimple) {
1014
1265
  return "return " + string + ";";
1015
1266
  } else {
1016
- return "buffer += " + string + ";";
1267
+ return {
1268
+ appendToBuffer: true,
1269
+ content: string,
1270
+ toString: function() { return "buffer += " + string + ";"; }
1271
+ };
1017
1272
  }
1018
1273
  },
1019
1274
 
@@ -1028,18 +1283,23 @@ Handlebars.JavaScriptCompiler = function() {};
1028
1283
  this.environment = environment;
1029
1284
  this.options = options || {};
1030
1285
 
1286
+ Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
1287
+
1031
1288
  this.name = this.environment.name;
1032
1289
  this.isChild = !!context;
1033
1290
  this.context = context || {
1034
1291
  programs: [],
1035
- aliases: { self: 'this' },
1036
- registers: {list: []}
1292
+ environments: [],
1293
+ aliases: { }
1037
1294
  };
1038
1295
 
1039
1296
  this.preamble();
1040
1297
 
1041
1298
  this.stackSlot = 0;
1042
1299
  this.stackVars = [];
1300
+ this.registers = { list: [] };
1301
+ this.compileStack = [];
1302
+ this.inlineStack = [];
1043
1303
 
1044
1304
  this.compileChildren(environment, options);
1045
1305
 
@@ -1048,59 +1308,35 @@ Handlebars.JavaScriptCompiler = function() {};
1048
1308
  this.i = 0;
1049
1309
 
1050
1310
  for(l=opcodes.length; this.i<l; this.i++) {
1051
- opcode = this.nextOpcode(0);
1311
+ opcode = opcodes[this.i];
1052
1312
 
1053
- if(opcode[0] === 'DECLARE') {
1054
- this.i = this.i + 2;
1055
- this[opcode[1]] = opcode[2];
1313
+ if(opcode.opcode === 'DECLARE') {
1314
+ this[opcode.name] = opcode.value;
1056
1315
  } else {
1057
- this.i = this.i + opcode[1].length;
1058
- this[opcode[0]].apply(this, opcode[1]);
1316
+ this[opcode.opcode].apply(this, opcode.args);
1059
1317
  }
1060
1318
  }
1061
1319
 
1062
1320
  return this.createFunctionContext(asObject);
1063
1321
  },
1064
1322
 
1065
- nextOpcode: function(n) {
1066
- var opcodes = this.environment.opcodes, opcode = opcodes[this.i + n], name, val;
1067
- var extraParams, codes;
1068
-
1069
- if(opcode === 'DECLARE') {
1070
- name = opcodes[this.i + 1];
1071
- val = opcodes[this.i + 2];
1072
- return ['DECLARE', name, val];
1073
- } else {
1074
- name = Compiler.DISASSEMBLE_MAP[opcode];
1075
-
1076
- extraParams = Compiler.multiParamSize(opcode);
1077
- codes = [];
1078
-
1079
- for(var j=0; j<extraParams; j++) {
1080
- codes.push(opcodes[this.i + j + 1 + n]);
1081
- }
1082
-
1083
- return [name, codes];
1084
- }
1323
+ nextOpcode: function() {
1324
+ var opcodes = this.environment.opcodes;
1325
+ return opcodes[this.i + 1];
1085
1326
  },
1086
1327
 
1087
- eat: function(opcode) {
1088
- this.i = this.i + opcode.length;
1328
+ eat: function() {
1329
+ this.i = this.i + 1;
1089
1330
  },
1090
1331
 
1091
1332
  preamble: function() {
1092
1333
  var out = [];
1093
1334
 
1094
- // this register will disambiguate helper lookup from finding a function in
1095
- // a context. This is necessary for mustache compatibility, which requires
1096
- // that context functions in blocks are evaluated by blockHelperMissing, and
1097
- // then proceed as if the resulting value was provided to blockHelperMissing.
1098
- this.useRegister('foundHelper');
1099
-
1100
1335
  if (!this.isChild) {
1101
1336
  var namespace = this.namespace;
1102
1337
  var copies = "helpers = helpers || " + namespace + ".helpers;";
1103
- if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
1338
+ if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
1339
+ if (this.options.data) { copies = copies + " data = data || {};"; }
1104
1340
  out.push(copies);
1105
1341
  } else {
1106
1342
  out.push('');
@@ -1119,10 +1355,7 @@ Handlebars.JavaScriptCompiler = function() {};
1119
1355
  },
1120
1356
 
1121
1357
  createFunctionContext: function(asObject) {
1122
- var locals = this.stackVars;
1123
- if (!this.isChild) {
1124
- locals = locals.concat(this.context.registers.list);
1125
- }
1358
+ var locals = this.stackVars.concat(this.registers.list);
1126
1359
 
1127
1360
  if(locals.length > 0) {
1128
1361
  this.source[1] = this.source[1] + ", " + locals.join(", ");
@@ -1130,7 +1363,6 @@ Handlebars.JavaScriptCompiler = function() {};
1130
1363
 
1131
1364
  // Generate minimizer alias mappings
1132
1365
  if (!this.isChild) {
1133
- var aliases = []
1134
1366
  for (var alias in this.context.aliases) {
1135
1367
  this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
1136
1368
  }
@@ -1155,22 +1387,114 @@ Handlebars.JavaScriptCompiler = function() {};
1155
1387
  params.push("depth" + this.environment.depths.list[i]);
1156
1388
  }
1157
1389
 
1390
+ // Perform a second pass over the output to merge content when possible
1391
+ var source = this.mergeSource();
1392
+
1393
+ if (!this.isChild) {
1394
+ var revision = Handlebars.COMPILER_REVISION,
1395
+ versions = Handlebars.REVISION_CHANGES[revision];
1396
+ source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source;
1397
+ }
1398
+
1158
1399
  if (asObject) {
1159
- params.push(this.source.join("\n "));
1400
+ params.push(source);
1160
1401
 
1161
1402
  return Function.apply(this, params);
1162
1403
  } else {
1163
- var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}';
1404
+ var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
1164
1405
  Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
1165
1406
  return functionSource;
1166
1407
  }
1167
1408
  },
1409
+ mergeSource: function() {
1410
+ // WARN: We are not handling the case where buffer is still populated as the source should
1411
+ // not have buffer append operations as their final action.
1412
+ var source = '',
1413
+ buffer;
1414
+ for (var i = 0, len = this.source.length; i < len; i++) {
1415
+ var line = this.source[i];
1416
+ if (line.appendToBuffer) {
1417
+ if (buffer) {
1418
+ buffer = buffer + '\n + ' + line.content;
1419
+ } else {
1420
+ buffer = line.content;
1421
+ }
1422
+ } else {
1423
+ if (buffer) {
1424
+ source += 'buffer += ' + buffer + ';\n ';
1425
+ buffer = undefined;
1426
+ }
1427
+ source += line + '\n ';
1428
+ }
1429
+ }
1430
+ return source;
1431
+ },
1432
+
1433
+ // [blockValue]
1434
+ //
1435
+ // On stack, before: hash, inverse, program, value
1436
+ // On stack, after: return value of blockHelperMissing
1437
+ //
1438
+ // The purpose of this opcode is to take a block of the form
1439
+ // `{{#foo}}...{{/foo}}`, resolve the value of `foo`, and
1440
+ // replace it on the stack with the result of properly
1441
+ // invoking blockHelperMissing.
1442
+ blockValue: function() {
1443
+ this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
1444
+
1445
+ var params = ["depth0"];
1446
+ this.setupParams(0, params);
1447
+
1448
+ this.replaceStack(function(current) {
1449
+ params.splice(1, 0, current);
1450
+ return "blockHelperMissing.call(" + params.join(", ") + ")";
1451
+ });
1452
+ },
1453
+
1454
+ // [ambiguousBlockValue]
1455
+ //
1456
+ // On stack, before: hash, inverse, program, value
1457
+ // Compiler value, before: lastHelper=value of last found helper, if any
1458
+ // On stack, after, if no lastHelper: same as [blockValue]
1459
+ // On stack, after, if lastHelper: value
1460
+ ambiguousBlockValue: function() {
1461
+ this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
1462
+
1463
+ var params = ["depth0"];
1464
+ this.setupParams(0, params);
1168
1465
 
1466
+ var current = this.topStack();
1467
+ params.splice(1, 0, current);
1468
+
1469
+ // Use the options value generated from the invocation
1470
+ params[params.length-1] = 'options';
1471
+
1472
+ this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
1473
+ },
1474
+
1475
+ // [appendContent]
1476
+ //
1477
+ // On stack, before: ...
1478
+ // On stack, after: ...
1479
+ //
1480
+ // Appends the string value of `content` to the current buffer
1169
1481
  appendContent: function(content) {
1170
1482
  this.source.push(this.appendToBuffer(this.quotedString(content)));
1171
1483
  },
1172
1484
 
1485
+ // [append]
1486
+ //
1487
+ // On stack, before: value, ...
1488
+ // On stack, after: ...
1489
+ //
1490
+ // Coerces `value` to a String and appends it to the current buffer.
1491
+ //
1492
+ // If `value` is truthy, or 0, it is coerced into a string and appended
1493
+ // Otherwise, the empty string is appended
1173
1494
  append: function() {
1495
+ // Force anything that is inlined onto the stack so we don't have duplication
1496
+ // when we examine local
1497
+ this.flushInline();
1174
1498
  var local = this.popStack();
1175
1499
  this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
1176
1500
  if (this.environment.isSimple) {
@@ -1178,168 +1502,279 @@ Handlebars.JavaScriptCompiler = function() {};
1178
1502
  }
1179
1503
  },
1180
1504
 
1505
+ // [appendEscaped]
1506
+ //
1507
+ // On stack, before: value, ...
1508
+ // On stack, after: ...
1509
+ //
1510
+ // Escape `value` and append it to the buffer
1181
1511
  appendEscaped: function() {
1182
- var opcode = this.nextOpcode(1), extra = "";
1183
1512
  this.context.aliases.escapeExpression = 'this.escapeExpression';
1184
1513
 
1185
- if(opcode[0] === 'appendContent') {
1186
- extra = " + " + this.quotedString(opcode[1][0]);
1187
- this.eat(opcode);
1188
- }
1189
-
1190
- this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
1514
+ this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
1191
1515
  },
1192
1516
 
1517
+ // [getContext]
1518
+ //
1519
+ // On stack, before: ...
1520
+ // On stack, after: ...
1521
+ // Compiler value, after: lastContext=depth
1522
+ //
1523
+ // Set the value of the `lastContext` compiler value to the depth
1193
1524
  getContext: function(depth) {
1194
1525
  if(this.lastContext !== depth) {
1195
1526
  this.lastContext = depth;
1196
1527
  }
1197
1528
  },
1198
1529
 
1199
- lookupWithHelpers: function(name, isScoped) {
1200
- if(name) {
1201
- var topStack = this.nextStack();
1202
-
1203
- this.usingKnownHelper = false;
1204
-
1205
- var toPush;
1206
- if (!isScoped && this.options.knownHelpers[name]) {
1207
- toPush = topStack + " = " + this.nameLookup('helpers', name, 'helper');
1208
- this.usingKnownHelper = true;
1209
- } else if (isScoped || this.options.knownHelpersOnly) {
1210
- toPush = topStack + " = " + this.nameLookup('depth' + this.lastContext, name, 'context');
1211
- } else {
1212
- this.register('foundHelper', this.nameLookup('helpers', name, 'helper'));
1213
- toPush = topStack + " = foundHelper || " + this.nameLookup('depth' + this.lastContext, name, 'context');
1214
- }
1215
-
1216
- toPush += ';';
1217
- this.source.push(toPush);
1218
- } else {
1219
- this.pushStack('depth' + this.lastContext);
1220
- }
1530
+ // [lookupOnContext]
1531
+ //
1532
+ // On stack, before: ...
1533
+ // On stack, after: currentContext[name], ...
1534
+ //
1535
+ // Looks up the value of `name` on the current context and pushes
1536
+ // it onto the stack.
1537
+ lookupOnContext: function(name) {
1538
+ this.push(this.nameLookup('depth' + this.lastContext, name, 'context'));
1539
+ },
1540
+
1541
+ // [pushContext]
1542
+ //
1543
+ // On stack, before: ...
1544
+ // On stack, after: currentContext, ...
1545
+ //
1546
+ // Pushes the value of the current context onto the stack.
1547
+ pushContext: function() {
1548
+ this.pushStackLiteral('depth' + this.lastContext);
1549
+ },
1550
+
1551
+ // [resolvePossibleLambda]
1552
+ //
1553
+ // On stack, before: value, ...
1554
+ // On stack, after: resolved value, ...
1555
+ //
1556
+ // If the `value` is a lambda, replace it on the stack by
1557
+ // the return value of the lambda
1558
+ resolvePossibleLambda: function() {
1559
+ this.context.aliases.functionType = '"function"';
1560
+
1561
+ this.replaceStack(function(current) {
1562
+ return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
1563
+ });
1221
1564
  },
1222
1565
 
1566
+ // [lookup]
1567
+ //
1568
+ // On stack, before: value, ...
1569
+ // On stack, after: value[name], ...
1570
+ //
1571
+ // Replace the value on the stack with the result of looking
1572
+ // up `name` on `value`
1223
1573
  lookup: function(name) {
1224
- var topStack = this.topStack();
1225
- this.source.push(topStack + " = (" + topStack + " === null || " + topStack + " === undefined || " + topStack + " === false ? " +
1226
- topStack + " : " + this.nameLookup(topStack, name, 'context') + ");");
1227
- },
1228
-
1229
- pushStringParam: function(string) {
1230
- this.pushStack('depth' + this.lastContext);
1231
- this.pushString(string);
1232
- },
1233
-
1234
- pushString: function(string) {
1235
- this.pushStack(this.quotedString(string));
1236
- },
1237
-
1238
- push: function(name) {
1239
- this.pushStack(name);
1240
- },
1241
-
1242
- invokeMustache: function(paramSize, original, hasHash) {
1243
- this.populateParams(paramSize, this.quotedString(original), "{}", null, hasHash, function(nextStack, helperMissingString, id) {
1244
- if (!this.usingKnownHelper) {
1245
- this.context.aliases.helperMissing = 'helpers.helperMissing';
1246
- this.context.aliases.undef = 'void 0';
1247
- this.source.push("else if(" + id + "=== undef) { " + nextStack + " = helperMissing.call(" + helperMissingString + "); }");
1248
- if (nextStack !== id) {
1249
- this.source.push("else { " + nextStack + " = " + id + "; }");
1250
- }
1251
- }
1574
+ this.replaceStack(function(current) {
1575
+ return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
1252
1576
  });
1253
1577
  },
1254
1578
 
1255
- invokeProgram: function(guid, paramSize, hasHash) {
1256
- var inverse = this.programExpression(this.inverse);
1257
- var mainProgram = this.programExpression(guid);
1258
-
1259
- this.populateParams(paramSize, null, mainProgram, inverse, hasHash, function(nextStack, helperMissingString, id) {
1260
- if (!this.usingKnownHelper) {
1261
- this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
1262
- this.source.push("else { " + nextStack + " = blockHelperMissing.call(" + helperMissingString + "); }");
1263
- }
1264
- });
1579
+ // [lookupData]
1580
+ //
1581
+ // On stack, before: ...
1582
+ // On stack, after: data[id], ...
1583
+ //
1584
+ // Push the result of looking up `id` on the current data
1585
+ lookupData: function(id) {
1586
+ this.push(this.nameLookup('data', id, 'data'));
1265
1587
  },
1266
1588
 
1267
- populateParams: function(paramSize, helperId, program, inverse, hasHash, fn) {
1268
- var needsRegister = hasHash || this.options.stringParams || inverse || this.options.data;
1269
- var id = this.popStack(), nextStack;
1270
- var params = [], param, stringParam, stringOptions;
1589
+ // [pushStringParam]
1590
+ //
1591
+ // On stack, before: ...
1592
+ // On stack, after: string, currentContext, ...
1593
+ //
1594
+ // This opcode is designed for use in string mode, which
1595
+ // provides the string value of a parameter along with its
1596
+ // depth rather than resolving it immediately.
1597
+ pushStringParam: function(string, type) {
1598
+ this.pushStackLiteral('depth' + this.lastContext);
1271
1599
 
1272
- if (needsRegister) {
1273
- this.register('tmp1', program);
1274
- stringOptions = 'tmp1';
1275
- } else {
1276
- stringOptions = '{ hash: {} }';
1277
- }
1278
-
1279
- if (needsRegister) {
1280
- var hash = (hasHash ? this.popStack() : '{}');
1281
- this.source.push('tmp1.hash = ' + hash + ';');
1282
- }
1600
+ this.pushString(type);
1283
1601
 
1284
- if(this.options.stringParams) {
1285
- this.source.push('tmp1.contexts = [];');
1602
+ if (typeof string === 'string') {
1603
+ this.pushString(string);
1604
+ } else {
1605
+ this.pushStackLiteral(string);
1286
1606
  }
1607
+ },
1287
1608
 
1288
- for(var i=0; i<paramSize; i++) {
1289
- param = this.popStack();
1290
- params.push(param);
1609
+ emptyHash: function() {
1610
+ this.pushStackLiteral('{}');
1291
1611
 
1292
- if(this.options.stringParams) {
1293
- this.source.push('tmp1.contexts.push(' + this.popStack() + ');');
1294
- }
1612
+ if (this.options.stringParams) {
1613
+ this.register('hashTypes', '{}');
1295
1614
  }
1615
+ },
1616
+ pushHash: function() {
1617
+ this.hash = {values: [], types: []};
1618
+ },
1619
+ popHash: function() {
1620
+ var hash = this.hash;
1621
+ this.hash = undefined;
1296
1622
 
1297
- if(inverse) {
1298
- this.source.push('tmp1.fn = tmp1;');
1299
- this.source.push('tmp1.inverse = ' + inverse + ';');
1623
+ if (this.options.stringParams) {
1624
+ this.register('hashTypes', '{' + hash.types.join(',') + '}');
1300
1625
  }
1626
+ this.push('{\n ' + hash.values.join(',\n ') + '\n }');
1627
+ },
1301
1628
 
1302
- if(this.options.data) {
1303
- this.source.push('tmp1.data = data;');
1629
+ // [pushString]
1630
+ //
1631
+ // On stack, before: ...
1632
+ // On stack, after: quotedString(string), ...
1633
+ //
1634
+ // Push a quoted version of `string` onto the stack
1635
+ pushString: function(string) {
1636
+ this.pushStackLiteral(this.quotedString(string));
1637
+ },
1638
+
1639
+ // [push]
1640
+ //
1641
+ // On stack, before: ...
1642
+ // On stack, after: expr, ...
1643
+ //
1644
+ // Push an expression onto the stack
1645
+ push: function(expr) {
1646
+ this.inlineStack.push(expr);
1647
+ return expr;
1648
+ },
1649
+
1650
+ // [pushLiteral]
1651
+ //
1652
+ // On stack, before: ...
1653
+ // On stack, after: value, ...
1654
+ //
1655
+ // Pushes a value onto the stack. This operation prevents
1656
+ // the compiler from creating a temporary variable to hold
1657
+ // it.
1658
+ pushLiteral: function(value) {
1659
+ this.pushStackLiteral(value);
1660
+ },
1661
+
1662
+ // [pushProgram]
1663
+ //
1664
+ // On stack, before: ...
1665
+ // On stack, after: program(guid), ...
1666
+ //
1667
+ // Push a program expression onto the stack. This takes
1668
+ // a compile-time guid and converts it into a runtime-accessible
1669
+ // expression.
1670
+ pushProgram: function(guid) {
1671
+ if (guid != null) {
1672
+ this.pushStackLiteral(this.programExpression(guid));
1673
+ } else {
1674
+ this.pushStackLiteral(null);
1304
1675
  }
1305
-
1306
- params.push(stringOptions);
1307
-
1308
- this.populateCall(params, id, helperId || id, fn, program !== '{}');
1309
1676
  },
1310
1677
 
1311
- populateCall: function(params, id, helperId, fn, program) {
1312
- var paramString = ["depth0"].concat(params).join(", ");
1313
- var helperMissingString = ["depth0"].concat(helperId).concat(params).join(", ");
1678
+ // [invokeHelper]
1679
+ //
1680
+ // On stack, before: hash, inverse, program, params..., ...
1681
+ // On stack, after: result of helper invocation
1682
+ //
1683
+ // Pops off the helper's parameters, invokes the helper,
1684
+ // and pushes the helper's return value onto the stack.
1685
+ //
1686
+ // If the helper is not found, `helperMissing` is called.
1687
+ invokeHelper: function(paramSize, name) {
1688
+ this.context.aliases.helperMissing = 'helpers.helperMissing';
1689
+
1690
+ var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
1691
+
1692
+ this.push(helper.name);
1693
+ this.replaceStack(function(name) {
1694
+ return name + ' ? ' + name + '.call(' +
1695
+ helper.callParams + ") " + ": helperMissing.call(" +
1696
+ helper.helperMissingParams + ")";
1697
+ });
1698
+ },
1314
1699
 
1700
+ // [invokeKnownHelper]
1701
+ //
1702
+ // On stack, before: hash, inverse, program, params..., ...
1703
+ // On stack, after: result of helper invocation
1704
+ //
1705
+ // This operation is used when the helper is known to exist,
1706
+ // so a `helperMissing` fallback is not required.
1707
+ invokeKnownHelper: function(paramSize, name) {
1708
+ var helper = this.setupHelper(paramSize, name);
1709
+ this.push(helper.name + ".call(" + helper.callParams + ")");
1710
+ },
1711
+
1712
+ // [invokeAmbiguous]
1713
+ //
1714
+ // On stack, before: hash, inverse, program, params..., ...
1715
+ // On stack, after: result of disambiguation
1716
+ //
1717
+ // This operation is used when an expression like `{{foo}}`
1718
+ // is provided, but we don't know at compile-time whether it
1719
+ // is a helper or a path.
1720
+ //
1721
+ // This operation emits more code than the other options,
1722
+ // and can be avoided by passing the `knownHelpers` and
1723
+ // `knownHelpersOnly` flags at compile-time.
1724
+ invokeAmbiguous: function(name, helperCall) {
1725
+ this.context.aliases.functionType = '"function"';
1726
+
1727
+ this.pushStackLiteral('{}'); // Hash value
1728
+ var helper = this.setupHelper(0, name, helperCall);
1729
+
1730
+ var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
1731
+
1732
+ var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
1315
1733
  var nextStack = this.nextStack();
1316
1734
 
1317
- if (this.usingKnownHelper) {
1318
- this.source.push(nextStack + " = " + id + ".call(" + paramString + ");");
1319
- } else {
1320
- this.context.aliases.functionType = '"function"';
1321
- var condition = program ? "foundHelper && " : ""
1322
- this.source.push("if(" + condition + "typeof " + id + " === functionType) { " + nextStack + " = " + id + ".call(" + paramString + "); }");
1323
- }
1324
- fn.call(this, nextStack, helperMissingString, id);
1325
- this.usingKnownHelper = false;
1735
+ this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
1736
+ this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
1326
1737
  },
1327
1738
 
1328
- invokePartial: function(context) {
1329
- params = [this.nameLookup('partials', context, 'partial'), "'" + context + "'", this.popStack(), "helpers", "partials"];
1739
+ // [invokePartial]
1740
+ //
1741
+ // On stack, before: context, ...
1742
+ // On stack after: result of partial invocation
1743
+ //
1744
+ // This operation pops off a context, invokes a partial with that context,
1745
+ // and pushes the result of the invocation back.
1746
+ invokePartial: function(name) {
1747
+ var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
1330
1748
 
1331
1749
  if (this.options.data) {
1332
1750
  params.push("data");
1333
1751
  }
1334
1752
 
1335
- this.pushStack("self.invokePartial(" + params.join(", ") + ");");
1753
+ this.context.aliases.self = "this";
1754
+ this.push("self.invokePartial(" + params.join(", ") + ")");
1336
1755
  },
1337
1756
 
1757
+ // [assignToHash]
1758
+ //
1759
+ // On stack, before: value, hash, ...
1760
+ // On stack, after: hash, ...
1761
+ //
1762
+ // Pops a value and hash off the stack, assigns `hash[key] = value`
1763
+ // and pushes the hash back onto the stack.
1338
1764
  assignToHash: function(key) {
1339
- var value = this.popStack();
1340
- var hash = this.topStack();
1765
+ var value = this.popStack(),
1766
+ type;
1341
1767
 
1342
- this.source.push(hash + "['" + key + "'] = " + value + ";");
1768
+ if (this.options.stringParams) {
1769
+ type = this.popStack();
1770
+ this.popStack();
1771
+ }
1772
+
1773
+ var hash = this.hash;
1774
+ if (type) {
1775
+ hash.types.push("'" + key + "': " + type);
1776
+ }
1777
+ hash.values.push("'" + key + "': (" + value + ")");
1343
1778
  },
1344
1779
 
1345
1780
  // HELPERS
@@ -1353,19 +1788,40 @@ Handlebars.JavaScriptCompiler = function() {};
1353
1788
  child = children[i];
1354
1789
  compiler = new this.compiler();
1355
1790
 
1356
- this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
1357
- var index = this.context.programs.length;
1358
- child.index = index;
1359
- child.name = 'program' + index;
1360
- this.context.programs[index] = compiler.compile(child, options, this.context);
1791
+ var index = this.matchExistingProgram(child);
1792
+
1793
+ if (index == null) {
1794
+ this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
1795
+ index = this.context.programs.length;
1796
+ child.index = index;
1797
+ child.name = 'program' + index;
1798
+ this.context.programs[index] = compiler.compile(child, options, this.context);
1799
+ this.context.environments[index] = child;
1800
+ } else {
1801
+ child.index = index;
1802
+ child.name = 'program' + index;
1803
+ }
1804
+ }
1805
+ },
1806
+ matchExistingProgram: function(child) {
1807
+ for (var i = 0, len = this.context.environments.length; i < len; i++) {
1808
+ var environment = this.context.environments[i];
1809
+ if (environment && environment.equals(child)) {
1810
+ return i;
1811
+ }
1361
1812
  }
1362
1813
  },
1363
1814
 
1364
1815
  programExpression: function(guid) {
1365
- if(guid == null) { return "self.noop"; }
1816
+ this.context.aliases.self = "this";
1817
+
1818
+ if(guid == null) {
1819
+ return "self.noop";
1820
+ }
1366
1821
 
1367
1822
  var child = this.environment.children[guid],
1368
- depths = child.depths.list;
1823
+ depths = child.depths.list, depth;
1824
+
1369
1825
  var programParams = [child.index, child.name, "data"];
1370
1826
 
1371
1827
  for(var i=0, l = depths.length; i<l; i++) {
@@ -1389,29 +1845,122 @@ Handlebars.JavaScriptCompiler = function() {};
1389
1845
  },
1390
1846
 
1391
1847
  useRegister: function(name) {
1392
- if(!this.context.registers[name]) {
1393
- this.context.registers[name] = true;
1394
- this.context.registers.list.push(name);
1848
+ if(!this.registers[name]) {
1849
+ this.registers[name] = true;
1850
+ this.registers.list.push(name);
1395
1851
  }
1396
1852
  },
1397
1853
 
1854
+ pushStackLiteral: function(item) {
1855
+ return this.push(new Literal(item));
1856
+ },
1857
+
1398
1858
  pushStack: function(item) {
1399
- this.source.push(this.nextStack() + " = " + item + ";");
1400
- return "stack" + this.stackSlot;
1859
+ this.flushInline();
1860
+
1861
+ var stack = this.incrStack();
1862
+ if (item) {
1863
+ this.source.push(stack + " = " + item + ";");
1864
+ }
1865
+ this.compileStack.push(stack);
1866
+ return stack;
1867
+ },
1868
+
1869
+ replaceStack: function(callback) {
1870
+ var prefix = '',
1871
+ inline = this.isInline(),
1872
+ stack;
1873
+
1874
+ // If we are currently inline then we want to merge the inline statement into the
1875
+ // replacement statement via ','
1876
+ if (inline) {
1877
+ var top = this.popStack(true);
1878
+
1879
+ if (top instanceof Literal) {
1880
+ // Literals do not need to be inlined
1881
+ stack = top.value;
1882
+ } else {
1883
+ // Get or create the current stack name for use by the inline
1884
+ var name = this.stackSlot ? this.topStackName() : this.incrStack();
1885
+
1886
+ prefix = '(' + this.push(name) + ' = ' + top + '),';
1887
+ stack = this.topStack();
1888
+ }
1889
+ } else {
1890
+ stack = this.topStack();
1891
+ }
1892
+
1893
+ var item = callback.call(this, stack);
1894
+
1895
+ if (inline) {
1896
+ if (this.inlineStack.length || this.compileStack.length) {
1897
+ this.popStack();
1898
+ }
1899
+ this.push('(' + prefix + item + ')');
1900
+ } else {
1901
+ // Prevent modification of the context depth variable. Through replaceStack
1902
+ if (!/^stack/.test(stack)) {
1903
+ stack = this.nextStack();
1904
+ }
1905
+
1906
+ this.source.push(stack + " = (" + prefix + item + ");");
1907
+ }
1908
+ return stack;
1401
1909
  },
1402
1910
 
1403
1911
  nextStack: function() {
1912
+ return this.pushStack();
1913
+ },
1914
+
1915
+ incrStack: function() {
1404
1916
  this.stackSlot++;
1405
1917
  if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
1918
+ return this.topStackName();
1919
+ },
1920
+ topStackName: function() {
1406
1921
  return "stack" + this.stackSlot;
1407
1922
  },
1923
+ flushInline: function() {
1924
+ var inlineStack = this.inlineStack;
1925
+ if (inlineStack.length) {
1926
+ this.inlineStack = [];
1927
+ for (var i = 0, len = inlineStack.length; i < len; i++) {
1928
+ var entry = inlineStack[i];
1929
+ if (entry instanceof Literal) {
1930
+ this.compileStack.push(entry);
1931
+ } else {
1932
+ this.pushStack(entry);
1933
+ }
1934
+ }
1935
+ }
1936
+ },
1937
+ isInline: function() {
1938
+ return this.inlineStack.length;
1939
+ },
1940
+
1941
+ popStack: function(wrapped) {
1942
+ var inline = this.isInline(),
1943
+ item = (inline ? this.inlineStack : this.compileStack).pop();
1408
1944
 
1409
- popStack: function() {
1410
- return "stack" + this.stackSlot--;
1945
+ if (!wrapped && (item instanceof Literal)) {
1946
+ return item.value;
1947
+ } else {
1948
+ if (!inline) {
1949
+ this.stackSlot--;
1950
+ }
1951
+ return item;
1952
+ }
1411
1953
  },
1412
1954
 
1413
- topStack: function() {
1414
- return "stack" + this.stackSlot;
1955
+ topStack: function(wrapped) {
1956
+ var stack = (this.isInline() ? this.inlineStack : this.compileStack),
1957
+ item = stack[stack.length - 1];
1958
+
1959
+ if (!wrapped && (item instanceof Literal)) {
1960
+ return item.value;
1961
+ } else {
1962
+ return item;
1963
+ }
1415
1964
  },
1416
1965
 
1417
1966
  quotedString: function(str) {
@@ -1420,6 +1969,76 @@ Handlebars.JavaScriptCompiler = function() {};
1420
1969
  .replace(/"/g, '\\"')
1421
1970
  .replace(/\n/g, '\\n')
1422
1971
  .replace(/\r/g, '\\r') + '"';
1972
+ },
1973
+
1974
+ setupHelper: function(paramSize, name, missingParams) {
1975
+ var params = [];
1976
+ this.setupParams(paramSize, params, missingParams);
1977
+ var foundHelper = this.nameLookup('helpers', name, 'helper');
1978
+
1979
+ return {
1980
+ params: params,
1981
+ name: foundHelper,
1982
+ callParams: ["depth0"].concat(params).join(", "),
1983
+ helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
1984
+ };
1985
+ },
1986
+
1987
+ // the params and contexts arguments are passed in arrays
1988
+ // to fill in
1989
+ setupParams: function(paramSize, params, useRegister) {
1990
+ var options = [], contexts = [], types = [], param, inverse, program;
1991
+
1992
+ options.push("hash:" + this.popStack());
1993
+
1994
+ inverse = this.popStack();
1995
+ program = this.popStack();
1996
+
1997
+ // Avoid setting fn and inverse if neither are set. This allows
1998
+ // helpers to do a check for `if (options.fn)`
1999
+ if (program || inverse) {
2000
+ if (!program) {
2001
+ this.context.aliases.self = "this";
2002
+ program = "self.noop";
2003
+ }
2004
+
2005
+ if (!inverse) {
2006
+ this.context.aliases.self = "this";
2007
+ inverse = "self.noop";
2008
+ }
2009
+
2010
+ options.push("inverse:" + inverse);
2011
+ options.push("fn:" + program);
2012
+ }
2013
+
2014
+ for(var i=0; i<paramSize; i++) {
2015
+ param = this.popStack();
2016
+ params.push(param);
2017
+
2018
+ if(this.options.stringParams) {
2019
+ types.push(this.popStack());
2020
+ contexts.push(this.popStack());
2021
+ }
2022
+ }
2023
+
2024
+ if (this.options.stringParams) {
2025
+ options.push("contexts:[" + contexts.join(",") + "]");
2026
+ options.push("types:[" + types.join(",") + "]");
2027
+ options.push("hashTypes:hashTypes");
2028
+ }
2029
+
2030
+ if(this.options.data) {
2031
+ options.push("data:data");
2032
+ }
2033
+
2034
+ options = "{" + options.join(",") + "}";
2035
+ if (useRegister) {
2036
+ this.register('options', options);
2037
+ params.push('options');
2038
+ } else {
2039
+ params.push(options);
2040
+ }
2041
+ return params.join(", ");
1423
2042
  }
1424
2043
  };
1425
2044
 
@@ -1447,29 +2066,41 @@ Handlebars.JavaScriptCompiler = function() {};
1447
2066
  compilerWords[reservedWords[i]] = true;
1448
2067
  }
1449
2068
 
1450
- JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
1451
- if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
1452
- return true;
1453
- }
1454
- return false;
1455
- }
2069
+ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
2070
+ if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
2071
+ return true;
2072
+ }
2073
+ return false;
2074
+ };
1456
2075
 
1457
2076
  })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
1458
2077
 
1459
- Handlebars.precompile = function(string, options) {
1460
- options = options || {};
2078
+ Handlebars.precompile = function(input, options) {
2079
+ if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
2080
+ throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
2081
+ }
1461
2082
 
1462
- var ast = Handlebars.parse(string);
2083
+ options = options || {};
2084
+ if (!('data' in options)) {
2085
+ options.data = true;
2086
+ }
2087
+ var ast = Handlebars.parse(input);
1463
2088
  var environment = new Handlebars.Compiler().compile(ast, options);
1464
2089
  return new Handlebars.JavaScriptCompiler().compile(environment, options);
1465
2090
  };
1466
2091
 
1467
- Handlebars.compile = function(string, options) {
1468
- options = options || {};
2092
+ Handlebars.compile = function(input, options) {
2093
+ if (!input || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) {
2094
+ throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
2095
+ }
1469
2096
 
2097
+ options = options || {};
2098
+ if (!('data' in options)) {
2099
+ options.data = true;
2100
+ }
1470
2101
  var compiled;
1471
2102
  function compile() {
1472
- var ast = Handlebars.parse(string);
2103
+ var ast = Handlebars.parse(input);
1473
2104
  var environment = new Handlebars.Compiler().compile(ast, options);
1474
2105
  var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
1475
2106
  return Handlebars.template(templateSpec);
@@ -1504,12 +2135,32 @@ Handlebars.VM = {
1504
2135
  }
1505
2136
  },
1506
2137
  programWithDepth: Handlebars.VM.programWithDepth,
1507
- noop: Handlebars.VM.noop
2138
+ noop: Handlebars.VM.noop,
2139
+ compilerInfo: null
1508
2140
  };
1509
2141
 
1510
2142
  return function(context, options) {
1511
2143
  options = options || {};
1512
- return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
2144
+ var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
2145
+
2146
+ var compilerInfo = container.compilerInfo || [],
2147
+ compilerRevision = compilerInfo[0] || 1,
2148
+ currentRevision = Handlebars.COMPILER_REVISION;
2149
+
2150
+ if (compilerRevision !== currentRevision) {
2151
+ if (compilerRevision < currentRevision) {
2152
+ var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
2153
+ compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
2154
+ throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
2155
+ "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
2156
+ } else {
2157
+ // Use the embedded version info since the runtime doesn't know about this revision yet
2158
+ throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
2159
+ "Please update your runtime to a newer version ("+compilerInfo[1]+").";
2160
+ }
2161
+ }
2162
+
2163
+ return result;
1513
2164
  };
1514
2165
  },
1515
2166
 
@@ -1531,7 +2182,7 @@ Handlebars.VM = {
1531
2182
  },
1532
2183
  noop: function() { return ""; },
1533
2184
  invokePartial: function(partial, name, context, helpers, partials, data) {
1534
- options = { helpers: helpers, partials: partials, data: data };
2185
+ var options = { helpers: helpers, partials: partials, data: data };
1535
2186
 
1536
2187
  if(partial === undefined) {
1537
2188
  throw new Handlebars.Exception("The partial " + name + " could not be found");
@@ -1540,11 +2191,11 @@ Handlebars.VM = {
1540
2191
  } else if (!Handlebars.compile) {
1541
2192
  throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
1542
2193
  } else {
1543
- partials[name] = Handlebars.compile(partial);
2194
+ partials[name] = Handlebars.compile(partial, {data: data !== undefined});
1544
2195
  return partials[name](context, options);
1545
2196
  }
1546
2197
  }
1547
2198
  };
1548
2199
 
1549
2200
  Handlebars.template = Handlebars.VM.template;
1550
- ;
2201
+ ;