Skip to content

Fix the json parser example #360

@MarcWeber

Description

@MarcWeber

It doesn't parse repeated ". Try ""A\" B\""".

Drop interpretedEscapes, replace string by

    string: () =>
        token(
          P.string('"')
            .then(
              P.alt(
                P.regexp(/[^"\\]+/), // Match normal characters
                P.string("\\u")
                 .then(P.regexp(/[0-9a-fA-F]{4}/))
                 .map(hex => String.fromCharCode(parseInt(hex, 16))),
                P.string('\\"').result('"'), // Handle escaped double-quote
                P.string("\\\\").result("\\"), // Handle escaped backslash
                P.string("\\n").result("\n"), // Handle escaped newline
                P.string("\\b").result("\b"), // Handle backspace
                P.string("\\f").result("\f"),
                P.string("\\r").result("\r"),
                P.string("\\t").result("\t")
              ).many()
            )
            .skip(P.string('"'))
        )
          .map(chars => chars.join("")) // Convert character array to string
          .desc("string"),

I didn't test everything but at least that bug is fixed. Maybe there are alternative ways.

The problem was that the example string gets parsed as A" B\", the last \ is wrong.
``

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