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-08-04 23:25:57 +00:00
|
|
|
const { AtLeast, Choose, End, Regex, Sequence, Use } = Peg;
|
2023-07-29 04:11:54 +00:00
|
|
|
|
2023-08-04 15:30:41 +00:00
|
|
|
const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace");
|
2023-07-29 04:11:54 +00:00
|
|
|
|
2023-08-04 21:43:42 +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 05:14:00 +00:00
|
|
|
|
2023-08-04 19:19:36 +00:00
|
|
|
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
|
2023-07-29 04:11:54 +00:00
|
|
|
|
2023-08-04 19:19:36 +00:00
|
|
|
const BasicWord = Regex(/(?!\{)[^\s;]+/y)
|
|
|
|
.map(([word]) => ({ text: word }))
|
|
|
|
.expects("BASIC_WORD");
|
2023-07-29 04:11:54 +00:00
|
|
|
|
2023-07-29 17:41:47 +00:00
|
|
|
// WIP, need to be able to escape braces correctly
|
2023-07-29 17:50:13 +00:00
|
|
|
|
2023-07-29 18:50:49 +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-07-29 17:41:47 +00:00
|
|
|
)
|
|
|
|
),
|
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(""));
|
2023-08-04 21:43:51 +00:00
|
|
|
|
2023-08-04 19:19:36 +00:00
|
|
|
const Word = Choose(
|
2023-08-04 19:52:26 +00:00
|
|
|
BasicWord,
|
|
|
|
Brace.map((text) => ({ text }))
|
2023-07-29 05:45:55 +00:00
|
|
|
);
|
|
|
|
|
2023-08-04 21:43:51 +00:00
|
|
|
const CommandTerminator = Regex(/[\n;]/y).expects("NEWLINE | ;");
|
|
|
|
|
2023-08-04 19:19:36 +00:00
|
|
|
/** @type {Peg.Pattern<Notcl.Command>} */
|
2023-08-04 21:43:51 +00:00
|
|
|
const Command = Sequence(
|
|
|
|
Word,
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Sequence(PreWordWhitespace, Word).map(([, word]) => word)
|
|
|
|
),
|
|
|
|
AtLeast(0, PreWordWhitespace)
|
|
|
|
).map(([word, moreWords]) => [word].concat(moreWords));
|
2023-07-29 05:45:55 +00:00
|
|
|
|
2023-08-04 03:15:14 +00:00
|
|
|
/** @type {Peg.Pattern<Notcl.Script>} */
|
2023-08-04 19:19:36 +00:00
|
|
|
const Script = Sequence(
|
2023-08-04 21:43:51 +00:00
|
|
|
PreCommand,
|
|
|
|
AtLeast(0, Command),
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Sequence(CommandTerminator, PreCommand, Command).map(
|
|
|
|
([, , command]) => command
|
|
|
|
)
|
|
|
|
),
|
|
|
|
AtLeast(0, PreCommand),
|
2023-08-04 23:25:47 +00:00
|
|
|
End()
|
2023-08-04 21:43:51 +00:00
|
|
|
).map(([, command, moreCommands]) => command.concat(moreCommands));
|
2023-08-04 03:15:14 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2023-08-04 04:26:15 +00:00
|
|
|
* @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
|
|
|
|
2023-08-04 03:15:14 +00:00
|
|
|
/* Parse */
|
2023-08-04 22:31:21 +00:00
|
|
|
const [commands, errorPos, expected] = Script(code, 0);
|
2023-07-27 05:54:06 +00:00
|
|
|
|
2023-08-04 22:31:21 +00:00
|
|
|
if (commands) {
|
|
|
|
return [true, commands[0]];
|
2023-08-04 04:17:14 +00:00
|
|
|
} else {
|
|
|
|
ERROR_CONTEXT.lastIndex = errorPos;
|
|
|
|
const [, before, after] = /** @type {RegExpExecArray} */ (
|
|
|
|
ERROR_CONTEXT.exec(code)
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
false,
|
2023-08-04 22:31:21 +00:00
|
|
|
`<pre>Error at position ${errorPos}
|
2023-08-04 04:26:15 +00:00
|
|
|
${escapeHtml(before + "" + after)}
|
2023-08-04 05:24:02 +00:00
|
|
|
${"-".repeat(before.length)}^
|
|
|
|
|
|
|
|
Expected: ${escapeHtml(expected)}</pre>`,
|
2023-08-04 04:17:14 +00:00
|
|
|
];
|
|
|
|
}
|
2023-07-29 04:37:25 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|