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 14:50:49 -04:00
|
|
|
const { AtLeast, Choose, End, Map, Regex, Sequence, Use } = Peg;
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const InterCommandWhitespace = Regex(/\s+/y);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const Comment = Regex(/#.*\n/y);
|
|
|
|
|
|
|
|
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
|
2023-07-29 01:14:00 -04:00
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const PreWordWhitespace = Regex(/[^\S\n;]*/y);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const BasicWord = Map(Regex(/[^\s;]+/y), ([word]) => ({
|
2023-07-29 00:37:25 -04:00
|
|
|
text: word,
|
|
|
|
}));
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 13:41:47 -04:00
|
|
|
// WIP, need to be able to escape braces correctly
|
|
|
|
// WIP, error if anything after closing brace
|
2023-07-29 13:50:13 -04:00
|
|
|
|
2023-07-29 14:50:49 -04:00
|
|
|
/** @type {Peg.Pattern<string>} */
|
|
|
|
const Brace = Map(
|
2023-07-29 13:50:13 -04:00
|
|
|
Sequence(
|
|
|
|
Regex(/\{/y),
|
|
|
|
AtLeast(
|
2023-07-29 13:41:47 -04:00
|
|
|
0,
|
2023-07-29 13:50:13 -04:00
|
|
|
Choose(
|
2023-07-29 14:50:49 -04:00
|
|
|
Map(
|
|
|
|
Use(() => Brace),
|
|
|
|
(text) => `{${text}}`
|
|
|
|
),
|
2023-07-29 13:50:13 -04:00
|
|
|
Map(Regex(/[^{}]+/y), ([text]) => text)
|
2023-07-29 13:41:47 -04:00
|
|
|
)
|
|
|
|
),
|
2023-07-29 13:50:13 -04:00
|
|
|
Regex(/\}/y)
|
2023-07-29 13:41:47 -04:00
|
|
|
),
|
|
|
|
([_left, fragments, _right]) => fragments.join("")
|
|
|
|
);
|
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const Word = Map(
|
|
|
|
Sequence(
|
2023-07-29 13:41:47 -04:00
|
|
|
PreWordWhitespace,
|
2023-07-29 13:50:13 -04:00
|
|
|
Choose(
|
2023-07-29 14:50:49 -04:00
|
|
|
Map(Brace, (text) => ({
|
2023-07-29 13:41:47 -04:00
|
|
|
text,
|
|
|
|
})),
|
|
|
|
BasicWord
|
|
|
|
)
|
|
|
|
),
|
2023-07-29 00:37:25 -04:00
|
|
|
([_, word]) => word
|
|
|
|
);
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const CommandTerminator = Sequence(
|
2023-07-29 01:45:55 -04:00
|
|
|
PreWordWhitespace,
|
2023-07-29 13:50:13 -04:00
|
|
|
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End)
|
2023-07-29 01:45:55 -04:00
|
|
|
);
|
|
|
|
|
2023-07-29 13:50:13 -04:00
|
|
|
const Command = Map(
|
|
|
|
Sequence(PreCommand, AtLeast(0, Word), CommandTerminator),
|
2023-07-29 01:45:55 -04:00
|
|
|
([_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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|