Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Better minification of regex preceder pattern, fixed misoptimization …
Browse files Browse the repository at this point in the history
…of keyword lists, and other minification tweaks.
  • Loading branch information
[email protected] committed Jun 1, 2011
1 parent db3a755 commit 4aceceb
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 398 deletions.
95 changes: 65 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
CLOSURE_COMPILER=java -jar closure-compiler/compiler.jar \
--warning_level VERBOSE \
--charset UTF-8 \
--compilation_level ADVANCED_OPTIMIZATIONS \
--output_wrapper '(function(){%output%})()'
--language_in ECMASCRIPT5 \
--compilation_level ADVANCED_OPTIMIZATIONS

YUI_COMPRESSOR=java -jar yui-compressor/yuicompressor-2.4.4.jar \
--charset UTF-8

src/prettify.js: js-modules/*.js
if [ -e $@ ]; then chmod +w $@; fi
perl -e '\
TAR_ROOT=distrib/google-code-prettify

all: src/prettify.js distrib

clean:
rm -rf distrib.tstamp distrib src/prettify.js

src/prettify.js: js-modules/*.js js-modules/*.pl
@if [ -e $@ ]; then chmod +w $@; fi
@perl -e '\
sub readInclude($$$$) {\
my $$prefix = $$_[0];\
my $$name = "js-modules/" . $$_[1];\
my $$buf = "";\
open(IN, "<$$name") or die "$$name: $$!";\
if ($$name =~ /\.pl$$/) {\
open(IN, "|perl $$name") or die "$$name: $$!";\
} else {\
open(IN, "<$$name") or die "$$name: $$!";\
}\
while (<IN>) {\
$$buf .= "$$prefix$$_";\
}\
Expand All @@ -24,33 +35,57 @@ src/prettify.js: js-modules/*.js
js-modules/prettify.js \
> src/prettify.js \
|| rm src/prettify.js
if [ -e $@ ]; then chmod -w $@; fi
@if [ -e $@ ]; then chmod -w $@; fi

distrib: distrib.tstamp distrib/google-code-prettify-minimized.tgz
distrib: distrib.tstamp distrib/google-code-prettify-minimized.tgz \
distrib/google-code-prettify-minimized.tar.bz2
@wc -c distrib/google-code-prettify-minimized.{tar.bz2,tgz} \
| grep -v total

distrib.tstamp: src/*.js src/*.css
mkdir -p distrib/google-code-prettify
for f in src/*.css; do \
$(YUI_COMPRESSOR) --type css $$f > distrib/google-code-prettify/$$(basename $$f); \
@echo Compiling
@mkdir -p $(TAR_ROOT)
@for f in src/*.css; do \
$(YUI_COMPRESSOR) --type css $$f \
> $(TAR_ROOT)/$$(basename $$f); \
wc -c $$f $(TAR_ROOT)/$$(basename $$f) \
| grep -v total; \
done
$(CLOSURE_COMPILER) --js src/prettify.js --externs closure-compiler/console-externs.js \
> distrib/google-code-prettify/prettify.js
for f in src/lang*.js; do \
$(CLOSURE_COMPILER) --js $$f --externs js-modules/externs.js \
| perl -pe 's/\bPR\.PR_ATTRIB_NAME\b/"atn"/g; \
s/\bPR\.PR_ATTRIB_VALUE\b/"atv"/g; \
s/\bPR\.PR_COMMENT\b/"com"/g; \
s/\bPR\.PR_DECLARATION\b/"dec"/g; \
s/\bPR\.PR_KEYWORD\b/"kwd"/g; \
s/\bPR\.PR_LITERAL\b/"lit"/g; \
s/\bPR\.PR_PLAIN\b/"pln"/g; \
s/\bPR\.PR_PUNCTUATION\b/"pun"/g; \
s/\bPR\.PR_STRING\b/"str"/g; \
s/\bPR\.PR_TAG\b/"tag"/g; \
s/\bPR\.PR_TYPE\b/"typ"/g;' \
> distrib/google-code-prettify/$$(basename $$f); \
@$(CLOSURE_COMPILER) --js src/prettify.js \
--externs closure-compiler/console-externs.js \
| perl -e 'binmode STDIN, ":utf8";' -pe 's/\xA0/\\xa0/' \
> $(TAR_ROOT)/prettify.js
@wc -c src/prettify.js $(TAR_ROOT)/prettify.js \
| grep -v total
@for f in src/lang*.js; do \
if [ $$f -nt $(TAR_ROOT)/$$(basename $$f) ]; then \
$(CLOSURE_COMPILER) --js $$f --externs js-modules/externs.js \
| perl -e 'binmode STDIN, ":utf8";' -pe 's/\xA0/\\xa0/' \
| perl -pe 's/\bPR\.PR_ATTRIB_NAME\b/"atn"/g; \
s/\bPR\.PR_ATTRIB_VALUE\b/"atv"/g; \
s/\bPR\.PR_COMMENT\b/"com"/g; \
s/\bPR\.PR_DECLARATION\b/"dec"/g; \
s/\bPR\.PR_KEYWORD\b/"kwd"/g; \
s/\bPR\.PR_LITERAL\b/"lit"/g; \
s/\bPR\.PR_PLAIN\b/"pln"/g; \
s/\bPR\.PR_PUNCTUATION\b/"pun"/g; \
s/\bPR\.PR_STRING\b/"str"/g; \
s/\bPR\.PR_TAG\b/"tag"/g; \
s/\bPR\.PR_TYPE\b/"typ"/g;' \
> $(TAR_ROOT)/$$(basename $$f); \
wc -c $$f $(TAR_ROOT)/$$(basename $$f) \
| grep -v total; \
fi \
done
touch distrib.tstamp
@touch distrib.tstamp

%.tgz: %.tar
@gzip -c -9 $^ > $@

%.tar.bz2: %.tar
@bzip2 -9f $^

distrib/google-code-prettify-minimized.tgz: distrib/google-code-prettify/*
pushd distrib; tar cfz google-code-prettify-minimized.tgz google-code-prettify; popd
distrib/google-code-prettify-minimized.tar: distrib.tstamp
@pushd distrib >& /dev/null; \
tar cf ../$@ google-code-prettify; \
popd >& /dev/null
82 changes: 44 additions & 38 deletions js-modules/combinePrefixPatterns.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
* matches the union of the sets of strings matched by the input RegExp.
Expand All @@ -24,22 +23,30 @@ function combinePrefixPatterns(regexs) {
}
}

var escapeCharToCodeUnit = {
'b': 8,
't': 9,
'n': 0xa,
'v': 0xb,
'f': 0xc,
'r': 0xd
};

function decodeEscape(charsetPart) {
if (charsetPart.charAt(0) !== '\\') { return charsetPart.charCodeAt(0); }
switch (charsetPart.charAt(1)) {
case 'b': return 8;
case 't': return 9;
case 'n': return 0xa;
case 'v': return 0xb;
case 'f': return 0xc;
case 'r': return 0xd;
case 'u': case 'x':
return parseInt(charsetPart.substring(2), 16)
|| charsetPart.charCodeAt(1);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7':
return parseInt(charsetPart.substring(1), 8);
default: return charsetPart.charCodeAt(1);
var cc0 = charsetPart.charCodeAt(0);
if (cc0 !== 92 /* \\ */) {
return cc0;
}
var c1 = charsetPart.charAt(1);
cc0 = escapeCharToCodeUnit[c1];
if (cc0) {
return cc0;
} else if ('0' <= c1 && c1 <= '7') {
return parseInt(charsetPart.substring(1), 8);
} else if (c1 === 'u' || c1 === 'x') {
return parseInt(charsetPart.substring(2), 16);
} else {
return charsetPart.charCodeAt(1);
}
}

Expand Down Expand Up @@ -70,30 +77,29 @@ function combinePrefixPatterns(regexs) {
var inverse = charsetParts[0] === '^';
for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
var p = charsetParts[i];
switch (p) {
case '\\B': case '\\b':
case '\\D': case '\\d':
case '\\S': case '\\s':
case '\\W': case '\\w':
groups.push(p);
continue;
}
var start = decodeEscape(p);
var end;
if (i + 2 < n && '-' === charsetParts[i + 1]) {
end = decodeEscape(charsetParts[i + 2]);
i += 2;
if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups.
groups.push(p);
} else {
end = start;
}
ranges.push([start, end]);
// If the range might intersect letters, then expand it.
if (!(end < 65 || start > 122)) {
if (!(end < 65 || start > 90)) {
ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
var start = decodeEscape(p);
var end;
if (i + 2 < n && '-' === charsetParts[i + 1]) {
end = decodeEscape(charsetParts[i + 2]);
i += 2;
} else {
end = start;
}
if (!(end < 97 || start > 122)) {
ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
ranges.push([start, end]);
// If the range might intersect letters, then expand it.
// This case handling is too simplistic.
// It does not deal with non-latin case folding.
// It works for latin source code identifiers though.
if (!(end < 65 || start > 122)) {
if (!(end < 65 || start > 90)) {
ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
}
if (!(end < 97 || start > 122)) {
ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions js-modules/extractSourceSpans.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* It will produce the output:</p>
* <pre>
* {
* source: "print 'Hello '\n + 'World';",
* sourceCode: "print 'Hello '\n + 'World';",
* // 1 2
* // 012345678901234 5678901234567
* spans: [0, #1, 6, #2, 14, #3, 15, #4]
Expand Down Expand Up @@ -93,7 +93,7 @@ function extractSourceSpans(node) {
walk(node);

return {
source: chunks.join('').replace(/\n$/, ''),
sourceCode: chunks.join('').replace(/\n$/, ''),
spans: spans
};
}
Loading

0 comments on commit 4aceceb

Please sign in to comment.