Rearrange grammar to report text-after-closing-brace errors

This commit is contained in:
Tangent Wantwight 2023-08-04 15:19:36 -04:00
parent ee618fa244
commit 0d469abffc

View file

@ -14,12 +14,13 @@ var Notcl = (() => {
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
const PreWordWhitespace = Regex(/[^\S\n;]*/y);
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
const BasicWord = Regex(/[^\s;]+/y).map(([word]) => ({ text: word }));
const BasicWord = Regex(/(?!\{)[^\s;]+/y)
.map(([word]) => ({ text: word }))
.expects("BASIC_WORD");
// WIP, need to be able to escape braces correctly
// WIP, error if anything after closing brace
/** @type {Peg.Pattern<string>} */
const Brace = Sequence(
@ -33,29 +34,35 @@ var Notcl = (() => {
),
Regex(/\}/y).expects("}")
).map(([_left, fragments, _right]) => fragments.join(""));
const Word = Sequence(
PreWordWhitespace,
Choose(
Brace.map((text) => ({ text })),
BasicWord
)
).map(([_padding, word]) => word);
const CommandTerminator = Sequence(
PreWordWhitespace,
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End())
const Word = Choose(
Brace.map((text) => ({ text })),
BasicWord
);
const CommandTerminator = Choose(
/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)).expects(
"NEWLINE | ;"
),
End()
);
/** @type {Peg.Pattern<Notcl.Command>} */
const Command = Sequence(PreCommand, AtLeast(0, Word), CommandTerminator).map(
([_padding, words, _end]) => words
);
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");
/** @type {Peg.Pattern<Notcl.Script>} */
const Script = Sequence(AtLeast(0, Command), End()).map(
([commands, _eof]) => commands
);
const Script = Sequence(
AtLeast(0, Sequence(PreCommand, Command)),
Choose(End(), Sequence(PreCommand, Command))
).map(([commands, _eof]) => commands.map(([_padding, command]) => command));
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;