2023-09-08 04:47:47 +00:00
|
|
|
import { escapeHtml } from './helpers';
|
|
|
|
import { AtLeast, Choose, End, Pattern, Peek, Regex, Sequence, Use } from './peg';
|
2023-08-25 20:44:50 +00:00
|
|
|
import {
|
2023-09-08 04:47:47 +00:00
|
|
|
InterpolatedPiece, Script, ScriptPiece, SimplifyWord, TextWord, Word as WordType
|
|
|
|
} from './words';
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-11-19 05:56:31 +00:00
|
|
|
const Comment = Regex(/#([^\\\n]|\\[^])*/y)
|
2023-08-05 05:09:33 +00:00
|
|
|
.expects("#")
|
|
|
|
.map(() => []);
|
|
|
|
|
2023-11-19 05:56:31 +00:00
|
|
|
const PreWordWhitespace = Regex(/([^\S\n;]|\\\n)+/y).expects("whitespace");
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-09-08 18:47:24 +00:00
|
|
|
const BackslashEscape = Sequence(
|
|
|
|
Regex(/\\/y).expects("BACKSLASH"),
|
|
|
|
Regex(/./y).expects("CHAR")
|
|
|
|
).map(([, [char]]) => ({ text: char }));
|
2023-08-23 05:09:56 +00:00
|
|
|
|
2023-11-20 21:45:13 +00:00
|
|
|
const BARE_WORD_CHAR = /[^\s\\;\[\]]+/y;
|
2023-08-25 23:10:45 +00:00
|
|
|
|
|
|
|
let BracketScript: Pattern<Script>;
|
|
|
|
const Bracket: Pattern<ScriptPiece> = Sequence(
|
|
|
|
Regex(/\[/y).expects("["),
|
|
|
|
Use(() => BracketScript)
|
|
|
|
)
|
|
|
|
.expects("[")
|
|
|
|
.map(([, script]) => ({ script }));
|
2023-08-25 16:46:16 +00:00
|
|
|
|
2023-11-20 21:45:13 +00:00
|
|
|
const BareWord = Sequence(
|
|
|
|
Regex(/(?!["{])/y),
|
|
|
|
AtLeast(
|
|
|
|
1,
|
|
|
|
Choose<InterpolatedPiece>(
|
|
|
|
BackslashEscape,
|
|
|
|
Bracket,
|
|
|
|
Regex(BARE_WORD_CHAR)
|
|
|
|
.expects("CHAR")
|
|
|
|
.map(([text]) => ({ bare: text }))
|
2023-08-23 05:09:56 +00:00
|
|
|
)
|
2023-11-20 21:45:13 +00:00
|
|
|
)
|
|
|
|
).map(([, pieces], pos) => SimplifyWord(pieces, pos));
|
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,
|
2023-08-25 23:10:45 +00:00
|
|
|
Bracket,
|
|
|
|
Regex(/[^"\\\[]+/y)
|
2023-08-23 05:09:56 +00:00
|
|
|
.expects("CHAR")
|
|
|
|
.map(([text]) => ({ text }))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Regex(/"/y).expects('"')
|
2023-11-19 01:48:57 +00:00
|
|
|
).map(([, pieces], pos) => SimplifyWord(pieces, pos));
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-11-21 03:54:56 +00:00
|
|
|
export const TemplateBlock = Sequence(
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Choose<InterpolatedPiece>(
|
2023-11-21 04:00:10 +00:00
|
|
|
Regex(/\\\n\s*/y)
|
|
|
|
.map(() => ({ text: " " }))
|
|
|
|
.expects("BACKSLASH"),
|
2023-11-21 03:54:56 +00:00
|
|
|
BackslashEscape,
|
|
|
|
Bracket,
|
|
|
|
Regex(/[^\\\[]+/y)
|
|
|
|
.expects("CHAR")
|
|
|
|
.map(([text]) => ({ text }))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
End()
|
|
|
|
).map(([pieces], pos) => SimplifyWord(pieces, pos));
|
|
|
|
|
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-11-19 05:56:31 +00:00
|
|
|
Regex(/\\[^]/y)
|
2023-09-08 18:47:24 +00:00
|
|
|
.expects("BACKSLASH")
|
2023-08-22 03:57:27 +00:00
|
|
|
.map(([escape]) => escape),
|
|
|
|
Regex(/[^\\{}]+/y)
|
2023-09-08 18:47:24 +00:00
|
|
|
.expects("CHAR")
|
2023-08-05 05:09:33 +00:00
|
|
|
.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-11-20 21:45:13 +00:00
|
|
|
export const WordPattern = Choose<WordType>(
|
|
|
|
Brace.map((text, pos) => ({ text, pos } as TextWord)),
|
|
|
|
QuotedWord,
|
|
|
|
BareWord
|
|
|
|
);
|
2023-11-20 21:35:07 +00:00
|
|
|
|
2023-08-05 05:09:33 +00:00
|
|
|
const CommandTerminator = Regex(/[\n;]/y)
|
|
|
|
.expects("NEWLINE | ;")
|
|
|
|
.map(() => true);
|
|
|
|
|
2023-11-20 21:45:13 +00:00
|
|
|
const Command = Sequence(
|
|
|
|
WordPattern,
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Sequence(PreWordWhitespace, WordPattern).map(([, word]) => word)
|
|
|
|
),
|
|
|
|
AtLeast(0, PreWordWhitespace)
|
|
|
|
).map(([word, moreWords]) => [word].concat(moreWords));
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-11-20 21:45:13 +00:00
|
|
|
function scriptTmpl(endPattern: Pattern<unknown>) {
|
2023-08-25 16:46:16 +00:00
|
|
|
return Sequence(
|
|
|
|
AtLeast(
|
|
|
|
0,
|
|
|
|
Choose(
|
|
|
|
PreWordWhitespace.map(() => []),
|
|
|
|
CommandTerminator.map(() => []),
|
2023-08-25 20:44:50 +00:00
|
|
|
Sequence(Comment, Choose(CommandTerminator, Peek(endPattern))).map(
|
|
|
|
() => []
|
|
|
|
),
|
2023-11-20 21:45:13 +00:00
|
|
|
Sequence(Command, Choose(CommandTerminator, Peek(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));
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:45:13 +00:00
|
|
|
const Script = scriptTmpl(End());
|
|
|
|
BracketScript = scriptTmpl(Regex(/\]/y).expects("]"));
|
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.
|
|
|
|
*
|
2024-06-05 04:32:04 +00:00
|
|
|
* @param code code to parse
|
|
|
|
* @param offset source position of code, if embedded in a larger source document
|
|
|
|
* @returns parsed list of commands, or error message on failure
|
2023-08-05 05:09:33 +00:00
|
|
|
*/
|
2024-06-05 04:32:04 +00:00
|
|
|
export function parse(code: string, offset = 0): [true, Script] | [false, string] {
|
2023-08-05 05:09:33 +00:00
|
|
|
/* Parse */
|
2023-09-08 04:47:47 +00:00
|
|
|
const [commands, errorPos, expected] = Script.match(code, 0);
|
2023-08-05 05:09:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|