Hacking lpegrex into several small pieces (2023)(Note: this will become a video soon - I hope...) Update: this will become a part of my presentation in the EmacsConf2023. I use Lua mainly from inside Emacs, and I do most of my coding using REPLs, using eepitch and test blocks... and my favorite way of doing OO in Lua is with eoo.lua, that (ta-da!) is very REPL-friendly. So: I decided to learn lpegrex, because it looked like a great tool for writing complex grammars - for example, its grammar for Lua is just this... but I quickly found that 1) it has too many concepts to me, 2) I would like to make lots of small changes and extensions to it, 3) its code is not REPL-friendly at all, 4) the best way to do all that would be to rewrite lpegrex in my style, factoring it in another way, as a lower-level half that is a series of extensions to Lpeg, and on top of that an upper-level half that is an (extensible) re-like language. 1. A prototype: Gram1.luaThis file - Gram1.lua - implements the core ideas and nothing else. It starts with a test block - here - that shows how we can build grammars step by step in a REPL using just plain Lua and Lpeg, and then it defines a class - Gram - that lets us do that, i.e., to build grammars step by step in a REPL, in a more convenient way. Here is a screenshot of its test block: An object of the class Gram holds an "uncompiled grammar". The class Gram defines methods for compiling that grammar (:compile), for compiling and matching (:cm), and for compiling, matching and (pretty-)printing the result (:cmp). 2. Everything: ELpeg1.luaThe name LPegRex means "LPeg Regular Expression eXtended". My hacked version of it is called ELpeg, where the "E" means lots of things at the same time: extended, Emacs, eev, and eepitch... and we can also think that it is "REPL lpeg" with several letters dropped. The file ELpeg1.lua is quite big, but it is composed of several parts - most of them classes - than can be tested separately:
TODO: rewrite all the rest!
|