Skip to main content

Ast

An abstract syntax tree that contains all the nodes used in the parsed Lua code.
pub struct Ast {
    pub(crate) nodes: Block,
    pub(crate) eof: TokenReference,
}

Methods

nodes

Returns a reference to the entire code block of the AST.
pub fn nodes(&self) -> &Block
&Block
&Block
A reference to the block containing all statements in the AST
Example:
let ast = full_moon::parse("local x = 1; local y = 2")?;
assert_eq!(ast.nodes().stmts().count(), 2);

nodes_mut

Returns a mutable reference to the entire code block of the AST.
pub fn nodes_mut(&mut self) -> &mut Block
&mut Block
&mut Block
A mutable reference to the block containing all statements

eof

Returns the EOF (end-of-file) token at the end of the AST.
pub fn eof(&self) -> &TokenReference
&TokenReference
&TokenReference
The EOF token at the end of every Ast

with_nodes

Returns a new Ast with the given nodes.
pub fn with_nodes(self, nodes: Block) -> Self
nodes
Block
required
The new block to use in the AST
Self
Ast
A new Ast instance with the updated nodes

with_eof

Returns a new Ast with the given EOF token.
pub fn with_eof(self, eof: TokenReference) -> Self
eof
TokenReference
required
The new EOF token to use
Self
Ast
A new Ast instance with the updated EOF token

Display Trait

The Ast implements Display, allowing you to convert it back to Lua code:
use full_moon::parse;

let code = "local x = 1";
let ast = parse(code)?;

// Print the AST back to code
assert_eq!(ast.to_string(), "local x = 1");

AstResult

A produced Ast along with any errors found during parsing. This Ast may not be exactly the same as the input code, as reconstruction may have occurred.
pub struct AstResult {
    ast: Ast,
    errors: Vec<crate::Error>,
}

Methods

ast

Returns a reference to the Ast that was parsed.
pub fn ast(&self) -> &Ast
&Ast
&Ast
A reference to the parsed AST (may contain phantom tokens if there were errors)

into_ast

Consumes the AstResult and returns the parsed Ast.
pub fn into_ast(self) -> Ast
Ast
Ast
The owned Ast instance

errors

Returns all errors that occurred during parsing.
pub fn errors(&self) -> &[crate::Error]
&[Error]
&[Error]
A slice of all parsing errors

into_result

Consumes this AstResult, converting it to a Result.
pub fn into_result(self) -> Result<Ast, Vec<crate::Error>>
Result
Result<Ast, Vec<Error>>
Returns Ok(Ast) if there were no errors, otherwise Err(Vec<Error>)

Example Usage

use full_moon::{parse_fallible, LuaVersion};

let result = parse_fallible("local x = 1", LuaVersion::new());

// Always get an AST, even if there are errors
let ast = result.ast();

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

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

See Also

  • parse - Parse Lua code into an AST
  • parse_fallible - Parse with error recovery
  • Block - The block structure containing statements

Build docs developers (and LLMs) love