Trying to understand how parsers work in parser-ts, but encountered a pretty unexpected behaviour, a simple P.many
parser run on a string just hangs for ever, what am I doing wrong?
const everything = pipe(
Ch.alphanum,
P.alt(() => S.spaces)
);
const input1 = `hello [123]`;
const res = run(P.many(everything), input1); // this never finishes, i expect "hello "
const res = run(everything, input1); // this finishes, but only reads one char
console.log(JSON.stringify(res, null, 2));
The ultimate goal of this parser is to be able to distinguish tokens (that look like [123]) and all other text, whatever it may be
You need to use the
many
function inside char.ts instead of Parser.tsSince S.spaces matches 0 or more whitespace characters, when you use Parser.many, I believe what is happening is that it keeps matching 0 characters, returning a new parser and then continuing to match 0 characters.