Rereorganize grammar for better feedback

This commit is contained in:
Tangent Wantwight 2023-08-04 23:29:53 -04:00
parent 1c04d8dece
commit 57c65b10f2
1 changed files with 21 additions and 11 deletions

View File

@ -10,9 +10,11 @@ var Notcl = (() => {
const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace");
const Comment = Regex(/#[^\n]*/y).expects("#");
const Comment = Regex(/#[^\n]*/y)
.expects("#")
.map(() => []);
const PreCommand = AtLeast(0, Choose(InterCommandWhitespace, Comment));
const PreCommand = AtLeast(0, InterCommandWhitespace);
const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace");
@ -28,8 +30,12 @@ var Notcl = (() => {
AtLeast(
0,
Choose(
Use(() => Brace).map((text) => `{${text}}`),
Regex(/[^{}]+/y).map(([text]) => text)
Use(() => Brace)
.expects("{")
.map((text) => `{${text}}`),
Regex(/[^{}]+/y)
.expects("text")
.map(([text]) => text)
)
),
Regex(/\}/y).expects("}")
@ -40,7 +46,9 @@ var Notcl = (() => {
Brace.map((text) => ({ text }))
);
const CommandTerminator = Regex(/[\n;]/y).expects("NEWLINE | ;");
const CommandTerminator = Regex(/[\n;]/y)
.expects("NEWLINE | ;")
.map(() => true);
/** @type {Peg.Pattern<Notcl.Command>} */
const Command = Sequence(
@ -54,17 +62,19 @@ var Notcl = (() => {
/** @type {Peg.Pattern<Notcl.Script>} */
const Script = Sequence(
PreCommand,
AtLeast(0, Command),
AtLeast(
0,
Sequence(CommandTerminator, PreCommand, Command).map(
([, , command]) => command
Choose(
PreWordWhitespace.map(() => []),
CommandTerminator.map(() => []),
Sequence(Comment, Choose(CommandTerminator, End())).map(() => []),
Sequence(Command, Choose(CommandTerminator, End())).map(
([words]) => words
)
)
),
AtLeast(0, PreCommand),
End()
).map(([, command, moreCommands]) => command.concat(moreCommands));
).map(([commands]) => commands.filter((command) => command.length > 0));
const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;