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 theLookahead = EOF;
35 /* isAlphanum -- return true if the character is a letter, digit, underscore,
36 dollar sign, or non-ASCII character.
42 return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
43 (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
48 /* get -- return the next character from stdin. Watch out for lookahead. If
49 the character is a control character, translate it to a space or
61 if (c >= ' ' || c == '\n' || c == EOF) {
71 /* peek -- get the next character without getting it.
82 /* next -- get the next character, excluding comments. peek() is used to see
83 if a '/' is followed by a '/' or '*'.
110 fprintf(stderr, "Error: JSMIN Unterminated comment.\n");
122 /* action -- do something! What you do is determined by the argument:
123 1 Output A. Copy B to A. Get the next B.
124 2 Copy B to A. Get the next B. (Delete A).
125 3 Get the next B. (Delete B).
126 action treats a string as a single character. Wow!
127 action recognizes a regular expression if it is preceded by ( or , or =.
138 if (theA == '\'' || theA == '"') {
147 "Error: JSMIN unterminated string literal: %c\n", theA);
158 if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
159 theA == ':' || theA == '[' || theA == '!' || theA == '&' ||
167 } else if (theA =='\\') {
170 } else if (theA <= '\n') {
172 "Error: JSMIN unterminated Regular Expression literal.\n", theA);
183 /* jsmin -- Copy the input to the output, deleting the characters which are
184 insignificant to JavaScript. Comments will be removed. Tabs will be
185 replaced with spaces. Carriage returns will be replaced with linefeeds.
186 Most spaces and linefeeds will be removed.
194 while (theA != EOF) {
197 if (isAlphanum(theB)) {
216 if (isAlphanum(theB)) {
226 if (isAlphanum(theA)) {
244 if (isAlphanum(theA)) {
260 /* main -- Output any command line arguments as comments
261 and then minify the input.
264 main(int argc, char* argv[])
267 for (i = 1; i < argc; i += 1) {
268 fprintf(stdout, "// %s\n", argv[i]);