99 lines
2.3 KiB
JavaScript
99 lines
2.3 KiB
JavaScript
/**
|
|
* @typedef {Notcl.Command[]} Notcl.Script
|
|
* @typedef {Notcl.Word[]} Notcl.Command
|
|
* @typedef {object} Notcl.Word
|
|
* @property {string} text
|
|
*/
|
|
|
|
var Notcl = (() => {
|
|
const { AtLeast, Choose, End, Map, Regex, Sequence } = Peg;
|
|
|
|
const InterCommandWhitespace = Regex(/\s+/y);
|
|
|
|
const Comment = Regex(/#.*\n/y);
|
|
|
|
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
|
|
|
|
const PreWordWhitespace = Regex(/[^\S\n;]*/y);
|
|
|
|
const BasicWord = Map(Regex(/[^\s;]+/y), ([word]) => ({
|
|
text: word,
|
|
}));
|
|
|
|
// WIP, need to be able to escape braces correctly
|
|
// WIP, error if anything after closing brace
|
|
|
|
const BracePattern = Map(
|
|
Sequence(
|
|
Regex(/\{/y),
|
|
AtLeast(
|
|
0,
|
|
Choose(
|
|
Map(Brace, (text) => `{${text}}`),
|
|
Map(Regex(/[^{}]+/y), ([text]) => text)
|
|
)
|
|
),
|
|
Regex(/\}/y)
|
|
),
|
|
([_left, fragments, _right]) => fragments.join("")
|
|
);
|
|
/**
|
|
* @type {Peg.Pattern<string>}
|
|
*/
|
|
function Brace(source, index) {
|
|
// Thunk to allow Brace to recurse
|
|
return BracePattern(source, index);
|
|
}
|
|
|
|
const Word = Map(
|
|
Sequence(
|
|
PreWordWhitespace,
|
|
Choose(
|
|
Map(BracePattern, (text) => ({
|
|
text,
|
|
})),
|
|
BasicWord
|
|
)
|
|
),
|
|
([_, word]) => word
|
|
);
|
|
|
|
const CommandTerminator = Sequence(
|
|
PreWordWhitespace,
|
|
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End)
|
|
);
|
|
|
|
const Command = Map(
|
|
Sequence(PreCommand, AtLeast(0, Word), CommandTerminator),
|
|
([_padding, words, _end]) => words
|
|
);
|
|
|
|
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");
|
|
|
|
function nextCommand() {
|
|
const [words, nextIndex] = Command(code, 0) ?? [[], 0];
|
|
code = code.substring(nextIndex);
|
|
return words;
|
|
}
|
|
|
|
/* Loop through commands, with safety check */
|
|
const script = /** @type {Notcl.Command[]} */ ([]);
|
|
for (let i = 0; i < 1000 && code != ""; i++) {
|
|
script.push(nextCommand());
|
|
}
|
|
|
|
return script;
|
|
},
|
|
};
|
|
})();
|