-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp_balance_check.js
More file actions
42 lines (42 loc) · 1.88 KB
/
tmp_balance_check.js
File metadata and controls
42 lines (42 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fs = require('fs');
const code = fs.readFileSync('src/App.jsx', 'utf8');
let stack = [];
let line = 1, col = 1;
let inSingle = false, inDouble = false, inTemplate = false, inComment = false, inBlock = false;
for (let i = 0; i < code.length; i++) {
const c = code[i];
const next = code[i+1];
if (c === '\n') { line++; col = 1; if (inComment) inComment = false; continue; }
if (inComment) { col++; continue; }
if (inBlock) {
if (c === '*' && next === '/') { inBlock = false; i++; col += 2; continue; }
col++; continue;
}
if (!inSingle && !inDouble && !inTemplate && c === '/' && next === '/') { inComment = true; i++; col += 2; continue; }
if (!inSingle && !inDouble && !inTemplate && c === '/' && next === '*') { inBlock = true; i++; col += 2; continue; }
if (!inSingle && !inDouble && !inTemplate && c === '`') { inTemplate = true; col++; continue; }
if (inTemplate) { if (c === '`' && code[i-1] !== '\\') inTemplate = false; col++; continue; }
if (!inSingle && !inDouble && !inTemplate && c === '"') { inDouble = true; col++; continue; }
if (inDouble) { if (c === '"' && code[i-1] !== '\\') inDouble = false; col++; continue; }
if (!inSingle && !inDouble && !inTemplate && c === "'") { inSingle = true; col++; continue; }
if (inSingle) { if (c === "'" && code[i-1] !== '\\') inSingle = false; col++; continue; }
if (c === '(' || c === '{' || c === '[') stack.push({ch: c, line, col});
if (c === ')' || c === '}' || c === ']') {
const last = stack.pop();
if (!last) {
console.log('unmatched closing', c, 'at', line, col);
process.exit(1);
}
const match = { ')': '(', '}': '{', ']': '[' };
if (last.ch !== match[c]) {
console.log('mismatch', last, c, line, col);
process.exit(1);
}
}
col++;
}
if (stack.length === 0) {
console.log('stack OK');
} else {
console.log('stack not empty', stack.length, stack.slice(-10));
}