开发者

Lua equivalent to shlex?

开发者 https://www.devze.com 2022-12-13 19:00 出处:网络
Is there a Lua equivalent for pyth开发者_开发技巧on\'s shlex library?See also LPeg.I found a reference to a project called LuaParse:

Is there a Lua equivalent for pyth开发者_开发技巧on's shlex library?


See also LPeg.


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.


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.


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.


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

Disclaimer: It's GPL-3 licensed.

0

精彩评论

暂无评论...
验证码 换一张
取 消