Function getChompedString

  • Sometimes parsers like int or variable cannot do exactly what you need. The "chomping" family of functions is meant for that case!

    Remarks

    Maybe you need to parse valid PHP variables like $x and $txt:

        const php: P.Parser<string> = P.getChompedString(
    P.succeed(P.Unit)
    .skip(P.chompIf((c: string) => c === "$"))
    .skip(P.chompIf((c: string) => Helpers.isAlphaNum(c) || c === "_"))
    .skip(P.chompWhile((c: string) => Helpers.isAlphaNum(c) || c === "_"))
    );

    The idea is that you create a bunch of chompers that validate the underlying characters. Then getChompedString extracts the underlying String efficiently.

    Note: Maybe it is helpful to see how you can use getOffset and getSource to implement this function:

        const getChompedString = (parser: P.Parser<any>) => {
    return P.succeed(
    (from: number) => (to: number) => (str: string) => str.slice(from, to)
    )
    .apply(P.getOffset())
    .skip(parser)
    .apply(P.getOffset())
    .apply(P.getSource());
    };

    Parameters

    Returns Simple.Parser<string>