grat 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,522 @@
1
+ /* 20090220, as
2
+
3
+ the javascript functions are from:
4
+
5
+ JS Beautifier
6
+ ---------------
7
+
8
+
9
+ Written by Einar Lielmanis, <einars@gmail.com>
10
+ http://jsbeautifier.org/
11
+
12
+ Originally converted to javascript by Vital, <vital76@gmail.com>
13
+
14
+ You are free to use this in any way you want, in case you find this useful or working for you.
15
+
16
+ Usage:
17
+ js_beautify(js_source_text);
18
+ js_beautify(js_source_text, options);
19
+
20
+ The options are:
21
+ indent_size (default 4) — indentation size,
22
+ indent_char (default space) — character to indent with,
23
+ preserve_newlines (default true) — whether existing line breaks should be preserved,
24
+ indent_level (default 0) — initial indentation level, you probably won't need this ever,
25
+
26
+ e.g
27
+
28
+ js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
29
+
30
+
31
+ */
32
+
33
+
34
+ var sourceWindow, sourceText, beautifiedText, SelectionFlag
35
+ BBEdit = MacOS.appBySignature("R*ch");
36
+ BBEdit._strict = false;
37
+
38
+ try {
39
+ sourceWindow = BBEdit.window[1];
40
+ sourceText = sourceWindow.selection.contents;
41
+ SelectionFlag = true;
42
+ if (sourceText == "") {
43
+ sourceText = sourceWindow.text;
44
+ SelectionFlag = false;
45
+ }
46
+ if (sourceText != "") {
47
+ beautifiedText = js_beautify(sourceText, {indent_size: 4, indent_char: ' ', indent_level: 0});
48
+ if (SelectionFlag) {
49
+ sourceWindow.selection.contents = beautifiedText;
50
+ } else {
51
+ sourceWindow.text = beautifiedText;
52
+ }
53
+ }
54
+ } catch(what) {
55
+ Core.message(what.toString())
56
+ }
57
+
58
+ function js_beautify(js_source_text, options) {
59
+ var input, output, token_text, last_type, last_text, last_word, current_mode, modes, indent_string;
60
+ var whitespace, wordchar, punct, parser_pos, line_starters, in_case;
61
+ var prefix, token_type, do_block_just_closed, var_line, var_line_tainted, if_line_flag;
62
+ var indent_level;
63
+ var options = options || {};
64
+ var opt_indent_size = options['indent_size'] || 4;
65
+ var opt_indent_char = options['indent_char'] || ' ';
66
+ var opt_preserve_newlines = typeof options['preserve_newlines'] === 'undefined' ? true: options['preserve_newlines'];
67
+ var opt_indent_level = options['indent_level'] || 0; // starting indentation
68
+ function trim_output() {
69
+ while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
70
+ output.pop();
71
+ }
72
+ }
73
+ function print_newline(ignore_repeated) {
74
+ ignore_repeated = typeof ignore_repeated === 'undefined' ? true: ignore_repeated;
75
+ if_line_flag = false;
76
+ trim_output();
77
+ if (!output.length) {
78
+ return; // no newline on start of file
79
+ }
80
+ if (output[output.length - 1] !== "\n" || !ignore_repeated) {
81
+ output.push("\n");
82
+ }
83
+ for (var i = 0; i < indent_level; i++) {
84
+ output.push(indent_string);
85
+ }
86
+ }
87
+ function print_space() {
88
+ var last_output = output.length ? output[output.length - 1] : ' ';
89
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
90
+ output.push(' ');
91
+ }
92
+ }
93
+ function print_token() {
94
+ output.push(token_text);
95
+ }
96
+ function indent() {
97
+ indent_level++;
98
+ }
99
+ function unindent() {
100
+ if (indent_level) {
101
+ indent_level--;
102
+ }
103
+ }
104
+ function remove_indent() {
105
+ if (output.length && output[output.length - 1] === indent_string) {
106
+ output.pop();
107
+ }
108
+ }
109
+ function set_mode(mode) {
110
+ modes.push(current_mode);
111
+ current_mode = mode;
112
+ }
113
+ function restore_mode() {
114
+ do_block_just_closed = current_mode === 'DO_BLOCK';
115
+ current_mode = modes.pop();
116
+ }
117
+ function in_array(what, arr) {
118
+ for (var i = 0; i < arr.length; i++) {
119
+ if (arr[i] === what) {
120
+ return true;
121
+ }
122
+ }
123
+ return false;
124
+ }
125
+ function get_next_token() {
126
+ var n_newlines = 0;
127
+ var c = '';
128
+ do {
129
+ if (parser_pos >= input.length) {
130
+ return ['', 'TK_EOF'];
131
+ }
132
+ c = input.charAt(parser_pos);
133
+ parser_pos += 1;
134
+ if (c === "\n") {
135
+ n_newlines += 1;
136
+ }
137
+ } while ( in_array ( c , whitespace ));
138
+ var wanted_newline = false;
139
+ if (opt_preserve_newlines) {
140
+ if (n_newlines > 1) {
141
+ for (var i = 0; i < 2; i++) {
142
+ print_newline(i === 0);
143
+ }
144
+ }
145
+ wanted_newline = (n_newlines === 1);
146
+ }
147
+ if (in_array(c, wordchar)) {
148
+ if (parser_pos < input.length) {
149
+ while (in_array(input.charAt(parser_pos), wordchar)) {
150
+ c += input.charAt(parser_pos);
151
+ parser_pos += 1;
152
+ if (parser_pos === input.length) {
153
+ break;
154
+ }
155
+ }
156
+ } // small and surprisingly unugly hack for 1E-10 representation
157
+ if (parser_pos !== input.length && c.match(/^[0-9]+[Ee]$/) && input.charAt(parser_pos) === '-') {
158
+ parser_pos += 1;
159
+ var t = get_next_token(parser_pos);
160
+ c += '-' + t[0];
161
+ return [c, 'TK_WORD'];
162
+ }
163
+ if (c === 'in') { // hack for 'in' operator
164
+ return [c, 'TK_OPERATOR'];
165
+ }
166
+ if (wanted_newline && last_type !== 'TK_OPERATOR' && !if_line_flag) {
167
+ print_newline();
168
+ }
169
+ return [c, 'TK_WORD'];
170
+ }
171
+ if (c === '(' || c === '[') {
172
+ return [c, 'TK_START_EXPR'];
173
+ }
174
+ if (c === ')' || c === ']') {
175
+ return [c, 'TK_END_EXPR'];
176
+ }
177
+ if (c === '{') {
178
+ return [c, 'TK_START_BLOCK'];
179
+ }
180
+ if (c === '}') {
181
+ return [c, 'TK_END_BLOCK'];
182
+ }
183
+ if (c === ';') {
184
+ return [c, 'TK_SEMICOLON'];
185
+ }
186
+ if (c === '/') {
187
+ var comment = ''; // peek for comment /* ... */
188
+ if (input.charAt(parser_pos) === '*') {
189
+ parser_pos += 1;
190
+ if (parser_pos < input.length) {
191
+ while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input.length) {
192
+ comment += input.charAt(parser_pos);
193
+ parser_pos += 1;
194
+ if (parser_pos >= input.length) {
195
+ break;
196
+ }
197
+ }
198
+ }
199
+ parser_pos += 2;
200
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
201
+ } // peek for comment // ...
202
+ if (input.charAt(parser_pos) === '/') {
203
+ comment = c;
204
+ while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") {
205
+ comment += input.charAt(parser_pos);
206
+ parser_pos += 1;
207
+ if (parser_pos >= input.length) {
208
+ break;
209
+ }
210
+ }
211
+ parser_pos += 1;
212
+ if (wanted_newline) {
213
+ print_newline();
214
+ }
215
+ return [comment, 'TK_COMMENT'];
216
+ }
217
+ }
218
+ if (c === "'" || // string
219
+ c === '"' || // string
220
+ (c === '/' && ((last_type === 'TK_WORD' && last_text === 'return') || (last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
221
+ var sep = c;
222
+ var esc = false;
223
+ var resulting_string = '';
224
+ if (parser_pos < input.length) {
225
+ while (esc || input.charAt(parser_pos) !== sep) {
226
+ resulting_string += input.charAt(parser_pos);
227
+ if (!esc) {
228
+ esc = input.charAt(parser_pos) === '\\';
229
+ } else {
230
+ esc = false;
231
+ }
232
+ parser_pos += 1;
233
+ if (parser_pos >= input.length) {
234
+ break;
235
+ }
236
+ }
237
+ }
238
+ parser_pos += 1;
239
+ resulting_string = sep + resulting_string + sep;
240
+ if (sep == '/') { // regexps may have modifiers /regexp/MOD , so fetch those, too
241
+ while (parser_pos < input.length && in_array(input.charAt(parser_pos), wordchar)) {
242
+ resulting_string += input.charAt(parser_pos);
243
+ parser_pos += 1;
244
+ }
245
+ }
246
+ return [resulting_string, 'TK_STRING'];
247
+ }
248
+ if (in_array(c, punct)) {
249
+ while (parser_pos < input.length && in_array(c + input.charAt(parser_pos), punct)) {
250
+ c += input.charAt(parser_pos);
251
+ parser_pos += 1;
252
+ if (parser_pos >= input.length) {
253
+ break;
254
+ }
255
+ }
256
+ return [c, 'TK_OPERATOR'];
257
+ }
258
+ return [c, 'TK_UNKNOWN'];
259
+ } //----------------------------------
260
+ indent_string = '';
261
+ while (opt_indent_size--) {
262
+ indent_string += opt_indent_char;
263
+ }
264
+ indent_level = opt_indent_level;
265
+ input = js_source_text;
266
+ last_word = ''; // last 'TK_WORD' passed
267
+ last_type = 'TK_START_EXPR'; // last token type
268
+ last_text = ''; // last token text
269
+ output = [];
270
+ do_block_just_closed = false;
271
+ var_line = false; // currently drawing var .... ;
272
+ var_line_tainted = false; // false: var a = 5; true: var a = 5, b = 6
273
+ whitespace = "\n\r\t ".split('');
274
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
275
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' '); // words which should always start on new line.
276
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(','); // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
277
+ // some formatting depends on that.
278
+ current_mode = 'BLOCK';
279
+ modes = [current_mode];
280
+ parser_pos = 0;
281
+ in_case = false; // flag for parser that case/default has been processed, and next colon needs special attention
282
+ while (true) {
283
+ var t = get_next_token(parser_pos);
284
+ token_text = t[0];
285
+ token_type = t[1];
286
+ if (token_type === 'TK_EOF') {
287
+ break;
288
+ }
289
+ switch (token_type) {
290
+ case 'TK_START_EXPR':
291
+ var_line = false;
292
+ set_mode('EXPRESSION');
293
+ if (last_text === ';') {
294
+ print_newline();
295
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR') { // do nothing on (( and )( and ][ and ]( ..
296
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
297
+ print_space();
298
+ } else if (in_array(last_word, line_starters) && last_word !== 'function') {
299
+ print_space();
300
+ }
301
+ print_token();
302
+ break;
303
+ case 'TK_END_EXPR':
304
+ print_token();
305
+ restore_mode();
306
+ break;
307
+ case 'TK_START_BLOCK':
308
+ if (last_word === 'do') {
309
+ set_mode('DO_BLOCK');
310
+ } else {
311
+ set_mode('BLOCK');
312
+ }
313
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
314
+ if (last_type === 'TK_START_BLOCK') {
315
+ print_newline();
316
+ } else {
317
+ print_space();
318
+ }
319
+ }
320
+ print_token();
321
+ indent();
322
+ break;
323
+ case 'TK_END_BLOCK':
324
+ if (last_type === 'TK_START_BLOCK') { // nothing
325
+ trim_output();
326
+ unindent();
327
+ } else {
328
+ unindent();
329
+ print_newline();
330
+ }
331
+ print_token();
332
+ restore_mode();
333
+ break;
334
+ case 'TK_WORD':
335
+ if (do_block_just_closed) {
336
+ print_space();
337
+ print_token();
338
+ print_space();
339
+ break;
340
+ }
341
+ if (token_text === 'case' || token_text === 'default') {
342
+ if (last_text === ':') { // switch cases following one another
343
+ remove_indent();
344
+ } else { // case statement starts in the same line where switch
345
+ unindent();
346
+ print_newline();
347
+ indent();
348
+ }
349
+ print_token();
350
+ in_case = true;
351
+ break;
352
+ }
353
+ prefix = 'NONE';
354
+ if (last_type === 'TK_END_BLOCK') {
355
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
356
+ prefix = 'NEWLINE';
357
+ } else {
358
+ prefix = 'SPACE';
359
+ print_space();
360
+ }
361
+ } else if (last_type === 'TK_SEMICOLON' && (current_mode === 'BLOCK' || current_mode === 'DO_BLOCK')) {
362
+ prefix = 'NEWLINE';
363
+ } else if (last_type === 'TK_SEMICOLON' && current_mode === 'EXPRESSION') {
364
+ prefix = 'SPACE';
365
+ } else if (last_type === 'TK_STRING') {
366
+ prefix = 'NEWLINE';
367
+ } else if (last_type === 'TK_WORD') {
368
+ prefix = 'SPACE';
369
+ } else if (last_type === 'TK_START_BLOCK') {
370
+ prefix = 'NEWLINE';
371
+ } else if (last_type === 'TK_END_EXPR') {
372
+ print_space();
373
+ prefix = 'NEWLINE';
374
+ }
375
+ if (last_type !== 'TK_END_BLOCK' && in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
376
+ print_newline();
377
+ } else if (in_array(token_text, line_starters) || prefix === 'NEWLINE') {
378
+ if (last_text === 'else') { // no need to force newline on else break
379
+ print_space();
380
+ } else if ((last_type === 'TK_START_EXPR' || last_text === '=') && token_text === 'function') { // no need to force newline on 'function': (function
381
+ // DONOTHING
382
+ } else if (last_type === 'TK_WORD' && (last_text === 'return' || last_text === 'throw')) { // no newline between 'return nnn'
383
+ print_space();
384
+ } else if (last_type !== 'TK_END_EXPR') {
385
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') { // no need to force newline on 'var': for (var x = 0...)
386
+ if (token_text === 'if' && last_type === 'TK_WORD' && last_word === 'else') { // no newline for } else if {
387
+ print_space();
388
+ } else {
389
+ print_newline();
390
+ }
391
+ }
392
+ } else {
393
+ if (in_array(token_text, line_starters) && last_text !== ')') {
394
+ print_newline();
395
+ }
396
+ }
397
+ } else if (prefix === 'SPACE') {
398
+ print_space();
399
+ }
400
+ print_token();
401
+ last_word = token_text;
402
+ if (token_text === 'var') {
403
+ var_line = true;
404
+ var_line_tainted = false;
405
+ }
406
+ if (token_text === 'if' || token_text === 'else') {
407
+ if_line_flag = true;
408
+ }
409
+ break;
410
+ case 'TK_SEMICOLON':
411
+ print_token();
412
+ var_line = false;
413
+ break;
414
+ case 'TK_STRING':
415
+ if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type == 'TK_SEMICOLON') {
416
+ print_newline();
417
+ } else if (last_type === 'TK_WORD') {
418
+ print_space();
419
+ }
420
+ print_token();
421
+ break;
422
+ case 'TK_OPERATOR':
423
+ var start_delim = true;
424
+ var end_delim = true;
425
+ if (var_line && token_text !== ',') {
426
+ var_line_tainted = true;
427
+ if (token_text === ':') {
428
+ var_line = false;
429
+ }
430
+ }
431
+ if (var_line && token_text === ',' && current_mode === 'EXPRESSION') { // do not break on comma, for(var a = 1, b = 2)
432
+ var_line_tainted = false;
433
+ }
434
+ if (token_text === ':' && in_case) {
435
+ print_token(); // colon really asks for separate treatment
436
+ print_newline();
437
+ break;
438
+ }
439
+ if (token_text === '::') { // no spaces around exotic namespacing syntax operator
440
+ print_token();
441
+ break;
442
+ }
443
+ in_case = false;
444
+ if (token_text === ',') {
445
+ if (var_line) {
446
+ if (var_line_tainted) {
447
+ print_token();
448
+ print_newline();
449
+ var_line_tainted = false;
450
+ } else {
451
+ print_token();
452
+ print_space();
453
+ }
454
+ } else if (last_type === 'TK_END_BLOCK') {
455
+ print_token();
456
+ print_newline();
457
+ } else {
458
+ if (current_mode === 'BLOCK') {
459
+ print_token();
460
+ print_newline();
461
+ } else { // EXPR od DO_BLOCK
462
+ print_token();
463
+ print_space();
464
+ }
465
+ }
466
+ break;
467
+ } else if (token_text === '--' || token_text === '++') { // unary operators special case
468
+ if (last_text === ';') { // space for (;; ++i)
469
+ start_delim = true;
470
+ end_delim = false;
471
+ } else {
472
+ start_delim = false;
473
+ end_delim = false;
474
+ }
475
+ } else if (token_text === '!' && last_type === 'TK_START_EXPR') { // special case handling: if (!a)
476
+ start_delim = false;
477
+ end_delim = false;
478
+ } else if (last_type === 'TK_OPERATOR') {
479
+ start_delim = false;
480
+ end_delim = false;
481
+ } else if (last_type === 'TK_END_EXPR') {
482
+ start_delim = true;
483
+ end_delim = true;
484
+ } else if (token_text === '.') { // decimal digits or object.property
485
+ start_delim = false;
486
+ end_delim = false;
487
+ } else if (token_text === ':') { // zz: xx
488
+ // can't differentiate ternary op, so for now it's a ? b: c; without space before colon
489
+ if (last_text.match(/^\d+$/)) { // a little help for ternary a ? 1 : 0;
490
+ start_delim = true;
491
+ } else {
492
+ start_delim = false;
493
+ }
494
+ }
495
+ if (start_delim) {
496
+ print_space();
497
+ }
498
+ print_token();
499
+ if (end_delim) {
500
+ print_space();
501
+ }
502
+ break;
503
+ case 'TK_BLOCK_COMMENT':
504
+ print_newline();
505
+ print_token();
506
+ print_newline();
507
+ break;
508
+ case 'TK_COMMENT':
509
+ // print_newline();
510
+ print_space();
511
+ print_token();
512
+ print_newline();
513
+ break;
514
+ case 'TK_UNKNOWN':
515
+ print_token();
516
+ break;
517
+ }
518
+ last_type = token_type;
519
+ last_text = token_text;
520
+ }
521
+ return output.join('');
522
+ }MARK1.00ANYAJscr ��ޭ