MOD passage du README dans le wiki
[euphorik.git] / tools / 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 # saute les commentaires (également les lignes commencant pas ;;)
70
71 if (c == ";" and peek == ";")
72 while(true)
73 c = get
74 if (c[0] <= "\n"[0])
75 return c
76 end
77 end
78 end
79 if (c == "/")
80 prochain = peek
81 if(prochain == "/")
82 while(true)
83 c = get
84 if (c[0] <= "\n"[0])
85 return c
86 end
87 end
88 end
89 if(peek == "*")
90 get
91 while(true)
92 case get
93 when "*"
94 if (peek == "/")
95 get
96 return " "
97 end
98 when EOF
99 raise "Unterminated comment"
100 end
101 end
102 end
103 end
104 return c
105 end
106
107
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 =.
113 def action(a)
114 if(a==1)
115 $stdout.write $theA
116 end
117 if(a==1 || a==2)
118 $theA = $theB
119 if ($theA == "\'" || $theA == "\"")
120 while (true)
121 $stdout.write $theA
122 $theA = get
123 break if ($theA == $theB)
124 raise "Unterminated string literal" if ($theA <= "\n")
125 if ($theA == "\\")
126 $stdout.write $theA
127 $theA = get
128 end
129 end
130 end
131 end
132 if(a==1 || a==2 || a==3)
133 $theB = mynext
134 if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
135 $theA == ":" || $theA == "[" || $theA == "!" ||
136 $theA == "&" || $theA == "|" || $theA == "?" ||
137 $theA == "{" || $theA == "}" || $theA == ";" ||
138 $theA == "\n"))
139 $stdout.write $theA
140 $stdout.write $theB
141 while (true)
142 $theA = get
143 if ($theA == "/")
144 break
145 elsif ($theA == "\\")
146 $stdout.write $theA
147 $theA = get
148 elsif ($theA <= "\n")
149 raise "Unterminated RegExp Literal"
150 end
151 $stdout.write $theA
152 end
153 $theB = mynext
154 end
155 end
156 end
157
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.
162 def jsmin
163 $theA = "\n"
164 action(3)
165 while ($theA != EOF)
166 case $theA
167 when " "
168 if (isAlphanum($theB))
169 action(1)
170 else
171 action(2)
172 end
173 when "\n"
174 case ($theB)
175 when "{","[","(","+","-"
176 action(1)
177 when " "
178 action(3)
179 else
180 if (isAlphanum($theB))
181 action(1)
182 else
183 action(2)
184 end
185 end
186 else
187 case ($theB)
188 when " "
189 if (isAlphanum($theA))
190 action(1)
191 else
192 action(3)
193 end
194 when "\n"
195 case ($theA)
196 when "}","]",")","+","-","\"","\\", "'", '"'
197 action(1)
198 else
199 if (isAlphanum($theA))
200 action(1)
201 else
202 action(3)
203 end
204 end
205 else
206 action(1)
207 end
208 end
209 end
210 end
211
212 ARGV.each do |anArg|
213 $stdout.write "// #{anArg}\n"
214 end
215
216 jsmin