Overview
Full Moon provides powerful parsing functions that convert Lua source code into an Abstract Syntax Tree (AST). The parser is lossless, meaning it preserves all whitespace, comments, and formatting.Basic Parsing
The simplest way to parse Lua code is using theparse function:
How It Works
Theparse function:
- Uses the most complete Lua version enabled in your feature set
- Returns
Result<Ast, Vec<Error>> - Fails if the code cannot be tokenized or is invalid Lua
The
parse function automatically selects the appropriate Lua version based on your enabled features. For explicit version control, use parse_fallible with a pinned LuaVersion.Error Handling
Parsing can fail with two types of errors:Getting Error Details
Fallible Parsing
For resilient parsing that always produces some AST (even if invalid), useparse_fallible:
Partial AST Guarantees
When usingparse_fallible with errors:
Phantom Tokens
Tokens may be inserted that aren’t in the code (e.g., a missing
then token). These have null positions initially.Invalid Output
The printed AST may not be valid Lua. Example:
local x = if produces local x = when printed.Pinning Lua Versions
Explicitly control which Lua version to parse:The
LuaVersion::new() constructor automatically selects the most feature-rich version based on your enabled Cargo features.Working with the AST
Once parsed, you can traverse and inspect the AST:Converting Back to Code
Full Moon is lossless - you can convert the AST back to source code:Performance Tips
Reuse Parsers
If parsing many files, the parser is stateless - you can call
parse repeatedly without overhead.Use parse_fallible
For tools like linters that need to work with partial code,
parse_fallible provides better UX.Next Steps
Modifying the AST
Learn how to modify and transform AST nodes
Error Handling
Deep dive into error handling patterns