From 4b982a64ef54af0bf5aaea966e35f42d214627f1 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Fri, 4 Aug 2023 17:43:51 -0400 Subject: [PATCH] Rearrange grammer --- notcl.js | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/notcl.js b/notcl.js index d1c85ba..56ccd92 100644 --- a/notcl.js +++ b/notcl.js @@ -6,7 +6,7 @@ */ var Notcl = (() => { - const { AtLeast, Choose, End, Regex, Sequence, Use } = Peg; + const { AtLeast, Choose, End, Regex, Sequence, Use, Hint } = Peg; const InterCommandWhitespace = Regex(/\s+/y).expects("whitespace"); @@ -34,38 +34,37 @@ var Notcl = (() => { ), Regex(/\}/y).expects("}") ).map(([_left, fragments, _right]) => fragments.join("")); + const Word = Choose( BasicWord, Brace.map((text) => ({ text })) ); - const CommandTerminator = Choose( - /** @type {Peg.Pattern} */ (Regex(/[\n;]/y)).expects( - "NEWLINE | ;" - ), - End() - ); + const CommandTerminator = Regex(/[\n;]/y).expects("NEWLINE | ;"); + /** @type {Peg.Pattern} */ - const Command = Choose( - CommandTerminator.map(() => []), - Sequence( - Word, - AtLeast( - 0, - Sequence(PreWordWhitespace, Word).map(([_padding, word]) => word) - ), - Choose( - Sequence(PreWordWhitespace, Word).expects("whitespace"), - Sequence(AtLeast(0, PreWordWhitespace), CommandTerminator).expects(";") - ) - ).map(([word, moreWords, _end]) => [word].concat(moreWords)) - ).expects("COMMAND"); + const Command = Sequence( + Word, + AtLeast( + 0, + Sequence(PreWordWhitespace, Word).map(([, word]) => word) + ), + AtLeast(0, PreWordWhitespace) + ).map(([word, moreWords]) => [word].concat(moreWords)); /** @type {Peg.Pattern} */ const Script = Sequence( - AtLeast(0, Sequence(PreCommand, Command)), - Choose(End(), Sequence(PreCommand, Command)) - ).map(([commands, _eof]) => commands.map(([_padding, command]) => command)); + PreCommand, + AtLeast(0, Command), + AtLeast( + 0, + Sequence(CommandTerminator, PreCommand, Command).map( + ([, , command]) => command + ) + ), + AtLeast(0, PreCommand), + Choose(End(), Hint(Command)) + ).map(([, command, moreCommands]) => command.concat(moreCommands)); const ERROR_CONTEXT = /(?<=([^\n]{0,50}))([^\n]{0,50})/y;