Skip to main content
Lua 5.4 introduces attributes for local variables, particularly the <const> and <close> attributes.

Feature Flag

To parse Lua 5.4 syntax, you must enable the lua54 feature flag in your Cargo.toml:
[dependencies]
full_moon = { version = "2.1", features = ["lua54"] }
Note: The lua54 feature automatically enables lua53 and lua52, so you get all previous features.

Version-Specific Nodes

Attribute

An attribute on a local variable declaration. Location: full_moon::ast::lua54::Attribute
pub struct Attribute {
    #[node(full_range)]
    #[visit(contains = "name")]
    pub(crate) brackets: ContainedSpan,
    pub(crate) name: TokenReference,
}

Methods

  • new(name: TokenReference) -> Self - Creates a new Attribute with the given name
  • name(&self) -> &TokenReference - The attribute name (e.g., const or close)
  • brackets(&self) -> &ContainedSpan - The angle brackets (< and >) surrounding the attribute
  • with_name(self, name: TokenReference) -> Self - Returns a new Attribute with the given name
  • with_brackets(self, brackets: ContainedSpan) -> Self - Returns a new Attribute with the given brackets

Attribute Types

<const> Attribute

Marks a variable as constant (cannot be reassigned).
local x <const> = 10
-- x = 20  -- Error: attempt to assign to const variable

<close> Attribute

Marks a variable as to-be-closed. When the variable goes out of scope, its __close metamethod is called.
local file <close> = io.open("test.txt", "r")
-- file is automatically closed when it goes out of scope

Examples

Constant Variables

local PI <const> = 3.14159
local MAX_SIZE <const> = 1000

function calculateArea(radius)
  return PI * radius * radius
end

To-Be-Closed Variables

do
  local file <close> = io.open("data.txt", "r")
  local content = file:read("*a")
  print(content)
  -- file:close() is called automatically here
end

Multiple Attributes

local x <const> = 5
local y <close> = setmetatable({}, {
  __close = function()
    print("Cleaning up")
  end
})

Usage

use full_moon::parse;
use full_moon::ast::lua54::Attribute;

let ast = parse(r#"
  local x <const> = 10
  local file <close> = io.open("test.txt")
"#)?;

Inherited Features

Since lua54 enables lua53 and lua52, you also get:
  • Bitwise operators (see Lua 5.3)
  • Goto statements and labels (see Lua 5.2)
  • All Lua 5.2 and 5.3 syntax features

Notes

  • Attributes are only valid on local variable declarations
  • A variable can only have one attribute
  • The attribute must immediately follow the variable name
  • Constant variables must be initialized when declared
  • To-be-closed variables require a value with a __close metamethod (or nil/false)

Build docs developers (and LLMs) love