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