forked from janus/JSDev
/
jsdev.js
494 lines (436 loc) · 13.9 KB
/
jsdev.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
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
// jsdev.js
// Douglas Crockford
// 2012-01-08
//
// Public Domain
//
// JSDev is a simple JavaScript preprocessor. It implements a tiny macro
// language that is written in the form of tagged comments. These comments are
// normally ignored, and will be removed by JSMin. But JSDev will activate
// these comments, replacing them with executable forms that can be used to do
// debugging, testing, logging, or tracing. JSDev scans a source looking for
// and replacing patterns. A pattern is a slashstar comment containing a
// tag and some stuff, and optionally a condition wrapped in parens.
// There must be no space between the slashstar and the <tag>.
//
// /*<tag> <stuff>*/
// /*<tag>(<condition>) <stuff>*/
//
// The command line will contain a list of <tag> names, each of which can
// optionally be followed by a colon and <method> name. There must not be
// any spaces around the colon.
//
// A <tag> may contain any short sequence of ASCII letters, digits,
// underbar (_), dollar ($), and period(.). The active <tag> strings are
// declared in the method line. All <tag>s that are not declared in the
// command line are ignored.
//
// The <condition> and <stuff> may not include a string or regexp containing
// starslash, or a comment.
//
// If a <tag> does not have a :<method>, then it will expand into
//
// {<stuff>}
//
// Effectively, the outer part of the comment is replaced with braces, turning
// an inert comment into an executable block. If a <condition> was included,
// it will expand into
//
// if (<condition>) {<stuff>}
//
// Note that there can be no space between the <tag> and the paren that
// encloses the <condition>. If there is a space, then everything is <stuff>.
//
// If <tag> was declared with :<method>, then it will expand into
//
// {<method>(<stuff>);}
//
// A function call is constructed, invoking the <method>, and using the
// <stuff> as the arguments. If a condition was included, it will expand into
//
// if (<condition>) {<method>(<stuff>);}
//
// The JSDEV function takes a program text (either a string or an array of
// string), an array of tag strings, and optionally, an array of comment
// strings.
//
// Sample invocation:
//
// output = JSDEV(source, [
// "debug", "log:console.log", "alarm:alert"
// ], "Devel Edition");
//
// That will enable
//
// /*debug <stuff>*/
//
// comments that expand into
//
// {<stuff>;}
//
// as well as
//
// /*log <stuff>*/
//
// comments that expand into
//
// {console.log(<stuff>);}
//
// and
//
// /*alarm(<condition>) <stuff>*/
//
// comments that expand into
//
// if (<condition>) {alert(<stuff>);}
//
// It will also insert the comment
//
// // Devel Edition
//
// at the top of the output file.
module.exports = function JSDEV(source, tags, comments) {
'use strict';
var line,
line_nr = -1,
lines,
methods,
outputs = [],
preview,
tagx = /^([0-9A-Za-z_$.]+)(?::([0-9A-Za-z_$.]+))?$/;
function error(message) {
throw new Error("JSDev: " + ((line_nr + 1) || "bad tag") + ' ' + message);
}
function is_alphanum(c) {
// Return true if the character is a letter, digit, underscore,
// dollar sign, or period.
return ((c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
c === '_' || c === '$' || c === '.');
}
function emit(string) {
if (string) {
outputs.push(string);
}
return string;
}
function get(echo) {
// Return the next character from the input. If the echo argument is true,
// then the character will also be emitted.
var c;
if (preview) {
c = preview;
preview = null;
} else {
if (!line) {
if (typeof line === 'string') {
c = '\n';
line = null;
} else {
line_nr += 1;
line = lines[line_nr];
if (!line) {
if (typeof line === 'string') {
c = '\n';
line = null;
} else {
c = null;
}
} else {
c = line.charAt(0);
line = line.slice(1);
}
}
} else {
c = line.charAt(0);
line = line.slice(1);
}
}
if (echo) {
emit(c);
}
return c;
}
function peek() {
if (!preview) {
preview = get(false);
}
return preview;
}
function unget(c) {
preview = c;
}
function string(quote, in_comment) {
var c, was = line_nr;
for (;;) {
c = get(true);
if (c === quote) {
return;
}
if (c === '\\') {
c = get(true);
}
if (in_comment && c === '*' && peek() === '/') {
error("unexpected close comment in string.");
}
if (c === null) {
line_nr = was;
error("unterminated string literal.");
}
}
}
function pre_regexp(left) {
return (left === '(' || left === ',' || left === '=' ||
left === ':' || left === '[' || left === '!' ||
left === '&' || left === '|' || left === '?' ||
left === '{' || left === '}' || left === ';');
}
function regexp(in_comment) {
var c, was = line_nr;
for (;;) {
c = get(true);
if (c === '[') {
for (;;) {
c = get(true);
if (c === ']') {
break;
}
if (c === '\\') {
c = get(true);
}
if (in_comment && c === '*' && peek() === '/') {
error("unexpected close comment in regexp.");
}
if (c === null) {
error("unterminated set in Regular Expression literal.");
}
}
} else if (c === '/') {
if (in_comment && (peek() === '/' || peek() === '*')) {
error("unexpected comment.");
}
return;
} else if (c === '\\') {
c = get(true);
}
if (in_comment && c === '*' && peek() === '/') {
error("unexpected comment.");
}
if (c === null) {
line_nr = was;
error("unterminated regexp literal.");
}
}
}
function condition() {
var c, left = '{', paren = 0;
for (;;) {
c = get(true);
if (c === '(' || c === '{' || c === '[') {
paren += 1;
} else if (c === ')' || c === '}' || c === ']') {
paren -= 1;
if (paren === 0) {
return;
}
} else if (c === null) {
error("Unterminated condition.");
} else if (c === '\'' || c === '"' || c === '`') {
string(c, true);
} else if (c === '/') {
if (peek() === '/' || peek() === '*') {
error("unexpected comment.");
}
if (pre_regexp(left)) {
regexp(true);
}
} else if (c === '*' && peek() === '/') {
error("unclosed condition.");
}
if (c > ' ') {
left = c;
}
}
}
function stuff() {
var c, left = '{', paren = 0;
while (peek() === ' ') {
get(false);
}
for (;;) {
while (peek() === '*') {
get(false);
if (peek() === '/') {
get(false);
if (paren > 0) {
error("Unbalanced stuff");
}
return;
}
emit('*');
}
c = get(true);
if (c === null) {
error("Unterminated stuff.");
} else if (c === '\'' || c === '"' || c === '`') {
string(c, true);
} else if (c === '(' || c === '{' || c === '[') {
paren += 1;
} else if (c === ')' || c === '}' || c === ']') {
paren -= 1;
if (paren < 0) {
error("Unbalanced stuff");
}
} else if (c === '/') {
if (peek() === '/' || peek() === '*') {
error("unexpected comment.");
}
if (pre_regexp(left)) {
regexp(true);
}
}
if (c > ' ') {
left = c;
}
}
}
function expand(tag_nr) {
var c;
c = peek();
if (c === '(') {
emit("if ");
condition();
emit(' ');
}
emit('{');
if (methods[tag_nr]) {
emit(methods[tag_nr] + '(');
stuff();
emit(");}");
} else {
stuff();
emit('}');
}
}
function process() {
// Loop through the program text, looking for patterns.
var c = get(false), i, left = 0, tag;
for (;;) {
if (c === null) {
break;
} else if (c === '\'' || c === '"' || c === '`') {
emit(c);
string(c, false);
c = 0;
// The most complicated case is the slash. It can mean division or a regexp
// literal or a line comment or a block comment. A block comment can also be
// a pattern to be expanded.
} else if (c === '/') {
// A slash slash comment skips to the end of the file.
if (peek() === '/') {
emit('/');
for (;;) {
c = get(true);
if (c === '\n' || c === '\r' || c === null) {
break;
}
}
c = get(false);
// The first component of a slash star comment might be the tag.
} else {
if (peek() === '*') {
get(false);
tag = '';
for (;;) {
c = get(false);
if (!is_alphanum(c)) {
break;
}
tag += c;
}
unget(c);
// Did the tag matches something?
i = !tag ? -1 : tags.indexOf(tag);
if (i >= 0) {
expand(i);
c = get(false);
} else {
// If the tag didn't match, then echo the comment.
emit("/*");
emit(tag);
for (;;) {
if (c === null) {
error("unterminated comment.");
}
if (c === '*') {
c = get(true);
if (c === '/') {
break;
}
} else {
c = get(true);
}
}
c = get(false);
}
} else {
emit('/');
// We are looking at a single slash. Is it a division operator, or is it the
// start of a regexp literal? If is not possible to tell for sure without doing
// a complete parse of the program, and we are not going to do that. Instead,
// we are adopting the convention that a regexp literal must have one of a
// small set of characters to its left.
if (pre_regexp(left)) {
regexp(false);
}
left = '/';
c = get(false);
}
}
} else {
// The character was nothing special, to just echo it.
// If it wasn't whitespace, remember it as the character to the left of the
// next character.
emit(c);
if (c > ' ') {
left = c;
}
c = get(false);
}
}
}
// Begin. If there is a comments argument, then make each string into a
// comment at the top of the output.
if (typeof comments === 'string') {
emit("// " + comments + "\n");
} else if (Array.isArray(comments)) {
comments.forEach(function (value) {
emit("// " + value + "\n");
});
}
// Inspect the tags for well formedness, and parse out the methods.
if (Array.isArray(tags)) {
methods = tags.map(function (value, i) {
var result = tagx.exec(value);
if (!result) {
error(value);
}
if (result[2]) {
tags[i] = result[1];
}
return result[2];
});
} else {
error("no tags");
}
// If the source is a string, bust it into lines.
if (typeof source === 'string') {
lines = source.split(/\n|\r\n?/);
} else {
lines = source;
}
// Process the stuff, and return the output as a string.
process();
return outputs.join('');
}