prototype-3x5/notcl.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-07-27 05:54:06 +00:00
/**
2023-07-29 04:37:25 +00:00
* @typedef {Notcl.Command[]} Notcl.Script
* @typedef {Notcl.Word[]} Notcl.Command
* @typedef {object} Notcl.Word
2023-07-27 05:54:06 +00:00
* @property {string} text
*/
2023-07-29 04:37:25 +00:00
var Notcl = (() => {
2023-07-29 20:26:41 +00:00
const { AtLeast, Choose, End, Regex, Sequence, Use } = Peg;
2023-08-04 15:30:41 +00:00
const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace");
2023-08-04 15:30:41 +00:00
const Comment = Regex(/#.*\n/y).expects("#");
2023-07-29 17:50:13 +00:00
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
2023-07-29 17:50:13 +00:00
const PreWordWhitespace = Regex(/[^\S\n;]*/y);
2023-07-29 20:26:41 +00:00
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
2023-07-29 17:50:13 +00:00
/** @type {Peg.Pattern<string>} */
2023-07-29 20:26:41 +00:00
const Brace = Sequence(
2023-08-04 15:30:41 +00:00
Regex(/\{/y).expects("{"),
2023-07-29 20:26:41 +00:00
AtLeast(
0,
2023-07-29 17:50:13 +00:00
Choose(
2023-07-29 20:26:41 +00:00
Use(() => Brace).map((text) => `{${text}}`),
Regex(/[^{}]+/y).map(([text]) => text)
)
),
2023-08-04 15:30:41 +00:00
Regex(/\}/y).expects("}")
2023-07-29 20:26:41 +00:00
).map(([_left, fragments, _right]) => fragments.join(""));
const Word = Sequence(
PreWordWhitespace,
Choose(
Brace.map((text) => ({ text })),
BasicWord
)
2023-08-04 15:30:41 +00:00
).map(([_padding, word]) => word);
2023-07-29 17:50:13 +00:00
const CommandTerminator = Sequence(
2023-07-29 05:45:55 +00:00
PreWordWhitespace,
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End())
2023-07-29 05:45:55 +00:00
);
/** @type {Peg.Pattern<Notcl.Command>} */
2023-07-29 20:26:41 +00:00
const Command = Sequence(PreCommand, AtLeast(0, Word), CommandTerminator).map(
2023-07-29 05:45:55 +00:00
([_padding, words, _end]) => words
);
/** @type {Peg.Pattern<Notcl.Script>} */
const Script = Sequence(AtLeast(0, Command), End()).map(
([commands, _eof]) => commands
);
2023-08-04 04:17:14 +00:00
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;
2023-07-29 04:37:25 +00:00
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
2023-07-29 04:37:25 +00:00
*/
parse(code) {
/* Preprocess */
// fold line endings
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
2023-07-27 05:54:06 +00:00
/* Parse */
const commands = Script(code, 0);
2023-07-27 05:54:06 +00:00
2023-08-04 04:17:14 +00:00
if (commands[0]) {
return [true, commands[1]];
} else {
const [, errorPos, expected] = commands;
2023-08-04 04:17:14 +00:00
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)}^
Expected: ${escapeHtml(expected)}</pre>`,
2023-08-04 04:17:14 +00:00
];
}
2023-07-29 04:37:25 +00:00
},
};
})();