My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708

jsal.module({
name : 'heist',
depends : ['heist.runtime'],
onload : function () {
if (! this.heist) this.heist = {};
var heist = this.heist;

heist.ast = {};

if (! Array.prototype.map) {
Array.prototype.map = function (callback) {
if (typeof(callback) != "function")
throw "parameter to map must be a function";
var len = this.length;
var res = new Array(len);
var context = arguments[1] || glob;
for (var i = 0; i < len; i++) {
if (i in this) res[i] = callback.apply(context, [this[i], i, this]);
}
return res;
};
}

heist.quote = function (str) {
if (typeof str == 'undefined') return 'undefined';
return ('"' + str.replace(/(?:(\r\n|\n|\r)|([\\\"]))/g, function (lineBreak, character) {
return lineBreak ? "\\n\"+\n\"" : "\\" + character;
}) + '"').replace(/\+(?:\r\n|\n|\r)\"\"$/, "\n");
};

heist.htmlEscape = function (str) {
if (str == undefined) return '';
return str.replace(/([&\"<>])/g, function (character) {
return '&' + (
character == '&' ? 'amp' :
character == '"' ? 'quot' :
character == '<' ? 'lt' :
'gt'
) + ';';
});
};

heist.ast.Method = function (
preIdentifierWhitespace,
methodName,
preArgumentListWhitespace,
argumentList,
preTagCloseWhitespace,
subnodes
) {
this.type = 'Method';
this.preIdentifierWhitespace = preIdentifierWhitespace;
this.methodName = methodName;
this.preArgumentListWhitespace = preArgumentListWhitespace;
this.argumentList = argumentList == "" ? undefined : argumentList;
this.preTagCloseWhitespace = preTagCloseWhitespace;

this.subnodes = subnodes || [];
this.lastSubnode = function () {
return this.subnodes[this.subnodes.length - 1];
};
};

heist.ast.Root = function (root, path, subnodes) {
this.type = 'Root';
this.root = root;
this.path = path;
this.subnodes = subnodes || [];
this.lastSubnode = function () {
return this.subnodes[this.subnodes.length - 1];
};
};

heist.ast.Root.prototype.methods = function () {
var methods = [];
for (var i = 0, l = this.subnodes.length; i < l; i++) {
if (this.subnodes[i] instanceof heist.ast.Method)
methods.push(this.subnodes[i]);
}
return methods;
}

heist.ast.Root.prototype.contents = function () {
var contents = [];
for (var i = 0, l = this.subnodes.length; i < l; i++) {
if (! (this.subnodes[i] instanceof heist.ast.Method))
contents.push(this.subnodes[i]);
}
return contents;
}

heist.ast.ContainingCode = function (code) {
this.type = 'ContainingCode';
this.code = code;
};

heist.ast.Content = function (text) {
this.type = 'Content';
this.text = text;
};

heist.ast.CodeLines = function (code) {
this.type = 'CodeLines';
this.code = code;
};

heist.ast.ExpressionTag = function (code, filters) {
this.type = 'ExpressionTag';
this.code = code;
this.filters = filters;
};

heist.ast.CodeTag = function (code) {
this.type = 'CodeTag';
this.code = code;
};

heist.ast.OutputStart = function () {
this.type = 'OutputStart';
};

heist.ast.OutputStop = function () {
this.type = 'OutputStop';
};

heist.ast.IgnoredContent = function (text) {
this.type = 'IgnoredContent';
this.text = text;
};

heist.ast.IgnoredContent.prototype.newlines = function () {
return this.text.replace(/[^\r\n\f]/g, '');
};

if (! heist.generate) heist.generate = {};

heist.generate.addGenerator = function (name, proto) {
var Renderer = function () {
this.content = '';
};
Renderer.prototype = proto;
heist.generate[name] = function (ast) {
var renderer = new Renderer();
renderer.Root(ast);
return renderer.content;
};
};

heist.generate.dispatch = function (nodes, separator) {
for (var i = 0, len = nodes.length; i < len; i++) {
var gen = this[nodes[i].type];
if (! gen) throw 'no generator method for ' + nodes[i].type;
if (separator && i != 0) this.content += separator;
gen.apply(this, [nodes[i]]);
}
};

heist.generate.addGenerator('javascript', {
Root: function (node) {
this.content += 'function(comp){comp.addDefaultMethod(function(){';
heist.generate.dispatch.call(this, node.contents());
this.content += '});';
heist.generate.dispatch.call(this, node.methods());
this.content += '}';
},
ContainingCode: function (node) {
this.content += node.code;
},
Method: function (node) {
this.content += 'comp.addMethod(';
this.content += node.preIdentifierWhitespace;
this.content += heist.quote(node.methodName) + ',function';
this.content += node.preArgumentListWhitespace;
this.content += node.argumentList || '()';
this.content += node.preTagCloseWhitespace;
this.content += '{';
heist.generate.dispatch.apply(this, [node.subnodes]);
this.content += '});';
},
Content: function (node) {
this.content += 'this.append(' + heist.quote(node.text) + ');';
},
IgnoredContent: function (node) {
this.content += node.newlines();
},
CodeLines: function (node) {
this.content += node.code;
},
ExpressionTag: function (node) {
this.content += 'this.append(' + node.code + ');';
},
CodeTag: function (node) {
this.content += node.code + ';';
},
OutputStop: function () {},
OutputStart: function () {}
});

heist.generate.addGenerator('ast', {
Root: function (node) {
this.indent = '';
this.content += 'new heist.ast.Root(' +
heist.quote(node.root) + ',' +
heist.quote(node.path) + ',';
heist.generate.dispatch.apply(this, [node.subnodes, ', ']);
this.content += ')';
},
ContainingCode: function (node) {
this.content += 'new heist.ast.ContainingCode(';
this.content += heist.quote(node.code);
this.content += ')';
},
Method: function (node) {
this.indent += ' ';
this.content += 'new heist.ast.Method(';
this.content += (
[ node.preIdentifierWhitespace,
node.methodName,
node.preArgumentListWhitespace,
node.argumentList,
node.preTagCloseWhitespace
].map(function (str) { return heist.quote(str); }).join(', '));
this.content += ', [';
heist.generate.dispatch.apply(this, [node.subnodes, ', ']);
this.content += '])';
},
Content: function (node) {
this.content += 'new heist.ast.Content(' + heist.quote(node.text) + ')';
},
IgnoredContent: function (node) {
this.content += 'new heist.ast.IgnoredContent(' + heist.quote(node.text) + ')';
},
CodeLines: function (node) {
this.content += 'new heist.ast.CodeLines(' + heist.quote(node.code) + ')';
},
ExpressionTag: function (node) {
this.content += 'new heist.ast.ExpressionTag(' + heist.quote(node.code) + ')';
},
CodeTag: function (node) {
this.content += 'new heist.ast.CodeTag(' + heist.quote(node.code) + ')';
},
OutputStop: function () {
this.content += 'new heist.ast.OutputStop()';
},
OutputStart: function () {
this.content += 'new heist.ast.OutputStart()';
}
});

heist.generate.addGenerator('html', {
Root: function (node) {
this.content += '<pre>';
heist.generate.dispatch.apply(this, [node.subnodes]);
this.content += '</pre>';
},
ContainingCode: function (node) {
this.content += '<span class="containingcode">';
this.content += heist.htmlEscape(node.code);
this.content += '</span>';
},
Method: function (node) {
this.content += '<span class="method-start">&lt;~</span>';
this.content += node.preIdentifierWhitespace;
this.content += '<span class="method-name">' + heist.htmlEscape(node.methodName) + '</span>';
this.content += node.preArgumentListWhitespace;
this.content += '<span class="method-arglist">' + heist.htmlEscape(node.argumentList) + '</span>';
this.content += node.preTagCloseWhitespace;
this.content += '<span class="method-end">&gt;</span>';
heist.generate.dispatch.apply(this, [node.subnodes]);
this.content += '<span class="method-closing">&lt;/~&gt;</span>';
},
Content: function (node) {
this.content += heist.htmlEscape(node.text);
},
IgnoredContent: function (node) {
this.content += '<span class="ignored-content">' + heist.htmlEscape(node.text) + '</span>';
},
CodeLines: function (node) {
this.content += '<span class="codeline-start">:</span><span class="codeline-code">' + heist.htmlEscape(node.code) + '</span>';
},
ExpressionTag: function (node) {
this.content += '<span class="expressiontag-start">&lt;(</span><span class="expressiontag-code">' + heist.htmlEscape(node.code) + '</span><span class="expressiontag-stop">)&gt;</span>';
},
CodeTag: function (node) {
this.content += '<span class="codetag-start">&lt;[</span><span class="codetag-code">' + node.code + '</span><span class="codetag-stop">]&gt;</span>';
},
OutputStop: function () {
this.content += '<span class="output-stop">|-</span>';
},
OutputStart: function () {
this.content += '<span class="output-start">-|</span>';
}
});


heist.Parser = function(){

var Parser = function (opts) {
this.embeddedInCode = opts.embeddedInCode;
this.defaultOutputOn = opts.defaultOutputOn;
};

var i = 0;
var topLevelContext = i++;
var topLevelInCodeContext = i++;
var inMethodContext = i++;
var directiveContext = i++;
var methodContext = i++;
var methodCloseContext = i++;
var codeTagStartContext = i++;
var expressionTagStartContext = i++;
var codeLineContext = i++;
var suppressedLineBreakContext = i++;
var outputStartContext = i++;
var outputStopContext = i++;
var endOfInput = i++;

Parser.prototype = {
comp : function (content) {
return heist.runtime.compFromJs(
heist.generate.javascript(this.parseAst(content))
);
},
parseAst: function (content, path, root) {
var ast = heist.ast;
var position;
var newPosition;
var tokenStart = 0;

var root = new ast.Root(root, path);
var stack = [root];
var res;
var outputOn = this.defaultOutputOn;
var methodNode;

var context = topLevelContext;

if (content.charAt(0) == ':') {
newPosition = 1;
context = codeLineContext;
}

var codeLineFollowingSuppressedLineBreak;

var topLevel = /([\S\s]*?)(?:(\\\n\:?)|(\n\:)|(\<\~)(\=?)|(\<\()|(\<\[)|(\|\-)|(\-\|)|(\<\/\~)|$)/g;
var topLevelInCode = /([\S\s]*?)(?:(\<\~)(\=?)|$)/g;
var inMethod = /([\S\s]*?)(?:(\\\n\:?)|(\n\:)|(\<\()|(\<\[)|(\|\-)|(\-\|)|(\<\~(?!\\=))|(\<\/\~)|$)/g;
var directive = /(\s*)(\w+)(\s+)([^>\s]+?)(\s*)\>/g;
var method = /(\s*)([a-zA-Z$][\w$]*)(\s*)((?:\([^\)]*\))?)(\s*)\>/g;
var methodClose = /(\s*)((?:[a-zA-Z$][\w$]*)?)(\s*)\>/g;
var codeTagStart = /([\S\s]*?)\]\>/g;
var expressionTagStart = /([\S\s]*?)\)((?:\w+(?:\,\w+)*)?)\>/g;
var codeLine = /(.*?(?:\n|$))/g;

var extraNewline;

try {

SUCCESS:
while (true) {
position = newPosition || 0;

if (false) {
var debugMethod = print || alert;
debugMethod("--- parsing from " + position + " with context: " + context + "\n" +
" - stack has " + stack.length + " item(s)\n" +
" - " + stack.join(','));
}

switch (context) {

case topLevelContext:
topLevel.lastIndex = position;
res = topLevel.exec(content);
if (res == null) throw 'could not match template';
newPosition = topLevel.lastIndex;

extraNewline = res[3] && res[3].length == 2 ? "\n" : "";
if (res[1] || extraNewline) {
contentWithNewline = res[1] + extraNewline;
if (outputOn)
stack[stack.length - 1].subnodes.push(new ast.Content(contentWithNewline));
else
stack[stack.length - 1].subnodes.push(new ast.IgnoredContent(contentWithNewline));
}

if (res[2]) {
context = suppressedLineBreakContext;
codeLineFollowingSuppressedLineBreak = (res[2].length == 3);
}
else if (res[3]) {
tokenStart = newPosition - 1;
context = codeLineContext;
}
else if (res[5]) {
tokenStart = newPosition - 3;
context = directiveContext;
}
else if (res[4]) {
tokenStart = newPosition - 2;
context = methodContext;
}
else if (res[6]) {
tokenStart = newPosition - 2;
context = expressionTagStartContext;
}
else if (res[7]) {
tokenStart = newPosition - 2;
context = codeTagStartContext;
}
else if (res[8]) {
tokenStart = newPosition - 2;
context = outputStopContext;
}
else if (res[9]) {
tokenStart = newPosition - 2;
context = outputStartContext;
}
else if (res[10]) {
tokenStart = newPosition - 3;
context = methodCloseContext;
}
else {
context = endOfInput;
}
break;

case topLevelInCodeContext:

topLevelInCode.lastIndex = position;
res = topLevelInCode.exec(content);
if (res == null) throw 'could not match template';
newPosition = topLevelInCode.lastIndex;
if (res[1])
stack[stack.length - 1].subnodes.push(
new ast.ContainingCode(res[1])
);

if (res[3]) {
tokenStart = newPosition - 3;
context = directiveContext;
}
else if (res[2]) {
tokenStart = newPosition - 2;
context = methodContext;
}
else {
context = endOfInput;
}
break;

case inMethodContext:
inMethod.lastIndex = position;
res = inMethod.exec(content);
if (res == null) throw 'could not match template';
newPosition = inMethod.lastIndex;
extraNewline = res[3] && res[3].length == 2 ? "\n" : "";
if (res[1] || extraNewline) {
contentWithNewline = res[1] + extraNewline;
if (outputOn)
stack[stack.length - 1].subnodes.push(new ast.Content(contentWithNewline));
else
stack[stack.length - 1].subnodes.push(new ast.IgnoredContent(contentWithNewline));
}

if (res[2]) {
context = suppressedLineBreakContext;
codeLineFollowingSuppressedLineBreak = (res[2].length == 3);
}
else if (res[3]) {
tokenStart = newPosition - 1;
context = codeLineContext;
}
else if (res[4]) {
tokenStart = newPosition - 2;
context = expressionTagStartContext;
}
else if (res[5]) {
tokenStart = newPosition - 2;
context = codeTagStartContext;
}
else if (res[6]) {
tokenStart = newPosition - 2;
context = outputStopContext;
}
else if (res[7]) {
tokenStart = newPosition - 2;
context = outputStartContext;
}
else if (res[8]) {
tokenStart = newPosition - 2;
context = methodContext;
}
else if (res[9]) {
tokenStart = newPosition - 3;
context = methodCloseContext;
}
else {
context = endOfInput;
}
break;

case directiveContext:

directive.lastIndex = position;
res = directive.exec(content);
if (res == null) throw 'invalid directive';
newPosition = directive.lastIndex;

if (res[2] != 'base')
throw 'unknown directive "' + res[2] + '"';

stack[stack.length - 1].subnodes.push(new ast.BaseDirective({
preDirectiveWhitespace : res[1],
postDirectiveNameWhitespace : res[2],
value : res[3],
postValueWhitespace : res[4]
}));

// TODO - record the base comp somehow

context = topLevelContext;
break;

case suppressedLineBreakContext:

if (outputOn)
stack[stack.length - 1].subnodes.push(new ast.SuppressedLineBreak());
else {
var lastSubnode = stack[stack.length].lastSubnode();
if (lastSubnode && lastSubnode instanceof ast.IgnoredContent)
lastSubnode.setText(lastSubnode.text() + "\\\n");
else
stack[stack.length - 1].subnodes.push(new ast.IgnoredContent('\\\n'));
}
context = codeLineFollowingSuppressedLineBreak ?
codeLineContext : inMethodContext;
codeLineFollowingSuppressedLineBreak = false;
break;

case outputStartContext:

if (outputOn)
throw 'found -| output start sequence where output already on';
outputOn = true;
stack[stack.length - 1].subnodes.push(new ast.OutputStart());
context = inMethodContext;
break;

case outputStopContext:

if (! outputOn)
throw 'found |- output stop sequence where output is already off';
outputOn = false;
stack[stack.length - 1].subnodes.push(new ast.OutputStop());
context = inMethodContext;
break;

case methodContext:

method.lastIndex = position;
res = method.exec(content);
if (res == null) throw 'malformed <~...> method tag';
newPosition = method.lastIndex;

methodNode = new ast.Method(res[1], res[2], res[3], res[4], res[5]);
stack[stack.length - 1].subnodes.push(methodNode);
stack.push(methodNode);

context = inMethodContext;
break;

case methodCloseContext:

methodClose.lastIndex = position;
res = methodClose.exec(content);
if (res == null) throw 'malformed </~...> closing method tag';
newPosition = methodClose.lastIndex;

if (! stack[stack.length - 1] || ! (stack[stack.length - 1] instanceof ast.Method))
throw '</~' + (res[2] || '') + '> without corresponding <~' + (res[2] || '') + '...> tag';
if (res[2] && stack[stack.length - 1].methodName != res[2])
throw 'mismatched closing tag </~' + (res[2] || '') + '> for <~' + (stack[stack.length - 1].methodName || '') + '...> tag';

stack.pop();
if (stack.length == 1) {
outputOn = true;
context = topLevelContext;
}
else
context = inMethodContext;
break;

case codeTagStartContext:

codeTagStart.lastIndex = position;
res = codeTagStart.exec(content);
if (res == null) throw 'cannot find end of code tag \']>\'';
newPosition = codeTagStart.lastIndex;

var lastSubnode = stack[stack.length - 1].lastSubnode();
if (lastSubnode && (lastSubnode instanceof ast.CodeTag))
lastSubnode.code += res[1];
else
stack[stack.length - 1].subnodes.push(new ast.CodeTag(res[1]));
context = inMethodContext;
break;

case expressionTagStartContext:

expressionTagStart.lastIndex = position;
res = expressionTagStart.exec(content);
if (res == null) throw 'cannot find end of expression tag \')...>\'';
newPosition = expressionTagStart.lastIndex;
if (res[1].search(/\S/) == -1)
throw "empty expression tag";
var filters = [];
/* var ftrs = res[2].split(/\,/); */
/* for (var i = 0; i < ftrs.length; i++) { */
/* if (this.filters[ftrs[i]]) { */
/* filters[filters.length] = new ast.UserFilter({ */
/* identifier : ftrs[i], */
/* method : this.filters(ftrs[i]) */
/* }); */
/* } */
/* else if (builtinFilters[ftrs[i]]) { */
/* filters[filters.length] = new builtinFilters[ftrs[i]]({ */
/* identifier : ftrs[i] */
/* }); */
/* } */
/* else */
/* throw 'unknown filter \'' + ftrs[i] + '\' in expression tag'; */
/* } */
stack[stack.length - 1].subnodes.push(new ast.ExpressionTag(res[1], filters));
context = inMethodContext;
break;

case codeLineContext:

codeLine.lastIndex = position;

res = codeLine.exec(content);
if (res == null) throw 'cannot parse code line';
newPosition = codeLine.lastIndex;

var lastSubnode = stack[stack.length - 1].lastSubnode();

if (lastSubnode && (lastSubnode instanceof ast.CodeLines))
lastSubnode.code += res[1];
else
stack[stack.length - 1].subnodes.push(new ast.CodeLines(res[1]));

if (content.charAt(newPosition) == ':') {
newPosition++;
break;
}
// why in method context here?
context = inMethodContext;
break;

case endOfInput:
if (! stack.shift())
throw 'no root on container stack';
if (stack.length > 0) {
throw stack.reverse().map(function (node) {
if (! (node instanceof ast.Method))
throw 'unknown node type: ' + node;

return '<~' + (node.methodName || '') + '...> tag without corresponding </~> closing tag';
}).join(', inside ');
}
break SUCCESS;

default:
throw 'unknown parse context: \'' + context + '\'';


}
}
}
catch (e) {
var lineRegex = /[^\n\r\f]*?(?:\015\012|\012|\015)/g;
var beginningOfErrorLine = 0;
lineRegex.lastIndex = 0;
var line = 1;
while (lineRegex.exec(content)) {
if (lineRegex.lastIndex <= tokenStart) {
line++;
beginningOfErrorLine = lineRegex.lastIndex;
}
else {
break;
}
}
//line += this.lineOffset;
var character = tokenStart - beginningOfErrorLine;
character++; // make it 1 based
throw e + " at line " + line + ", character " + character;
}
return root;
}
};
return Parser;
}();
}
});

Show details Hide details

Change log

r8 by thomasyandell on May 21, 2008   Diff
Changes for testjs changes. Minor fixes.
Starting to flesh out client-side loading.
Go to: 
Project members, sign in to write a code review

Older revisions

r7 by thomasyandell on Jan 31, 2008   Diff
Improved javascript engine.
r6 by thomasyandell on Jan 27, 2008   Diff
Build script. Docs.
r4 by thomasyandell on Nov 13, 2007   Diff
Parser with tests.
All revisions of this file

File info

Size: 32670 bytes, 708 lines
Hosted by Google Code