spotifyovercastrssapple-podcasts

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, 2020
#6

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

Benchmarking

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 and Done)

Error Messages

Getting Started with a Parser Project

There's likely a specification doc if you're parsing a language or formal syntax

Look at examples of parser projects

Look at elm/parser docs and resources

Transcript

[00:00:00]
Hello, Jeroen.
[00:00:02]
Hello, Dillon.
[00:00:04]
How are you doing today?
[00:00:06]
I'm doing pretty well. How are you?
[00:00:08]
I'm good. And I am excited to chat with you about Elm Parsers.
[00:00:14]
And I understand you've done some weekend hacking using Elm Parser for the first time.
[00:00:19]
So I'm excited to hear about your experience with that.
[00:00:21]
Yeah, I tried it. Before I tried it, I was like,
[00:00:24]
I'm going to try it out. I'm going to try it out.
[00:00:26]
I'm excited to hear about your experience with that. Yeah, I tried it. Before I tried it, I ran a Twitter poll asking people,
[00:00:34]
Do you think I will have fun or do you think I will have a lot of pain?
[00:00:39]
And I was kind of surprised that only four people out of 25 replied that I will have pain.
[00:00:47]
So most people would think I was going to have fun.
[00:00:50]
And I kind of heard the opposite. So I'm pretty I was pretty surprised.
[00:00:55]
I found it to be quite easy. But then you get into pitfalls and then it's hard to figure things out.
[00:01:02]
Yeah, I guess there's the question of how hard is writing a parser in Elm compared to doing other things in Elm?
[00:01:11]
And then there's the question of how hard is writing a parser in Elm compared to writing a parser in another language?
[00:01:17]
I would say for the latter, Elm makes it really nice to write parsers.
[00:01:23]
For the question of how hard is writing a parser in Elm compared to doing other things?
[00:01:27]
I mean, I've got my take. What was your initial experience with that? Was it pretty intuitive?
[00:01:33]
Did you run into a lot of things that were confusing and surprising?
[00:01:37]
Well, I didn't get too far. I just basically tried to do an Elm code parser because that's where my interests lie.
[00:01:46]
Just for fun. I don't want to replace Elm syntax.
[00:01:52]
Right. But you depend heavily on an Elm parser project for Elm review, which you've spent a lot of time with the result of a parser.
[00:02:02]
Yeah. And you've thought a lot about the syntax tree that that gives you.
[00:02:06]
And now you're looking at the other side of how you traverse the raw source code to build up that data structure.
[00:02:12]
Yeah, I wrote a compiler back when I was a student. So I do have some experience with parsing.
[00:02:19]
I think I used YACC or LEXT. I don't know. All the things.
[00:02:24]
I think you use both. I think that like LEXT is a lexer and YACC is a parser and you have to like, yeah, find the tokens.
[00:02:33]
Yeah, exactly. And I did the exact same thing in college.
[00:02:36]
And I had a lot of fun doing that.
[00:02:39]
Me too. And that was like using C or C++ and it was still fun.
[00:02:44]
Yeah. Yeah, I had a lot of fun doing that project too. Writing parsers and writing languages were always something that I quite liked.
[00:02:54]
I still think I will write a language parser at some point just for fun, just for kicks.
[00:03:02]
It is satisfying.
[00:03:03]
It is very satisfying, I think.
[00:03:05]
I mean, when you're working with Elm and you can parse things and then having parsed into a nice data structure, you can then use that data structure in Elm and do case statements on this well defined data type.
[00:03:20]
That's really satisfying. So, OK, before we get too far into this, talking about our experiences with parsers and all of that, let's get a definition.
[00:03:29]
What is a parser and what is Elm parser, which we're talking about today?
[00:03:34]
Yeah. So the way I understand it is a parser is something that takes a raw string and then decodes it into something else.
[00:03:46]
A data structure, an abstract syntax tree, concrete syntax tree.
[00:03:51]
Yeah. And then you do whatever you want with it.
[00:03:53]
So you compile a language or you just extract some information like Richard Feldman's ISO 8601.
[00:04:04]
Yes, you got it.
[00:04:08]
I trained so hard before this podcast.
[00:04:13]
That package extracts data information from a string and Elm syntax extracts the abstract syntax tree of Elm code.
[00:04:23]
Right. The input is like a string, like your Elm.
[00:04:26]
If you have an Elm source file, an Elm module, and you feed it to Elm parser to steal 4M Elm syntax, this parser project, you give it a string and then it takes that string and it either fails to parse or it gives you nicely structured data, which represents the abstract syntax tree of Elm.
[00:04:49]
And OK, so we should probably define an abstract syntax tree.
[00:04:53]
Yeah. So an abstract syntax tree. I don't know what it is. I've never played with it.
[00:04:58]
It's a little abstract. Yeah. You haven't spent any time with abstract syntax trees, have you?
[00:05:03]
Yeah. So little. Only five years of my life, something like that.
[00:05:09]
When you've got your code, your Elm code, for instance, it is one giant string.
[00:05:15]
But you have keywords and expressions, A plus B, and they all mean something. And the meaning is represented often as an AST, an abstract syntax tree, where we have removed all the unnecessary information like spacing or the limitation of elements where that doesn't matter.
[00:05:38]
What you get is some kind of representation, often as a tree, that represents what the code means.
[00:05:46]
And then you try to do whatever you want with it.
[00:05:49]
Right. So it's still you don't have things like how much white space something had, if something was on a new line.
[00:05:58]
But if something is defined in a let or as a top level value, that's part of the syntax tree that here's a let. It has these bindings to these expressions.
[00:06:12]
That's part of the abstract syntax tree.
[00:06:14]
Exactly. And if you want the white space information, then what you're dealing with is a concrete syntax tree, which there is none with for Elm at the moment, as far as I know.
[00:06:26]
Right. And a concrete syntax tree might be useful if you're building editor tooling that needs to be able to recreate your exact source code.
[00:06:36]
Whereas an abstract syntax tree, you lose information about how it was written, but you preserve all the information about what the code means in order to execute or compile it.
[00:06:47]
So these terms are helpful and they're useful concepts if you're building a parser.
[00:06:52]
But, you know, the ultimate point is you're turning this source code or some sort of string into some structured data, much like you would do with a JSON decoder.
[00:07:05]
Except that the data is different in JSON, even if it's stringified, you get things by name, usually for when it's a record or JSON object.
[00:07:17]
But with parsing, it's always about ordering. So you get this, then you get that, then you get this. It's what I'm getting.
[00:07:26]
Right. You expect some kind of order, some kind of syntax.
[00:07:31]
Yes. And if those expectations are not met, then you have a parsing failure.
[00:07:37]
That is a really great point. And that's a great way to frame the distinction.
[00:07:41]
So, okay, so JSON decoder, you have something which has already been parsed, actually.
[00:07:47]
I mean, you could do like JSON.decode.decodeString and you could give it malformed JSON.
[00:07:55]
And Elm is going to say, I couldn't parse this. So technically it does the parsing step somewhere in there.
[00:08:02]
But it's basically checking, I mean, under the hood, I'm guessing it does JSON.parse.
[00:08:09]
Right. Okay. So it does JSON.parse. So like the browser is saying, hey, I'm going to take this string and I'm going to parse it for you into well formed JSON.
[00:08:19]
So you have this structure that's already been pieced together. So now you have this structure that has JSON data.
[00:08:26]
It has fields, it has values, and the types are well defined, matching this sort of JSON specification.
[00:08:36]
So you know things about the structure because you've parsed it successfully as JSON.
[00:08:40]
So in a JSON decoder, as you're saying, you can sort of reach in and say, I want this field.
[00:08:46]
And so parsers are very different because parsers, you're going through one character at a time and eating the symbols to define what the structure of the content is.
[00:08:58]
And with JSON, you're sort of dealing with this data type that's already been parsed into a sort of structure and you're making assertions about the shape of that data.
[00:09:10]
Yeah, you're basically already hitting a dictionary with JSON.parse, for records at least.
[00:09:17]
Yes, exactly. Yeah. So you're doing some sort of validations on it.
[00:09:21]
And in a sense, writing a parser, you're doing validations because your parser could fail or succeed. And if it succeeds, then you're going to end up with data of a certain type.
[00:09:32]
But the similarity sort of ends there. A parser is a different category because it's processing things in a way where it's stepping through each character and building up some structure.
[00:09:44]
So I think that's a pretty good introduction to the general concepts we're working with. What is a parser? What's an abstract syntax tree? The distinction between JSON decoders and parsers.
[00:09:57]
So maybe let's dive into the building blocks a little bit that you use to actually define these things and how they work in a way that's going to be familiar if you've done JSON decoders and in a way that's going to feel new if you've done JSON decoders.
[00:10:12]
Yeah. So who do you mean with Elm Parser? With specifically the Elm Parser library. Yes. Which is the official and I think default parser for Elm?
[00:10:22]
Oh, yeah. I mean, it's a really lovely library. It's a unique take on parsing. And Evan built this library. He has a lot of experience working with parsers, having spent a lot of time maintaining the Elm parser itself in Haskell.
[00:10:37]
Yeah. And I believe he took this Haskell parser library, Parsec, and initially built the Elm parser using that and then found he wanted to do things a little differently, both for performance reasons and for maintainability and kind of built his own tool on top of that.
[00:10:55]
Or maybe just from scratch and learned some lessons and applied those lessons to the Elm parser library.
[00:11:02]
Yeah, you really get the feeling that this is made to be very performance. Right. Kind of like painfully so sometimes. But when things work, things are performance right out of the box.
[00:11:17]
Unless you use some construct that was advised against, I guess. Right. And we'll get into some of those topics like backtracking.
[00:11:26]
So like, what was your initial experience? You dived in, you tried writing your first parser. Did you get something to work initially in an intuitive way or did you have to try things out for a while?
[00:11:38]
No, the first few things were very easy, very intuitive. So basically what I tried to do was A equals one with some spacing.
[00:11:47]
Basically, you already had examples doing that. So I was looking for a variable, then some spaces, a symbol, the equal sign, some spaces again, potentially you can ignore those.
[00:12:02]
And then some values. In this case, it was just an integer. So I don't remember what it was for the string, though. But the other things were very simple.
[00:12:12]
Right. OK, so you were able to pretty intuitively get that functioning and get it successfully parsing A equals one.
[00:12:21]
Yeah, I don't think I've even hit an error at the point. So yeah, quite intuitive.
[00:12:27]
Yeah. And I guess there's like a helper that lets you define like an identifier that's.
[00:12:33]
Yeah, I think that's what I used.
[00:12:36]
Which I mean, really, it's not that hard to define yourself, but there are certain rules like you can have numbers in an identifier name, but they can't be the first letter.
[00:12:46]
So you can have A123 equals something, but you can't have 123A equals something because an identifier must start with A through Z, lowercase A through Z in the case of Elm.
[00:12:59]
Yeah, you kind of list the steps of what you're expecting. So that's the order that matters. Kind of like decoding pipeline where you say decode.succeed.
[00:13:12]
You do parser.succeed and then the function that takes the extracted information.
[00:13:19]
And then you do pipelines where you say the first thing that I expect is an identifier, then expect spaces, then expect an equal sign, et cetera.
[00:13:30]
So in that sense, it really feels familiar to decoding when you're used to the decoding pipeline or using decode.map2, map3, et cetera.
[00:13:41]
Right. When all goes well, when you're on that happy path and you're finding the tokens that you expect to define sequentially, you say, I expect zero or more spaces.
[00:13:52]
I expect an equal symbol here. Then that all works as you'd expect. So let's stay on that happy path for a little bit before we veer off of it.
[00:14:02]
Yeah. OK, so when you're on that happy path. So first of all, you mentioned starting with parsing, was it like variable? Is it parser.variable? Is that the helper for that?
[00:14:12]
I can't remember. There's some sort of. Yeah. It's really not the most important detail.
[00:14:18]
But the point is that the library happens to give you a pretty small helper function that defines something for parsing variables, which you could very easily build yourself.
[00:14:29]
But so it is parser.variable. OK, great. So we have parser.variable. And then now we have these sort of I think of them as like keep and discard.
[00:14:45]
Yeah, I was thinking there's like a nicer four letter word for both. But keep and discard is good.
[00:14:51]
But there's this pipe equals like vertical bar equals operator and there's vertical bar dot. So these are operators that you change, just like you would in a JSON decoder pipeline style.
[00:15:03]
You do pipe greater than and pipe a bunch of things through. But you do pipe equals or pipe dot.
[00:15:11]
If you do pipe equals, it's going to capture the result of that parser and include it when you put the results together. And if you do pipe dot, it's going to say.
[00:15:21]
So, for example, if you're doing whitespace, you want to discard it. That's going to be a pipe dot because you say, well, I want to get past this whitespace or it's fine if there's no whitespace.
[00:15:32]
I expect some potentially, but I don't care about it. But I don't care about it.
[00:15:38]
I don't want to use that raw input for something. But if it's like a variable name, you want to get that value and you're going to put that in some data structure that says this is an assignment expression or in a I guess it's a statement in Elm, isn't it?
[00:15:54]
When it's a binding declaration, a declaration.
[00:15:59]
And you need the name of that variable so you can have that in your data structure. So you could have like a let binding where it's you have some string that's the variable name and then some expression that's that it's bound to.
[00:16:15]
Yeah, let's talk about building blocks afterwards.
[00:16:18]
So the next thing I tried was to have other kinds of expressions. So A equals one. So one is an integer, but potentially it could be string.
[00:16:29]
So I tried making a data structure that could accommodate both integers and strings or flows or Booleans, whatever.
[00:16:38]
So I extracted the integer parser, which was just parser int to a new function for parsing expressions or different parser, I guess, because it's not really a function as it is declared.
[00:16:54]
That's right. Just like a JSON decoder is a decoder. It's like a value of type decoder.
[00:17:01]
Yeah, it is probably a function under the hood, but maybe.
[00:17:05]
Right. As far as you know, it could be some magical value that just does the right thing, although it's a sort of hint that it must be a function somewhere because you can do map.
[00:17:18]
And if you can do map, it's got to store your function somewhere.
[00:17:21]
So that is a good point. Very good. But we'll never know. Just.
[00:17:26]
That's right.
[00:17:29]
I extracted that to a parser expression, expression parser, sorry, where I say parser dot one off so it could decode either the expression that I tried to decode is either an integer or something else, a float or string, whatever.
[00:17:51]
I tried a string and that worked fine.
[00:17:53]
Right. Right. And this is going to feel very familiar to people to coming from some experience with JSON decoders and other similar techniques in Elm where you just do one of and you you can combine these things together and they'll try something until it succeeds.
[00:18:09]
So at least when you're on the happy path, it's going to feel like a very familiar concept.
[00:18:14]
And by the way, what you're talking about of sort of defining something that deals with this one part of the parsing as a separate top level parser.
[00:18:24]
I spent a lot of time writing Elmarkdown because it turns out Markdown is a very, very large specification that has a lot of different cases.
[00:18:34]
So like two days worth of work or something.
[00:18:38]
Times some number. Yeah.
[00:18:43]
Huge number. Yeah, I'd say so.
[00:18:47]
And I've found that to be extremely helpful to extract to give yourself the building blocks for your specific parsing domain, because that's the thing is, in a way, in some cases, parsing feels high level.
[00:19:01]
You know, you do parser dot one of parser dot map.
[00:19:05]
Those things feel very high level, but then there are certain things which we're about to get into, which are very low level where you have to go one character at a time.
[00:19:15]
And the thing is, you can define what you need. You say, I wish I had something that could parse in this way.
[00:19:23]
Give yourself that tool. So you keep talking about staying on the happy path.
[00:19:28]
Are we going to walk off the happy path? I'm feeling so ominous.
[00:19:34]
There's a dark cloud off of the happy path. Can you can you hear the thunder?
[00:19:41]
What is the name of the unhappy path? Well, are you afraid of commitment your own?
[00:19:49]
I'll tell my girlfriend I'm not. OK, OK.
[00:19:52]
Well, your experience with parsing may change that because after having spent a lot of time writing parsers, it makes me fear commitment because because when you write a parser,
[00:20:08]
as soon as you chomp a value, as soon as you eat a character, you've committed down that path.
[00:20:15]
And so for me, at least when I first started with parsing, it took a while to get used to. And it almost felt like, wait a minute, this is like there's some state here that feels very unfamiliar because you're writing a JSON decoder and you know,
[00:20:29]
you do one of and you just throw a bunch of things at it. And as long as you have the right thing at the top of your one of.
[00:20:36]
It kind of works as you'd expect. If you put a succeed for a default case in the one of at the top, then it's not going to hit the other ones.
[00:20:44]
That's pretty intuitive, but it doesn't feel like there's this state that it's holding on to.
[00:20:49]
But as soon as you commit down a path with parsing, you've committed down that path.
[00:20:54]
So, yeah, that's what I noticed. That's where things got tricky too.
[00:21:00]
When I tried to parse a float, I was trying first trying to parse an integer. The integer failed and then tried floating, but it went fast. The initial numbers is what I'm understanding.
[00:21:16]
So you are afraid of commitment.
[00:21:19]
Yeah. So you mentioned chomping. So that's a new word for me. And from what I'm getting is I'm imagining Pacman and I don't know if that's the right mental model.
[00:21:30]
So you chomp, you eat.
[00:21:33]
Waka, waka, waka, waka, waka.
[00:21:34]
Yeah, those little balls and then you'd never see them again because you can backtrack.
[00:21:41]
You can go back, but that ball has disappeared forever.
[00:21:44]
That's right. Exactly.
[00:21:46]
Well, in Elm Parser, the backtracking, you get it back, the ball.
[00:21:52]
Right. So perhaps if the default for Elm Parser were backtracking, it would feel more intuitive.
[00:21:59]
Yeah, it feels easier.
[00:22:03]
Yeah, when you're first starting, the behavior might match what you're expecting at first a little bit better.
[00:22:10]
But for performance reasons, it's not a good idea to make everything backtrackable. So by default, you know, there are these helpers like like chomp if and chomp if just says there's chomp if and there's chomp while.
[00:22:24]
So if you say chomp if it takes a function that gives you a single character and then you return true or false.
[00:22:34]
If you're parsing a float, maybe you say chomp while it's one, two, three, four, five, six, seven, eight or nine or zero.
[00:22:44]
Perhaps you start with chomp if one through nine and you don't allow it to start with zero or who knows what your syntax rules are.
[00:22:51]
But the point is, once you've started to chomp, once you chomp something and it succeeds.
[00:22:58]
So if you say chomp if and it gets false as the first chomp statement, then it's going to go down a different path.
[00:23:06]
Yeah, because you're decoding what? Because you're decoding A for instance, which is not expected.
[00:23:11]
Right. Exactly. Exactly. So if you're trying to decode either a float or an int or a string.
[00:23:21]
Or it could be a variable and it's just A, like you said, you start trying to parse a float.
[00:23:28]
You're expecting some sort of number first. Oh, hey, it's not a number. It's the letter A or it's a double quote.
[00:23:34]
Well, I'm not going to go down that path anymore. And that was your first step on that path, your first chomp.
[00:23:41]
And so you're good. You just don't even take a single step down that path.
[00:23:45]
But as you were saying, if you're going to first see if I can parse this into a float and then if that fails,
[00:23:53]
then you're going to see if you can parse it as an integer.
[00:23:57]
Then you first have to chomp onto those integers at the beginning of the float.
[00:24:03]
So you start chomping, you chomp. If it's one, two, three, four, five, you chomp one, you chomp two, you chomp three.
[00:24:12]
And you say, OK, now chomp another numeric character or chomp a dot. And it chomps a dot and you're good.
[00:24:22]
But if it's not one, two, three, four, five, if it's just one, two, three and you're trying to chomp a float,
[00:24:30]
now you do chomp one, chomp two, chomp three. And then you reach a new line and it says, wait a minute,
[00:24:38]
I was expecting either another number or a dot. This isn't a float. And that, OK, that's OK.
[00:24:45]
It fails, but it's not going to go in your one of where you say, try doing a float.
[00:24:50]
If the float fails, try parsing into an integer. Not going to happen.
[00:24:56]
Because you've already taken a step down the float parser path. You're committed. That's committing.
[00:25:02]
Yeah. So when you use decode that one off, then it's really try this.
[00:25:08]
And if at any point it fails, it doesn't matter. We just go to the next one. And that is not going to fail on parser.
[00:25:17]
Exactly. So that's what's unintuitive because it puts it in a particular state.
[00:25:21]
As soon as any chomping occurs, it could fail or it could succeed.
[00:25:27]
But if it partially succeeds, it's not going to hit the other cases in the one of because you've committed down that path.
[00:25:37]
And then the whole decoder fails. And that's for performance reasons. So what do you do instead?
[00:25:44]
There are two ways to approach this. One is just throw backtrackable on it.
[00:25:49]
You do parser dot backtrackable with your float parser. And what's it going to do?
[00:25:54]
It's going to eat the one, eat the two, eat the three. It finds an unexpected character, a new line.
[00:26:02]
Instead of a dot or more characters or more numerical characters. And it says, oh, I failed to decode this, but it's backtrackable.
[00:26:14]
So now it unwinds its commitment. It can go back on the path that it's already taken a few steps down because it's backtrackable.
[00:26:23]
And now it can try your integer parser. Yeah, but that is less efficient.
[00:26:28]
But that's less efficient because now it's stepped through the one, the two, the three, however many characters it's needed to in order to check if it's a float.
[00:26:37]
Now it has to revisit those characters to check if it's an integer.
[00:26:42]
Yeah. OK, so it's very important for performance reasons to avoid backtracking.
[00:26:48]
So how would we write that same parser? We could solve your problem and get your parser working with backtrackable, right? That would work fine.
[00:26:56]
But it's not optimal for performance. So how do we solve that problem without using backtrackable?
[00:27:02]
I'm imagining from the examples that I saw is that you would try to parse an integer, then expect potentially a dot and then some numbers.
[00:27:14]
And if you find the dots and some numbers, then it's a float and otherwise it's an integer.
[00:27:19]
So in both cases, you try to do the integers. And then if it's at least an integer, then try to go the extra path of finding a dot.
[00:27:28]
And if that doesn't work, then it's an integer. Exactly. That's exactly it. Yeah, you got it.
[00:27:33]
So conceptually, you know, conceptually, it's not so complex to actually like write the code for that.
[00:27:41]
It takes a little practice. But just if you if you can wrap your head around what you need to do conceptually to avoid backtracking, then you're half of the way there, you know.
[00:27:52]
And the concept is exactly what you described. You you have a parser where instead of one of for your parsing, where you say, I expect this expression to be either a float or an integer or and you're doing one up for each of those cases.
[00:28:08]
You know, a one of list that contains each of those parsers. Instead of that, you're going to say try parsing it as an integer or float.
[00:28:17]
You have an integer or float parser. And what it's going to do is it's going to have a common parser that captures as many characters as it can until it finds some sort of signal,
[00:28:31]
some sort of signal that tells it it's done and it's an integer like a new line would tell it, OK, this part of the source code that's an integer is done.
[00:28:41]
You can move on to parsing the next thing and we're all done here. Here's the value.
[00:28:46]
So basically, you try to group together the things that start with the same symbols.
[00:28:52]
Exactly, exactly. And then you do a continuation.
[00:28:55]
So you branch off. So you have a single parser that starts out capturing all of the integers.
[00:29:01]
It has that input and then it's going to continue either saying I'm done. It was an integer.
[00:29:06]
We're good. Or, oh, I see a dot. Now I know that it's a float.
[00:29:11]
I have the part that comes before the dot. Now I'm going to continue parsing the part that comes after the dot.
[00:29:17]
And now you commit down the float path, but you started by going down a path that connects to another fork in the road.
[00:29:27]
So, you know, it's very much like just taking a walk in the park.
[00:29:31]
You take a walk in the park and you see a sign that says, oh, there's either a float or an integer down that path.
[00:29:39]
But then the path splits. So you go down that path. You know it takes you to both of those places.
[00:29:44]
You know you want to go to one of those places, but you don't have to decide until you get to the next fork in the road.
[00:29:50]
So you follow the sign that says integers and floats this way. Oh, great. I know I want one of those because I'm looking at a numeric character.
[00:29:57]
You follow that path. Now you hit another fork in the road. That's when you hit either a new line or a dot.
[00:30:04]
Now you have to commit to either integer or float. You've started down that path and you know it's going to be a number.
[00:30:11]
And if you, you know, if you have one, two, three, a, now there's a problem and you've committed down that path.
[00:30:18]
And that's actually the desired behavior. Now you're able to give a message that says, hmm, I was expecting this to be an integer or a float.
[00:30:27]
But because I was going down the integer or float path, but then I saw this character a which doesn't really fit here.
[00:30:33]
So that's exactly what you want in terms of giving feedback to the user that there's some syntax error in their code.
[00:30:40]
And so now you have you've done it in a performant way because you've combined those two paths into a single path.
[00:30:48]
Yeah, I feel like that's a pretty good mental model and that does make it feel pretty approachable.
[00:30:55]
When I learned of all the backtracking and performance issues, I was thinking I will probably have to do some backtracking at some point.
[00:31:03]
But with this specific way of branching things, it all feels pretty doable in my mind.
[00:31:12]
So do you use backtrackable in LMarkdown?
[00:31:16]
Yes, we do use backtrackable in a few cases and we're trying to remove those.
[00:31:23]
But there are some cases where it's a lot of work to fold things into a common path.
[00:31:30]
You know, in the case we were talking about of this is a float or an integer if it starts with a numeric character.
[00:31:37]
That's a that's a pretty straightforward one. But you can imagine as you're folding eight potential paths into one, it becomes more difficult.
[00:31:47]
And so there are a few cases where we're trying to remove those as much as possible.
[00:31:52]
And Fulkart has been doing some really awesome work to improve the performance and making a bunch of pull requests, which has been so, so nice.
[00:32:02]
And he's been been doing some benchmarking there, too, which, by the way, benchmarking.
[00:32:06]
If you're trying to build a parser project and it's like a non trivial parser, you know, it's not like parse a phone number and we use it in the UI once.
[00:32:16]
Right. Like, OK, you're not going to have any performance bottlenecks, even if you use backtrackable. If that gets the job done, it's not going to matter.
[00:32:24]
Yeah, but if you're building something that parses Elm syntax for Elm projects, you're going to notice a performance bottleneck if you're not doing these micro optimizations.
[00:32:33]
And so in cases like that, where performance is critical and where it's like a sort of community asset to have this parsing project,
[00:32:42]
I would recommend benchmark first before you make assumptions about what's going to improve performance.
[00:32:48]
After you've written it, you mean.
[00:32:50]
So, yes, that's a good that's a good question. Step one is definitely write tests, right?
[00:32:55]
Because if you are if you are doing performance tuning before you have benchmarks, that's not good.
[00:33:04]
But what's even worse is to do performance tuning before you have tests. That's a nightmare.
[00:33:12]
I can't even imagine. And in general, with parsers, you want lots of tests and parsers testing in Elm is really nice.
[00:33:22]
Like if you're just testing, I have this source code and I run it through this parser and I expect it to fail in this way here.
[00:33:30]
And I expect it to parse into this data structure here. It's really so easy. It's so much easier than manually testing it with parser projects.
[00:33:40]
There's no reason not to do lots and lots of tests because they're really fast to run. They don't have any side effects.
[00:33:47]
They're just very simple. Well, that's simple, but very straightforwardly.
[00:33:53]
Yeah, they're functional, right?
[00:33:55]
The thing about testing is testing is inherently functional. And when you're testing in, you know, languages that are more, you know, imperative, that have side effects that have environment, you know, objects that have a bunch of state,
[00:34:09]
then you have to like mock things out and stub things and try to capture the side effects that have happened.
[00:34:16]
And so you use all these things that are very messy and brittle and they make you less confident about your test because you're inherently trying to take these things which are nonfunctional.
[00:34:24]
Like side effects and like global state and environment. And you're trying to make them functional as in with this input, I get this output.
[00:34:34]
Well, with Elm, that's all you have. So if you're writing a parser, that's all it is. You give it this input, this source code, which is just a string.
[00:34:44]
You get this data type. It's so easy. It's way easier than doing it without tests.
[00:34:49]
So I cannot recommend highly enough, whether it's a very complicated parser or a very simple parser, just write lots of tests.
[00:34:57]
And certainly before you do any performance tuning, write tests. And before you do performance tuning, even if you've written your tests, benchmark it to figure out where the bottlenecks are.
[00:35:08]
Yeah. Another good thing is that you work with building blocks. So you parse statements and inside of those you use a parser for expressions and you can unit test the expression parser and you can unit test the statement parser.
[00:35:23]
And then you can unit test the whole parser, but you can do it at the level that you need to.
[00:35:29]
That's a really good point. Yeah. And one thing I like to do, we kind of demonstrated it on this live stream that I did with a couple of people who have done a bunch of great contributions to the Elm Markdown project.
[00:35:41]
We did a live stream where we implemented at least a lot of the functionality for the Markdown table parsing for the GitHub flavored Markdown spec.
[00:35:52]
And one of the techniques we use there, which I find makes it a lot easier to do this process, is to just like, I mean, this, I hope that people get sick of me saying this because if they do, then I've accomplished my goal, which is to drill it into people's heads.
[00:36:11]
Start with a hard coded success, get your tests passing as fast as possible. I mean, this is the basic sort of TDD concept of, you know, fake it till you make it where you make it dumb, then make it smarter.
[00:36:25]
Exactly. Exactly. Do the stupidest thing you could possibly think of the first thing that comes to mind. Use all the dirty tricks you can to get it green and then refactor.
[00:36:35]
But now you've got a starting point that you know works. It's all wired through. You have a test that's telling you if you have the expected result or not. Right.
[00:36:43]
So we did that in the live stream where we said, well, I expect if I had like a table like this, like what's the most basic case of a GitHub flavored Markdown table?
[00:36:53]
OK, that's our test case. And I would expect it to decode in to parse into this data structure.
[00:36:59]
So we write that test. It's not compiling. And then what do we do? We use data structure inline.
[00:37:06]
You hard code it. Exactly. We hard code it. And what what function do we use to hard code the result? Succeed? Succeed.
[00:37:15]
Yeah, succeed. Succeed is the key to success. I'm going to make that a T shirt.
[00:37:22]
I want one. Succeed in in monospace font. Succeed is the key to success. That's that's good.
[00:37:30]
I'm going to tweet that. I'm going to tweet that right after this. Give people a sneak peek of this episode.
[00:37:35]
But I really think it's a great tool because it lets you take a small step that, you know, you've got something to work towards and to iterate towards.
[00:37:43]
But you know that the types can all line up. You know that like here's the result I'm looking for and you can break it down into smaller steps.
[00:37:51]
So one thing I think we should talk about is when should you use a parser and when should you use something else like probably a RegEx?
[00:38:02]
Absolutely. So yes, I'm thinking when it gets very complicated. But that's I don't know any specific data points where you should say, oh, this is definitely parser material.
[00:38:17]
So when it gets complicated to use a parser, it's not complicated to use RegEx. So try RegEx and then if it fails, if it doesn't work, try a parser.
[00:38:27]
Yeah, this is this is a great question. And I think there are a few things that come to mind for maybe some some code smells that might point you in the direction of parser.
[00:38:39]
So I like the idea that if you can throw together a RegEx in three minutes that does what you need and it's not super complicated, then great.
[00:38:49]
But let's say that you're working with a RegEx and from Stack Overflow, you have from Stack Overflow is starting to do really complicated things.
[00:39:01]
And perhaps you're capturing a lot of pieces, you have a lot of capture groups. So first of all, the API for dealing with capture groups is, you know, you don't get these nice sort of types where the Elm compiler says, oh, because of the way that you wrote this, you're going to get these types like you do with JSON decoders and things like that.
[00:39:21]
Yeah, it's just like, OK, maybe there are some strings here or maybe not. Like maybe there's a list of things you have to check.
[00:39:29]
And then you always get it as a string, which is not always what you want.
[00:39:33]
Yeah, exactly. So you have to come back around later and check, does this match this RegEx that it's a string or do some other checks on it?
[00:39:46]
If you're doing a lot of that, that's probably a smell that you parser might be a good fit. If you want to give very precise error messages.
[00:39:56]
That's definitely a sign that Elm parser is a good fit, I think. I mean, I think it's safe to say that Elm and Evan's work have been very influential in the broader software development community and set an example of they've kind of set the bar for good error messages.
[00:40:17]
And, you know, a lot of it's in our work too.
[00:40:21]
We have ruthlessly stolen the formatting and inspiration from Elm error messages and so have we were inspired and we were inspired so much that it made us steal ideas.
[00:40:39]
It's the ultimate sign of flattery, right? Yeah.
[00:40:43]
No, I mean, I think that Evan has really been influential in what good error messages can look like.
[00:40:51]
And definitely. He built Elm parser with that in mind, right? So Elm parser gives you some tools to give some really precise, high quality error messages.
[00:41:03]
So there's something else I've been wondering, because in Elm syntax, you get to play with expressions, statements or declarations, or both.
[00:41:15]
And you get to know the location of each element. So you know where this number expression appeared in, where the type signature happens to be.
[00:41:29]
How do you get that information? Is that something complex? Is that something that you do with parser advanced or?
[00:41:36]
It's actually ridiculously easy. That's one task that, yeah, so all you do is if you're trying to capture, like, let's take your example where you're saying A equals one, two, three.
[00:41:52]
When you have your parser that's either going to try an integer or float, then what you can actually do, you can say, actually, so this is my expression parser.
[00:42:02]
So it's going to be one of integer or float, which is that one parser we defined, right?
[00:42:07]
So you reach a fork in the road and that fork is all expressions this way.
[00:42:13]
So this is your starting point. You're standing at this big fork in the road that has all these paths that branch off for all the expression types.
[00:42:20]
Because you know you need an expression here. And so you have, OK, if you go down this path, it's going to be a float or integer.
[00:42:27]
If you go down this path, it's going to be a string. If you go down this path, it's going to be a variable.
[00:42:33]
Well, you can do something called get offset. Yeah. And get offset, you can just capture the value of get offset, and that's just going to give you the line number.
[00:42:44]
I think there's also like get row and get column or maybe it's get row and get call or whatever. But they're actually equivalent.
[00:42:51]
You can derive it from that. But the point is that you just chain it on.
[00:42:56]
So we talked about these pipelines that you build where you say pipe equals and pipe dot.
[00:43:04]
If you say pipe equals, it's always going to succeed and just give you, you say pipe equals get row, pipe equals get column.
[00:43:14]
It's always going to succeed and give you the current row and column as integers.
[00:43:20]
And it's not going to jump either. Exactly. It's just getting a hard coded value based on the state of the parser.
[00:43:29]
Exactly. It will never cause the parser to fail. It doesn't change the state of the parser at all. It just includes that value there.
[00:43:37]
So what you can do is, you know, you're taking a walk in the park, you get to this expression fork where you say, OK, I need to parse some sort of expression.
[00:43:46]
And when you're standing there at that point, you say, oh, let me grab the row and column number.
[00:43:51]
And now you just have that data and you include it as part of your expression. And then when you reach the end of that, one of those paths, you do the same thing.
[00:44:01]
So what you would do is you would say, OK, I'm doing like a let binding parser or I'm doing like, you know, a top level.
[00:44:11]
What's it called? A top level value parser, right? A top level declaration.
[00:44:16]
So I parse some sort of identifier like A and then I parse white space, then I parse equals, then I parse white space, then I parse an expression.
[00:44:28]
But before you parse that expression, like you could just include it in your expression parser where your expression parser is.
[00:44:35]
Get the current row and column. Run the expression parser, then get the current row and column, and then you just include that with your data.
[00:44:43]
So now your data is start set, end set, plus whatever expression data structure you had.
[00:44:49]
Yeah, that sounds pretty simple.
[00:44:52]
Quite straightforward. So that's that's a really nice feature.
[00:44:56]
Plus, I guess that you can just write a helper function that just says get location over a parser.
[00:45:03]
Exactly.
[00:45:04]
OK, that sounds pretty nice.
[00:45:06]
Yeah, maybe one last building block to touch on before we move on to some other topics.
[00:45:13]
There's one more thing called parser dot loop. Did you encounter that at all in your...
[00:45:17]
I encountered it, but I haven't played with it.
[00:45:21]
Yeah, it's an important tool. I don't think we need to cover it in depth here.
[00:45:25]
I think people can look at examples and get a sense of it.
[00:45:28]
But just suffice it to say it exists.
[00:45:33]
It is a tool that you can use to solve certain problems where you where you need to keep track of context.
[00:45:38]
So like if you're doing a regular expression where you wanted to count the number of times that a certain character appears or where like a certain condition is met, regular expression isn't really going to do that for you.
[00:45:52]
It can't help you track state. It just executes, right?
[00:45:55]
Yeah.
[00:45:56]
But if you wanted to write a parser where you do that, then what you can do is you can use this helper called parser dot loop.
[00:46:03]
And what it does is it's effectively like a while loop, which feels weird to do in Elm, but it's an abstraction that feels similar to that.
[00:46:12]
And what you're doing is you're just calling this parser and you have a parser that either returns loop that says keep running the parser or the parser will parse into done.
[00:46:25]
If it parses into done, then the parser will stop. If it parses into loop, then it will continue and it maintains state.
[00:46:32]
So I think of it kind of like a fold expression in Elm where you can do like list dot fold L where like compared to list dot map list dot map,
[00:46:41]
you're just going over every item and you don't have a context that you carry with you.
[00:46:47]
But parser dot loop allows you to retain context as you go through that parsing.
[00:46:55]
So basically you use parser dot loop when you have things that can be duplicated, like you can have several statements in an Elm code file.
[00:47:04]
And when you parse lists, then you have a certain number of elements that is undefined at the beginning.
[00:47:14]
So you loop through those and at every step of the way, you have a parser that says stop here because I found a closing bracket or continue because I found a comma or something.
[00:47:28]
Right. Yeah, yeah, you can you can do that and you can track state as you do that.
[00:47:35]
And that's I think the sort of significant thing about looping is it allows you to maintain that state.
[00:47:42]
And as you say, you you yeah, you can tell it when to terminate running a parser repeatedly until it finds some end condition.
[00:47:53]
What kind of information would you gather, for instance, like if you're parsing a tuple, would you count the number of elements to see if they're bigger than three or something?
[00:48:03]
Yeah, yeah, I think you would. I think you would do that.
[00:48:08]
If you want tuple for elements to be a syntax error, for instance, then you could. OK.
[00:48:14]
Yes, exactly. Yeah, you could do that. And you could you can do that with parser dot problem.
[00:48:20]
But that's exactly right. So like if you yeah, if you just run a parser and you don't have this context from loop, you don't know how many times you've gone through it because you don't have any state.
[00:48:34]
So, yeah, exactly as you say, you say, I am going to parse a tuple, but I need to know how many items have I seen because if it's greater than three, then I'll fail.
[00:48:43]
So that's that's exactly how you would do that. You would do. Yes.
[00:48:49]
So you would do that, just like what you said. But if you couldn't do that, then you would not have a syntax error.
[00:48:55]
You would have a check. Yeah.
[00:48:59]
Because, hey, I have a tuple here. Is it bigger than three? OK, then I have a different problem.
[00:49:05]
Exactly. Exactly. So you'd have to do a to pass to parse it into the raw syntax and then check for syntax errors in this thing that you parsed in a second step rather than.
[00:49:18]
Yeah, exactly as you say, you you can as you're parsing that tuple, you have the context of how many elements you found in that tuple and then you can fail.
[00:49:29]
And the way you fail is, you know, much like we have Jason decode fail.
[00:49:35]
We have parser dot problem. And that allows us to just say if you went down a path that led you here, give this error right now.
[00:49:44]
Is it when the Elm compiler says, hey, I got something very confusing. Is that what it uses on the third, like parser problem?
[00:49:53]
Because I'm expecting when you do parser dot one of this, it says, oh, I was expecting a this or that or this.
[00:50:00]
Right. Probably. So I'm not sure how similar because the Elm parser is written in Haskell.
[00:50:08]
I'm not sure how similar Evans API that he built to do parsing in Haskell for the Elm compiler is to the Elm parser library.
[00:50:16]
I would imagine pretty similar. But yeah, that's that's the idea. You can you can do one of and then you can do parser dot problem as one of those.
[00:50:24]
So you so if you say it's either you say I am going to parse a value that's either a number or a string.
[00:50:34]
So try parsing a number. And of course, as we talked about, if you take a single step down any of those paths, you're committed.
[00:50:43]
So you you say, OK, try parsing a number. If that doesn't work out, try parsing a string.
[00:50:48]
If that doesn't work out, here's the problem. Hey, I expected to see either a number or a string.
[00:50:54]
And you put that as the one of one of parse number parsing problem.
[00:50:59]
I expected number string. Yeah. So what about syntax error messages?
[00:51:06]
So you know, parser dot problem, you can say whatever you want.
[00:51:10]
You can try and make it as helpful as possible with as much knowledge that you have.
[00:51:17]
How do you get other kind of error messages like when you do parser dot one of does it give you a nice error message like I just said before?
[00:51:26]
Or you have to write them yourself. Parser dot one of is basically going to give you whatever error it encounters first.
[00:51:36]
So in Elm parser terminology, that's a dead end.
[00:51:40]
I believe it's just going to hit a dead end and then stop.
[00:51:44]
And then I was expecting a colon or something. Yes.
[00:51:49]
So not that's really useful. Yeah.
[00:51:53]
So the way that you write very precise, expressive error messages with Elm parser to try to get the type of quality of error messages that you see in the Elm compiler,
[00:52:06]
the tools that were given to do that in Elm parser are in parser dot advanced.
[00:52:11]
So this module, parser dot advanced, as I think you saw, it pretty much mirrors the regular parser module.
[00:52:18]
But it's got some a couple of changes and a couple of extra functions.
[00:52:22]
That's what it's for. That was going to be one of my questions. Like, when do you use parser at events?
[00:52:27]
Basically, it's because of the error messages. Is that it?
[00:52:30]
Exactly. So basically, when you do the regular parser module, when you run a parser, there's actually a hard coded list of problems.
[00:52:41]
There's like a problem type in the parser module.
[00:52:43]
So the type parser dot problem is a hard coded type that says I was expecting this token, I was expecting this type of value, or it could just be a string that says this was the problem.
[00:52:54]
If you say parser dot problem, it's going to be your custom problem string.
[00:52:58]
It just gives you a place to provide a string that gives an error. Right.
[00:53:02]
And if you use parser dot advanced now, that problem type is your own custom type.
[00:53:09]
So a problem could be a very expressive custom type that you define and you define how to build up that type as you build up your parsers.
[00:53:19]
But to to give good error messages, you need to know in what state you are.
[00:53:24]
So that's the other thing that parser advanced gives you.
[00:53:28]
Couldn't have segued better myself. So it allows you.
[00:53:32]
So if you look at the parser type that's defined in the parser module and that's defined in the parser advanced module,
[00:53:40]
you'll notice that there are some extra type variables in the advanced type for the context and the type of problem.
[00:53:47]
So in the regular parser module, the parser type has, as I said, a hard coded.
[00:53:54]
This is your type of problem that you could have. And if you have a custom problem, it's a string in the parser advanced.
[00:54:01]
Parser advanced parser type. You have a custom type for your problems and you.
[00:54:07]
Isn't that convenient? A custom type for your problems? What I've needed my whole life.
[00:54:12]
Just simplify it to a custom type. I prefer it to be an empty two.
[00:54:18]
That would be nice, wouldn't it? There's another T.
[00:54:20]
Shirt idea. We'll work on that. And then the other extra type variable that you have for the parser advanced parser type is for the context.
[00:54:31]
So you can have special context in your parser.
[00:54:36]
And so you can you can do something in context. So you could say in the context of parsing a let statement.
[00:54:43]
And so now you have this sort of stack of context that says, OK, I was I was in a let statement and within that let statement, I was in another let statement.
[00:54:52]
And then within that, I was trying to parse a list and then I encountered this error.
[00:54:57]
And so that's basically what the Elm compiler uses, a similar technique to provide you with more context that tells you exactly where the problem is coming from.
[00:55:06]
OK, so you would probably use the regular normal parser module to do simple things like parsing a phone number.
[00:55:15]
If you try to parse a language, then you will probably want to use parser advanced.
[00:55:21]
Yes, exactly. Exactly. Because there's a lot more context and nesting of different types of expressions and that sort of thing.
[00:55:31]
So, yeah, exactly. I think that's a good rule of thumb. And actually, well, it's maybe a bit of a tangent, but I'm actually starting to wonder for my Elm Markdown parser,
[00:55:42]
whether I should just use the regular parser module, because Markdown is unique in that it's not supposed to fail.
[00:55:51]
There's no invalid Markdown. If you have some sort of like closing token that you forgot,
[00:55:59]
like you forgot the closing parenthesis for a link tag in your Markdown. Well, it's just a valid string literal instead of being an actual link block.
[00:56:12]
Yeah, you always have a fallback that is just regular string. Exactly. That's the smallest tangent that you've ever done.
[00:56:22]
My whole life is a tangent, Jeroen. This whole podcast is a tangent.
[00:56:30]
Yeah. OK, well, I think I know how to write an Elm syntax parser now. Just like the matrix. You know, Kung Fu.
[00:56:39]
I know Kung Fu. I know Elm parser. I know Elm parser.
[00:56:44]
If we can give people that feeling with some of our podcast episodes, then I will be happy. Yeah. Let us know.
[00:56:53]
Just tweet at us and say, I know Elm parser and we'll understand.
[00:57:00]
Yeah. You can also say I succeeded at parsing. The key to success is succeed.
[00:57:09]
Succeed is the key to success. Yeah, it's good. I think the monospace font is what what makes it.
[00:57:14]
So it's going to be a better T shirt. Yeah.
[00:57:20]
I think we've covered the basic building blocks pretty well.
[00:57:25]
And of course, there's there's always more to explore. You're always going to find more.
[00:57:30]
But my biggest advice of anything is please, please, please write unit tests.
[00:57:35]
If you're building a project, you will thank me later is very worth it with that in mind.
[00:57:40]
I mean, if you learn one thing, write tests for your parser if you take one thing away.
[00:57:46]
But if you take two things away, maybe we should talk a little bit about some some things to keep in mind when you're starting a project.
[00:57:54]
I think one one thing that is really valuable is if you're writing a parser, there's a good chance that you're working with some sort of specification.
[00:58:03]
And if if there's a specification, there may be a formal specification document for it. Those are very helpful.
[00:58:10]
Like for for dates, there's some specification. What is it?
[00:58:14]
That number again? 80 ISO 8601.
[00:58:20]
That's right. That's right. Oh, yes. That's right. That's the formal specification for that.
[00:58:25]
Yeah. So like for Markdown, there's something called the common mark specification.
[00:58:31]
GitHub flavored Markdown is an extension of that that builds off of that.
[00:58:35]
It's been very handy to be able to look through a formal description of it, and it's it's actually very thoughtfully put together.
[00:58:42]
So it's it's a very useful resource. Actually, for my own Markdown parser, I was able to steal the test suite from the MarksJS project,
[00:58:53]
which what they do is they take all of the Markdown specs and they actually run them as tests.
[00:59:00]
So they say, OK, the Markdown spec gives us all these examples of this Markdown input should give this HTML output.
[00:59:08]
I run all of those thousands of tests on Dillon Kern's L Markdown, and it is excellent.
[00:59:14]
It is so nice. It's like saved me so much time.
[00:59:17]
So use those resources if you can find them. And chances are, if you're writing a parser for something, there are probably good resources for that.
[00:59:25]
That's a great place to start.
[00:59:27]
I think another thing that's very useful is just looking at other people's Elm parser code.
[00:59:33]
There are starting to be more and more examples of this out there.
[00:59:36]
So you can take a look at Dillon Kern's L Markdown and we've done a live stream on that, too.
[00:59:41]
So that's another resource.
[00:59:43]
So when you go to ask for help or do you have any resources to troubleshoot your problems?
[00:59:52]
For sure. If you ask in the Elm Slack, there will be someone to help.
[00:59:55]
If I see someone ask a parser question, I will help them.
[00:59:58]
But I just write lots of tests. It's not easy, but I just write lots of tests and then I keep trying things until the test pass.
[01:00:07]
I'm not a smart man, Jeroen, but I am good at writing tests.
[01:00:11]
Yeah, you're good at hacking is what I heard, too.
[01:00:15]
Well, it's not hacking if you have tests. Then it's very professional and refined.
[01:00:22]
It's performance. That's what it is. Performance.
[01:00:25]
Yes, it makes me happy. It makes me happy to have tests because hacking is just no fun if you don't have tests.
[01:00:32]
But just trying out a bunch of random things until something works with some tests telling you if it actually works.
[01:00:38]
Oh, bliss. Love it. Highly recommended.
[01:00:42]
Yeah, that's really been what I've done is just written a lot of tests and figured it out over time.
[01:00:48]
There are some helpful resources in the Elm Parser repository that kind of explain, give you like a conceptual overview of a few things.
[01:00:57]
So that's a good thing to look at. It kind of talks about backtrackable and those types of things.
[01:01:03]
Yeah, you can feel that Evan gave it quite a bit of love.
[01:01:07]
He did. It's really well written.
[01:01:09]
I would even say that it's one of the best things about Elm actually is the Elm Parser project.
[01:01:16]
So if you love Elm, try the parser.
[01:01:19]
Yeah, it opens up some really cool possibilities.
[01:01:22]
And I think that there's been, as with many of the really wonderful things about Elm, you see this feature in Elm.
[01:01:31]
And then there's this vibrant innovation going on in the ecosystem.
[01:01:35]
And I see Elm Parser as being the same thing that just creates this space for innovation where we see people doing some really cool things.
[01:01:43]
Yeah, you couldn't have Elm pages without the Elm Markdown Parser.
[01:01:48]
And I couldn't have Elm Review without the Elm Syntax Parser.
[01:01:51]
The Elm Syntax Parser. Exactly.
[01:01:53]
That it's opened up some very cool things.
[01:01:57]
Elm pages would still, I would still find it useful even if it was just using the Elm Explorations Markdown.
[01:02:04]
But you're right that I built my Markdown Parser because I wanted to do certain things in the context of like a static site where I wanted to render highly custom views in my Markdown.
[01:02:18]
So, yeah, look at examples.
[01:02:21]
Martin Janacek has his Elm in Elm compiler, which is not fully completed, still a work in progress, but that's something to check out and you can look at his talk on that at Elm Europe.
[01:02:34]
Yeah.
[01:02:35]
Matt Griffith has a really cool project called Elm Markup, which is it's very different from Markdown in that Markdown is designed to never fail.
[01:02:43]
Elm Markup is designed to give you a well defined syntax that will fail in specific cases with nice error messages.
[01:02:52]
So Matt has actually done some really cool stuff with the parser there to both give you very nice error messages.
[01:02:59]
That's actually a great repository to look at if you want to learn how to do precise, expressive error messages.
[01:03:05]
But he also recovers from those errors gracefully so you can have partially rendered views so it can recover from errors and still present you with something when you're in like dev mode.
[01:03:18]
So you got a parser and errors.
[01:03:20]
Yeah, he gives you a parser, nice errors and fault tolerance.
[01:03:27]
So it's a fault tolerant.
[01:03:29]
Parser with nice errors, which is very cool.
[01:03:32]
I recommend taking a look.
[01:03:34]
Teresa has a cool parser project where she does a YAML parser and she gave a talk at Elm Conf a couple of years back, three years back.
[01:03:43]
I don't think we're at this time.
[01:03:46]
But we'll link to that.
[01:03:48]
And it's a very nice introduction to some of the core concepts that we've talked about.
[01:03:53]
She's got like lots of great code examples in her slides, and I definitely recommend watching that.
[01:03:58]
Yeah, and I've got an example in an early somewhere about a equals one.
[01:04:04]
Oh, yeah, we should link to that.
[01:04:05]
Yeah, definitely.
[01:04:06]
Great.
[01:04:09]
All right. Well, I think with that, let's free the people to go play around with Elm parser and build some cool stuff.
[01:04:18]
Maybe we'll see some cool innovations popping up to continue pushing the boundaries with what you can do with Elm.
[01:04:24]
Yeah. Good luck.
[01:04:26]
And have fun, especially have fun as people have told me.
[01:04:31]
Parsers are fun.
[01:04:33]
Parsing Elm is even more fun.
[01:04:35]
Enjoy.