This bitty bit of code takes JavaScript and makes it smaller. It could do the wrong thing if it misparsed a division operator as a regular expression. JavaScript::Squish was misparsing a regexp so I wrote this instead.
I sorted list rules to check for syntax by frequency within my code. Here's my table:
| Name | Count |
|---|---|
| WhiteSpace | 3621 |
| * | 3281 |
| "..." | 231 |
| // ... | 166 |
| /* ... */ | 80 |
| '...' | 60 |
| /.../ | 5 |
| division | 1 |
#!/usr/bin/perl $/ = undef; print smoosh( scalar <> ); use constant DEBUG => !!$ENV{DEBUG}; use constant { PRE => ( DEBUG ? '<' : '' ), POST => ( DEBUG ? '>' : '' ), }; my ( $UnicodeEscapeSequence, $IdentifierStart, $WhiteSpace, $LineTermi +nator ); BEGIN { $UnicodeEscapeSequence = '\u[\da-fA-F]{4}'; $IdentifierStart = '$_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{ +Pc}'; $WhiteSpace = '\t\x0b\f \xa0\p{Zs}'; $LineTerminator = '\r\n\x{2028}\x{2029}'; } sub smoosh { local $_ = shift @_; my $out = ''; my %stat if DEBUG; while ( not m{\G\z}gc ) { if (0) { } # whitespace elsif (m{\G[$WhiteSpace]+}gco) { DEBUG and ++$stat{WhiteSpace}; my $pos = pos; # Insert whitespace only if I'm joining things that could # be identifiers. if (( $out =~ /[$IdentifierStart]\z/o or $out =~ /$UnicodeEscapeSequence\z/o ) and ( m{\G[$IdentifierStart]}gco or m{\G$UnicodeEscapeSequence}gco ) ) { $out .= ' '; } pos() = $pos; } # everything else elsif (m{\G[^'"/$LineTerminator$WhiteSpace]+}gco) { DEBUG and ++$stat{'*'}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # line endings. Must be separated from other whitespace # because somtimes there are implicit semicolons here. elsif (m{\G[$LineTerminator]+}gco) { DEBUG and ++$stat{LineTerminator}; if ( $out !~ /[$LineTerminator]\z/o ) { $out .= "\n"; } } # String literals elsif (m{\G"(?:[^\\"$LineTerminator]*|\\.)*"}sgco) { DEBUG and ++$stat{'"StringLiteral"'}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } elsif (m{\G'(?:[^\\'$LineTerminator]*|\\.)*'}sgco) { DEBUG and ++$stat{q['StringLiteral']}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # Comments elsif (m{\G//[^$LineTerminator]*[$LineTerminator]}gco) { DEBUG and ++$stat{SingleLineComment}; } elsif (m{\G/\*(?:[^*\\]*|\\.)*\*/}sgc) { DEBUG and ++$stat{MultiLineComment}; } # Regexps elsif (m{\G/(?:[^/\\$LineTerminator]*|\\.)+/}gco) { DEBUG and ++$stat{RegularExpressionLiteral}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # Division elsif (m{\G/(?![/*])}gc) { DEBUG and ++$stat{Division}; $out .= PRE . '/' . POST; } else { my $pos = pos; my $line = 1 + tr/\x0a\x0d\x2028\x2029//; die 'Error parsing JavaScript: "' . substr( $_, pos(), 20 ) . q[" at line $line.\n]; } } if (DEBUG) { print "STATS:\n"; for ( sort { $stat{$b} <=> $stat{$a} } keys %stat ) { print " $_: $stat{$_}\n"; } } return $out; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: JavaScript Smoosher
by hossman (Prior) on Mar 05, 2007 at 00:24 UTC | |
by diotalevi (Canon) on Mar 05, 2007 at 00:39 UTC |