Skip to main content

parse

Creates an Ast from Lua code. Will use the most complete set of Lua versions enabled in your feature set.
pub fn parse(code: &str) -> Result<ast::Ast, Vec<Error>>
code
&str
required
The Lua source code to parse
Result
Result<ast::Ast, Vec<Error>>
Returns Ok(Ast) if the code is valid, or Err(Vec<Error>) if parsing fails

Errors

This function will return an error in the following cases:
  • If the code cannot be tokenized, a TokenizerError will be returned
  • If the code is not valid Lua code, an AstError will be returned (specifically AstError::UnexpectedToken)

Example

use full_moon;

// Valid code returns Ok
assert!(full_moon::parse("local x = 1").is_ok());

// Invalid code returns Err
assert!(full_moon::parse("local x = ").is_err());

Usage

use full_moon::parse;

let ast = parse("local function hello() return 'world' end")?;

// Access the parsed nodes
let nodes = ast.nodes();

// Print the AST back to code
println!("{}", ast);

parse_fallible

Given code and a pinned Lua version, will produce an AstResult. This AstResult always produces some Ast, regardless of errors.
pub fn parse_fallible(code: &str, lua_version: LuaVersion) -> ast::AstResult
code
&str
required
The Lua source code to parse
lua_version
LuaVersion
required
The specific Lua version to parse as (e.g., LuaVersion::lua51(), LuaVersion::luau())
AstResult
ast::AstResult
Always returns an AstResult containing both an Ast and any errors encountered

Partial AST Behavior

If a partial Ast is produced (i.e., if there are any errors), a few guarantees are lost:
  1. Phantom tokens - Tokens may be produced that aren’t in the code itself. For example, if x == 2 code() will produce a phantom then token in order to produce a usable If struct. These phantom tokens will have a null position. If you need accurate positions from phantom tokens, you can call Ast::update_positions.
  2. 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 a LocalAssignment that would print to local x =.
  3. Stability - There are no stability guarantees for partial Ast results, but they are consistent within the same exact version of full-moon.

Example

use full_moon::{parse_fallible, LuaVersion};

// Parse with error recovery
let result = parse_fallible("local x = if", LuaVersion::new());

// Always get an AST, even with errors
let ast = result.ast();
println!("Parsed code: {}", ast);

// Check for errors
if !result.errors().is_empty() {
    for error in result.errors() {
        eprintln!("Parse error: {}", error.error_message());
    }
}

// Convert to Result if needed
let ast_or_errors = result.into_result();

When to Use

  • Use parse_fallible when 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 parse when 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

Build docs developers (and LLMs) love