const fs = require('fs'); const s = fs.readFileSync('methods/system.json', 'utf8'); const stack = []; let inStr = false; function isEscaped(str, idx) { let cnt = 0; let j = idx - 1; while (j >= 0 && str[j] == '\\') { cnt++; j--; } return cnt % 2 === 1; } for (let i = 0; i < s.length; i++) { const c = s[i]; if (c === '"' && !isEscaped(s, i)) { inStr = !inStr; } if (inStr) continue; if (c === '{' || c === '[') stack.push({ c, pos: i }); else if (c === '}') { if (!stack.length || stack[stack.length - 1].c !== '{') { console.log('UNMATCHED } at', i); break; } stack.pop(); } else if (c === ']') { if (!stack.length || stack[stack.length - 1].c !== '[') { console.log('UNMATCHED ] at', i); break; } stack.pop(); } } if (stack.length) { console.log('STACK NOT EMPTY len', stack.length); stack.forEach(x => { console.log('---STACK ITEM---'); console.log(x.c, x.pos); console.log(s.slice(Math.max(0, x.pos - 120), x.pos + 120)); }); } else { console.log('ALL MATCHED'); }