diff --git a/notcl.js b/notcl.js index 187e36a..95facf5 100644 --- a/notcl.js +++ b/notcl.js @@ -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);