Elm Radio

006: elm/parser


Listen Later

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 and chompWhile
  • 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.getRow and getCol
  • Parser.getSource
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 and Done)
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
...more
View all episodesView all episodes
Download on the App Store

Elm RadioBy Dillon Kearns, Jeroen Engels

  • 5
  • 5
  • 5
  • 5
  • 5

5

16 ratings