History

Add pretty-printing command line tool.

Tor Andersson committed 8 years ago

Commit d04cbf31 parent 25821e64 files · +300 −126Browse files

Unified · Split

4 files
  1. Makefile +21 −15
  2. jsdump.c +163 −110
  3. jsparse.h +1 −1
  4. pp.c +115 −0
Makefile+21−15
@@ -8,9 +8,9 @@ incdir ?= $(prefix)/include · 7 unmodified lines
88 libdir ?= $(prefix)/lib
99
1010 ifeq "$(wildcard .git)" ".git"
11−VERSION := $(shell git describe --tags --always)
11+ VERSION := $(shell git describe --tags --always)
1212 else
13−VERSION := $(shell basename $$PWD | sed -e s,^mujs-,,)
13+ VERSION := $(shell basename $$PWD | sed -e s,^mujs-,,)
1414 endif
1515
1616 # Compiler flags for various configurations:
@@ -18,22 +18,22 @@ endif · 1 unmodified line
1818 CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter
1919
2020 ifeq "$(CC)" "clang"
21−CFLAGS += -Wunreachable-code
21+ CFLAGS += -Wunreachable-code
2222 endif
2323
2424 ifeq "$(shell uname)" "Linux"
25−CFLAGS += -ffunction-sections -fdata-sections
26−LDFLAGS += -Wl,--gc-sections
25+ CFLAGS += -ffunction-sections -fdata-sections
26+ LDFLAGS += -Wl,--gc-sections
2727 endif
2828
2929 ifeq "$(build)" "debug"
30−CFLAGS += -g
30+ CFLAGS += -g
3131 else ifeq "$(build)" "sanitize"
32−CFLAGS += -pipe -g -fsanitize=address -fno-omit-frame-pointer
33−LDFLAGS += -fsanitize=address
32+ CFLAGS += -pipe -g -fsanitize=address -fno-omit-frame-pointer
33+ LDFLAGS += -fsanitize=address
3434 else
35−CFLAGS += -Os
36−LDFLAGS += -Wl,-s
35+ CFLAGS += -Os
36+ LDFLAGS += -Wl,-s
3737 endif
3838
3939 CFLAGS += $(XCFLAGS)
@@ -46,7 +46,7 @@ SRCS := $(wildcard js*.c utf*.c regexp.c) · 6 unmodified lines
4646 HDRS := $(wildcard js*.h mujs.h utf.h regexp.h)
4747
4848 default: static
49−static: $(OUT) $(OUT)/mujs $(OUT)/libmujs.a $(OUT)/mujs.pc
49+static: $(OUT)/mujs-pp $(OUT)/mujs $(OUT)/libmujs.a $(OUT)/mujs.pc
5050 shared: static $(OUT)/libmujs.so
5151
5252 astnames.h: jsparse.h
@@ -60,22 +60,28 @@ one.c: $(SRCS) · 7 unmodified lines
6060
6161 jsdump.c: astnames.h opnames.h
6262
63−$(OUT):
64− mkdir -p $(OUT)
65−
66−$(OUT)/main.o: main.c $(HDRS)
63+$(OUT)/%.o: %.c $(HDRS)
64+ @ mkdir -p $(dir $@)
6765 $(CC) $(CFLAGS) -o $@ -c $<
6866
6967 $(OUT)/libmujs.o: one.c $(HDRS)
68+ @ mkdir -p $(dir $@)
7069 $(CC) $(CFLAGS) -o $@ -c $<
7170
7271 $(OUT)/libmujs.a: $(OUT)/libmujs.o
72+ @ mkdir -p $(dir $@)
7373 $(AR) cru $@ $^
7474
7575 $(OUT)/libmujs.so: one.c $(HDRS)
76+ @ mkdir -p $(dir $@)
7677 $(CC) $(CFLAGS) -fPIC -shared -o $@ $< -lm
7778
7879 $(OUT)/mujs: $(OUT)/libmujs.o $(OUT)/main.o
80+ @ mkdir -p $(dir $@)
81+ $(CC) $(LDFLAGS) -o $@ $^ -lm
82+
83+$(OUT)/mujs-pp: $(OUT)/libmujs.o $(OUT)/pp.o
84+ @ mkdir -p $(dir $@)
7985 $(CC) $(LDFLAGS) -o $@ $^ -lm
8086
8187 $(OUT)/mujs.pc:
jsdump.c+163−110
@@ -17,6 +17,8 @@ static const char *opname[] = { · 16 unmodified lines
1717 NULL
1818 };
1919
20+static int minify = 0;
21+
2022 const char *jsP_aststring(enum js_AstType type)
2123 {
2224 if (type < nelem(astname)-1)
@@ -34,6 +36,7 @@ const char *jsC_opcodestring(enum js_OpCode opcode) · 11 unmodified lines
3436 static int prec(enum js_AstType type)
3537 {
3638 switch (type) {
39+ case AST_IDENTIFIER:
3740 case EXP_IDENTIFIER:
3841 case EXP_NUMBER:
3942 case EXP_STRING:
@@ -142,13 +145,27 @@ static void ps(const char *s) · 102 unmodified lines
142145
143146 static void in(int d)
144147 {
145− while (d-- > 0)
146− putchar('\t');
148+ if (minify < 1)
149+ while (d-- > 0)
150+ putchar('\t');
147151 }
148152
149153 static void nl(void)
150154 {
151− putchar('\n');
155+ if (minify < 2)
156+ putchar('\n');
157+}
158+
159+static void sp(void)
160+{
161+ if (minify < 1)
162+ putchar(' ');
163+}
164+
165+static void comma(void)
166+{
167+ putchar(',');
168+ sp();
152169 }
153170
154171 /* Pretty-printed Javascript syntax */
@@ -166,68 +183,79 @@ static void pargs(int d, js_Ast *list) · 11 unmodified lines
166183 pexpi(d, COMMA, list->a);
167184 list = list->b;
168185 if (list)
169− ps(", ");
186+ comma();
170187 }
171188 }
172189
173190 static void parray(int d, js_Ast *list)
174191 {
175− ps("[");
192+ pc('[');
176193 while (list) {
177194 assert(list->type == AST_LIST);
178195 pexpi(d, COMMA, list->a);
179196 list = list->b;
180197 if (list)
181− ps(", ");
198+ comma();
182199 }
183− ps("]");
200+ pc(']');
184201 }
185202
186203 static void pobject(int d, js_Ast *list)
187204 {
188− ps("{");
205+ pc('{');
206+ if (list) {
207+ nl();
208+ in(d+1);
209+ }
189210 while (list) {
190211 js_Ast *kv = list->a;
191212 assert(list->type == AST_LIST);
192213 switch (kv->type) {
193214 default: break;
194215 case EXP_PROP_VAL:
195− pexpi(d, COMMA, kv->a);
196− ps(": ");
197− pexpi(d, COMMA, kv->b);
216+ pexpi(d+1, COMMA, kv->a);
217+ pc(':'); sp();
218+ pexpi(d+1, COMMA, kv->b);
198219 break;
199220 case EXP_PROP_GET:
200221 ps("get ");
201− pexpi(d, COMMA, kv->a);
202− ps("() {\n");
203− pstmlist(d, kv->c);
204− in(d); ps("}");
222+ pexpi(d+1, COMMA, kv->a);
223+ ps("()"); sp(); pc('{'); nl();
224+ pstmlist(d+1, kv->c);
225+ in(d+1); pc('}');
205226 break;
206227 case EXP_PROP_SET:
207228 ps("set ");
208− pexpi(d, COMMA, kv->a);
209− ps("(");
210− pargs(d, kv->b);
211− ps(") {\n");
212− pstmlist(d, kv->c);
213− in(d); ps("}");
229+ pexpi(d+1, COMMA, kv->a);
230+ pc('(');
231+ pargs(d+1, kv->b);
232+ pc(')'); sp(); pc('{'); nl();
233+ pstmlist(d+1, kv->c);
234+ in(d+1); pc('}');
214235 break;
215236 }
216237 list = list->b;
217− if (list)
218− ps(", ");
238+ if (list) {
239+ pc(',');
240+ nl();
241+ in(d+1);
242+ } else {
243+ nl();
244+ in(d);
245+ }
219246 }
220− ps("}");
247+ pc('}');
221248 }
222249
223250 static void pstr(const char *s)
224251 {
225252 static const char *HEX = "0123456789ABCDEF";
226253 Rune c;
227− pc('"');
254+ pc(minify ? '\'' : '"');
228255 while (*s) {
229256 s += chartorune(&c, s);
230257 switch (c) {
258+ case '\'': ps("\\'"); break;
231259 case '"': ps("\\\""); break;
232260 case '\\': ps("\\\\"); break;
233261 case '\b': ps("\\b"); break;
@@ -247,7 +275,7 @@ static void pstr(const char *s) · 13 unmodified lines
247275 }
248276 }
249277 }
250− pc('"');
278+ pc(minify ? '\'' : '"');
251279 }
252280
253281 static void pregexp(const char *prog, int flags)
@@ -263,7 +291,9 @@ static void pregexp(const char *prog, int flags) · 9 unmodified lines
263291 static void pbin(int d, int p, js_Ast *exp, const char *op)
264292 {
265293 pexpi(d, p, exp->a);
294+ sp();
266295 ps(op);
296+ sp();
267297 pexpi(d, p, exp->b);
268298 }
269299
@@ -316,49 +346,58 @@ static void pexpi(int d, int p, js_Ast *exp) · 46 unmodified lines
316346 case EXP_BITNOT: puna(d, p, exp, "~", ""); break;
317347 case EXP_LOGNOT: puna(d, p, exp, "!", ""); break;
318348
319− case EXP_LOGOR: pbin(d, p, exp, " || "); break;
320− case EXP_LOGAND: pbin(d, p, exp, " && "); break;
321− case EXP_BITOR: pbin(d, p, exp, " | "); break;
322− case EXP_BITXOR: pbin(d, p, exp, " ^ "); break;
323− case EXP_BITAND: pbin(d, p, exp, " & "); break;
324− case EXP_EQ: pbin(d, p, exp, " == "); break;
325− case EXP_NE: pbin(d, p, exp, " != "); break;
326− case EXP_STRICTEQ: pbin(d, p, exp, " === "); break;
327− case EXP_STRICTNE: pbin(d, p, exp, " !== "); break;
328− case EXP_LT: pbin(d, p, exp, " < "); break;
329− case EXP_GT: pbin(d, p, exp, " > "); break;
330− case EXP_LE: pbin(d, p, exp, " <= "); break;
331− case EXP_GE: pbin(d, p, exp, " >= "); break;
332− case EXP_INSTANCEOF: pbin(d, p, exp, " instanceof "); break;
333− case EXP_IN: pbin(d, p, exp, " in "); break;
334− case EXP_SHL: pbin(d, p, exp, " << "); break;
335− case EXP_SHR: pbin(d, p, exp, " >> "); break;
336− case EXP_USHR: pbin(d, p, exp, " >>> "); break;
337− case EXP_ADD: pbin(d, p, exp, " + "); break;
338− case EXP_SUB: pbin(d, p, exp, " - "); break;
339− case EXP_MUL: pbin(d, p, exp, " * "); break;
340− case EXP_DIV: pbin(d, p, exp, " / "); break;
341− case EXP_MOD: pbin(d, p, exp, " % "); break;
342− case EXP_ASS: pbin(d, p, exp, " = "); break;
343− case EXP_ASS_MUL: pbin(d, p, exp, " *= "); break;
344− case EXP_ASS_DIV: pbin(d, p, exp, " /= "); break;
345− case EXP_ASS_MOD: pbin(d, p, exp, " %= "); break;
346− case EXP_ASS_ADD: pbin(d, p, exp, " += "); break;
347− case EXP_ASS_SUB: pbin(d, p, exp, " -= "); break;
348− case EXP_ASS_SHL: pbin(d, p, exp, " <<= "); break;
349− case EXP_ASS_SHR: pbin(d, p, exp, " >>= "); break;
350− case EXP_ASS_USHR: pbin(d, p, exp, " >>>= "); break;
351− case EXP_ASS_BITAND: pbin(d, p, exp, " &= "); break;
352− case EXP_ASS_BITXOR: pbin(d, p, exp, " ^= "); break;
353− case EXP_ASS_BITOR: pbin(d, p, exp, " |= "); break;
354−
355− case EXP_COMMA: pbin(d, p, exp, ", "); break;
349+ case EXP_LOGOR: pbin(d, p, exp, "||"); break;
350+ case EXP_LOGAND: pbin(d, p, exp, "&&"); break;
351+ case EXP_BITOR: pbin(d, p, exp, "|"); break;
352+ case EXP_BITXOR: pbin(d, p, exp, "^"); break;
353+ case EXP_BITAND: pbin(d, p, exp, "&"); break;
354+ case EXP_EQ: pbin(d, p, exp, "=="); break;
355+ case EXP_NE: pbin(d, p, exp, "!="); break;
356+ case EXP_STRICTEQ: pbin(d, p, exp, "==="); break;
357+ case EXP_STRICTNE: pbin(d, p, exp, "!=="); break;
358+ case EXP_LT: pbin(d, p, exp, "<"); break;
359+ case EXP_GT: pbin(d, p, exp, ">"); break;
360+ case EXP_LE: pbin(d, p, exp, "<="); break;
361+ case EXP_GE: pbin(d, p, exp, ">="); break;
362+ case EXP_IN: pbin(d, p, exp, "in"); break;
363+ case EXP_SHL: pbin(d, p, exp, "<<"); break;
364+ case EXP_SHR: pbin(d, p, exp, ">>"); break;
365+ case EXP_USHR: pbin(d, p, exp, ">>>"); break;
366+ case EXP_ADD: pbin(d, p, exp, "+"); break;
367+ case EXP_SUB: pbin(d, p, exp, "-"); break;
368+ case EXP_MUL: pbin(d, p, exp, "*"); break;
369+ case EXP_DIV: pbin(d, p, exp, "/"); break;
370+ case EXP_MOD: pbin(d, p, exp, "%"); break;
371+ case EXP_ASS: pbin(d, p, exp, "="); break;
372+ case EXP_ASS_MUL: pbin(d, p, exp, "*="); break;
373+ case EXP_ASS_DIV: pbin(d, p, exp, "/="); break;
374+ case EXP_ASS_MOD: pbin(d, p, exp, "%="); break;
375+ case EXP_ASS_ADD: pbin(d, p, exp, "+="); break;
376+ case EXP_ASS_SUB: pbin(d, p, exp, "-="); break;
377+ case EXP_ASS_SHL: pbin(d, p, exp, "<<="); break;
378+ case EXP_ASS_SHR: pbin(d, p, exp, ">>="); break;
379+ case EXP_ASS_USHR: pbin(d, p, exp, ">>>="); break;
380+ case EXP_ASS_BITAND: pbin(d, p, exp, "&="); break;
381+ case EXP_ASS_BITXOR: pbin(d, p, exp, "^="); break;
382+ case EXP_ASS_BITOR: pbin(d, p, exp, "|="); break;
383+
384+ case EXP_INSTANCEOF:
385+ pexpi(d, p, exp->a);
386+ ps(" instanceof ");
387+ pexpi(d, p, exp->b);
388+ break;
389+
390+ case EXP_COMMA:
391+ pexpi(d, p, exp->a);
392+ pc(','); sp();
393+ pexpi(d, p, exp->b);
394+ break;
356395
357396 case EXP_COND:
358397 pexpi(d, p, exp->a);
359− ps(" ? ");
398+ sp(); pc('?'); sp();
360399 pexpi(d, p, exp->b);
361− ps(" : ");
400+ sp(); pc(':'); sp();
362401 pexpi(d, p, exp->c);
363402 break;
364403
@@ -396,7 +435,7 @@ static void pexpi(int d, int p, js_Ast *exp) · 31 unmodified lines
396435 pexpi(d, 0, exp->a);
397436 pc('(');
398437 pargs(d, exp->b);
399− ps(") {\n");
438+ pc(')'); sp(); pc('{'); nl();
400439 pstmlist(d, exp->c);
401440 in(d); pc('}');
402441 if (p == 0) pc(')');
@@ -420,7 +459,7 @@ static void pvar(int d, js_Ast *var) · 17 unmodified lines
420459 assert(var->type == EXP_VAR);
421460 pexp(d, var->a);
422461 if (var->b) {
423− ps(" = ");
462+ sp(); pc('='); sp();
424463 pexp(d, var->b);
425464 }
426465 }
@@ -432,23 +471,24 @@ static void pvarlist(int d, js_Ast *list) · 5 unmodified lines
432471 pvar(d, list->a);
433472 list = list->b;
434473 if (list)
435− ps(", ");
474+ comma();
436475 }
437476 }
438477
439478 static void pblock(int d, js_Ast *block)
440479 {
441480 assert(block->type == STM_BLOCK);
442− ps(" {\n");
481+ pc('{'); nl();
443482 pstmlist(d, block->a);
444483 in(d); pc('}');
445484 }
446485
447486 static void pstmh(int d, js_Ast *stm)
448487 {
449− if (stm->type == STM_BLOCK)
488+ if (stm->type == STM_BLOCK) {
489+ sp();
450490 pblock(d, stm);
451− else {
491+ } else {
452492 nl();
453493 pstm(d+1, stm);
454494 }
@@ -459,11 +499,11 @@ static void pcaselist(int d, js_Ast *list) · 4 unmodified lines
459499 while (list) {
460500 js_Ast *stm = list->a;
461501 if (stm->type == STM_CASE) {
462− in(d); ps("case "); pexp(d, stm->a); ps(":\n");
502+ in(d); ps("case "); pexp(d, stm->a); pc(':'); nl();
463503 pstmlist(d, stm->b);
464504 }
465505 if (stm->type == STM_DEFAULT) {
466− in(d); ps("default:\n");
506+ in(d); ps("default:"); nl();
467507 pstmlist(d, stm->a);
468508 }
469509 list = list->b;
@@ -485,9 +525,9 @@ static void pstm(int d, js_Ast *stm) · 15 unmodified lines
485525 pexp(d, stm->a);
486526 pc('(');
487527 pargs(d, stm->b);
488− ps(") {\n");
528+ pc(')'); sp(); pc('{'); nl();
489529 pstmlist(d, stm->c);
490− in(d); ps("}");
530+ in(d); pc('}');
491531 break;
492532
493533 case STM_EMPTY:
@@ -497,11 +537,11 @@ static void pstm(int d, js_Ast *stm) · 3 unmodified lines
497537 case STM_VAR:
498538 ps("var ");
499539 pvarlist(d, stm->a);
500− ps(";");
540+ pc(';');
501541 break;
502542
503543 case STM_IF:
504− ps("if ("); pexp(d, stm->a); ps(")");
544+ ps("if"); sp(); pc('('); pexp(d, stm->a); pc(')');
505545 pstmh(d, stm->b);
506546 if (stm->c) {
507547 nl(); in(d); ps("else");
@@ -513,87 +553,89 @@ static void pstm(int d, js_Ast *stm) · 5 unmodified lines
513553 ps("do");
514554 pstmh(d, stm->a);
515555 nl();
516− in(d); ps("while ("); pexp(d, stm->b); ps(");");
556+ in(d); ps("while"); sp(); pc('('); pexp(d, stm->b); pc(')'); pc(';');
517557 break;
518558
519559 case STM_WHILE:
520− ps("while ("); pexp(d, stm->a); ps(")");
560+ ps("while"); sp(); pc('('); pexp(d, stm->a); pc(')');
521561 pstmh(d, stm->b);
522562 break;
523563
524564 case STM_FOR:
525− ps("for (");
526− pexp(d, stm->a); ps("; ");
527− pexp(d, stm->b); ps("; ");
528− pexp(d, stm->c); ps(")");
565+ ps("for"); sp(); pc('(');
566+ pexp(d, stm->a); pc(';'); sp();
567+ pexp(d, stm->b); pc(';'); sp();
568+ pexp(d, stm->c); pc(')');
529569 pstmh(d, stm->d);
530570 break;
531571 case STM_FOR_VAR:
532− ps("for (var ");
533− pvarlist(d, stm->a); ps("; ");
534− pexp(d, stm->b); ps("; ");
535− pexp(d, stm->c); ps(")");
572+ ps("for"); sp(); ps("(var ");
573+ pvarlist(d, stm->a); pc(';'); sp();
574+ pexp(d, stm->b); pc(';'); sp();
575+ pexp(d, stm->c); pc(')');
536576 pstmh(d, stm->d);
537577 break;
538578 case STM_FOR_IN:
539− ps("for (");
579+ ps("for"); sp(); pc('(');
540580 pexp(d, stm->a); ps(" in ");
541− pexp(d, stm->b); ps(")");
581+ pexp(d, stm->b); pc(')');
542582 pstmh(d, stm->c);
543583 break;
544584 case STM_FOR_IN_VAR:
545− ps("for (var ");
585+ ps("for"); sp(); ps("(var ");
546586 pvarlist(d, stm->a); ps(" in ");
547− pexp(d, stm->b); ps(")");
587+ pexp(d, stm->b); pc(')');
548588 pstmh(d, stm->c);
549589 break;
550590
551591 case STM_CONTINUE:
592+ ps("continue");
552593 if (stm->a) {
553− ps("continue "); pexp(d, stm->a); ps(";");
554− } else {
555− ps("continue;");
594+ pc(' '); pexp(d, stm->a);
556595 }
596+ pc(';');
557597 break;
558598
559599 case STM_BREAK:
600+ ps("break");
560601 if (stm->a) {
561− ps("break "); pexp(d, stm->a); ps(";");
562− } else {
563− ps("break;");
602+ pc(' '); pexp(d, stm->a);
564603 }
604+ pc(';');
565605 break;
566606
567607 case STM_RETURN:
608+ ps("return");
568609 if (stm->a) {
569− ps("return "); pexp(d, stm->a); ps(";");
570− } else {
571− ps("return;");
610+ pc(' '); pexp(d, stm->a);
572611 }
612+ pc(';');
573613 break;
574614
575615 case STM_WITH:
576− ps("with ("); pexp(d, stm->a); ps(")");
616+ ps("with"); sp(); pc('('); pexp(d, stm->a); pc(')');
577617 pstmh(d, stm->b);
578618 break;
579619
580620 case STM_SWITCH:
581− ps("switch (");
621+ ps("switch"); sp(); pc('(');
582622 pexp(d, stm->a);
583− ps(") {\n");
623+ pc(')'); sp(); pc('{'); nl();
584624 pcaselist(d, stm->b);
585− in(d); ps("}");
625+ in(d); pc('}');
586626 break;
587627
588628 case STM_THROW:
589− ps("throw "); pexp(d, stm->a); ps(";");
629+ ps("throw "); pexp(d, stm->a); pc(';');
590630 break;
591631
592632 case STM_TRY:
593633 ps("try");
634+ if (minify && stm->a->type != STM_BLOCK)
635+ pc(' ');
594636 pstmh(d, stm->a);
595637 if (stm->b && stm->c) {
596− nl(); in(d); ps("catch ("); pexp(d, stm->b); ps(")");
638+ nl(); in(d); ps("catch"); sp(); pc('('); pexp(d, stm->b); pc(')');
597639 pstmh(d, stm->c);
598640 }
599641 if (stm->d) {
@@ -603,15 +645,17 @@ static void pstm(int d, js_Ast *stm) · 3 unmodified lines
603645 break;
604646
605647 case STM_LABEL:
606− pexp(d, stm->a); ps(": "); pstm(d, stm->b);
648+ pexp(d, stm->a); pc(':'); sp(); pstm(d, stm->b);
607649 break;
608650
609651 case STM_DEBUGGER:
610− ps("debugger;");
652+ ps("debugger");
653+ pc(';');
611654 break;
612655
613656 default:
614− pexp(d, stm); pc(';');
657+ pexp(d, stm);
658+ pc(';');
615659 }
616660 }
617661
@@ -625,14 +669,17 @@ static void pstmlist(int d, js_Ast *list) · 7 unmodified lines
625669 }
626670 }
627671
628−void jsP_dumpsyntax(js_State *J, js_Ast *prog)
672+void jsP_dumpsyntax(js_State *J, js_Ast *prog, int dominify)
629673 {
674+ minify = dominify;
630675 if (prog->type == AST_LIST)
631676 pstmlist(-1, prog);
632677 else {
633678 pstm(0, prog);
634679 nl();
635680 }
681+ if (minify > 1)
682+ putchar('\n');
636683 }
637684
638685 /* S-expression list representation */
@@ -708,6 +755,7 @@ static void sblock(int d, js_Ast *list) · 69 unmodified lines
708755
709756 void jsP_dumplist(js_State *J, js_Ast *prog)
710757 {
758+ minify = 0;
711759 if (prog->type == AST_LIST)
712760 sblock(0, prog);
713761 else
@@ -723,6 +771,8 @@ void jsC_dumpfunction(js_State *J, js_Function *F) · 9 unmodified lines
723771 js_Instruction *end = F->code + F->codelen;
724772 int i;
725773
774+ minify = 0;
775+
726776 printf("%s(%d)\n", F->name, F->numparams);
727777 if (F->lightweight) printf("\tlightweight\n");
728778 if (F->arguments) printf("\targuments\n");
@@ -802,6 +852,7 @@ void jsC_dumpfunction(js_State *J, js_Function *F) · 73 unmodified lines
802852
803853 void js_dumpvalue(js_State *J, js_Value v)
804854 {
855+ minify = 0;
805856 switch (v.type) {
806857 case JS_TUNDEFINED: printf("undefined"); break;
807858 case JS_TNULL: printf("null"); break;
@@ -843,6 +894,7 @@ void js_dumpvalue(js_State *J, js_Value v) · 35 unmodified lines
843894
844895 static void js_dumpproperty(js_State *J, js_Property *node)
845896 {
897+ minify = 0;
846898 if (node->left->level)
847899 js_dumpproperty(J, node->left);
848900 printf("\t%s: ", node->name);
@@ -854,6 +906,7 @@ static void js_dumpproperty(js_State *J, js_Property *node) · 5 unmodified lines
854906
855907 void js_dumpobject(js_State *J, js_Object *obj)
856908 {
909+ minify = 0;
857910 printf("{\n");
858911 if (obj->properties->level)
859912 js_dumpproperty(J, obj->properties);
jsparse.h+1−1
@@ -140,7 +140,7 @@ js_Ast *jsP_parse(js_State *J, const char *filename, const char *source); · 139 unmodified lines
140140 void jsP_freeparse(js_State *J);
141141
142142 const char *jsP_aststring(enum js_AstType type);
143−void jsP_dumpsyntax(js_State *J, js_Ast *prog);
143+void jsP_dumpsyntax(js_State *J, js_Ast *prog, int minify);
144144 void jsP_dumplist(js_State *J, js_Ast *prog);
145145
146146 #endif
pp.c+115−0
@@ -0,0 +1,115 @@
1+/* Pretty-print input source by emitting parse tree back as syntax.
2+ * with no flags: pretty-printed source
3+ * with -m: minified source with line breaks
4+ * with -mm: minified source without line breaks
5+ * with -s: s-expression syntax tree
6+ */
7+
8+#include <stdio.h>
9+
10+#include "jsi.h"
11+#include "jsparse.h"
12+
13+static void js_ppstring(js_State *J, const char *filename, const char *source, int minify)
14+{
15+ js_Ast *P;
16+ if (js_try(J)) {
17+ jsP_freeparse(J);
18+ js_throw(J);
19+ }
20+ P = jsP_parse(J, filename, source);
21+ if (minify > 2)
22+ jsP_dumplist(J, P);
23+ else
24+ jsP_dumpsyntax(J, P, minify);
25+ jsP_freeparse(J);
26+ js_endtry(J);
27+}
28+
29+void js_ppfile(js_State *J, const char *filename, int minify)
30+{
31+ FILE *f;
32+ char *s;
33+ int n, t;
34+
35+ f = fopen(filename, "rb");
36+ if (!f) {
37+ js_error(J, "cannot open file: '%s'", filename);
38+ }
39+
40+ if (fseek(f, 0, SEEK_END) < 0) {
41+ fclose(f);
42+ js_error(J, "cannot seek in file: '%s'", filename);
43+ }
44+
45+ n = ftell(f);
46+ if (n < 0) {
47+ fclose(f);
48+ js_error(J, "cannot tell in file: '%s'", filename);
49+ }
50+
51+ if (fseek(f, 0, SEEK_SET) < 0) {
52+ fclose(f);
53+ js_error(J, "cannot seek in file: '%s'", filename);
54+ }
55+
56+ s = js_malloc(J, n + 1); /* add space for string terminator */
57+ if (!s) {
58+ fclose(f);
59+ js_error(J, "cannot allocate storage for file contents: '%s'", filename);
60+ }
61+
62+ t = fread(s, 1, (size_t)n, f);
63+ if (t != n) {
64+ js_free(J, s);
65+ fclose(f);
66+ js_error(J, "cannot read data from file: '%s'", filename);
67+ }
68+
69+ s[n] = 0; /* zero-terminate string containing file data */
70+
71+ if (js_try(J)) {
72+ js_free(J, s);
73+ fclose(f);
74+ js_throw(J);
75+ }
76+
77+ js_ppstring(J, filename, s, minify);
78+
79+ js_free(J, s);
80+ fclose(f);
81+ js_endtry(J);
82+}
83+
84+int
85+main(int argc, char **argv)
86+{
87+ js_State *J;
88+ int minify = 0;
89+ int i;
90+
91+ J = js_newstate(NULL, NULL, 0);
92+
93+ for (i = 1; i < argc; ++i) {
94+ if (!strcmp(argv[i], "-m"))
95+ minify = 1;
96+ else if (!strcmp(argv[i], "-mm"))
97+ minify = 2;
98+ else if (!strcmp(argv[i], "-s"))
99+ minify = 3;
100+ else {
101+ if (js_try(J)) {
102+ js_report(J, js_trystring(J, -1, "Error"));
103+ js_pop(J, 1);
104+ continue;
105+ }
106+ js_ppfile(J, argv[i], minify);
107+ js_endtry(J);
108+ }
109+ }
110+
111+ js_gc(J, 0);
112+ js_freestate(J);
113+
114+ return 0;
115+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t