-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
compiler.js
272 lines (241 loc) · 6.01 KB
/
compiler.js
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
'use strict';
var use = require('use');
var util = require('snapdragon-util');
var Emitter = require('component-emitter');
var define = require('define-property');
var extend = require('extend-shallow');
var error = require('./error');
/**
* Create a new `Compiler` with the given `options`.
*
* ```js
* var Snapdragon = require('snapdragon');
* var Compiler = Snapdragon.Compiler;
* var compiler = new Compiler();
* ```
* @param {Object} `options`
* @param {Object} `state` Optionally pass a "state" object to use inside visitor functions.
* @api public
*/
function Compiler(options, state) {
this.options = extend({source: 'string'}, options);
this.emitter = new Emitter();
this.on = this.emitter.on.bind(this.emitter);
this.isCompiler = true;
this.state = state || {};
this.state.inside = this.state.inside || {};
this.compilers = {};
this.output = '';
this.indent = '';
this.set('eos', function(node) {
return this.emit(node.val, node);
});
this.set('bos', function(node) {
return this.emit(node.val, node);
});
use(this);
}
/**
* Prototype methods
*/
Compiler.prototype = {
/**
* Throw a formatted error message with details including the cursor position.
*
* ```js
* compiler.set('foo', function(node) {
* if (node.val !== 'foo') {
* throw this.error('expected node.val to be "foo"', node);
* }
* });
* ```
* @name .error
* @param {String} `msg` Message to use in the Error.
* @param {Object} `node`
* @return {undefined}
* @api public
*/
error: function(/*msg, node*/) {
return error.apply(this, arguments);
},
/**
* Concat the given string to `compiler.output`.
*
* ```js
* compiler.set('foo', function(node) {
* this.emit(node.val, node);
* });
* ```
* @name .emit
* @param {String} `string`
* @param {Object} `node` Optionally pass the node to use for position if source maps are enabled.
* @return {String} returns the string
* @api public
*/
emit: function(val, node) {
this.output += val;
return val;
},
/**
* Emit an empty string to effectively "skip" the string for the given `node`,
* but still emit the position and node type.
*
* ```js
* // example: do nothing for beginning-of-string
* snapdragon.compiler.set('bos', compiler.noop);
* ```
* @name .noop
* @param {Object} node
* @api public
*/
noop: function(node) {
this.emit('', node);
},
/**
* Define a non-enumberable property on the `Compiler` instance. This is useful
* in plugins, for exposing methods inside handlers.
*
* ```js
* compiler.define('customMethod', function() {
* // do stuff
* });
* ```
* @name .define
* @param {String} `key` propery name
* @param {any} `val` property value
* @return {Object} Returns the Compiler instance for chaining.
* @api public
*/
define: function(key, val) {
define(this, key, val);
return this;
},
/**
* Add a compiler `fn` for the given `type`. Compilers are called
* when the `.compile` method encounters a node of the given type to
* generate the output string.
*
* ```js
* compiler
* .set('comma', function(node) {
* this.emit(',');
* })
* .set('dot', function(node) {
* this.emit('.');
* })
* .set('slash', function(node) {
* this.emit('/');
* });
* ```
* @name .set
* @param {String} `type`
* @param {Function} `fn`
* @api public
*/
set: function(type, fn) {
this.compilers[type] = fn;
return this;
},
/**
* Get the compiler of the given `type`.
*
* ```js
* var fn = compiler.get('slash');
* ```
* @name .get
* @param {String} `type`
* @api public
*/
get: function(type) {
return this.compilers[type];
},
/**
* Visit `node` using the registered compiler function associated with the
* `node.type`.
*
* ```js
* compiler
* .set('i', function(node) {
* this.visit(node);
* })
* ```
* @name .visit
* @param {Object} `node`
* @return {Object} returns the node
* @api public
*/
visit: function(node) {
if (util.isOpen(node)) {
util.addType(this.state, node);
}
this.emitter.emit('node', node);
var fn = this.compilers[node.type] || this.compilers.unknown;
if (typeof fn !== 'function') {
throw this.error('compiler "' + node.type + '" is not registered', node);
}
var val = fn.call(this, node) || node;
if (util.isNode(val)) {
node = val;
}
if (util.isClose(node)) {
util.removeType(this.state, node);
}
return node;
},
/**
* Iterate over `node.nodes`, calling [visit](#visit) on each node.
*
* ```js
* compiler
* .set('i', function(node) {
* utils.mapVisit(node);
* })
* ```
* @name .mapVisit
* @param {Object} `node`
* @return {Object} returns the node
* @api public
*/
mapVisit: function(parent) {
var nodes = parent.nodes || parent.children;
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (!node.parent) node.parent = parent;
nodes[i] = this.visit(node) || node;
}
},
/**
* Compile the given `AST` and return a string. Iterates over `ast.nodes`
* with [mapVisit](#mapVisit).
*
* ```js
* var ast = parser.parse('foo');
* var str = compiler.compile(ast);
* ```
* @name .compile
* @param {Object} `ast`
* @param {Object} `options` Compiler options
* @return {Object} returns the node
* @api public
*/
compile: function(ast, options) {
var opts = extend({}, this.options, options);
this.ast = ast;
this.output = '';
// source map support
if (opts.sourcemap) {
var sourcemaps = require('./source-maps');
sourcemaps(this);
this.mapVisit(this.ast);
this.applySourceMaps();
this.map = opts.sourcemap === 'generator' ? this.map : this.map.toJSON();
} else {
this.mapVisit(this.ast);
}
return this;
}
};
/**
* Expose `Compiler`
*/
module.exports = Compiler;