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 16:26:41 -04:00
|
|
|
const { AtLeast, Choose, End, Regex, Sequence, Use } = Peg;
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-08-04 11:30:41 -04:00
|
|
|
const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace");
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-08-04 11:30:41 -04:00
|
|
|
const Comment = Regex(/#.*\n/y).expects("#");
|
2023-07-29 13:50:13 -04:00
|
|
|
|
|
|
|
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
|
2023-07-29 01:14:00 -04:00
|
|
|
|
2023-08-04 15:19:36 -04:00
|
|
|
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
|
2023-07-29 00:11:54 -04:00
|
|
|
|
2023-08-04 15:19:36 -04:00
|
|
|
const BasicWord = Regex(/(?!\{)[^\s;]+/y)
|
|
|
|
.map(([word]) => ({ text: word }))
|
|
|
|
.expects("BASIC_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
|
2023-07-29 13:50:13 -04:00
|
|
|
|
2023-07-29 14:50:49 -04:00
|
|
|
/** @type {Peg.Pattern<string>} */
|
2023-07-29 16:26:41 -04:00
|
|
|
const Brace = Sequence(
|
2023-08-04 11:30:41 -04:00
|
|
|
Regex(/\{/y).expects("{"),
|
2023-07-29 16:26:41 -04:00
|
|
|
AtLeast(
|
|
|
|
0,
|
2023-07-29 13:50:13 -04:00
|
|
|
Choose(
|
2023-07-29 16:26:41 -04:00
|
|
|
Use(() => Brace).map((text) => `{${text}}`),
|
|
|
|
Regex(/[^{}]+/y).map(([text]) => text)
|
2023-07-29 13:41:47 -04:00
|
|
|
)
|
|
|
|
),
|
2023-08-04 11:30:41 -04:00
|
|
|
Regex(/\}/y).expects("}")
|
2023-07-29 16:26:41 -04:00
|
|
|
).map(([_left, fragments, _right]) => fragments.join(""));
|
2023-08-04 15:19:36 -04:00
|
|
|
const Word = Choose(
|
|
|
|
Brace.map((text) => ({ text })),
|
|
|
|
BasicWord
|
2023-07-29 01:45:55 -04:00
|
|
|
);
|
|
|
|
|
2023-08-04 15:19:36 -04:00
|
|
|
const CommandTerminator = Choose(
|
|
|
|
/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)).expects(
|
|
|
|
"NEWLINE | ;"
|
|
|
|
),
|
|
|
|
End()
|
2023-07-29 01:45:55 -04:00
|
|
|
);
|
2023-08-04 15:19:36 -04:00
|
|
|
/** @type {Peg.Pattern<Notcl.Command>} */
|
|
|
|
const Command = Choose(
|
|
|
|
CommandTerminator.map(() => []),
|
|
|
|
Sequence(
|
|
|
|
Word,
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Sequence(PreWordWhitespace, Word).map(([_padding, word]) => word)
|
|
|
|
),
|
|
|
|
Choose(Sequence(PreWordWhitespace, Word), CommandTerminator)
|
|
|
|
).map(([word, moreWords, _end]) => [word].concat(moreWords))
|
|
|
|
).expects("COMMAND");
|
2023-07-29 01:45:55 -04:00
|
|
|
|
2023-08-03 23:15:14 -04:00
|
|
|
/** @type {Peg.Pattern<Notcl.Script>} */
|
2023-08-04 15:19:36 -04:00
|
|
|
const Script = Sequence(
|
|
|
|
AtLeast(0, Sequence(PreCommand, Command)),
|
|
|
|
Choose(End(), Sequence(PreCommand, Command))
|
|
|
|
).map(([commands, _eof]) => commands.map(([_padding, command]) => command));
|
2023-08-03 23:15:14 -04:00
|
|
|
|
2023-08-04 00:17:14 -04:00
|
|
|
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;
|
|
|
|
|
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.
|
|
|
|
*
|
2023-08-04 00:26:15 -04:00
|
|
|
* @param {string} code to parse
|
|
|
|
* @returns {[true, Notcl.Script] | [false, string]} parsed list of commands, or error message on failure
|
2023-07-29 00:37:25 -04:00
|
|
|
*/
|
|
|
|
parse(code) {
|
|
|
|
/* Preprocess */
|
|
|
|
// fold line endings
|
|
|
|
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
|
2023-07-27 01:54:06 -04:00
|
|
|
|
2023-08-03 23:15:14 -04:00
|
|
|
/* Parse */
|
|
|
|
const commands = Script(code, 0);
|
2023-07-27 01:54:06 -04:00
|
|
|
|
2023-08-04 00:17:14 -04:00
|
|
|
if (commands[0]) {
|
|
|
|
return [true, commands[1]];
|
|
|
|
} else {
|
2023-08-04 01:24:02 -04:00
|
|
|
const [, errorPos, expected] = commands;
|
2023-08-04 00:17:14 -04:00
|
|
|
ERROR_CONTEXT.lastIndex = errorPos;
|
|
|
|
const [, before, after] = /** @type {RegExpExecArray} */ (
|
|
|
|
ERROR_CONTEXT.exec(code)
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
false,
|
2023-08-04 00:26:15 -04:00
|
|
|
`<pre>Error at position ${commands[1]}
|
|
|
|
${escapeHtml(before + "" + after)}
|
2023-08-04 01:24:02 -04:00
|
|
|
${"-".repeat(before.length)}^
|
|
|
|
|
|
|
|
Expected: ${escapeHtml(expected)}</pre>`,
|
2023-08-04 00:17:14 -04:00
|
|
|
];
|
|
|
|
}
|
2023-07-29 00:37:25 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|