
Episode 6
006: elm/parser
We discuss parsers, how to build them in Elm, and how to try to make your error messages as nice as Elm's.
May 25, 20201h 4m
Audio is streamed directly from the publisher (cdn.simplecast.com) as published in their RSS feed. Play Podcasts does not host this file. Rights-holders can request removal through the copyright & takedown page.
Show Notes
What is a parser?
- yacc/lex
- AST (Abstract Syntax Tree) vs. CST (Concrete Syntax Tree)
- JSON decoding vs. parsing
- JSON decoding is validating a data structure that has already been parsed. Assumes a valid structure.
elm/parser- Haskell parsec library - initially used for the Elm compiler, now uses custom parser
What is a parser?
- One character at a time
- Takes input string, turns it into structued data (or error)
Comitting
- Backtrackable parsers
chompIfandchompWhileParser.oneOf
Benchmarking
elm-explorations/benchmark- Benchmark before making assumptions about where performance bottlenecks are
- Write unit tests for your parser
- GFM table parsing live stream
Parser.succeed
Elm regex vs elm parser
Indications that you might be better off with parser
- Lots of regex capture groups
- Want very precise error messages
Getting source code locations
Parser.loop
- Loop docs in
elm/parser - Looping allows you to track state and parse groups of expressions
- Loop over repeated expression type, tell it termination condition with
Steptype (LoopandDone)
Error Messages
- You can fail with Parser.problem
- Parser.Advanced module is designed to give you more precise context and error messages on failure
- Parser.Advanced.inContext
Getting Started with a Parser Project
- Write lots of unit tests with
elm-test!
There's likely a specification doc if you're parsing a language or formal syntax
- CommonMark Spec
- GitHub-Flavored Markdown Spec
- dillonkearns/elm-markdown test results directory from executing spec examples
Look at examples of parser projects
dillonkearns/elm-markdownelm-in-elmparser- elm-in-elm conference talk
mdgriffith/elm-markup- good reference for parsing, fault-tolerant parsing, and giving nice error messages- Tereza's YAML parser
- Tereza's elm conf talk "Demystifying Parsers"
- Jeroen's elm/parser Ellie
- "It's not hacking if you have tests."
Look at elm/parser docs and resources
- elm/parser project's semantics document describes backtrackable behavior in detail