Skip to content

Instantly share code, notes, and snippets.

@Constellation
Created March 28, 2010 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Constellation/346579 to your computer and use it in GitHub Desktop.
Save Constellation/346579 to your computer and use it in GitHub Desktop.
function trimComment(str){
var lines = str.split(/\n|\r\n/);
var end = lines.length-1;
var current = 0;
var buffer = lines[current];
var exps = [];
// 現在primary expressionの立ち居地にあるのかどうかをtestする
// 直前のものしか調べてないから, 正確性に欠けるかも...
function isPrimary(exps){
var len = exps.length;
if(len === 0){
return true;
}
// len !== 0
if(exps[len-1].type === 'IDENT'){
var val = exps[len-1].val;
return val === 'new' || val === 'typeof' || val === 'void' || val === 'delete' || val === 'instanceof' || val === 'in' || val === 'case' || 'throw' || val === 'do' || val === 'else' || val === 'return';
}
if(exps[len-1].type === 'OP'){
var val = exps[len-1].val;
return val === '+' ||
val === '-' ||
val === '!' ||
val === '*' ||
val === '/' ||
val === '%' ||
val === '~' ||
val === '>>' ||
val === '<<' ||
val === '>>>'||
val === '>' ||
val === '<' ||
val === '<=' ||
val === '>=' ||
val === '==' ||
val === '!=' ||
val === '==='||
val === '!=='||
val === '&' ||
val === '^' ||
val === '|' ||
val === '&&' ||
val === '||' ||
val === ':' ||
val === '?' ||
val === ',' ||
val === '=' ||
val === '*=' ||
val === '/=' ||
val === '%=' ||
val === '+=' ||
val === '-=' ||
val === '>>=' ||
val === '<<=' ||
val === '>>>=' ||
val === '&=' ||
val === '^=' ||
val === '|=' ||
val === '[' ||
val === '}' ||
val === ';' ||
val === ')' ||
val === '(';
}
return false;
}
var comment = false;
var single_comment = false;
var invalid = false;
var result = [];
var match = null;
while(true){
if(!buffer){
if(current === end){
break;
}
current+=1;
buffer = lines[current];
if(!comment && !single_comment)
result.push("\n");
} else if(comment){
if(/\*\//.test(buffer)){
buffer = RegExp.rightContext;
comment = false;
} else {
buffer = null;
}
} else {
if(/^[ \t\r\n]+/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
} else if(/^\//.test(buffer)){
// 正規表現は/や*から始まらない RegularExpressionFirstChar
// よって即座に判断可能
if(/^\/\//.test(buffer)){
buffer = null;
} else if(/^\/\*/.test(buffer)){
buffer = RegExp.rightContext;
comment = true;
} else {
// 正規表現の可能性がある
// 現在PrimaryExpressionがこれるかどうか
if(isPrimary(exps)){
if(/^\/(?:[^\/\\]|\\.)?(?:\[.*\]|[^\/\\]|\\.)*\/[igIG]*/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "REGEXP",
val : RegExp.lastMatch
});
} else {
invalid = true;
break;
}
} else if(/^\/=|^\//.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "OP",
val : RegExp.lastMatch
});
}
}
} else if(/^"(?:[^"\\]|\\.)*"|^'(?:[^'\\]|\\.)*'/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "STRING",
val : RegExp.lastMatch
});
} else if(/^0x[0-9A-Fa-f]+|^[0-9]*\.[0-9]+(?:e|E)(?:\-|\+|)[0-9]+|^[0-9]+(?:e|E)(?:\-|\+|)[0-9]+|^[0-9]*\.[0-9]+|^[0-9]+/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "NUMBER",
val : RegExp.lastMatch
});
} else if(/^[_\$A-Za-z][_\$0-9A-Za-z]*/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "IDENT",
val : RegExp.lastMatch
});
} else if(/^===|^!==|^>>>=|^>>>|^>>=|^<<=|^\-\-|^\+\+|^>>|^\|\||^\|=|^\^=|^>=|^==|^<=|^&=|^&&|^!=|^\+=|^\/=|^\-=|^\*=|^%=|^<<|^\}|^\||^\{|^\^|^\[|^\?|^=|^;|^:|^,|^\(|^&|^!|^~|^\+|^>|^<|^\]|^\/|^.|^\*|^\)|^%/.test(buffer)){
result.push(RegExp.lastMatch);
buffer = RegExp.rightContext;
exps.push({
type: "OP",
val : RegExp.lastMatch
});
} else {
invalid = true;
break;
}
}
}
return (invalid)? "invalid result" : result.join("");
}
var val = <><![CDATA[
function ok(){
// single line comment
var comment = "これは//こめんとじゃないよ!"
/* multiple comment */
/*
*
*
*/
var i = /.\/*/;// 正規表現...
print(i);
}
]]></>.trim();
print("===============ORIGINAL=================");
print(val);
print("================TRIMED==================");
print(trimComment(val));
print(trimComment('hel/*as/[/*]/df*/lo /\\/*hello[*/]///asdf'));
print("var i = ~~/0[/*]/; // asa");
print(trimComment("var i = +/0[/*]/; // asa"));
print(trimComment("var i = 10;if(true){ i *= /0[/*]/;}; i = 0 * /0[/*]/;// asa"));
print("ORIGINAL");
print("var i = 100 / 10 / [1,2,3][~~/[/*]/] // hogeeee");
print("TRIMED");
print(trimComment("var i = 100 / 10 / [1,2,3][~~/[/*]/] // hogeeee"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment