-
Notifications
You must be signed in to change notification settings - Fork 45
Debugging the Reference Parser
Debugging the reference parser is easy!
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"
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"
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")