31 lines
1.8 KiB
JavaScript
31 lines
1.8 KiB
JavaScript
// This is a machine-family guard, not a replacement for the LinuxCNC parser.
|
|
// Ignore comments, named parameters, O labels and bracket expression contents;
|
|
// inspect address words at block level, including zero and parameter values.
|
|
export function programMachineMismatch(source, coordinates, path = "") {
|
|
if (!["XYZAC", "XYZBC"].includes(coordinates) || typeof source !== "string") return "";
|
|
const family = String(path).match(/xyz[ab]c/ig)?.map((s) => s.toUpperCase()) ?? [];
|
|
if (family.some((value) => value !== coordinates)) return `程序机型不匹配:${path} 不适用于 ${coordinates}`;
|
|
let comment = 0, bracket = 0, semicolon = false, named = false, code = "";
|
|
for (const char of source) {
|
|
if (char === '\n' || char === '\r') { semicolon = false; code += '\n'; continue; }
|
|
if (semicolon) continue;
|
|
if (char === '(') { comment++; continue; }
|
|
if (comment) { if (char === ')') comment--; continue; }
|
|
if (char === ';') { semicolon = true; continue; }
|
|
if (char === '<') { named = true; code += ' '; continue; }
|
|
if (named) { if (char === '>') named = false; continue; }
|
|
if (char === '[') { if (!bracket) code += '['; bracket++; continue; }
|
|
if (bracket) { if (char === ']') bracket--; continue; }
|
|
code += char;
|
|
}
|
|
const forbidden = coordinates === "XYZBC" ? "A" : "B";
|
|
const pattern = new RegExp(`${forbidden}\\s*(?=[+\\-\\d.#\\[])`, "i");
|
|
const lines = code.split('\n');
|
|
const index = lines.findIndex((line) => pattern.test(line));
|
|
return index < 0 ? "" : `程序机型不匹配:${coordinates} 不支持 ${forbidden} 轴指令(第 ${index + 1} 行),请选择对应机型的 G 代码`;
|
|
}
|
|
export function assertProgramMachine(source, coordinates, path = "") {
|
|
const error = programMachineMismatch(source, coordinates, path);
|
|
if (error) throw new Error(error);
|
|
}
|