Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80728 views
1
"use strict";
2
var Visitor = require("./visitor")["default"];
3
4
function print(ast) {
5
return new PrintVisitor().accept(ast);
6
}
7
8
exports.print = print;function PrintVisitor() {
9
this.padding = 0;
10
}
11
12
exports.PrintVisitor = PrintVisitor;PrintVisitor.prototype = new Visitor();
13
14
PrintVisitor.prototype.pad = function(string) {
15
var out = "";
16
17
for(var i=0,l=this.padding; i<l; i++) {
18
out = out + " ";
19
}
20
21
out = out + string + "\n";
22
return out;
23
};
24
25
PrintVisitor.prototype.Program = function(program) {
26
var out = '',
27
body = program.body,
28
i, l;
29
30
if (program.blockParams) {
31
var blockParams = 'BLOCK PARAMS: [';
32
for(i=0, l=program.blockParams.length; i<l; i++) {
33
blockParams += ' ' + program.blockParams[i];
34
}
35
blockParams += ' ]';
36
out += this.pad(blockParams);
37
}
38
39
for(i=0, l=body.length; i<l; i++) {
40
out = out + this.accept(body[i]);
41
}
42
43
this.padding--;
44
45
return out;
46
};
47
48
PrintVisitor.prototype.MustacheStatement = function(mustache) {
49
return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
50
};
51
52
PrintVisitor.prototype.BlockStatement = function(block) {
53
var out = "";
54
55
out = out + this.pad('BLOCK:');
56
this.padding++;
57
out = out + this.pad(this.SubExpression(block));
58
if (block.program) {
59
out = out + this.pad('PROGRAM:');
60
this.padding++;
61
out = out + this.accept(block.program);
62
this.padding--;
63
}
64
if (block.inverse) {
65
if (block.program) { this.padding++; }
66
out = out + this.pad('{{^}}');
67
this.padding++;
68
out = out + this.accept(block.inverse);
69
this.padding--;
70
if (block.program) { this.padding--; }
71
}
72
this.padding--;
73
74
return out;
75
};
76
77
PrintVisitor.prototype.PartialStatement = function(partial) {
78
var content = 'PARTIAL:' + partial.name.original;
79
if(partial.params[0]) {
80
content += ' ' + this.accept(partial.params[0]);
81
}
82
if (partial.hash) {
83
content += ' ' + this.accept(partial.hash);
84
}
85
return this.pad('{{> ' + content + ' }}');
86
};
87
88
PrintVisitor.prototype.ContentStatement = function(content) {
89
return this.pad("CONTENT[ '" + content.value + "' ]");
90
};
91
92
PrintVisitor.prototype.CommentStatement = function(comment) {
93
return this.pad("{{! '" + comment.value + "' }}");
94
};
95
96
PrintVisitor.prototype.SubExpression = function(sexpr) {
97
var params = sexpr.params, paramStrings = [], hash;
98
99
for(var i=0, l=params.length; i<l; i++) {
100
paramStrings.push(this.accept(params[i]));
101
}
102
103
params = "[" + paramStrings.join(", ") + "]";
104
105
hash = sexpr.hash ? " " + this.accept(sexpr.hash) : "";
106
107
return this.accept(sexpr.path) + " " + params + hash;
108
};
109
110
PrintVisitor.prototype.PathExpression = function(id) {
111
var path = id.parts.join('/');
112
return (id.data ? '@' : '') + 'PATH:' + path;
113
};
114
115
116
PrintVisitor.prototype.StringLiteral = function(string) {
117
return '"' + string.value + '"';
118
};
119
120
PrintVisitor.prototype.NumberLiteral = function(number) {
121
return "NUMBER{" + number.value + "}";
122
};
123
124
PrintVisitor.prototype.BooleanLiteral = function(bool) {
125
return "BOOLEAN{" + bool.value + "}";
126
};
127
128
PrintVisitor.prototype.Hash = function(hash) {
129
var pairs = hash.pairs;
130
var joinedPairs = [];
131
132
for (var i=0, l=pairs.length; i<l; i++) {
133
joinedPairs.push(this.accept(pairs[i]));
134
}
135
136
return 'HASH{' + joinedPairs.join(', ') + '}';
137
};
138
PrintVisitor.prototype.HashPair = function(pair) {
139
return pair.key + '=' + this.accept(pair.value);
140
};
141