comuns-rpgmaker/schach-parsing

Implement predicate parser

masked-rpgmaker opened this issue · 0 comments

Feature: parser that matches a predicate and either fails or returns and empty success.

Desired properties:

  • Any boolean function with a single argument can be passed in as a predicate.
  • Predicate parser receives a consumer function that returns a pair with the consumed value and the rest.

Example:

const charConsumer = (input: string): [string, string] => [ string[0], string.substr(1) ];
const digit = Schach.Parsing.match(charConsumer, chr => /[0-9]+/.test(chr))

digit.run('1'); // Success, rest = ""
digit.run('5a'); // Success, rest = "a"
digit.run('12'); // Success, rest = "2"
digit.run('a'); // Fail