Function inContext

  • This is how you mark that you are in a certain context. For example, here is a rough outline of some code that uses inContext to mark when you are parsing a specific definition:

    Example

    type Context =
    | {
    kind: "Definition";
    str: string;
    }
    | { kind: "List" };

    const functionName = P.variable({
    start: Helpers.isLower,
    inner: Helpers.isAlphaNum,
    reserved: new Set(["let", "in"]),
    expecting: ExpectingFunctionName,
    });

    const definitionBody = (name: string) => {
    P.inContext(Definition(name))(
    succeed(Function(name))
    .apply(arguments)
    .skip(symbol(P.Token("=", ExpectingEquals)))
    .apply(expression)
    )
    }

    const definition: Parser<Expr, Problem> =
    definitionBody.andThen(functionName);

    First we parse the function name, and then we parse the rest of the definition. Importantly, we call inContext so that any dead end that occurs in definitionBody will get this extra context information. That way you can say things like, “I was expecting an equals sign in the view definition.” Context!

    Type Parameters

    • CTX

    Parameters

    • ctx: CTX

    Returns (<A, PROBLEM>(parser: Parser.Parser<A, CTX, PROBLEM>) => Parser.Parser<A, CTX, PROBLEM>)