honungsburk/kombo

Typed `CTX`?

Closed this issue · 1 comments

I experimented a lot with having a CTX type parameter but it didn't pan out. The
problem was that all parsers had to have the exact same context or they couldn't be combined.
But some parsers like succeed and problem naturally have a context of never.

The core type of the current parser is

export interface Parser<A, PROBLEM> {
  exec: (s: State) => PStep<A, PROBLEM>;
}

I played around with

export interface Parser<A, CTX, PROBLEM> {
  exec: (s: State<CTX>) => PStep<A, CTX, PROBLEM>;
}

But notice how CTX is in the argument. That is a problem because it means that the parser has access to the CTX so we can not change its type. Instead, I think we can only keep it as a return type because luckily enough no combinator ever uses the context!

export interface Parser<A, CTX, PROBLEM> {
  exec: (s: State) => PStep<A, CTX, PROBLEM>;
}

Whenever you encounter two parsers with different contexts you simply combine them:

export const andThen =
  <A, B, CTX, PROBLEM>(fn: (a: A) => Parser<B, CTX, PROBLEM>) =>
  <CTX2, PROBLEM2>(p: Parser<A, CTX2, PROBLEM2>): Parser<B, CTX | CTX2, PROBLEM | PROBLEM2> 

This was resolved in the 1.0.0 release