Use PEG to strip whitespace/comments before commands

This commit is contained in:
Tangent Wantwight 2023-07-29 01:14:00 -04:00
parent 4430580412
commit 347af33bf1

View file

@ -6,10 +6,15 @@
*/
var Notcl = (() => {
const InterCommandWhitespace = Peg.Regex(/[^\S\n;]*/y);
const InterCommandWhitespace = Peg.Regex(/\s+/y);
const Comment = Peg.Regex(/#.*\n/y);
const PreCommand = Peg.AtLeast(
0,
Peg.Choose(InterCommandWhitespace, Comment)
);
const PreWordWhitespace = Peg.Regex(/[^\S\n;]*/y);
const BasicWord = Peg.Map(Peg.Regex(/[^\s;]+/y), ([word]) => ({
@ -49,13 +54,9 @@ var Notcl = (() => {
function nextCommand(/* TODO: null/]/" terminator */) {
const command = /** @type {Notcl.Word[]} */ ([]);
while (true) {
// Strip whitespace
code = code.replace(/^\s*/, "");
// Strip comments
if (code[0] == "#") {
code = code.replace(/^.*\n/, "");
continue;
}
// Strip whitespace and comments
const [preCmd, startIndex] = PreCommand(code, 0) ?? [null, 0];
code = code.substring(startIndex);
// Strip semicolons
if (code[0] == ";") {
code = code.substring(1);