2023-07-27 01:54:06 -04:00
|
|
|
/**
|
2023-07-29 00:37:25 -04:00
|
|
|
* @typedef {Notcl.Command[]} Notcl.Script
|
|
|
|
* @typedef {Notcl.Word[]} Notcl.Command
|
|
|
|
* @typedef {object} Notcl.Word
|
2023-07-27 01:54:06 -04:00
|
|
|
* @property {string} text
|
|
|
|
*/
|
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
var Notcl = (() => {
|
2023-07-29 01:14:00 -04:00
|
|
|
const InterCommandWhitespace = Peg.Regex(/\s+/y);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
const Comment = Peg.Regex(/#.*\n/y);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 01:14:00 -04:00
|
|
|
const PreCommand = Peg.AtLeast(
|
|
|
|
0,
|
|
|
|
Peg.Choose(InterCommandWhitespace, Comment)
|
|
|
|
);
|
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
const PreWordWhitespace = Peg.Regex(/[^\S\n;]*/y);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
const BasicWord = Peg.Map(Peg.Regex(/[^\s;]+/y), ([word]) => ({
|
|
|
|
text: word,
|
|
|
|
}));
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
const Word = Peg.Map(
|
|
|
|
Peg.Sequence(PreWordWhitespace, BasicWord),
|
|
|
|
([_, word]) => word
|
|
|
|
);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 01:45:55 -04:00
|
|
|
const CommandTerminator = Peg.Sequence(
|
|
|
|
PreWordWhitespace,
|
|
|
|
Peg.Choose(
|
|
|
|
/** @type {Peg.Pattern<unknown>} */ (Peg.Regex(/[\n;]/y)),
|
|
|
|
Peg.End
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const Command = Peg.Map(
|
|
|
|
Peg.Sequence(PreCommand, Peg.AtLeast(0, Word), CommandTerminator),
|
|
|
|
([_padding, words, _end]) => words
|
|
|
|
);
|
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Parse out a Notcl script into an easier-to-interpret representation.
|
|
|
|
* No script is actually executed yet.
|
|
|
|
*
|
|
|
|
* @param {string} code
|
|
|
|
* @returns Script
|
|
|
|
*/
|
|
|
|
parse(code) {
|
|
|
|
/* Preprocess */
|
|
|
|
// fold line endings
|
|
|
|
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
|
2023-07-27 01:54:06 -04:00
|
|
|
|
2023-07-29 01:45:55 -04:00
|
|
|
function nextCommand() {
|
|
|
|
const [words, nextIndex] = Command(code, 0) ?? [[], 0];
|
|
|
|
code = code.substring(nextIndex);
|
|
|
|
return words;
|
2023-07-29 00:37:25 -04:00
|
|
|
}
|
2023-07-27 01:54:06 -04:00
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
/* Loop through commands, with safety check */
|
|
|
|
const script = /** @type {Notcl.Command[]} */ ([]);
|
|
|
|
for (let i = 0; i < 1000 && code != ""; i++) {
|
|
|
|
script.push(nextCommand());
|
|
|
|
}
|
2023-07-27 01:54:06 -04:00
|
|
|
|
2023-07-29 00:37:25 -04:00
|
|
|
return script;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|