prototype-3x5/notcl.js

95 lines
2.5 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, Regex, Sequence, Use } = 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 = Regex(/[^\s;]+/y).map(([word]) => ({ text: word }));
// WIP, need to be able to escape braces correctly
// WIP, error if anything after closing brace
/** @type {Peg.Pattern<string>} */
const Brace = Sequence(
Regex(/\{/y),
AtLeast(
0,
Choose(
Use(() => Brace).map((text) => `{${text}}`),
Regex(/[^{}]+/y).map(([text]) => text)
)
),
Regex(/\}/y)
).map(([_left, fragments, _right]) => fragments.join(""));
const Word = Sequence(
PreWordWhitespace,
Choose(
Brace.map((text) => ({ text })),
BasicWord
)
).map(([_, word]) => word);
const CommandTerminator = Sequence(
PreWordWhitespace,
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End)
);
/** @type {Peg.Pattern<Notcl.Command>} */
const Command = Sequence(PreCommand, AtLeast(0, Word), CommandTerminator).map(
([_padding, words, _end]) => words
);
/** @type {Peg.Pattern<Notcl.Script>} */
const Script = Sequence(AtLeast(0, Command), End).map(
([commands, _eof]) => commands
);
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;
return {
/**
* Parse out a Notcl script into an easier-to-interpret representation.
* No script is actually executed yet.
*
* @param {string} code to parse
* @returns {[true, Notcl.Script] | [false, string]} parsed list of commands, or error message on failure
*/
parse(code) {
/* Preprocess */
// fold line endings
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
/* Parse */
const commands = Script(code, 0);
if (commands[0]) {
return [true, commands[1]];
} else {
const errorPos = commands[1];
ERROR_CONTEXT.lastIndex = errorPos;
const [, before, after] = /** @type {RegExpExecArray} */ (
ERROR_CONTEXT.exec(code)
);
return [
false,
`<pre>Error at position ${commands[1]}
${escapeHtml(before + "" + after)}
${"-".repeat(before.length)}^</pre>`,
];
}
},
};
})();