(Body Snatcher) Pass 3
This commit is contained in:
1 parent
d819146009
commit
832f3a3345
36 files changed
+6416
No files matched your search
@@ -0,0 +1,45 @@
|
||||
const fs=require('fs');
|
||||
const src=fs.readFileSync('index.html','utf8');
|
||||
const m=src.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i);
|
||||
const code=m[1];
|
||||
const chars=[...code];
|
||||
// mode stack: bottom is 'code'
|
||||
let mode=['code'];
|
||||
let braceStack=[]; // {line, kind:'brace'|'interp'}
|
||||
let parenStack=[];
|
||||
const fileOff = src.indexOf('<script');
|
||||
const fileLineBase = src.slice(0,fileOff).split('\n').length;
|
||||
function L(idx){ // 1-based file line from code char index
|
||||
let n=1; for(let k=0;k<idx;k++) if(code[k]==='\n') n++; return n+fileLineBase-1; }
|
||||
let i=0;
|
||||
const N=chars.length;
|
||||
let errs=[];
|
||||
while(i<N){
|
||||
const c=chars[i], n=chars[i+1];
|
||||
const top=mode[mode.length-1];
|
||||
if(top==='lc'){ if(c==='\n') mode.pop(); i++; continue; }
|
||||
if(top==='bc'){ if(c==='*'&&n==='/'){mode.pop();i+=2;continue;} i++; continue; }
|
||||
if(top==='sq'){ if(c==='\\'){i+=2;continue;} if(c==="'"){mode.pop();i++;continue;} i++; continue; }
|
||||
if(top==='dq'){ if(c==='\\'){i+=2;continue;} if(c==='"'){mode.pop();i++;continue;} i++; continue; }
|
||||
if(top==='tmp'){
|
||||
if(c==='\\'){i+=2;continue;}
|
||||
if(c==='`'){mode.pop();i++;continue;}
|
||||
if(c==='$'&&n==='{'){ braceStack.push({line:L(i),kind:'interp'}); mode.push('code'); i+=2; continue; }
|
||||
i++; continue;
|
||||
}
|
||||
// top==='code'
|
||||
if(c==='/'&&n==='/'){ mode.push('lc'); i+=2; continue; }
|
||||
if(c==='/'&&n==='*'){ mode.push('bc'); i+=2; continue; }
|
||||
if(c==="'"){ mode.push('sq'); i++; continue; }
|
||||
if(c==='"'){ mode.push('dq'); i++; continue; }
|
||||
if(c==='`'){ mode.push('tmp'); i++; continue; }
|
||||
if(c==='{'){ braceStack.push({line:L(i),kind:'brace'}); i++; continue; }
|
||||
if(c==='}'){ if(!braceStack.length){errs.push('extra } at '+L(i));} else braceStack.pop(); i++; continue; }
|
||||
if(c==='('){ parenStack.push(L(i)); i++; continue; }
|
||||
if(c===')'){ if(parenStack.length)parenStack.pop(); else errs.push('extra ) at '+L(i)); i++; continue; }
|
||||
i++;
|
||||
}
|
||||
console.log('mode stack leftover:', mode);
|
||||
console.log('errs:', errs.slice(0,5));
|
||||
console.log('unclosed {/interp (last 15):', braceStack.slice(-15).map(b=>b.kind+'@'+b.line));
|
||||
console.log('unclosed ( (last 8):', parenStack.slice(-8));
|
||||
Reference in new issue
Block a user