3 # Author: Uladzislau Latynski
4 # This work is a translation from C to Ruby of jsmin.c published by
5 # Douglas Crockford. Permission is hereby granted to use the Ruby
6 # version under the same conditions as the jsmin.c on which it is
12 # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
14 # Permission is hereby granted, free of charge, to any person obtaining a copy of
15 # this software and associated documentation files (the "Software"), to deal in
16 # the Software without restriction, including without limitation the rights to
17 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18 # of the Software, and to permit persons to whom the Software is furnished to do
19 # so, subject to the following conditions:
21 # The above copyright notice and this permission notice shall be included in all
22 # copies or substantial portions of the Software.
24 # The Software shall be used for Good, not Evil.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 # isAlphanum -- return true if the character is a letter, digit, underscore,
39 # dollar sign, or non-ASCII character
41 return false if !c
|| c
== EOF
42 return ((c
>= 'a' && c
<= 'z') || (c
>= '0' && c
<= '9') ||
43 (c
>= 'A' && c
<= 'Z') || c
== '_' || c
== '$' ||
44 c
== '\\' || c
[0] > 126)
47 # get -- return the next character from stdin. Watch out for lookahead. If
48 # the character is a control character, translate it to a space or linefeed.
53 return c
if (c
>= " " || c
== "\n" || c
.unpack("c") == EOF
)
54 return "\n" if (c
== "\r")
58 # Get the next character without getting it.
60 lookaheadChar
= $stdin.getc
61 $stdin.ungetc(lookaheadChar
)
62 return lookaheadChar
.chr
65 # mynext -- get the next character, excluding comments.
66 # peek() is used to see if a '/' is followed by a '/' or '*'.
88 raise "Unterminated comment"
97 # action -- do something! What you do is determined by the argument: 1
98 # Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B.
99 # (Delete A). 3 Get the next B. (Delete B). action treats a string as a
100 # single character. Wow! action recognizes a regular expression if it is
101 # preceded by ( or , or =.
108 if ($theA == "\'" || $theA == "\"")
112 break if ($theA == $theB)
113 raise "Unterminated string literal" if ($theA <= "\n")
121 if(a
==1 || a
==2 || a
==3)
123 if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
124 $theA == ":" || $theA == "[" || $theA == "!" ||
125 $theA == "&" || $theA == "|" || $theA == "?" ||
126 $theA == "{" || $theA == "}" || $theA == ";" ||
134 elsif ($theA == "\\")
137 elsif ($theA <= "\n")
138 raise "Unterminated RegExp Literal
"
147 # jsmin -- Copy the input to the output, deleting the characters which are
148 # insignificant to JavaScript. Comments will be removed. Tabs will be
149 # replaced with spaces. Carriage returns will be replaced with linefeeds.
150 # Most spaces and linefeeds will be removed.
157 if (isAlphanum($theB))
164 when "{","[","(","+","-"
169 if (isAlphanum($theB))
178 if (isAlphanum($theA))
185 when "}","]",")","+
","-","\"","\\", "'", '"'
188 if (isAlphanum($theA))
202 $stdout.write "// #{anArg}\n"