aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/contrib/jsbeautifier/__init__.py
blob: e319e8dd1a76116e9c179852a3443112813c0a08 (plain)
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
import sys
import getopt
import re
import string

#
# Originally written by Einar Lielmanis et al.,
# Conversion to python by Einar Lielmanis, einar@jsbeautifier.org,
# MIT licence, enjoy.
#
# Python is not my native language, feel free to push things around.
#
# Use either from command line (script displays its usage when run
# without any parameters),
#
#
# or, alternatively, use it as a module:
#
#   import jsbeautifier
#   res = jsbeautifier.beautify('your javascript string')
#   res = jsbeautifier.beautify_file('some_file.js')
#
#  you may specify some options:
#
#   opts = jsbeautifier.default_options()
#   opts.indent_size = 2
#   res = jsbeautifier.beautify('some javascript', opts)
#
#
# Here are the available options: (read source)


class BeautifierOptions:
    def __init__(self):
        self.indent_size = 4
        self.indent_char = ' '
        self.indent_with_tabs = False
        self.preserve_newlines = True
        self.max_preserve_newlines = 10.
        self.jslint_happy = False
        self.brace_style = 'collapse'
        self.keep_array_indentation = False
        self.keep_function_indentation = False
        self.eval_code = False



    def __repr__(self):
        return \
"""indent_size = %d
indent_char = [%s]
preserve_newlines = %s
max_preserve_newlines = %d
jslint_happy = %s
indent_with_tabs = %s
brace_style = %s
keep_array_indentation = %s
eval_code = %s
""" % ( self.indent_size,
        self.indent_char,
        self.preserve_newlines,
        self.max_preserve_newlines,
        self.jslint_happy,
        self.indent_with_tabs,
        self.brace_style,
        self.keep_array_indentation,
        self.eval_code,
        )


class BeautifierFlags:
    def __init__(self, mode):
        self.previous_mode = 'BLOCK'
        self.mode = mode
        self.var_line = False
        self.var_line_tainted = False
        self.var_line_reindented = False
        self.in_html_comment = False
        self.if_line = False
        self.in_case = False
        self.eat_next_space = False
        self.indentation_baseline = -1
        self.indentation_level = 0
        self.ternary_depth = 0


def default_options():
    return BeautifierOptions()


def beautify(string, opts = default_options() ):
    b = Beautifier()
    return b.beautify(string, opts)

def beautify_file(file_name, opts = default_options() ):

    if file_name == '-': # stdin
        f = sys.stdin
    else:
        try:
            f = open(file_name)
        except Exception as ex:
            return 'The file could not be opened'

    b = Beautifier()
    return b.beautify(''.join(f.readlines()), opts)


def usage():

    print("""Javascript beautifier (http://jsbeautifier.org/)

Usage: jsbeautifier.py [options] <infile>

    <infile> can be "-", which means stdin.
    <outfile> defaults to stdout

Input options:

 -i,  --stdin                      read input from stdin

Output options:

 -s,  --indent-size=NUMBER         indentation size. (default 4).
 -c,  --indent-char=CHAR           character to indent with. (default space).
 -t,  --indent-with-tabs           Indent with tabs, overrides -s and -c
 -d,  --disable-preserve-newlines  do not preserve existing line breaks.
 -j,  --jslint-happy               more jslint-compatible output
 -b,  --brace-style=collapse       brace style (collapse, expand, end-expand)
 -k,  --keep-array-indentation     keep array indentation.
 -o,  --outfile=FILE               specify a file to output to (default stdout)
 -f,  --keep-function-indentation  Do not re-indent function bodies defined in var lines.

Rarely needed options:

 --eval-code                       evaluate code if a JS interpreter is
                                   installed. May be useful with some obfuscated
                                   script but poses a potential security issue.

 -l,  --indent-level=NUMBER        initial indentation level. (default 0).

 -h,  --help, --usage              prints this help statement.

""")






class Beautifier:

    def __init__(self, opts = default_options() ):

        self.opts = opts
        self.blank_state()

    def blank_state(self):

        # internal flags
        self.flags = BeautifierFlags('BLOCK')
        self.flag_store = []
        self.wanted_newline = False
        self.just_added_newline = False
        self.do_block_just_closed = False

        if self.opts.indent_with_tabs:
            self.indent_string = "\t"
        else:
            self.indent_string = self.opts.indent_char * self.opts.indent_size

        self.preindent_string = ''
        self.last_word = ''              # last TK_WORD seen
        self.last_type = 'TK_START_EXPR' # last token type
        self.last_text = ''              # last token text
        self.last_last_text = ''         # pre-last token text

        self.input = None
        self.output = []                 # formatted javascript gets built here

        self.whitespace = ["\n", "\r", "\t", " "]
        self.wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'
        self.digits = '0123456789'
        self.punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'
        self.punct += ' <?= <? ?> <%= <% %>'
        self.punct = self.punct.split(' ')


        # Words which always should start on a new line
        self.line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',')
        self.set_mode('BLOCK')

        global parser_pos
        parser_pos = 0


    def beautify(self, s, opts = None ):

        if opts != None:
            self.opts = opts


        if self.opts.brace_style not in ['expand', 'collapse', 'end-expand']:
            raise(Exception('opts.brace_style must be "expand", "collapse" or "end-expand".'))

        self.blank_state()

        while s and s[0] in [' ', '\t']:
            self.preindent_string += s[0]
            s = s[1:]

        #self.input = self.unpack(s, opts.eval_code)
        # CORTESI
        self.input = s

        parser_pos = 0
        while True:
            token_text, token_type = self.get_next_token()
            #print (token_text, token_type, self.flags.mode)
            if token_type == 'TK_EOF':
                break

            handlers = {
                'TK_START_EXPR': self.handle_start_expr,
                'TK_END_EXPR': self.handle_end_expr,
                'TK_START_BLOCK': self.handle_start_block,
                'TK_END_BLOCK': self.handle_end_block,
                'TK_WORD': self.handle_word,
                'TK_SEMICOLON': self.handle_semicolon,
                'TK_STRING': self.handle_string,
                'TK_EQUALS': self.handle_equals,
                'TK_OPERATOR': self.handle_operator,
                'TK_BLOCK_COMMENT': self.handle_block_comment,
                'TK_INLINE_COMMENT': self.handle_inline_comment,
                'TK_COMMENT': self.handle_comment,
                'TK_UNKNOWN': self.handle_unknown,
            }

            handlers[token_type](token_text)

            self.last_last_text = self.last_text
            self.last_type = token_type
            self.last_text = token_text

        sweet_code = self.preindent_string + re.sub('[\n ]+$', '', ''.join(self.output))
        return sweet_code

    def unpack(self, source, evalcode=False):
        import jsbeautifier.unpackers as unpackers
        try:
            return unpackers.run(source, evalcode)
        except unpackers.UnpackingError as error:
            print('error:', error)
            return ''

    def trim_output(self, eat_newlines = False):
        while len(self.output) \
              and (
                  self.output[-1] == ' '\
                  or self.output[-1] == self.indent_string \
                  or self.output[-1] == self.preindent_string \
                  or (eat_newlines and self.output[-1] in ['\n', '\r'])):
            self.output.pop()

    def is_special_word(self, s):
        return s in ['case', 'return', 'do', 'if', 'throw', 'else'];

    def is_array(self, mode):
        return mode in ['[EXPRESSION]', '[INDENDED-EXPRESSION]']


    def is_expression(self, mode):
        return mode in ['[EXPRESSION]', '[INDENDED-EXPRESSION]', '(EXPRESSION)', '(FOR-EXPRESSION)', '(COND-EXPRESSION)']


    def append_newline_forced(self):
        old_array_indentation = self.opts.keep_array_indentation
        self.opts.keep_array_indentation = False
        self.append_newline()
        self.opts.keep_array_indentation = old_array_indentation

    def append_newline(self, ignore_repeated = True):

        self.flags.eat_next_space = False

        if self.opts.keep_array_indentation and self.is_array(self.flags.mode):
            return

        self.flags.if_line = False
        self.trim_output()

        if len(self.output) == 0:
            # no newline on start of file
            return

        if self.output[-1] != '\n' or not ignore_repeated:
            self.just_added_newline = True
            self.output.append('\n')

        if self.preindent_string:
            self.output.append(self.preindent_string)

        for i in range(self.flags.indentation_level):
            self.output.append(self.indent_string)

        if self.flags.var_line and self.flags.var_line_reindented:
            self.output.append(self.indent_string)


    def append(self, s):
        if s == ' ':
            # do not add just a single space after the // comment, ever
            if self.last_type == 'TK_COMMENT':
                return self.append_newline()

            # make sure only single space gets drawn
            if self.flags.eat_next_space:
                self.flags.eat_next_space = False
            elif len(self.output) and self.output[-1] not in [' ', '\n', self.indent_string]:
                self.output.append(' ')
        else:
            self.just_added_newline = False
            self.flags.eat_next_space = False
            self.output.append(s)


    def indent(self):
        self.flags.indentation_level = self.flags.indentation_level + 1


    def remove_indent(self):
        if len(self.output) and self.output[-1] in [self.indent_string, self.preindent_string]:
            self.output.pop()


    def set_mode(self, mode):

        prev = BeautifierFlags('BLOCK')

        if self.flags:
            self.flag_store.append(self.flags)
            prev = self.flags

        self.flags = BeautifierFlags(mode)

        if len(self.flag_store) == 1:
            self.flags.indentation_level = 0
        else:
            self.flags.indentation_level = prev.indentation_level
            if prev.var_line and prev.var_line_reindented:
                self.flags.indentation_level = self.flags.indentation_level + 1
        self.flags.previous_mode = prev.mode


    def restore_mode(self):
        self.do_block_just_closed = self.flags.mode == 'DO_BLOCK'
        if len(self.flag_store) > 0:
            mode = self.flags.mode
            self.flags = self.flag_store.pop()
            self.flags.previous_mode = mode


    def get_next_token(self):

        global parser_pos

        self.n_newlines = 0

        if parser_pos >= len(self.input):
            return '', 'TK_EOF'

        self.wanted_newline = False
        c = self.input[parser_pos]
        parser_pos += 1

        keep_whitespace = self.opts.keep_array_indentation and self.is_array(self.flags.mode)

        if keep_whitespace:
            # slight mess to allow nice preservation of array indentation and reindent that correctly
            # first time when we get to the arrays:
            # var a = [
            # ....'something'
            # we make note of whitespace_count = 4 into flags.indentation_baseline
            # so we know that 4 whitespaces in original source match indent_level of reindented source
            #
            # and afterwards, when we get to
            #    'something,
            # .......'something else'
            # we know that this should be indented to indent_level + (7 - indentation_baseline) spaces

            whitespace_count = 0
            while c in self.whitespace:
                if c == '\n':
                    self.trim_output()
                    self.output.append('\n')
                    self.just_added_newline = True
                    whitespace_count = 0
                elif c == '\t':
                    whitespace_count += 4
                elif c == '\r':
                    pass
                else:
                    whitespace_count += 1

                if parser_pos >= len(self.input):
                    return '', 'TK_EOF'

                c = self.input[parser_pos]
                parser_pos += 1

            if self.flags.indentation_baseline == -1:

                self.flags.indentation_baseline = whitespace_count

            if self.just_added_newline:
                for i in range(self.flags.indentation_level + 1):
                    self.output.append(self.indent_string)

                if self.flags.indentation_baseline != -1:
                    for i in range(whitespace_count - self.flags.indentation_baseline):
                        self.output.append(' ')

        else: # not keep_whitespace
            while c in self.whitespace:
                if c == '\n':
                    if self.opts.max_preserve_newlines == 0 or self.opts.max_preserve_newlines > self.n_newlines:
                        self.n_newlines += 1

                if parser_pos >= len(self.input):
                    return '', 'TK_EOF'

                c = self.input[parser_pos]
                parser_pos += 1

            if self.opts.preserve_newlines and self.n_newlines > 1:
                for i in range(self.n_newlines):
                    self.append_newline(i == 0)
                    self.just_added_newline = True

            self.wanted_newline = self.n_newlines > 0


        if c in self.wordchar:
            if parser_pos < len(self.input):
                while self.input[parser_pos] in self.wordchar:
                    c = c + self.input[parser_pos]
                    parser_pos += 1
                    if parser_pos == len(self.input):
                        break

            # small and surprisingly unugly hack for 1E-10 representation
            if parser_pos != len(self.input) and self.input[parser_pos] in '+-' \
               and re.match('^[0-9]+[Ee]$', c):

                sign = self.input[parser_pos]
                parser_pos += 1
                t = self.get_next_token()
                c += sign + t[0]
                return c, 'TK_WORD'

            if c == 'in': # in is an operator, need to hack
                return c, 'TK_OPERATOR'

            if self.wanted_newline and \
               self.last_type != 'TK_OPERATOR' and\
               self.last_type != 'TK_EQUALS' and\
               not self.flags.if_line and \
               (self.opts.preserve_newlines or self.last_text != 'var'):
                self.append_newline()

            return c, 'TK_WORD'

        if c in '([':
            return c, 'TK_START_EXPR'

        if c in ')]':
            return c, 'TK_END_EXPR'

        if c == '{':
            return c, 'TK_START_BLOCK'

        if c == '}':
            return c, 'TK_END_BLOCK'

        if c == ';':
            return c, 'TK_SEMICOLON'

        if c == '/':
            comment = ''
            inline_comment = True
            comment_mode = 'TK_INLINE_COMMENT'
            if self.input[parser_pos] == '*': # peek /* .. */ comment
                parser_pos += 1
                if parser_pos < len(self.input):
                    while not (self.input[parser_pos] == '*' and \
                               parser_pos + 1 < len(self.input) and \
                               self.input[parser_pos + 1] == '/')\
                          and parser_pos < len(self.input):
                        c = self.input[parser_pos]
                        comment += c
                        if c in '\r\n':
                            comment_mode = 'TK_BLOCK_COMMENT'
                        parser_pos += 1
                        if parser_pos >= len(self.input):
                            break
                parser_pos += 2
                return '/*' + comment + '*/', comment_mode
            if self.input[parser_pos] == '/': # peek // comment
                comment = c
                while self.input[parser_pos] not in '\r\n':
                    comment += self.input[parser_pos]
                    parser_pos += 1
                    if parser_pos >= len(self.input):
                        break
                parser_pos += 1
                if self.wanted_newline:
                    self.append_newline()
                return comment, 'TK_COMMENT'



        if c == "'" or c == '"' or \
           (c == '/' and ((self.last_type == 'TK_WORD' and self.is_special_word(self.last_text)) or \
                          (self.last_type == 'TK_END_EXPR' and self.flags.previous_mode in ['(FOR-EXPRESSION)', '(COND-EXPRESSION)']) or \
                          (self.last_type in ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK', 'TK_END_BLOCK', 'TK_OPERATOR',
                                              'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON']))):
            sep = c
            esc = False
            resulting_string = c
            in_char_class = False

            if parser_pos < len(self.input):
                if sep == '/':
                    # handle regexp
                    in_char_class = False
                    while esc or in_char_class or self.input[parser_pos] != sep:
                        resulting_string += self.input[parser_pos]
                        if not esc:
                            esc = self.input[parser_pos] == '\\'
                            if self.input[parser_pos] == '[':
                                in_char_class = True
                            elif self.input[parser_pos] == ']':
                                in_char_class = False
                        else:
                            esc = False
                        parser_pos += 1
                        if parser_pos >= len(self.input):
                            # incomplete regex when end-of-file reached
                            # bail out with what has received so far
                            return resulting_string, 'TK_STRING'
                else:
                    # handle string
                    while esc or self.input[parser_pos] != sep:
                        resulting_string += self.input[parser_pos]
                        if not esc:
                            esc = self.input[parser_pos] == '\\'
                        else:
                            esc = False
                        parser_pos += 1
                        if parser_pos >= len(self.input):
                            # incomplete string when end-of-file reached
                            # bail out with what has received so far
                            return resulting_string, 'TK_STRING'


            parser_pos += 1
            resulting_string += sep
            if sep == '/':
                # regexps may have modifiers /regexp/MOD, so fetch those too
                while parser_pos < len(self.input) and self.input[parser_pos] in self.wordchar:
                    resulting_string += self.input[parser_pos]
                    parser_pos += 1
            return resulting_string, 'TK_STRING'

        if c == '#':

            # she-bang
            if len(self.output) == 0 and len(self.input) > 1 and self.input[parser_pos] == '!':
                resulting_string = c
                while parser_pos < len(self.input) and c != '\n':
                    c = self.input[parser_pos]
                    resulting_string += c
                    parser_pos += 1
                self.output.append(resulting_string.strip() + "\n")
                self.append_newline()
                return self.get_next_token()


            # Spidermonkey-specific sharp variables for circular references
            # https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
            # http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
            sharp = '#'
            if parser_pos < len(self.input) and self.input[parser_pos] in self.digits:
                while True:
                    c = self.input[parser_pos]
                    sharp += c
                    parser_pos += 1
                    if parser_pos >= len(self.input)  or c == '#' or c == '=':
                        break
            if c == '#' or parser_pos >= len(self.input):
                pass
            elif self.input[parser_pos] == '[' and self.input[parser_pos + 1] == ']':
                sharp += '[]'
                parser_pos += 2
            elif self.input[parser_pos] == '{' and self.input[parser_pos + 1] == '}':
                sharp += '{}'
                parser_pos += 2
            return sharp, 'TK_WORD'

        if c == '<' and self.input[parser_pos - 1 : parser_pos + 3] == '<!--':
            parser_pos += 3
            c = '<!--'
            while parser_pos < len(self.input) and self.input[parser_pos] != '\n':
                c += self.input[parser_pos]
                parser_pos += 1
            self.flags.in_html_comment = True
            return c, 'TK_COMMENT'

        if c == '-' and self.flags.in_html_comment and self.input[parser_pos - 1 : parser_pos + 2] == '-->':
            self.flags.in_html_comment = False
            parser_pos += 2
            if self.wanted_newline:
                self.append_newline()
            return '-->', 'TK_COMMENT'

        if c in self.punct:
            while parser_pos < len(self.input) and c + self.input[parser_pos] in self.punct:
                c += self.input[parser_pos]
                parser_pos += 1
                if parser_pos >= len(self.input):
                    break
            if c == '=':
                return c, 'TK_EQUALS'
            else:
                return c, 'TK_OPERATOR'
        return c, 'TK_UNKNOWN'



    def handle_start_expr(self, token_text):
        if token_text == '[':
            if self.last_type == 'TK_WORD' or self.last_text == ')':
                if self.last_text in self.line_starters:
                    self.append(' ')
                self.set_mode('(EXPRESSION)')
                self.append(token_text)
                return

            if self.flags.mode in ['[EXPRESSION]', '[INDENTED-EXPRESSION]']:
                if self.last_last_text == ']' and self.last_text == ',':
                    # ], [ goes to a new line
                    if self.flags.mode == '[EXPRESSION]':
                        self.flags.mode = '[INDENTED-EXPRESSION]'
                        if not self.opts.keep_array_indentation:
                            self.indent()
                    self.set_mode('[EXPRESSION]')
                    if not self.opts.keep_array_indentation:
                        self.append_newline()
                elif self.last_text == '[':
                    if self.flags.mode == '[EXPRESSION]':
                        self.flags.mode = '[INDENTED-EXPRESSION]'
                        if not self.opts.keep_array_indentation:
                            self.indent()
                    self.set_mode('[EXPRESSION]')

                    if not self.opts.keep_array_indentation:
                        self.append_newline()
                else:
                    self.set_mode('[EXPRESSION]')
            else:
                self.set_mode('[EXPRESSION]')
        else:
            if self.last_text == 'for':
                self.set_mode('(FOR-EXPRESSION)')
            elif self.last_text in ['if', 'while']:
                self.set_mode('(COND-EXPRESSION)')
            else:
                self.set_mode('(EXPRESSION)')


        if self.last_text == ';' or self.last_type == 'TK_START_BLOCK':
            self.append_newline()
        elif self.last_type in ['TK_END_EXPR', 'TK_START_EXPR', 'TK_END_BLOCK'] or self.last_text == '.':
            # do nothing on (( and )( and ][ and ]( and .(
            if self.wanted_newline:
                self.append_newline();
        elif self.last_type not in ['TK_WORD', 'TK_OPERATOR']:
            self.append(' ')
        elif self.last_word == 'function' or self.last_word == 'typeof':
            # function() vs function (), typeof() vs typeof ()
            if self.opts.jslint_happy:
                self.append(' ')
        elif self.last_text in self.line_starters or self.last_text == 'catch':
            self.append(' ')

        self.append(token_text)


    def handle_end_expr(self, token_text):
        if token_text == ']':
            if self.opts.keep_array_indentation:
                if self.last_text == '}':
                    self.remove_indent()
                    self.append(token_text)
                    self.restore_mode()
                    return
            else:
                if self.flags.mode == '[INDENTED-EXPRESSION]':
                    if self.last_text == ']':
                        self.restore_mode()
                        self.append_newline()
                        self.append(token_text)
                        return
        self.restore_mode()
        self.append(token_text)


    def handle_start_block(self, token_text):
        if self.last_word == 'do':
            self.set_mode('DO_BLOCK')
        else:
            self.set_mode('BLOCK')

        if self.opts.brace_style == 'expand':
            if self.last_type != 'TK_OPERATOR':
                if self.last_text == '=' or (self.is_special_word(self.last_text) and self.last_text != 'else'):
                    self.append(' ')
                else:
                    self.append_newline(True)

            self.append(token_text)
            self.indent()
        else:
            if self.last_type not in ['TK_OPERATOR', 'TK_START_EXPR']:
                if self.last_type == 'TK_START_BLOCK':
                    self.append_newline()
                else:
                    self.append(' ')
            else:
                # if TK_OPERATOR or TK_START_EXPR
                if self.is_array(self.flags.previous_mode) and self.last_text == ',':
                    if self.last_last_text == '}':
                        self.append(' ')
                    else:
                        self.append_newline()
            self.indent()
            self.append(token_text)


    def handle_end_block(self, token_text):
        self.restore_mode()
        if self.opts.brace_style == 'expand':
            if self.last_text != '{':
                self.append_newline()
        else:
            if self.last_type == 'TK_START_BLOCK':
                if self.just_added_newline:
                    self.remove_indent()
                else:
                    # {}
                    self.trim_output()
            else:
                if self.is_array(self.flags.mode) and self.opts.keep_array_indentation:
                    self.opts.keep_array_indentation = False
                    self.append_newline()
                    self.opts.keep_array_indentation = True
                else:
                    self.append_newline()

        self.append(token_text)


    def handle_word(self, token_text):
        if self.do_block_just_closed:
            self.append(' ')
            self.append(token_text)
            self.append(' ')
            self.do_block_just_closed = False
            return

        if token_text == 'function':

            if self.flags.var_line:
                self.flags.var_line_reindented = not self.opts.keep_function_indentation
            if (self.just_added_newline or self.last_text == ';') and self.last_text != '{':
                # make sure there is a nice clean space of at least one blank line
                # before a new function definition
                have_newlines = self.n_newlines
                if not self.just_added_newline:
                    have_newlines = 0
                if not self.opts.preserve_newlines:
                    have_newlines = 1
                for i in range(2 - have_newlines):
                    self.append_newline(False)

        if token_text in ['case', 'default']:
            if self.last_text == ':':
                self.remove_indent()
            else:
                self.flags.indentation_level -= 1
                self.append_newline()
                self.flags.indentation_level += 1
            self.append(token_text)
            self.flags.in_case = True
            return

        prefix = 'NONE'

        if self.last_type == 'TK_END_BLOCK':
            if token_text not in ['else', 'catch', 'finally']:
                prefix = 'NEWLINE'
            else:
                if self.opts.brace_style in ['expand', 'end-expand']:
                    prefix = 'NEWLINE'
                else:
                    prefix = 'SPACE'
                    self.append(' ')
        elif self.last_type == 'TK_SEMICOLON' and self.flags.mode in ['BLOCK', 'DO_BLOCK']:
            prefix = 'NEWLINE'
        elif self.last_type == 'TK_SEMICOLON' and self.is_expression(self.flags.mode):
            prefix = 'SPACE'
        elif self.last_type == 'TK_STRING':
            prefix = 'NEWLINE'
        elif self.last_type == 'TK_WORD':
            if self.last_text == 'else':
                # eat newlines between ...else *** some_op...
                # won't preserve extra newlines in this place (if any), but don't care that much
                self.trim_output(True)
            prefix = 'SPACE'
        elif self.last_type == 'TK_START_BLOCK':
            prefix = 'NEWLINE'
        elif self.last_type == 'TK_END_EXPR':
            self.append(' ')
            prefix = 'NEWLINE'

        if self.flags.if_line and self.last_type == 'TK_END_EXPR':
            self.flags.if_line = False

        if token_text in self.line_starters:
            if self.last_text == 'else':
                prefix = 'SPACE'
            else:
                prefix = 'NEWLINE'

            if token_text == 'function' and self.last_text in ['get', 'set']:
                prefix = 'SPACE'

        if token_text in ['else', 'catch', 'finally']:
            if self.last_type != 'TK_END_BLOCK' \
               or self.opts.brace_style == 'expand' \
               or self.opts.brace_style == 'end-expand':
                self.append_newline()
            else:
                self.trim_output(True)
                self.append(' ')
        elif prefix == 'NEWLINE':
            if token_text == 'function' and (self.last_type == 'TK_START_EXPR' or self.last_text in '=,'):
                # no need to force newline on "function" -
                #   (function...
                pass
            elif token_text == 'function' and self.last_text == 'new':
                self.append(' ')
            elif self.is_special_word(self.last_text):
                # no newline between return nnn
                self.append(' ')
            elif self.last_type != 'TK_END_EXPR':
                if (self.last_type != 'TK_START_EXPR' or token_text != 'var') and self.last_text != ':':
                    # no need to force newline on VAR -
                    # for (var x = 0...
                    if token_text == 'if' and self.last_word == 'else' and self.last_text != '{':
                        self.append(' ')
                    else:
                        self.flags.var_line = False
                        self.flags.var_line_reindented = False
                        self.append_newline()
            elif token_text in self.line_starters and self.last_text != ')':
                self.flags.var_line = False
                self.flags.var_line_reindented = False
                self.append_newline()
        elif self.is_array(self.flags.mode) and self.last_text == ',' and self.last_last_text == '}':
            self.append_newline() # }, in lists get a newline
        elif prefix == 'SPACE':
            self.append(' ')


        self.append(token_text)
        self.last_word = token_text

        if token_text == 'var':
            self.flags.var_line = True
            self.flags.var_line_reindented = False
            self.flags.var_line_tainted = False


        if token_text == 'if':
            self.flags.if_line = True

        if token_text == 'else':
            self.flags.if_line = False


    def handle_semicolon(self, token_text):
        self.append(token_text)
        self.flags.var_line = False
        self.flags.var_line_reindented = False
        if self.flags.mode == 'OBJECT':
            # OBJECT mode is weird and doesn't get reset too well.
            self.flags.mode = 'BLOCK'


    def handle_string(self, token_text):
        if self.last_type == 'TK_END_EXPR' and self.flags.previous_mode in ['(COND-EXPRESSION)', '(FOR-EXPRESSION)']:
            self.append(' ')
        if self.last_type in ['TK_STRING', 'TK_START_BLOCK', 'TK_END_BLOCK', 'TK_SEMICOLON']:
            self.append_newline()
        elif self.last_type == 'TK_WORD':
            self.append(' ')

        # Try to replace readable \x-encoded characters with their equivalent,
        # if it is possible (e.g. '\x41\x42\x43\x01' becomes 'ABC\x01').
        def unescape(match):
            block, code = match.group(0, 1)
            char = chr(int(code, 16))
            if block.count('\\') == 1 and char in string.printable:
                return char
            return block

        token_text = re.sub(r'\\{1,2}x([a-fA-F0-9]{2})', unescape, token_text)

        self.append(token_text)

    def handle_equals(self, token_text):
        if self.flags.var_line:
            # just got an '=' in a var-line, different line breaking rules will apply
            self.flags.var_line_tainted = True

        self.append(' ')
        self.append(token_text)
        self.append(' ')


    def handle_operator(self, token_text):
        space_before = True
        space_after = True

        if self.flags.var_line and token_text == ',' and self.is_expression(self.flags.mode):
            # do not break on comma, for ( var a = 1, b = 2
            self.flags.var_line_tainted = False

        if self.flags.var_line and token_text == ',':
            if self.flags.var_line_tainted:
                self.append(token_text)
                self.flags.var_line_reindented = True
                self.flags.var_line_tainted = False
                self.append_newline()
                return
            else:
                self.flags.var_line_tainted = False

        if self.is_special_word(self.last_text):
            # return had a special handling in TK_WORD
            self.append(' ')
            self.append(token_text)
            return

        if token_text == ':' and self.flags.in_case:
            self.append(token_text)
            self.append_newline()
            self.flags.in_case = False
            return

        if token_text == '::':
            # no spaces around the exotic namespacing syntax operator
            self.append(token_text)
            return

        if token_text == ',':
            if self.flags.var_line:
                if self.flags.var_line_tainted:
                    # This never happens, as it's handled previously, right?
                    self.append(token_text)
                    self.append_newline()
                    self.flags.var_line_tainted = False
                else:
                    self.append(token_text)
                    self.append(' ')
            elif self.last_type == 'TK_END_BLOCK' and self.flags.mode != '(EXPRESSION)':
                self.append(token_text)
                if self.flags.mode == 'OBJECT' and self.last_text == '}':
                    self.append_newline()
                else:
                    self.append(' ')
            else:
                if self.flags.mode == 'OBJECT':
                    self.append(token_text)
                    self.append_newline()
                else:
                    # EXPR or DO_BLOCK
                    self.append(token_text)
                    self.append(' ')
            # comma handled
            return
        elif token_text in ['--', '++', '!'] \
                or (token_text in ['+', '-'] \
                    and self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) \
                or self.last_text in self.line_starters:

            space_before = False
            space_after = False

            if self.last_text == ';' and self.is_expression(self.flags.mode):
                # for (;; ++i)
                #         ^^
                space_before = True

            if self.last_type == 'TK_WORD' and self.last_text in self.line_starters:
                space_before = True

            if self.flags.mode == 'BLOCK' and self.last_text in ['{', ';']:
                # { foo: --i }
                # foo(): --bar
                self.append_newline()

        elif token_text == '.':
            # decimal digits or object.property
            space_before = False

        elif token_text == ':':
            if self.flags.ternary_depth == 0:
                self.flags.mode = 'OBJECT'
                space_before = False
            else:
                self.flags.ternary_depth -= 1
        elif token_text == '?':
            self.flags.ternary_depth += 1

        if space_before:
            self.append(' ')

        self.append(token_text)

        if space_after:
            self.append(' ')



    def handle_block_comment(self, token_text):

        lines = token_text.replace('\x0d', '').split('\x0a')
        # all lines start with an asterisk? that's a proper box comment
        if not any(l for l in lines[1:] if ( l.strip() == '' or (l.lstrip())[0] != '*')):
            self.append_newline()
            self.append(lines[0])
            for line in lines[1:]:
                self.append_newline()
                self.append(' ' + line.strip())
        else:
            # simple block comment: leave intact
            if len(lines) > 1:
                # multiline comment starts on a new line
                self.append_newline()
            else:
                # single line /* ... */ comment stays on the same line
                self.append(' ')
            for line in lines:
                self.append(line)
                self.append('\n')
        self.append_newline()


    def handle_inline_comment(self, token_text):
        self.append(' ')
        self.append(token_text)
        if self.is_expression(self.flags.mode):
            self.append(' ')
        else:
            self.append_newline_forced()


    def handle_comment(self, token_text):
        if self.wanted_newline:
            self.append_newline()
        else:
            self.append(' ')

        self.append(token_text)
        self.append_newline_forced()


    def handle_unknown(self, token_text):
        if self.last_text in ['return', 'throw']:
            self.append(' ')

        self.append(token_text)





def main():

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(argv, "s:c:o:djbkil:htf", ['indent-size=','indent-char=','outfile=', 'disable-preserve-newlines',
                                                          'jslint-happy', 'brace-style=',
                                                          'keep-array-indentation', 'indent-level=', 'help',
                                                          'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation'])
    except getopt.GetoptError:
        return usage()

    js_options = default_options()

    file = None
    outfile = 'stdout'
    if len(args) == 1:
        file = args[0]

    for opt, arg in opts:
        if opt in ('--keep-array-indentation', '-k'):
            js_options.keep_array_indentation = True
        if opt in ('--keep-function-indentation','-f'):
            js_options.keep_function_indentation = True
        elif opt in ('--outfile', '-o'):
            outfile = arg
        elif opt in ('--indent-size', '-s'):
            js_options.indent_size = int(arg)
        elif opt in ('--indent-char', '-c'):
            js_options.indent_char = arg
        elif opt in ('--indent-with-tabs', '-t'):
            js_options.indent_with_tabs = True
        elif opt in ('--disable-preserve_newlines', '-d'):
            js_options.preserve_newlines = False
        elif opt in ('--jslint-happy', '-j'):
            js_options.jslint_happy = True
        elif opt in ('--eval-code'):
            js_options.eval_code = True
        elif opt in ('--brace-style', '-b'):
            js_options.brace_style = arg
        elif opt in ('--stdin', '-i'):
            file = '-'
        elif opt in ('--help', '--usage', '-h'):
            return usage()

    if not file:
        return usage()
    else:
        if outfile == 'stdout':
            print(beautify_file(file, js_options))
        else:
            with open(outfile, 'w') as f:
                f.write(beautify_file(file, js_options) + '\n')