Skip to content

customSepByWithSequenceOf of tutorial 4 does allow trailing comma #110

@devurandom

Description

@devurandom

With following code, copied almost unchanged from tutorial 4 (changes are in template arguments, to workaround #109):

import {
    char,
    letters,
    Parser,
    possibly,
    sepBy,
    sequenceOf,
} from "arcsecond";

const customSepByWithSequenceOf = <T>(separatorParser: Parser<string>) => (valueParser: Parser<T>) =>
    sequenceOf([
        sepBy(separatorParser)(valueParser) as Parser<T>,//<< https://github.com/francisrstokes/arcsecond/issues/109
        possibly(separatorParser),
    ]).map(([values, _]) => values);

export const parse = (input: string) => customSepByWithSequenceOf(char(","))(letters).run(input);

I get:

❯ pnpm build

> arcsecond-repro2@0.1.0 build [REDACTED]/arcsecond-repro2
> tsc -p .

❯ node
Welcome to Node.js v18.17.1.
Type ".help" for more information.
> p = await import("./dist/parser.js?v1")
[Module: null prototype] { parse: [Function: parse] }
> p.parse("a,b,")
{
  isError: true,
  error: 'ParseError (position 4): Expecting letters',
  index: 4,
  data: null
}

customSepByWithSequenceOf is unable to parse a list with trailing comma, which according to https://github.com/francisrstokes/arcsecond/blob/main/tutorial/tutorial-part-4.md#writing-custom-combinators it should be able to do.

Please find a complete example in https://github.com/devurandom/arcsecond-issue-110-repro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions