Parsing Org mode with PEGs in Lisp: when regexes become the history of your parser's bugs
Parsing Expression Grammar vs. Regexes: Building Org Parser in Lisp, Export HTML

OrgWebAlchemy is a Guile Scheme library that parses Org-mode documents into an AST and renders them to HTML via SXML, using Parsing Expression Grammars instead of a mountain of regular expressions. The author walks through the grammar, recursive list handling, and customizable output, sharing a hackable free-software alternative to Emacs for Org export.
That is not really describing a language anymore. It is describing the history of your parser's bugs.
- zelphirkalt
Ha, that's interesting.
I started exactly such a project, for org syntax, a while ago. Didn't get very far though because it seems to me that things in org cannot be parsed and understood in one go. For example the TODO keywords can be specified at the top of an org file and then would influence how headings in the document are understood. This cannot be done with merely a PEG.
Another tricky point is nested inline markup. Bold inside italic? Verbatim inside bold, inside italic? And so on. Would be great to support all meaningful combinations via recursive rules. Org has many inline markup things. Even programming language specific inline markup elements.
Another issue is, that Guile's PEG library, when using the non-string form of grammar rules, does not allow mutually recursive grammar rules, due to being buggy (in that way it is not excellent, but in other ways it is!). One has to use the string form or rules, which makes it a lot less nice to use, unfortunately. I hope that will be fixed at some point, because I like the library except for that. And it is in the standard lib, no need to install any dependencies. Like to use it for AoC puzzle inputs for example.
Will be interesting to read how far they got.
EDIT: Also it's going to be great to have an actual grammar based parser for org. This will ease creating one for other tools, like for example parsers of readmes in repos on git hosters.
- remywang
One cool factoid about PEGs is that it is an open problem if they can parse all context free languages.
- le-mark
“Org” in this context is emacs org mode. Seems odd in this day and age that anyone would see hierarchical text format and think “regex”!