Tune in to the tools and techniques in the Elm ecosystem.
006: elm/parser
May 25, 2020
1:04:49
62.43 MB
Downloads: 0
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
chompIf
andchompWhile
Parser.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
Step
type (Loop
andDone
)
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-markdown
elm-in-elm
parser- 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