Skip to main content
Lua 5.2 introduces goto statements and labels for control flow.

Feature Flag

To parse Lua 5.2 syntax, you must enable the lua52 feature flag in your Cargo.toml:
[dependencies]
full_moon = { version = "2.1", features = ["lua52"] }

Version-Specific Nodes

Goto

A goto statement that jumps to a label. Location: full_moon::ast::lua52::Goto
pub struct Goto {
    pub(crate) goto_token: TokenReference,
    pub(crate) label_name: TokenReference,
}

Methods

  • new(label_name: TokenReference) -> Self - Creates a new Goto with the given label name
  • goto_token(&self) -> &TokenReference - The goto symbol
  • label_name(&self) -> &TokenReference - The label name
  • with_goto_token(self, goto_token: TokenReference) -> Self - Returns a new Goto with the given goto token
  • with_label_name(self, label_name: TokenReference) -> Self - Returns a new Goto with the given label name

Example

goto cleanup
print("This won't execute")

::cleanup::
print("Jumped to cleanup")

Label

A label that marks a position for goto statements. Location: full_moon::ast::lua52::Label
pub struct Label {
    pub(crate) left_colons: TokenReference,
    pub(crate) name: TokenReference,
    pub(crate) right_colons: TokenReference,
}

Methods

  • new(name: TokenReference) -> Self - Creates a new Label with the given name
  • left_colons(&self) -> &TokenReference - The :: symbol on the left
  • name(&self) -> &TokenReference - The label name
  • right_colons(&self) -> &TokenReference - The :: symbol on the right
  • with_left_colons(self, left_colons: TokenReference) -> Self - Returns a new Label with the given left colons
  • with_name(self, name: TokenReference) -> Self - Returns a new Label with the given name
  • with_right_colons(self, right_colons: TokenReference) -> Self - Returns a new Label with the given right colons

Example

::start::
local x = 1

::loop::
x = x + 1
if x < 10 then
  goto loop
end

::done::
print("Finished")

Usage

use full_moon::parse;
use full_moon::ast::lua52::{Goto, Label};

let ast = parse(r#"
  goto skip
  print("skipped")
  ::skip::
  print("executed")
"#)?;

Notes

  • Labels must be enclosed in double colons: ::label::
  • Goto statements can only jump to labels within the same function scope
  • You cannot jump into the scope of a local variable
  • Lua 5.2 also includes other features like \z escape sequences in strings and fractional hexadecimal numbers, which are automatically supported when the lua52 feature is enabled

Build docs developers (and LLMs) love