zkt25/sk1/app/janeParser.js
2025-04-29 19:12:53 +02:00

48 lines
1.9 KiB
JavaScript

function checkJaneSyntax(code) {
let errors = [];
let lines = code.split("\n");
let variables = new Set();
let assignmentRegex = /^\s*var\s+([a-zA-Z][a-zA-Z0-9]*)\s*;?\s*$/;
let valueAssignmentRegex = /^\s*([a-zA-Z][a-zA-Z0-9]*)\s*:=\s*[0-9a-zA-Z+\-*\/]+\s*;?\s*$/;
let ifRegex = /^\s*if\s*\(.+\)\s*then\s*\{.*\}\s*;?\s*$/;
let whileRegex = /^\s*while\s*\(.+\)\s*do\s*\{.*\}\s*;?\s*$/;
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (!line) continue;
if (assignmentRegex.test(line)) {
let varName = line.match(assignmentRegex)[1];
if (variables.has(varName)) {
errors.push(`Error on line ${i + 1}: variable '${varName}' is already declared.`);
} else {
variables.add(varName);
}
if (!line.endsWith(";")) {
errors.push(`Error on line ${i + 1}: Missing semicolon at the end of the line.`);
}
} else if (valueAssignmentRegex.test(line)) {
let varName = line.match(valueAssignmentRegex)[1];
if (!variables.has(varName)) {
errors.push(`Error on line ${i + 1}: variable '${varName}' is not declared.`);
}
if (!line.endsWith(";")) {
errors.push(`Error on line ${i + 1}: Missing semicolon at the end of the line.`);
}
} else if (ifRegex.test(line)) {
if (!line.endsWith(";")) {
errors.push(`Error on line ${i + 1}: Missing semicolon at the end of the line.`);
}
} else if (whileRegex.test(line)) {
if (!line.endsWith(";")) {
errors.push(`Error on line ${i + 1}: Missing semicolon at the end of the line.`);
}
} else {
errors.push(`Error on line ${i + 1}: invalid syntax.`);
}
}
return errors.length ? errors : ["Syntax is correct!"];
}