File: Beautifier.pm

package info (click to toggle)
libjavascript-beautifier-perl 0.25-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 204 kB
  • sloc: perl: 1,004; makefile: 7
file content (775 lines) | stat: -rw-r--r-- 28,213 bytes parent folder | download | duplicates (3)
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
package JavaScript::Beautifier;

use warnings;
use strict;
use JavaScript::Packer1 qw/js_packer/;

our $VERSION = '0.25';
our $AUTHORITY = 'cpan:FAYLAND';

use base 'Exporter';
use vars qw/@EXPORT_OK/;
@EXPORT_OK = qw/js_beautify/;

my ( @input, @output, @modes );
my ( $token_text, $last_type, $last_text, $last_last_text, $last_word, $current_mode, $indent_string, $parser_pos, $in_case, $prefix, $token_type, $do_block_just_closed, $var_line, $var_line_tainted, $if_line_flag, $wanted_newline, $just_added_newline );

my @whitespace = split('', "\n\r\t ");
my @wordchar   = split('', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$');
my @digits     = split('', '0123456789');
# <!-- is a special case (ok, it's a minor hack actually)
my @punct      = split(' ', '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= => >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::');
# words which should always start on new line.
my @line_starter = split(',', 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function');

my ( $opt_indent_level, $opt_indent_size, $opt_indent_character, $opt_preserve_newlines, $opt_space_after_anon_function );

sub js_beautify {
    my ( $js_source_code, $opts ) = @_;

    $js_source_code = js_packer($js_source_code);

    $opt_indent_size = $opts->{indent_size} || 4;
    $opt_indent_character = $opts->{indent_character} || ' ';
    $opt_preserve_newlines = exists $opts->{preserve_newlines} ? $opts->{preserve_newlines} : 1;
    $opt_indent_level = $opts->{indent_level} ||= 0;
    $opt_space_after_anon_function = exists $opts->{space_after_anon_function} ? $opts->{space_after_anon_function} : 0;

    # -------------------------------------
    $just_added_newline = 0;
    $indent_string = '';
    while ( $opt_indent_size-- ) {
        $indent_string .= $opt_indent_character;
    }
    @input = split('', $js_source_code);

    $last_word = ''; # last 'TK_WORD' passed
    $last_type = 'TK_START_EXPR'; # last token type
    $last_text = ''; # last token text
    $last_last_text = ''; # pre-last token text
    @output = ();

    $do_block_just_closed = 0;
    $var_line = 0;
    $var_line_tainted = 0;

    # states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
    # some formatting depends on that.
    $current_mode = 'BLOCK';
    @modes = ( $current_mode );

    $parser_pos = 0; # parser position
    $in_case = 0; # flag for parser that case/default has been processed, and next colon needs special attention
    while ( 1 ) {
        my $t = get_next_token($parser_pos);
        $token_text = $t->[0];
        $token_type = $t->[1];
        if ( $token_type eq 'TK_EOF' ) {
            last;
        }

        if ( $token_type eq 'TK_START_EXPR' ) {
            $var_line = 0;

            if ( $token_text eq '[' ) {
                if ( $last_type eq 'TK_WORD' || $last_text eq ')' ) {
                    # this is array index specifier, break immediately
		    # a[x], fn()[x]
                    set_mode('(EXPRESSION)');
                    print_token();
                    $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
                }
                if ( $current_mode eq '[EXPRESSION]' || $current_mode eq '[INDENTED-EXPRESSION]' ) {
                    if ( $last_last_text eq ']' && $last_text eq ',' ) {
                        # ], [ goes to new line
                        indent();
                        print_newline();
                        set_mode('[INDENTED-EXPRESSION]');
                    } elsif ($last_text eq '[') {
                        indent();
                        print_newline();
                        set_mode('[INDENTED-EXPRESSION]');
                    } else {
                        set_mode('[EXPRESSION]');
                    }
                } else {
                    set_mode('[EXPRESSION]');
                }
            } else {
                set_mode('(EXPRESSION)');
            }

            if ( $last_text eq ';' || $last_type eq 'TK_START_BLOCK' ) {
                print_newline();
            } elsif ( $last_type eq 'TK_END_EXPR' || $last_type eq 'TK_START_EXPR' ) {
                # do nothing on (( and )( and ][ and ]( ..
            } elsif ( $last_type ne 'TK_WORD' && $last_type ne 'TK_OPERATOR' ) {
                print_space();
            } elsif ( $last_word eq 'function' ) {
                # function() vs function ()
                if ( $opt_space_after_anon_function ) {
                    print_space();
                }
            } elsif ( grep { $last_word eq $_ } @line_starter ) {
                print_space();
            }
            print_token();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_END_EXPR' ) {
            if ( $token_text eq ']' && $current_mode eq '[INDENTED-EXPRESSION]' ) {
                unindent();
            }
            restore_mode();
            print_token();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_START_BLOCK' ) {
            if ( $last_word eq 'do' ) {
                set_mode('DO_BLOCK');
            } else {
                set_mode('BLOCK');
            }
            if ( $last_type ne 'TK_OPERATOR' && $last_type ne 'TK_START_EXPR' ) {
                if ( $last_type eq 'TK_START_BLOCK' ) {
                    print_newline();
                } else {
                    print_space();
                }
            }
            print_token();
            indent();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_END_BLOCK' ) {
            if ( $last_type eq 'TK_START_BLOCK' ) {
                # nothing
                if ($just_added_newline) {
                    remove_indent();
                    #  {
                    #
                    #  }
                } else {
                    #  {}
                    trim_output();
                }
                unindent();
            } else {
                unindent();
                print_newline();
            }
            print_token();
            restore_mode();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_WORD' ) {
            # no, it's not you. even I have problems understanding how this works
            # and what does what.
            if ( $do_block_just_closed ) {
                print_space();
                print_token();
                print_space();
                $do_block_just_closed = 0;
                $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
            }
            if ( $token_text eq 'case' || $token_text eq 'default' ) {
                if ( $last_text eq ':' ) {
                    # switch cases following one another
                    remove_indent();
                } else {
                    # case statement starts in the same line where switch
                    unindent();
                    print_newline();
                    indent();
                }
                print_token();
                $in_case = 1;
                $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
            }
            $prefix = 'NONE';
            if ( $last_type eq 'TK_END_BLOCK' ) {
                if ( not (grep { lc($token_text) eq $_ } ('else', 'catch', 'finally')) ) {
                    $prefix = 'NEWLINE';
                } else {
                    $prefix = 'SPACE';
                    print_space();
                }
            } elsif ( $last_type eq 'TK_SEMICOLON' && ( $current_mode eq 'BLOCK' || $current_mode eq 'DO_BLOCK' ) ) {
                $prefix = 'NEWLINE';
            } elsif ( $last_type eq 'TK_SEMICOLON' && is_expression($current_mode) ) {
                $prefix = 'SPACE';
            } elsif ( $last_type eq 'TK_STRING') {
                $prefix = 'NEWLINE';
            } elsif ( $last_type eq 'TK_WORD' ) {
                $prefix = 'SPACE';
            } elsif ( $last_type eq 'TK_START_BLOCK') {
                $prefix = 'NEWLINE';
            } elsif ( $last_type eq 'TK_END_EXPR' ) {
                print_space();
                $prefix = 'NEWLINE';
            }

            if ( $last_type ne 'TK_END_BLOCK' && (grep { lc($token_text) eq $_ } ('else', 'catch', 'finally')) ) {
                print_newline();
            } elsif ( (grep { $token_text eq $_ } @line_starter) || $prefix eq 'NEWLINE' ) {
                if ( $last_text eq 'else' ) {
                    # no need to force newline on else break
                    print_space();
                } elsif ( ( $last_type eq 'TK_START_EXPR' || $last_text eq '=' || $last_text eq ',' ) && $token_text eq 'function' ) {
                    # no need to force newline on 'function': (function
                    # DONOTHING
                } elsif ( $last_text eq 'return' || $last_text eq 'throw' ) {
                    # no newline between 'return nnn'
                    print_space();
                } elsif ( $last_type ne 'TK_END_EXPR' ) {
                    if ( ($last_type ne 'TK_START_EXPR' || $token_text ne 'var') && $last_text ne ':' ) {
                        # no need to force newline on 'var': for (var x = 0...)
                        if ( $token_text eq 'if' && $last_word eq 'else' && $last_text ne '{' ) {
                            # no newline for } else if {
                            print_space();
                        }  else {
                            print_newline();
                        }
                    }
                } else {
                    if ( (grep { $token_text eq $_ } @line_starter) && $last_text ne ')' ) {
                        print_newline();
                    }
                }
            } elsif ( $prefix eq 'SPACE' ) {
                print_space();
            }
            print_token();
            $last_word = $token_text;
            if ( $token_text eq 'var' ) {
                $var_line = 1;
                $var_line_tainted = 0;
            }
            if ( $token_text eq 'if' || $token_text eq 'else' ) {
                $if_line_flag = 1;
            }
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_SEMICOLON' ) {
            print_token();
            $var_line = 0;
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_STRING' ) {
            if ( $last_type eq 'TK_START_BLOCK' || $last_type eq 'TK_END_BLOCK' || $last_type eq 'TK_SEMICOLON' ) {
                print_newline();
            } elsif ( $last_type eq 'TK_WORD' ) {
                print_space();
            }
            print_token();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ( $token_type eq 'TK_OPERATOR' ) {
            my $start_delim = 1;
            my $end_delim = 1;
            if ( $var_line && $token_text ne ',' ) {
                $var_line_tainted = 1;
                if ( $token_text eq ':' ) {
                    $var_line = 0;
                }
            }
            if ($var_line && $token_text eq ',' && is_expression($current_mode) ) {
                # do not break on comma, for(var a = 1, b = 2)
                $var_line_tainted = 0;
            }
            if ( $token_text eq ':' && $in_case ) {
                print_token(); # colon really asks for separate treatment
                print_newline();
                $in_case = 0;
                $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
            }
            if ( $token_text eq '::' ) {
                # no spaces around exotic namespacing syntax operator
                print_token();
                $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
            }

            if ( $token_text eq ',' ) {
                if ($var_line) {
                    if ( $var_line_tainted ) {
                        print_token();
                        print_newline();
                        $var_line_tainted = 0;
                    } else {
                        print_token();
                        print_space();
                    }
                } elsif ( $last_type eq 'TK_END_BLOCK' ) {
                    print_token();
                    print_newline();
                } else {
                    if ( $current_mode eq 'BLOCK' ) {
                        print_token();
                        print_newline();
                    } else {
                        # EXPR or DO_BLOCK
                        print_token();
                        print_space();
                    }
                }
                $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
            } elsif ( $token_text eq '--' || $token_text eq '++' ) { # unary operators special case
                if ( $last_text eq ';' ) {
                    if ( $current_mode eq 'BLOCK') {
                        # { foo; --i }
                        print_newline();
                        $start_delim = 1;
                        $end_delim = 0;
                    } else {
                        # space for (;; ++i)
                        $start_delim = 1;
                        $end_delim = 0;
                    }
                } else {
                    if ($last_text eq '{') {
                        # {--i
                        print_newline();
                    }
                    $start_delim = 0;
                    $end_delim = 0;
                }
            } elsif ( ( $token_text eq '!' || $token_text eq '+' || $token_text eq '-' ) && ($last_text eq 'return' || $last_text eq 'case') ) {
                $start_delim = 1;
                $end_delim = 0;
            } elsif ( ( $token_text eq '!' || $token_text eq '+' || $token_text eq '-' ) && $last_type eq 'TK_START_EXPR' ) {
                # special case handling: if (!a)
                $start_delim = 0;
                $end_delim = 0;
            } elsif ( $last_type eq 'TK_OPERATOR' ) {
                $start_delim = 0;
                $end_delim = 0;
            } elsif ( $last_type eq 'TK_END_EXPR' ) {
                $start_delim = 1;
                $end_delim = 1;
            } elsif ( $token_text eq '.' ) {
                # decimal digits or object.property
                $start_delim = 0;
                $end_delim = 0;
            } elsif ( $token_text eq ':' ) {
                if (is_ternary_op()) {
                    $start_delim = 1;
                } else {
                    $start_delim = 0;
                }
            }
            if ( $start_delim ) {
                print_space();
            }
            print_token();
            if ( $end_delim ) {
                print_space();
            }
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ($token_type eq 'TK_BLOCK_COMMENT') {
            print_newline();
            print_token();
            print_newline();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ($token_type eq 'TK_COMMENT') {
            # print_newline();
            print_space();
            print_token();
            print_newline();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        } elsif ($token_type eq 'TK_UNKNOWN') {
            print_token();
            $last_last_text = $last_text;$last_type = $token_type;$last_text = $token_text;next;
        }
        $last_type = $token_type;
        $last_text = $token_text;
    }

    my $output = join('', @output);
    $output =~ s/\n+$//;
    return $output;
}

sub trim_output {
    while ( scalar @output && ( $output[ scalar @output - 1 ] eq ' ' || $output[ scalar @output - 1 ] eq $indent_string ) ) {
        pop @output;
    }
}
sub print_newline {
    my ( $ignore_repeated ) = @_;
    $ignore_repeated = 1 unless defined $ignore_repeated;
    $if_line_flag = 0;
    trim_output();

    if ( not scalar @output ) {
        return; # no newline on start of file
    }

    if ( $output[ scalar @output - 1 ] ne "\n" || ! $ignore_repeated ) {
        $just_added_newline = 1;
        push @output, "\n";
    }
    foreach my $i ( 0 .. $opt_indent_level - 1 ) {
        push @output, $indent_string;
    }
}

sub print_space {
    my $last_output = ' ';
    $last_output = $output[ scalar @output - 1 ] if scalar @output;
    if ( $last_output ne ' ' && $last_output ne "\n" && $last_output ne $indent_string ) { # prevent occassional duplicate space
        push @output, ' ';
    }
}

sub print_token {
    $just_added_newline = 0;
    push @output, $token_text;
}
sub indent {
    $opt_indent_level++;
}
sub unindent {
    if ( $opt_indent_level ) {
        $opt_indent_level--;
    }
}
sub remove_indent {
    if ( scalar @output && $output[ scalar @output - 1 ] eq $indent_string ) {
        pop @output;
    }
}
sub set_mode {
    my $mode = shift;
    push @modes, $current_mode;
    $current_mode = $mode;
}
sub is_expression {
    my $mode = shift;
    return ($mode eq '[EXPRESSION]' || $mode eq '[INDENTED-EXPRESSION]' || $mode eq '(EXPRESSION)') ? 1 : 0;
}
sub restore_mode {
    $do_block_just_closed = ( $current_mode eq 'DO_BLOCK' ) ? 1 : 0;
    $current_mode = pop @modes;
}

# Walk backwards from the colon to find a '?' (colon is part of a ternary op)
# or a '{' (colon is part of a class literal). Along the way, keep track of
# the blocks and expressions we pass so we only trigger on those chars in our
# own level, and keep track of the colons so we only trigger on the matching '?'.
sub is_ternary_op {
    my $level = 0;
    my $colon_count = 0;
    foreach my $o (reverse @output) {
        if ( $o eq ':' ) {
            if ( $level == 0 ) {
                $colon_count++;
            }
            next;
        } elsif ( $o eq '?' ) {
            if ( $level == 0 ) {
                if ( $colon_count == 0 ) {
                    return 1;
                } else {
                    $colon_count--;
                }
            }
            next;
        } elsif ( $o eq '{' ) {
            if ( $level == 0 ) {
                return 0;
            }
            $level--;
            next;
        }
        if ( $o eq '(' or $o eq '[' ) {
            $level--;
            next;
        } elsif ( $o eq ')' or $o eq ']' or $o eq '}' ) {
            $level++;
            next;
        }
    }
}

sub get_next_token {
    my $n_newlines = 0;

    if ( $parser_pos >= scalar @input ) {
        return ['', 'TK_EOF'];
    }

    my $c = $input[$parser_pos];
    $parser_pos++;

    while ( grep { $_ eq $c } @whitespace ) {
        if ( $parser_pos >= scalar @input ) {
            return ['', 'TK_EOF'];
        }
        if ( $c eq "\n" ) {
            $n_newlines += 1;
        }
        $c = $input[$parser_pos];
        $parser_pos++;
    };
    $wanted_newline = 0;
    if ( $opt_preserve_newlines ) {
        if ( $n_newlines > 1 ) {
            foreach my $i ( 0 .. 1 ) {
                my $flag = ( $i == 0 ) ? 1 : 0;
                print_newline( $flag );
            }
        }
        $wanted_newline = ($n_newlines == 1) ? 1 : 0;
    }
    if ( grep { $c eq $_ } @wordchar ) {
        if ( $parser_pos < scalar @input ) {
            while ( grep { $input[$parser_pos] eq $_ } @wordchar ) {
                $c .= $input[$parser_pos];
                $parser_pos++;
                if ( $parser_pos == scalar @input ) {
                    last;
                }
            }
        }

        # small and surprisingly unugly hack for 1E-10 representation
        if ( $parser_pos != scalar @input && $c =~ /^[0-9]+[Ee]$/ && ($input[$parser_pos] eq '-' || $input[$parser_pos] eq '+') ) {
            my $sign = $input[$parser_pos];
            $parser_pos++;
            my $t = get_next_token($parser_pos);
            $c .= $sign . $t->[0];
            return [$c, 'TK_WORD'];
        }
        if ( $c eq 'in' ) { # hack for 'in' operator
            return [$c, 'TK_OPERATOR'];
        }
        if ( $wanted_newline && $last_type ne 'TK_OPERATOR' && not $if_line_flag ) {
            print_newline();
        }
        return [$c, 'TK_WORD'];
    }
    if ( $c eq '(' || $c eq '[' ) {
        return [$c, 'TK_START_EXPR'];
    }
    if ( $c eq ')' || $c eq ']' ) {
        return [$c, 'TK_END_EXPR'];
    }
    if ( $c eq '{' ) {
        return [$c, 'TK_START_BLOCK'];
    }
    if ( $c eq '}' ) {
        return [$c, 'TK_END_BLOCK'];
    }
    if ( $c eq ';' ) {
        return [$c, 'TK_SEMICOLON'];
    }
    if ( $c eq '/' ) {
        my $comment;
        # peek for comment /* ... */
        if ( $input[$parser_pos] eq '*' ) {
            $parser_pos++;
            if ( $parser_pos < scalar @input ) {
                while (not ( $input[$parser_pos] eq '*' && $input[$parser_pos+1] && $input[$parser_pos+1] eq '/') && $parser_pos < scalar @input ) {
                    $comment .= $input[$parser_pos];
                    $parser_pos++;
                    if ( $parser_pos >= scalar @input ) {
                        last;
                    }
                }
            }
            $parser_pos += 2;
            return ['/*' . $comment . '*/', 'TK_BLOCK_COMMENT'];
        }
        # peek for comment // ...
        if ( $input[$parser_pos] eq '/' ) {
            $comment = $c;
            while ( $input[$parser_pos] ne "\x0d" && $input[$parser_pos] ne "\x0a" ) {
                $comment .= $input[$parser_pos];
                $parser_pos++;
                if ( $parser_pos >= scalar @input ) {
                    last;
                }
            }
            $parser_pos++;
            if ($wanted_newline) {
                print_newline();
            }
            return [$comment, 'TK_COMMENT'];
        }
    }
    if ( $c eq "'" || # string
         $c eq '"' || # string
        ($c eq '/' &&
        (( $last_type eq 'TK_WORD' && $last_text eq 'return') ||
         ( $last_type eq 'TK_START_EXPR' || $last_type eq 'TK_START_BLOCK' || $last_type eq 'TK_END_BLOCK' || $last_type eq 'TK_OPERATOR' || $last_type eq 'TK_EOF' || $last_type eq 'TK_SEMICOLON')))) { # regexp
         my $sep = $c;
         my $esc = 0;
         my $resulting_string = $c;
         if ( $parser_pos < scalar @input ) {
            if ( $sep eq '/') {
                # handle regexp separately...
                my $in_char_class = 0;
                while ( $esc || $in_char_class || $input[$parser_pos] ne $sep ) {
                    $resulting_string .= $input[$parser_pos];
                    if ( not $esc ) {
                        $esc = ( $input[$parser_pos] eq '\\' ) ? 1 : 0;
                        if ( $input[$parser_pos] eq '[' ) {
                            $in_char_class = 1;
                        } elsif ( $input[$parser_pos] eq ']' ) {
                            $in_char_class = 0;
                        }
                    } else {
                        $esc = 0;
                    }
                    $parser_pos++;
                    if ( $parser_pos >= scalar @input ) {
                        # incomplete string/rexp when end-of-file reached.
                        # bail out with what had been received so far.
                        return [$resulting_string, 'TK_STRING'];
                     }
                 }

             } else {
                # and handle string also separately
                while ( $esc || $input[$parser_pos] ne $sep ) {
                    $resulting_string .= $input[$parser_pos];
                    if ( not $esc ) {
                        $esc = ( $input[$parser_pos] eq '\\' ) ? 1 : 0;
                    } else {
                        $esc = 0;
                    }
                    $parser_pos++;
                    if ( $parser_pos >= scalar @input ) {
                        # incomplete string/rexp when end-of-file reached.
                        # bail out with what had been received so far.
                        return [$resulting_string, 'TK_STRING'];
                     }
                }
            }
         }
         $parser_pos++;
         $resulting_string .= $sep;
         if ( $sep eq '/' ) {
             # regexps may have modifiers /regexp/MOD , so fetch those, too
             while ( $parser_pos < scalar @input && (grep { $input[$parser_pos] eq $_ } @wordchar) ) {
                 $resulting_string .= $input[$parser_pos];
                 $parser_pos++;
            }
        }
        return [$resulting_string, 'TK_STRING'];
    }

    if ($c eq '#') {
        # 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
        my $sharp = '#';
        if ($parser_pos < scalar @input && (grep { $input[$parser_pos] eq $_ } @digits) ) {
            do {
                $c = $input[$parser_pos];
                $sharp .= $c;
                $parser_pos += 1;
            } while ($parser_pos < scalar @input && $c ne '#' && $c ne '=');
            if ($c eq '#') {
                return [$sharp, 'TK_WORD'];
            } else {
                return [$sharp, 'TK_OPERATOR'];
            }
        }
    }

    if ($c eq '<' && $parser_pos + 2 < scalar @input && join('', @input[$parser_pos-1 .. $parser_pos+2]) eq '<!--') {
        $parser_pos += 3;
        return ['<!--', 'TK_COMMENT'];
    }
    if ($c eq '-' && $parser_pos + 1 < scalar @input && join('', @input[$parser_pos-1 .. $parser_pos+1]) eq '-->') {
        $parser_pos += 2;
        if ($wanted_newline) {
            print_newline();
        }
        return ['-->', 'TK_COMMENT'];
    }

    if ( grep { $c eq $_ } @punct ) {
         while ( $parser_pos < scalar @input && (grep { $c . $input[$parser_pos] eq $_ } @punct) ) {
             $c .= $input[$parser_pos];
             $parser_pos++;
             last if ( $parser_pos >= scalar @input );
         }
         return [$c, 'TK_OPERATOR'];
    }
    return [$c, 'TK_UNKNOWN'];
}

1;
__END__

=head1 NAME

JavaScript::Beautifier - Beautify Javascript (beautifier for javascript)

=head1 SYNOPSIS

    use JavaScript::Beautifier qw/js_beautify/;

    my $pretty_js = js_beautify( $js_source_code, {
        indent_size => 4,
        indent_character => ' ',
    } );

=head1 DESCRIPTION

This module is mostly a Perl-rewrite of L<http://github.com/einars/js-beautify/tree/master/beautify.js>

You can check it through L<http://jsbeautifier.org/>

=head1 FUNCTIONS

$js_source_code = <<'EOF';
  a = 12;
  {return '\\w+';}
EOF

=head2 js_beautify( $js_source_code, $opts );

Beautify javascript source code contained in a string with the included options, described below.

=head3 Options

=over 4

=item indent_size

=item indent_character

if you prefer Tab than Space, try:

    {
        indent_size => 1,
        indent_character => "\t",
    }

=item preserve_newlines

Default is 1

    my $in = "var\na=dont_preserve_newlines";
    my $out = "var a = dont_preserve_newlines";
    my $js = js_beautify( $in, { preserve_newlines => 0 } );
    # $out eq $js
    $in = "var\na=do_preserve_newlines";
    $out = "var\na = do_preserve_newlines";
    $js = js_beautify( $in, { preserve_newlines => 1 } );
    # $out eq $js

=item space_after_anon_function

Default is 0

=back

=head1 AUTHOR

Fayland Lam, C<< <fayland at gmail.com> >>

=head1 COPYRIGHT & LICENSE

Copyright 2008-2018 Fayland Lam, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut