2023-08-05 05:09:33 +00:00
|
|
|
import { escapeHtml } from "./helpers";
|
2023-08-06 06:09:30 +00:00
|
|
|
import { AtLeast, Choose, End, Pattern, Regex, Sequence, Use } from "./peg";
|
2023-08-07 03:18:13 +00:00
|
|
|
import {
|
2023-08-25 16:05:06 +00:00
|
|
|
Script,
|
2023-08-07 03:18:13 +00:00
|
|
|
Word as WordType,
|
|
|
|
TextWord,
|
|
|
|
EnchantedWord as EnchantedWordType,
|
2023-08-23 05:09:56 +00:00
|
|
|
SimplifyWord,
|
2023-08-25 16:46:16 +00:00
|
|
|
InterpolatedPiece,
|
2023-08-07 03:18:13 +00:00
|
|
|
} from "./words";
|
2023-08-05 05:09:33 +00:00
|
|
|
|
|
|
|
const Comment = Regex(/#[^\n]*/y)
|
|
|
|
.expects("#")
|
|
|
|
.map(() => []);
|
|
|
|
|
|
|
|
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
|
|
|
|
|
2023-08-07 03:18:13 +00:00
|
|
|
const EnchantedWord = Regex(/[^\]\[\}\{$\\";\s]+(?=[\s;]|$)/y)
|
|
|
|
.map(([enchanted]) => ({ enchanted } as EnchantedWordType))
|
|
|
|
.expects("ENCHANTED_WORD");
|
|
|
|
|
2023-08-23 05:09:56 +00:00
|
|
|
const BackslashEscape = Regex(/\\(.)/y)
|
|
|
|
.expects("\\")
|
|
|
|
.map(([, char]) => ({ text: char }));
|
|
|
|
|
2023-08-25 16:46:16 +00:00
|
|
|
const BARE_WORD_CHAR = /[^\s\\;]+/y;
|
|
|
|
|
|
|
|
function bareWordTmpl(charRegex: RegExp) {
|
|
|
|
return Sequence(
|
|
|
|
AtLeast(
|
|
|
|
1,
|
|
|
|
Choose<InterpolatedPiece>(
|
|
|
|
BackslashEscape,
|
|
|
|
Regex(charRegex)
|
|
|
|
.expects("CHAR")
|
|
|
|
.map(([text]) => ({ text }))
|
|
|
|
)
|
2023-08-23 05:09:56 +00:00
|
|
|
)
|
2023-08-25 16:46:16 +00:00
|
|
|
).map(([pieces]) => SimplifyWord(pieces));
|
|
|
|
}
|
2023-08-23 05:09:56 +00:00
|
|
|
|
|
|
|
const QuotedWord = Sequence(
|
|
|
|
Regex(/"/y).expects('"'),
|
|
|
|
AtLeast(
|
|
|
|
0,
|
2023-08-25 16:46:16 +00:00
|
|
|
Choose<InterpolatedPiece>(
|
2023-08-23 05:09:56 +00:00
|
|
|
BackslashEscape,
|
|
|
|
Regex(/[^"\\]+/y)
|
|
|
|
.expects("CHAR")
|
|
|
|
.map(([text]) => ({ text }))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Regex(/"/y).expects('"')
|
|
|
|
).map(([, pieces]) => SimplifyWord(pieces));
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-08-06 06:09:30 +00:00
|
|
|
const Brace: Pattern<string> = Sequence(
|
2023-08-05 05:09:33 +00:00
|
|
|
Regex(/\{/y).expects("{"),
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Choose(
|
|
|
|
Use(() => Brace)
|
|
|
|
.expects("{")
|
|
|
|
.map((text) => `{${text}}`),
|
2023-08-22 03:57:27 +00:00
|
|
|
Regex(/\\./y)
|
|
|
|
.expects("\\")
|
|
|
|
.map(([escape]) => escape),
|
|
|
|
Regex(/[^\\{}]+/y)
|
2023-08-05 05:09:33 +00:00
|
|
|
.expects("text")
|
|
|
|
.map(([text]) => text)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Regex(/\}/y).expects("}")
|
2023-08-23 05:09:56 +00:00
|
|
|
).map(([, fragments]) => fragments.join(""));
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-08-25 16:46:16 +00:00
|
|
|
function wordTmpl(bareWordCharRegex: RegExp): Pattern<WordType> {
|
|
|
|
return Choose<WordType>(
|
|
|
|
EnchantedWord,
|
|
|
|
Brace.map((text) => ({ text } as TextWord)),
|
|
|
|
QuotedWord,
|
|
|
|
bareWordTmpl(bareWordCharRegex)
|
|
|
|
);
|
|
|
|
}
|
2023-08-05 05:09:33 +00:00
|
|
|
|
|
|
|
const CommandTerminator = Regex(/[\n;]/y)
|
|
|
|
.expects("NEWLINE | ;")
|
|
|
|
.map(() => true);
|
|
|
|
|
2023-08-25 16:46:16 +00:00
|
|
|
function commandTmpl(bareWordCharRegex: RegExp) {
|
|
|
|
const word = wordTmpl(bareWordCharRegex);
|
|
|
|
return Sequence(
|
|
|
|
word,
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Sequence(PreWordWhitespace, word).map(([, word]) => word)
|
|
|
|
),
|
|
|
|
AtLeast(0, PreWordWhitespace)
|
|
|
|
).map(([word, moreWords]) => [word].concat(moreWords));
|
|
|
|
}
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-08-25 16:46:16 +00:00
|
|
|
function scriptTmpl(bareWordCharRegex: RegExp, endPattern: Pattern<unknown>) {
|
|
|
|
return Sequence(
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Choose(
|
|
|
|
PreWordWhitespace.map(() => []),
|
|
|
|
CommandTerminator.map(() => []),
|
|
|
|
Sequence(Comment, Choose(CommandTerminator, endPattern)).map(() => []),
|
|
|
|
Sequence(
|
|
|
|
commandTmpl(bareWordCharRegex),
|
|
|
|
Choose(CommandTerminator, endPattern)
|
|
|
|
).map(([words]) => words)
|
2023-08-05 05:09:33 +00:00
|
|
|
)
|
2023-08-25 16:46:16 +00:00
|
|
|
),
|
|
|
|
endPattern
|
|
|
|
).map(([commands]) => commands.filter((command) => command.length > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
const Script = scriptTmpl(BARE_WORD_CHAR, End());
|
2023-07-29 04:11:54 +00:00
|
|
|
|
2023-08-05 05:09:33 +00:00
|
|
|
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;
|
2023-07-29 17:50:13 +00:00
|
|
|
|
2023-08-05 05:09:33 +00:00
|
|
|
/**
|
|
|
|
* Parse out a Notcl script into an easier-to-interpret representation.
|
|
|
|
* No script is actually executed yet.
|
|
|
|
*
|
|
|
|
* @param - code to parse
|
|
|
|
* @returns - parsed list of commands, or error message on failure
|
|
|
|
*/
|
|
|
|
export function parse(code: string): [true, Script] | [false, string] {
|
|
|
|
/* Preprocess */
|
|
|
|
// fold line endings
|
2023-08-07 00:18:38 +00:00
|
|
|
code = code.replace(/(?<!\\)((\\\\)*)\\\n[ \t]*/g, "$1 ");
|
2023-08-05 05:09:33 +00:00
|
|
|
|
|
|
|
/* Parse */
|
|
|
|
const [commands, errorPos, expected] = Script(code, 0);
|
|
|
|
|
|
|
|
if (commands) {
|
|
|
|
return [true, commands[0]];
|
|
|
|
} else {
|
|
|
|
ERROR_CONTEXT.lastIndex = errorPos;
|
|
|
|
const [, before, after] = ERROR_CONTEXT.exec(code)!;
|
|
|
|
return [
|
|
|
|
false,
|
|
|
|
`<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-05 05:09:33 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|