WMS_MAIN/check_brackets.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

39 lines
1.8 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const filePath = path.join('app', 'lib', 'printer_data_builder.js');
const code = fs.readFileSync(filePath, 'utf8');
const stack = [];
let isInString = null;
let isInComment = null;
let isEscaped = false;
let line = 1,
column = 1;
for (let i = 0; i < code.length; i++) {
const char = code[i],
nextChar = code[i + 1];
if (isEscaped) { isEscaped = false; if (char === '\n') { line++; column = 1; } else { column++; } continue; }
if (isInString) {
if (char === '\\') isEscaped = true;
else if (char === isInString) isInString = null;
else if (char === '\n') { line++; column = 1; }
} else if (isInComment === '//') {
if (char === '\n') { isInComment = null; line++; column = 1; }
} else if (isInComment === '/*') {
if (char === '*' && nextChar === '/') { isInComment = null; i++; column++; } else if (char === '\n') { line++; column = 1; }
} else {
if (char === '"' || char === "'" || char === '`') isInString = char;
else if (char === '/' && nextChar === '/') { isInComment = '//'; i++; column++; } else if (char === '/' && nextChar === '*') { isInComment = '/*'; i++; column++; } else if (char === '{' || char === '(' || char === '[') stack.push({ char, line, column });
else if (char === '}' || char === ')' || char === ']') {
const last = stack.pop();
const pairs = { '}': '{', ')': '(', ']': '[' };
if (!last || last.char !== pairs[char]) {
console.log(`Unmatched closing symbol "${char}" at line ${line}, column ${column}`);
if (last) stack.push(last);
}
} else if (char === '\n') { line++; column = 1; continue; }
}
column++;
}
stack.forEach(item => console.log(`Unclosed symbol "${item.char}" from line ${item.line}, column ${item.column}`));
if (stack.length === 0) console.log('All symbols are matched.');