lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 28/03/2019 02.39, Soni "They/Them" L. wrote:
> I'm rolling with this weird idea. is it possible to make a simple parser out of
> tables? I tried some stuff but ran into a few issues. I don't wanna use LPeg or
> anything more complicated than string.find(foo, bar, 1, true).
> 
> e.g.:
> 
> local ltk = {} -- lua_tokens
> ltk.string = {}
> ltk['"'] = "string"
> ltk["'"] = "string" -- how to handle end of string correctly?
> ltk.string['\\'] = "escape"
> ltk.string.escape = {}
> ltk.string.escape['z'] = {}
> ltk.string.escape['z'].next = ltk.string.escape['z']
> for i, v in ipairs({" ", "\t", "\n", etc}) do
>   ltk.string.escape['z'][v] = "next"
> end
> ltk.string.escape['z'][''] = ltk.string -- not sure if there'd be a better way
> to do this
> -- etc

Agree with Coda Highland, nice try.

Here is how I use Lua tables to parse Lua strings: [1]. It is not
finite-state-machine approach, it uses tables as lists. But they
were enough to describe Lua 5.3 syntax and create parser with fixed
number of base functions.

FSMs is still good way to parse less complicated things like
quoted CSV strings or Lua numbers. You may try your approach on
them first.

[1]:
https://github.com/martin-eden/workshop/blob/master/formats/lua/syntax/type_string.lua

-- Martin