#lexer #lua #parser #lua53 #ast

rslua

Yet another Lua lexer and Lua parser for Lua 5.3

11 releases

0.3.0 Sep 10, 2023
0.2.8 Nov 29, 2022
0.2.6 Jun 22, 2020
0.2.2 May 31, 2020
0.1.0 May 30, 2020

#2223 in Parser implementations


Used in wrk-api-bench

MIT/Apache

295KB
7.5K SLoC

Rust 4K SLoC // 0.1% comments Lua 3.5K SLoC // 0.2% comments

rslua

Crates.io Rust

Yet another Lua lexer and Lua parser for Lua 5.3 written in pure Rust.

Lexer

  • input str
  • output Result<&Vec<Token>, LexError>
use rslua::lexer::Lexer;
let mut lexer = Lexer::new();
let tokens = lexer.run(input_lua_code)?;

Lexer Config

Key Type Default Descripten
use_origin_string bool false Use origin string as token value instead of escaped one.
reserve_comments bool false Reserve comments as tokens.

Parser

  • input &Vec<Token>
  • output Result<Block, SyntaxError>
let mut parser = Parser::new();
let block = parser.run(tokens)?;

AST walker

Use ast_walker to travel the AST, implement the AstVisitor trait to run custom logic.

A complete example

Read Lua source files from ./lua folder, parse them, generate ASTs and walk them through, use a LuaWritter struct which impletements the AstVisitor trait to re-generate formatted Lua source again to ./tmp folder.

See tests/lua_to_lua.rs

Dependencies

~0.3–0.8MB
~19K SLoC