Merge pull request #3697 from howard0su/jsmin_update
luci-base: import latest version of jsmin
This commit is contained in:
commit
77ba1cfb2c
1 changed files with 150 additions and 123 deletions
|
@ -1,7 +1,7 @@
|
||||||
/* jsmin.c
|
/* jsmin.c
|
||||||
2011-09-30
|
2019-10-30
|
||||||
|
|
||||||
Copyright (c) 2002 Douglas Crockford (www.crockford.com)
|
Copyright (C) 2002 Douglas Crockford (www.crockford.com)
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
@ -27,21 +27,34 @@ SOFTWARE.
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static int theA;
|
static int the_a;
|
||||||
static int theB;
|
static int the_b;
|
||||||
static int theLookahead = EOF;
|
static int look_ahead = EOF;
|
||||||
|
static int the_x = EOF;
|
||||||
|
static int the_y = EOF;
|
||||||
|
|
||||||
|
|
||||||
/* isAlphanum -- return true if the character is a letter, digit, underscore,
|
static void error(char* string) {
|
||||||
|
fputs("JSMIN Error: ", stderr);
|
||||||
|
fputs(string, stderr);
|
||||||
|
fputc('\n', stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* is_alphanum -- return true if the character is a letter, digit, underscore,
|
||||||
dollar sign, or non-ASCII character.
|
dollar sign, or non-ASCII character.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int is_alphanum(int codeunit) {
|
||||||
isAlphanum(int c)
|
return (
|
||||||
{
|
(codeunit >= 'a' && codeunit <= 'z')
|
||||||
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
|| (codeunit >= '0' && codeunit <= '9')
|
||||||
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
|
|| (codeunit >= 'A' && codeunit <= 'Z')
|
||||||
c > 126);
|
|| codeunit == '_'
|
||||||
|
|| codeunit == '$'
|
||||||
|
|| codeunit == '\\'
|
||||||
|
|| codeunit > 126
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,32 +63,28 @@ isAlphanum(int c)
|
||||||
linefeed.
|
linefeed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int get() {
|
||||||
get()
|
int codeunit = look_ahead;
|
||||||
{
|
look_ahead = EOF;
|
||||||
int c = theLookahead;
|
if (codeunit == EOF) {
|
||||||
theLookahead = EOF;
|
codeunit = getc(stdin);
|
||||||
if (c == EOF) {
|
|
||||||
c = getc(stdin);
|
|
||||||
}
|
}
|
||||||
if (c >= ' ' || c == '\n' || c == EOF) {
|
if (codeunit >= ' ' || codeunit == '\n' || codeunit == EOF) {
|
||||||
return c;
|
return codeunit;
|
||||||
}
|
}
|
||||||
if (c == '\r') {
|
if (codeunit == '\r') {
|
||||||
return '\n';
|
return '\n';
|
||||||
}
|
}
|
||||||
return ' ';
|
return ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* peek -- get the next character without getting it.
|
/* peek -- get the next character without advancing.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int peek() {
|
||||||
peek()
|
look_ahead = get();
|
||||||
{
|
return look_ahead;
|
||||||
theLookahead = get();
|
|
||||||
return theLookahead;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -83,39 +92,38 @@ peek()
|
||||||
if a '/' is followed by a '/' or '*'.
|
if a '/' is followed by a '/' or '*'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int next() {
|
||||||
next()
|
int codeunit = get();
|
||||||
{
|
if (codeunit == '/') {
|
||||||
int c = get();
|
|
||||||
if (c == '/') {
|
|
||||||
switch (peek()) {
|
switch (peek()) {
|
||||||
case '/':
|
case '/':
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = get();
|
codeunit = get();
|
||||||
if (c <= '\n') {
|
if (codeunit <= '\n') {
|
||||||
return c;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
get();
|
get();
|
||||||
for (;;) {
|
while (codeunit != ' ') {
|
||||||
switch (get()) {
|
switch (get()) {
|
||||||
case '*':
|
case '*':
|
||||||
if (peek() == '/') {
|
if (peek() == '/') {
|
||||||
get();
|
get();
|
||||||
return ' ';
|
codeunit = ' ';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EOF:
|
case EOF:
|
||||||
fprintf(stderr, "Error: JSMIN Unterminated comment.\n");
|
error("Unterminated comment.");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
break;
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c;
|
the_y = the_x;
|
||||||
|
the_x = codeunit;
|
||||||
|
return codeunit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,77 +131,92 @@ next()
|
||||||
1 Output A. Copy B to A. Get the next B.
|
1 Output A. Copy B to A. Get the next B.
|
||||||
2 Copy B to A. Get the next B. (Delete A).
|
2 Copy B to A. Get the next B. (Delete A).
|
||||||
3 Get the next B. (Delete B).
|
3 Get the next B. (Delete B).
|
||||||
action treats a string as a single character. Wow!
|
action treats a string as a single character.
|
||||||
action recognizes a regular expression if it is preceded by ( or , or =.
|
action recognizes a regular expression if it is preceded by the likes of
|
||||||
|
'(' or ',' or '='.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void
|
static void action(int determined) {
|
||||||
action(int d)
|
switch (determined) {
|
||||||
{
|
|
||||||
switch (d) {
|
|
||||||
case 1:
|
case 1:
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
|
if (
|
||||||
|
(the_y == '\n' || the_y == ' ')
|
||||||
|
&& (the_a == '+' || the_a == '-' || the_a == '*' || the_a == '/')
|
||||||
|
&& (the_b == '+' || the_b == '-' || the_b == '*' || the_b == '/')
|
||||||
|
) {
|
||||||
|
putc(the_y, stdout);
|
||||||
|
}
|
||||||
case 2:
|
case 2:
|
||||||
theA = theB;
|
the_a = the_b;
|
||||||
if (theA == '\'' || theA == '"' || theA == '`') {
|
if (the_a == '\'' || the_a == '"' || the_a == '`') {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
theA = get();
|
the_a = get();
|
||||||
if (theA == theB) {
|
if (the_a == the_b) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (theA == '\\') {
|
if (the_a == '\\') {
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
theA = get();
|
the_a = get();
|
||||||
}
|
}
|
||||||
if (theA == EOF) {
|
if (the_a == EOF) {
|
||||||
fprintf(stderr, "Error: JSMIN unterminated string literal.");
|
error("Unterminated string literal.");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 3:
|
case 3:
|
||||||
theB = next();
|
the_b = next();
|
||||||
if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
|
if (the_b == '/' && (
|
||||||
theA == ':' || theA == '[' || theA == '!' ||
|
the_a == '(' || the_a == ',' || the_a == '=' || the_a == ':'
|
||||||
theA == '&' || theA == '|' || theA == '?' ||
|
|| the_a == '[' || the_a == '!' || the_a == '&' || the_a == '|'
|
||||||
theA == '{' || theA == '}' || theA == ';' ||
|
|| the_a == '?' || the_a == '+' || the_a == '-' || the_a == '~'
|
||||||
theA == '\n')) {
|
|| the_a == '*' || the_a == '/' || the_a == '{' || the_a == '}'
|
||||||
putc(theA, stdout);
|
|| the_a == ';'
|
||||||
putc(theB, stdout);
|
)) {
|
||||||
|
putc(the_a, stdout);
|
||||||
|
if (the_a == '/' || the_a == '*') {
|
||||||
|
putc(' ', stdout);
|
||||||
|
}
|
||||||
|
putc(the_b, stdout);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
theA = get();
|
the_a = get();
|
||||||
if (theA == '[') {
|
if (the_a == '[') {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
theA = get();
|
the_a = get();
|
||||||
if (theA == ']') {
|
if (the_a == ']') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (theA == '\\') {
|
if (the_a == '\\') {
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
theA = get();
|
the_a = get();
|
||||||
}
|
}
|
||||||
if (theA == EOF) {
|
if (the_a == EOF) {
|
||||||
fprintf(stderr,
|
error(
|
||||||
"Error: JSMIN unterminated set in Regular Expression literal.\n");
|
"Unterminated set in Regular Expression literal."
|
||||||
exit(1);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (theA == '/') {
|
} else if (the_a == '/') {
|
||||||
|
switch (peek()) {
|
||||||
|
case '/':
|
||||||
|
case '*':
|
||||||
|
error(
|
||||||
|
"Unterminated set in Regular Expression literal."
|
||||||
|
);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
} else if (theA =='\\') {
|
} else if (the_a =='\\') {
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
theA = get();
|
the_a = get();
|
||||||
}
|
}
|
||||||
if (theA == EOF) {
|
if (the_a == EOF) {
|
||||||
fprintf(stderr,
|
error("Unterminated Regular Expression literal.");
|
||||||
"Error: JSMIN unterminated Regular Expression literal.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
putc(theA, stdout);
|
putc(the_a, stdout);
|
||||||
}
|
}
|
||||||
theB = next();
|
the_b = next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,51 +228,56 @@ action(int d)
|
||||||
Most spaces and linefeeds will be removed.
|
Most spaces and linefeeds will be removed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void
|
static void jsmin() {
|
||||||
jsmin()
|
if (peek() == 0xEF) {
|
||||||
{
|
get();
|
||||||
theA = '\n';
|
get();
|
||||||
action(3);
|
get();
|
||||||
while (theA != EOF) {
|
|
||||||
switch (theA) {
|
|
||||||
case ' ':
|
|
||||||
if (isAlphanum(theB)) {
|
|
||||||
action(1);
|
|
||||||
} else {
|
|
||||||
action(2);
|
|
||||||
}
|
}
|
||||||
|
the_a = '\n';
|
||||||
|
action(3);
|
||||||
|
while (the_a != EOF) {
|
||||||
|
switch (the_a) {
|
||||||
|
case ' ':
|
||||||
|
action(
|
||||||
|
is_alphanum(the_b)
|
||||||
|
? 1
|
||||||
|
: 2
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
switch (theB) {
|
switch (the_b) {
|
||||||
case '{':
|
case '{':
|
||||||
case '[':
|
case '[':
|
||||||
case '(':
|
case '(':
|
||||||
case '+':
|
case '+':
|
||||||
case '-':
|
case '-':
|
||||||
|
case '!':
|
||||||
|
case '~':
|
||||||
action(1);
|
action(1);
|
||||||
break;
|
break;
|
||||||
case ' ':
|
case ' ':
|
||||||
action(3);
|
action(3);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isAlphanum(theB)) {
|
action(
|
||||||
action(1);
|
is_alphanum(the_b)
|
||||||
} else {
|
? 1
|
||||||
action(2);
|
: 2
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
switch (theB) {
|
switch (the_b) {
|
||||||
case ' ':
|
case ' ':
|
||||||
if (isAlphanum(theA)) {
|
action(
|
||||||
action(1);
|
is_alphanum(the_a)
|
||||||
break;
|
? 1
|
||||||
}
|
: 3
|
||||||
action(3);
|
);
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
switch (theA) {
|
switch (the_a) {
|
||||||
case '}':
|
case '}':
|
||||||
case ']':
|
case ']':
|
||||||
case ')':
|
case ')':
|
||||||
|
@ -261,11 +289,11 @@ jsmin()
|
||||||
action(1);
|
action(1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isAlphanum(theA)) {
|
action(
|
||||||
action(1);
|
is_alphanum(the_a)
|
||||||
} else {
|
? 1
|
||||||
action(3);
|
: 3
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -280,9 +308,8 @@ jsmin()
|
||||||
/* main -- Output any command line arguments as comments
|
/* main -- Output any command line arguments as comments
|
||||||
and then minify the input.
|
and then minify the input.
|
||||||
*/
|
*/
|
||||||
extern int
|
|
||||||
main(int argc, char* argv[])
|
extern int main(int argc, char* argv[]) {
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < argc; i += 1) {
|
for (i = 1; i < argc; i += 1) {
|
||||||
fprintf(stdout, "// %s\n", argv[i]);
|
fprintf(stdout, "// %s\n", argv[i]);
|
||||||
|
|
Loading…
Reference in a new issue