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;