parse
Creates anAst from Lua code. Will use the most complete set of Lua versions enabled in your feature set.
The Lua source code to parse
Returns
Ok(Ast) if the code is valid, or Err(Vec<Error>) if parsing failsErrors
This function will return an error in the following cases:- If the code cannot be tokenized, a
TokenizerErrorwill be returned - If the code is not valid Lua code, an
AstErrorwill be returned (specificallyAstError::UnexpectedToken)
Example
Usage
parse_fallible
Given code and a pinned Lua version, will produce anAstResult. This AstResult always produces some Ast, regardless of errors.
The Lua source code to parse
The specific Lua version to parse as (e.g.,
LuaVersion::lua51(), LuaVersion::luau())Always returns an
AstResult containing both an Ast and any errors encounteredPartial AST Behavior
If a partial Ast is produced (i.e., if there are any errors), a few guarantees are lost:-
Phantom tokens - Tokens may be produced that aren’t in the code itself. For example,
if x == 2 code()will produce a phantomthentoken in order to produce a usableIfstruct. These phantom tokens will have a null position. If you need accurate positions from phantom tokens, you can callAst::update_positions. -
Invalid output - The code, when printed, is not guaranteed to be valid Lua. This can happen in the case of something like
local x = if, which will produce aLocalAssignmentthat would print tolocal x =. - Stability - There are no stability guarantees for partial Ast results, but they are consistent within the same exact version of full-moon.
Example
When to Use
- Use
parse_falliblewhen you need to work with partially valid code - Useful for IDE features, syntax highlighting, or error recovery
- Guarantees you always get an AST structure to work with
- Use
parsewhen you only want to process completely valid code
See Also
- Ast - The Abstract Syntax Tree structure
- AstResult - Result type that includes both AST and errors
- Error - Error types returned by parsing
- Lua Versions - Version configuration for parsing