Skip to content

Debugging the Reference Parser

Staś Małolepszy edited this page Aug 1, 2018 · 2 revisions

Debugging the reference parser is easy!

map(print)

You can log the Success output of any parser with map(print). In the following identifier parser, map(print) is ued three times to inspect the parsing result after each step:

let identifier =
    sequence(
        charset("a-zA-Z"),
        repeat(
            charset("a-zA-Z0-9_-")))
    .map(print)
    .map(flatten(1))
    .map(print)
    .map(join)
    .map(print);

The result for parsing the string foo = Foo is:

[
    "f",
    [
        "o",
        "o"
    ]
]
[
    "f",
    "o",
    "o"
]
"foo"

bimap(print, print)

map(print) only prints successful parses. In order to dive deeper into why something doesn't parse as expected, you'll likely want to see which parses fail. bimap takes two functions: one will be used to map the Success value and the other will map the Failure value.

let identifier =
    sequence(
        charset("a-zA-Z"),
        repeat(
            charset("a-zA-Z0-9_-")))
    .map(flatten(1))
    .map(join)
    .bimap(print, print);

The result for parsing the string abć = ABĆ is:

"ab"
"regex did not match"

bin/parse.mjs

The bin/parse.mjs CLI utility can be used to parse FTL files. It prints out the resulting AST. Remember to turn on ES Modules with the --experimental-modules flag. (alias mode=node --experimental-modules did it for me).

$ node --experimental-modules bin/parse.mjs path/to/file.ftl

If - is given as the file name, bin/parse.mjs will read standard input until C-C or C-D are pressed. It's helpful to be able to pipe FTL to bin/parse.mjs:

$ echo "x = X" | node --experimental-modules bin/parse.mjs path/to/file.ftl -

Or using process substitution:

$ node --experimental-modules bin/parse.mjs <(echo "x = X")