(no commit message)
[euphorik.git] / tool / jsmin.rb
1 #!/usr/bin/ruby
2 # jsmin.rb 2007-07-20
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
7 # based.
8 #
9 # /* jsmin.c
10 # 2003-04-21
11 #
12 # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
13 #
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:
20 #
21 # The above copyright notice and this permission notice shall be included in all
22 # copies or substantial portions of the Software.
23 #
24 # The Software shall be used for Good, not Evil.
25 #
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
32 # SOFTWARE.
33
34 EOF = -1
35 $theA = ""
36 $theB = ""
37
38 # isAlphanum -- return true if the character is a letter, digit, underscore,
39 # dollar sign, or non-ASCII character
40 def isAlphanum(c)
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)
45 end
46
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.
49 def get()
50 c = $stdin.getc
51 return EOF if(!c)
52 c = c.chr
53 return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
54 return "\n" if (c == "\r")
55 return " "
56 end
57
58 # Get the next character without getting it.
59 def peek()
60 lookaheadChar = $stdin.getc
61 $stdin.ungetc(lookaheadChar)
62 return lookaheadChar.chr
63 end
64
65 # mynext -- get the next character, excluding comments.
66 # peek() is used to see if a '/' is followed by a '/' or '*'.
67 def mynext()
68 c = get
69 if (c == "/")
70 if(peek == "/")
71 while(true)
72 c = get
73 if (c <= "\n")
74 return c
75 end
76 end
77 end
78 if(peek == "*")
79 get
80 while(true)
81 case get
82 when "*"
83 if (peek == "/")
84 get
85 return " "
86 end
87 when EOF
88 raise "Unterminated comment"
89 end
90 end
91 end
92 end
93 return c
94 end
95
96
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 =.
102 def action(a)
103 if(a==1)
104 $stdout.write $theA
105 end
106 if(a==1 || a==2)
107 $theA = $theB
108 if ($theA == "\'" || $theA == "\"")
109 while (true)
110 $stdout.write $theA
111 $theA = get
112 break if ($theA == $theB)
113 raise "Unterminated string literal" if ($theA <= "\n")
114 if ($theA == "\\")
115 $stdout.write $theA
116 $theA = get
117 end
118 end
119 end
120 end
121 if(a==1 || a==2 || a==3)
122 $theB = mynext
123 if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
124 $theA == ":" || $theA == "[" || $theA == "!" ||
125 $theA == "&" || $theA == "|" || $theA == "?" ||
126 $theA == "{" || $theA == "}" || $theA == ";" ||
127 $theA == "\n"))
128 $stdout.write $theA
129 $stdout.write $theB
130 while (true)
131 $theA = get
132 if ($theA == "/")
133 break
134 elsif ($theA == "\\")
135 $stdout.write $theA
136 $theA = get
137 elsif ($theA <= "\n")
138 raise "Unterminated RegExp Literal"
139 end
140 $stdout.write $theA
141 end
142 $theB = mynext
143 end
144 end
145 end
146
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.
151 def jsmin
152 $theA = "\n"
153 action(3)
154 while ($theA != EOF)
155 case $theA
156 when " "
157 if (isAlphanum($theB))
158 action(1)
159 else
160 action(2)
161 end
162 when "\n"
163 case ($theB)
164 when "{","[","(","+","-"
165 action(1)
166 when " "
167 action(3)
168 else
169 if (isAlphanum($theB))
170 action(1)
171 else
172 action(2)
173 end
174 end
175 else
176 case ($theB)
177 when " "
178 if (isAlphanum($theA))
179 action(1)
180 else
181 action(3)
182 end
183 when "\n"
184 case ($theA)
185 when "}","]",")","+","-","\"","\\", "'", '"'
186 action(1)
187 else
188 if (isAlphanum($theA))
189 action(1)
190 else
191 action(3)
192 end
193 end
194 else
195 action(1)
196 end
197 end
198 end
199 end
200
201 ARGV.each do |anArg|
202 $stdout.write "// #{anArg}\n"
203 end
204
205 jsmin