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 '*'.
69 # saute les commentaires (également les lignes commencant pas ;;)
71 if (c
== ";" and peek
== ";")
99 raise "Unterminated comment"
108 # action -- do something! What you do is determined by the argument: 1
109 # Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B.
110 # (Delete A). 3 Get the next B. (Delete B). action treats a string as a
111 # single character. Wow! action recognizes a regular expression if it is
112 # preceded by ( or , or =.
119 if ($theA == "\'" || $theA == "\"")
123 break if ($theA == $theB)
124 raise "Unterminated string literal" if ($theA <= "\n")
132 if(a
==1 || a
==2 || a
==3)
134 if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
135 $theA == ":" || $theA == "[" || $theA == "!" ||
136 $theA == "&" || $theA == "|" || $theA == "?" ||
137 $theA == "{" || $theA == "}" || $theA == ";" ||
145 elsif ($theA == "\\")
148 elsif ($theA <= "\n")
149 raise "Unterminated RegExp Literal
"
158 # jsmin -- Copy the input to the output, deleting the characters which are
159 # insignificant to JavaScript. Comments will be removed. Tabs will be
160 # replaced with spaces. Carriage returns will be replaced with linefeeds.
161 # Most spaces and linefeeds will be removed.
168 if (isAlphanum($theB))
175 when "{","[","(","+","-"
180 if (isAlphanum($theB))
189 if (isAlphanum($theA))
196 when "}","]",")","+
","-","\"","\\", "'", '"'
199 if (isAlphanum($theA))
213 $stdout.write "// #{anArg}\n"