Use PEG to strip whitespace/comments before commands
This commit is contained in:
parent
4430580412
commit
347af33bf1
1 changed files with 9 additions and 8 deletions
17
notcl.js
17
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);
|
||||
|
|
Loading…
Reference in a new issue