4 Copyright (C) 2002 Douglas Crockford (www.crockford.com)
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 of the Software, and to permit persons to whom the Software is furnished to do
11 so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 static int look_ahead
= EOF
;
33 static int the_x
= EOF
;
34 static int the_y
= EOF
;
37 static void error(char* string
) {
38 fputs("JSMIN Error: ", stderr
);
39 fputs(string
, stderr
);
44 /* is_alphanum -- return true if the character is a letter, digit, underscore,
45 dollar sign, or non-ASCII character.
48 static int is_alphanum(int codeunit
) {
50 (codeunit
>= 'a' && codeunit
<= 'z')
51 || (codeunit
>= '0' && codeunit
<= '9')
52 || (codeunit
>= 'A' && codeunit
<= 'Z')
61 /* get -- return the next character from stdin. Watch out for lookahead. If
62 the character is a control character, translate it to a space or
67 int codeunit
= look_ahead
;
69 if (codeunit
== EOF
) {
70 codeunit
= getc(stdin
);
72 if (codeunit
>= ' ' || codeunit
== '\n' || codeunit
== EOF
) {
75 if (codeunit
== '\r') {
82 /* peek -- get the next character without advancing.
91 /* next -- get the next character, excluding comments. peek() is used to see
92 if a '/' is followed by a '/' or '*'.
97 if (codeunit
== '/') {
102 if (codeunit
<= '\n') {
109 while (codeunit
!= ' ') {
118 error("Unterminated comment.");
130 /* action -- do something! What you do is determined by the argument:
131 1 Output A. Copy B to A. Get the next B.
132 2 Copy B to A. Get the next B. (Delete A).
133 3 Get the next B. (Delete B).
134 action treats a string as a single character.
135 action recognizes a regular expression if it is preceded by the likes of
139 static void action(int determined
) {
140 switch (determined
) {
144 (the_y
== '\n' || the_y
== ' ')
145 && (the_a
== '+' || the_a
== '-' || the_a
== '*' || the_a
== '/')
146 && (the_b
== '+' || the_b
== '-' || the_b
== '*' || the_b
== '/')
152 if (the_a
== '\'' || the_a
== '"' || the_a
== '`') {
156 if (the_a
== the_b
) {
164 error("Unterminated string literal.");
170 if (the_b
== '/' && (
171 the_a
== '(' || the_a
== ',' || the_a
== '=' || the_a
== ':'
172 || the_a
== '[' || the_a
== '!' || the_a
== '&' || the_a
== '|'
173 || the_a
== '?' || the_a
== '+' || the_a
== '-' || the_a
== '~'
174 || the_a
== '*' || the_a
== '/' || the_a
== '{' || the_a
== '}'
178 if (the_a
== '/' || the_a
== '*') {
197 "Unterminated set in Regular Expression literal."
201 } else if (the_a
== '/') {
206 "Unterminated set in Regular Expression literal."
210 } else if (the_a
=='\\') {
215 error("Unterminated Regular Expression literal.");
225 /* jsmin -- Copy the input to the output, deleting the characters which are
226 insignificant to JavaScript. Comments will be removed. Tabs will be
227 replaced with spaces. Carriage returns will be replaced with linefeeds.
228 Most spaces and linefeeds will be removed.
231 static void jsmin() {
232 if (peek() == 0xEF) {
239 while (the_a
!= EOF
) {
308 /* main -- Output any command line arguments as comments
309 and then minify the input.
312 extern int main(int argc
, char* argv
[]) {
314 for (i
= 1; i
< argc
; i
+= 1) {
315 fprintf(stdout
, "// %s\n", argv
[i
]);