Lua equivalent to shlex?

Question:

Is there a Lua equivalent for python’s shlex library?

Asked By: RCIX

||

Answers:

I found a reference to a project called LuaParse:

LuaParse includes Blex, a general purpose pure-Lua lexer, and lparse, a pure-Lua Lua parser, as well as a variety of utility functions and a prototype package management system.

Maybe this will meed your needs.

Answered By: Carl Smotricz

See also LPeg.

Answered By: lhf

See also gg, mlp and clopts modules from Metalua.

Those are ‘not-quite’ Lua (as Metalua is a separate language), but may provide some useful insights. Also Metalua code compiles into plain Lua bytecode, so it is reusable.

Answered By: Alexander Gladysh

If the goal is to parse a shell-like language (which appears to be shlex’s actual design goal) then writing a grammar in LPeg as lhf suggests is probably the right answer. However, it is not all that easy to write a grammar that completely implements the hodgepodge of layered quoting rules that shells actually follow. As long as you aren’t expecting to correctly parse all valid /bin/sh (or worse, csh) scripts, it should be straightforward to write a grammar that is useful.

If the goal is actually to create a sensible “little language” for configuration files or application scripting, then I’d suggest that Lua itself is already very close to being that language. It was originally designed with data description in mind, and its parser is quite adept at handling even fairly large data declarations. See section 10.1 of PiL for one example of how to use Lua this way. The later chapter 25 describes integration of Lua with a larger application for the specific use case of configuration, and demonstrates a number of approaches.

Answered By: RBerteig

https://github.com/dromozoa/dromozoa-shlex has shlex.split implementation.

Disclaimer: It’s GPL-3 licensed.

Answered By: Jongwook Choi