parser-combinator is a parser library that delivers small parsers that can be combined into bigger parsers.
Generates a parser that parses the given input for the given character. Equivalent to any literal in a regular expression.
const parser = char('a'); // <=> Regex(/a/)
parser.parse('a'); // accepts
parser.parse('b'); // rejects
Takes an array of parsers. The generated parses parses a string and checks if the parsers are satisfied in the given order.
const parser = sequence(char('a'), char('b')); // <=> Regex(/ab/)
parser.parse('ab'); // accepts
parser.parse('ba'); // rejects
Takes an array of parsers. The input must only satisfy only one of the given parsers.
const parser = anyOf(char('a'), char('b')); // <=> Regex(/(a|b)/)
parser.parse('a'); // accepts
parser.parse('b'); // accepts
parser.parse('c'); // rejects
Takes a number and a parser. The given parser must be satisfied n consecutive times.
const parser = nOf(2, char('a')); // <=> Regex(/a{2}/)
parser.parse('aa'); // accepts
parser.parse('ab'); // rejects
Takes a parser. That parser can but must not be satisfied. If it is satisfied the parser greedily eats from the input until it cannot be satisfied anymore.
const parser = zeroOrMore(char('a')); // <=> Regex(/a*/)
parser.parse(''); // accepts
parser.parse('a'); // accepts
parser.parse('aaa'); // also accepts
Takes a parser. That parser but must be satisfied at least once. If it is satisfied the parser greedily eats from the input until it cannot be satisfied anymore.
const parser = zeroOrMore(char('a')); // <=> Regex(/a+/)
parser.parse('a'); // accepts
parser.parse('aaa'); // accepts
parser.parse(''); // rejects
Takes a number n and a parser. The input must satisfy the parser at least n times.
const parser = minOf(2, char('a')); // <=> Regex(/a{2,}/)
parser.parse('aa'); // accepts
parser.parse('aaaa'); // accepts
parser.parse('a'); // rejects
const parser = maxOf(2, char('a')); // <=> Regex(/a{,2}/)
parser.parse('aa'); // accepts
parser.parse('a'); // accepts
parser.parse('aaaa'); // rejects
const parser = rangeOf([2,3], char('a')); // <=> Regex(/a{2,}/)
parser.parse('aa'); // accepts
parser.parse('aaa'); // accepts
parser.parse('a'); // rejects
Takes a parser. That parser can be satisfied but mustn't. It will only be satisfied once.
const parser = optional(char('a')); // <=> Regex(/a?/)
parser.parse(''); // accepts
parser.parse('a'); // accepts
This parser generator represents accepts any character as input.
any().parse('?'); // accepts
any().parse('m'); // also fine
Generates a parser that will only accept when there is no rest after parsing.
const parser = strict(nOf(3, char('a'))); // <=> Regex(/^a{3}$/)
parser.parse('aaa'); // accepts
parser.parse('aaaa'); // rejects
This parser generator takes a string and transforms it into a sequence of char()
parsers.
This is a parser that accpets any lowercase latin character.
This is a parser that accepts any uppercase latin character.
In this section I am going to show you how to compose parser to get a more complex parser. We will build a parser that can parser numbers that are written in scientific notation (e.g. 12e-1 <=> 1.2).
const uint = anyOf(
char('0'),
char('1'),
char('2'),
char('3'),
char('4'),
char('5'),
char('6'),
char('7'),
char('8'),
char('9')
);
const int = sequence(
optional(char('-')),
uint
);
const float = sequence(int, char('.'), uint);
const scientific = sequence(
float,
char('e'),
int
);