jsmin.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* jsmin.c
  2. 2012-04-15
  3. Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. The Software shall be used for Good, not Evil.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. static int theA;
  24. static int theB;
  25. static int theLookahead = EOF;
  26. /* isAlphanum -- return true if the character is a letter, digit, underscore,
  27. dollar sign, or non-ASCII character.
  28. */
  29. static int
  30. isAlphanum(int c)
  31. {
  32. return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  33. (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
  34. c > 126);
  35. }
  36. /* get -- return the next character from stdin. Watch out for lookahead. If
  37. the character is a control character, translate it to a space or
  38. linefeed.
  39. */
  40. static int
  41. get()
  42. {
  43. int c = theLookahead;
  44. theLookahead = EOF;
  45. if (c == EOF) {
  46. c = getc(stdin);
  47. }
  48. if (c >= ' ' || c == '\n' || c == EOF) {
  49. return c;
  50. }
  51. if (c == '\r') {
  52. return '\n';
  53. }
  54. return ' ';
  55. }
  56. /* peek -- get the next character without getting it.
  57. */
  58. static int
  59. peek()
  60. {
  61. theLookahead = get();
  62. return theLookahead;
  63. }
  64. /* next -- get the next character, excluding comments. peek() is used to see
  65. if a '/' is followed by a '/' or '*'.
  66. */
  67. static int
  68. next()
  69. {
  70. int c = get();
  71. if (c == '/') {
  72. switch (peek()) {
  73. case '/':
  74. for (;;) {
  75. c = get();
  76. if (c <= '\n') {
  77. return c;
  78. }
  79. }
  80. case '*':
  81. get();
  82. for (;;) {
  83. switch (get()) {
  84. case '*':
  85. if (peek() == '/') {
  86. get();
  87. return ' ';
  88. }
  89. break;
  90. case EOF:
  91. fprintf(stderr, "Error: JSMIN Unterminated comment.\n");
  92. exit(1);
  93. }
  94. }
  95. default:
  96. return c;
  97. }
  98. }
  99. return c;
  100. }
  101. /* action -- do something! What you do is determined by the argument:
  102. 1 Output A. Copy B to A. Get the next B.
  103. 2 Copy B to A. Get the next B. (Delete A).
  104. 3 Get the next B. (Delete B).
  105. action treats a string as a single character. Wow!
  106. action recognizes a regular expression if it is preceded by ( or , or =.
  107. */
  108. static void
  109. action(int d)
  110. {
  111. switch (d) {
  112. case 1:
  113. putc(theA, stdout);
  114. case 2:
  115. theA = theB;
  116. if (theA == '\'' || theA == '"' || theA == '`') {
  117. for (;;) {
  118. putc(theA, stdout);
  119. theA = get();
  120. if (theA == theB) {
  121. break;
  122. }
  123. if (theA == '\\') {
  124. putc(theA, stdout);
  125. theA = get();
  126. }
  127. if (theA == EOF) {
  128. fprintf(stderr, "Error: JSMIN unterminated string literal.");
  129. exit(1);
  130. }
  131. }
  132. }
  133. case 3:
  134. theB = next();
  135. if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
  136. theA == ':' || theA == '[' || theA == '!' ||
  137. theA == '&' || theA == '|' || theA == '?' ||
  138. theA == '{' || theA == '}' || theA == ';' ||
  139. theA == '\n')) {
  140. putc(theA, stdout);
  141. putc(theB, stdout);
  142. for (;;) {
  143. theA = get();
  144. if (theA == '[') {
  145. for (;;) {
  146. putc(theA, stdout);
  147. theA = get();
  148. if (theA == ']') {
  149. break;
  150. }
  151. if (theA == '\\') {
  152. putc(theA, stdout);
  153. theA = get();
  154. }
  155. if (theA == EOF) {
  156. fprintf(stderr,
  157. "Error: JSMIN unterminated set in Regular Expression literal.\n");
  158. exit(1);
  159. }
  160. }
  161. } else if (theA == '/') {
  162. break;
  163. } else if (theA =='\\') {
  164. putc(theA, stdout);
  165. theA = get();
  166. }
  167. if (theA == EOF) {
  168. fprintf(stderr,
  169. "Error: JSMIN unterminated Regular Expression literal.\n");
  170. exit(1);
  171. }
  172. putc(theA, stdout);
  173. }
  174. theB = next();
  175. }
  176. }
  177. }
  178. /* jsmin -- Copy the input to the output, deleting the characters which are
  179. insignificant to JavaScript. Comments will be removed. Tabs will be
  180. replaced with spaces. Carriage returns will be replaced with linefeeds.
  181. Most spaces and linefeeds will be removed.
  182. */
  183. static void
  184. jsmin()
  185. {
  186. if (peek() == 0xEF) {
  187. get();
  188. get();
  189. get();
  190. }
  191. theA = '\n';
  192. action(3);
  193. while (theA != EOF) {
  194. switch (theA) {
  195. case ' ':
  196. if (isAlphanum(theB)) {
  197. action(1);
  198. } else {
  199. action(2);
  200. }
  201. break;
  202. case '\n':
  203. switch (theB) {
  204. case '{':
  205. case '[':
  206. case '(':
  207. case '+':
  208. case '-':
  209. case '!':
  210. case '~':
  211. action(1);
  212. break;
  213. case ' ':
  214. action(3);
  215. break;
  216. default:
  217. if (isAlphanum(theB)) {
  218. action(1);
  219. } else {
  220. action(2);
  221. }
  222. }
  223. break;
  224. default:
  225. switch (theB) {
  226. case ' ':
  227. if (isAlphanum(theA)) {
  228. action(1);
  229. break;
  230. }
  231. action(3);
  232. break;
  233. case '\n':
  234. switch (theA) {
  235. case '}':
  236. case ']':
  237. case ')':
  238. case '+':
  239. case '-':
  240. case '"':
  241. case '\'':
  242. case '`':
  243. action(1);
  244. break;
  245. default:
  246. if (isAlphanum(theA)) {
  247. action(1);
  248. } else {
  249. action(3);
  250. }
  251. }
  252. break;
  253. default:
  254. action(1);
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. /* main -- Output any command line arguments as comments
  261. and then minify the input.
  262. */
  263. extern int
  264. main(int argc, char* argv[])
  265. {
  266. int i;
  267. for (i = 1; i < argc; i += 1) {
  268. fprintf(stdout, "// %s\n", argv[i]);
  269. }
  270. jsmin();
  271. return 0;
  272. }