From 57c65b10f2cc0ab845546b3909ac94a6264060f5 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Fri, 4 Aug 2023 23:29:53 -0400 Subject: [PATCH] Rereorganize grammar for better feedback --- notcl.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/notcl.js b/notcl.js index 082a17c..c8b8724 100644 --- a/notcl.js +++ b/notcl.js @@ -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} */ const Command = Sequence( @@ -54,17 +62,19 @@ var Notcl = (() => { /** @type {Peg.Pattern} */ 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;