Skip to main content

Crate oak_lua

Crate oak_lua 

Source
Expand description

ยง๐Ÿ› ๏ธ Lua Parser Developer Guide

This guide is designed to help you quickly get started with developing and integrating oak-lua.

ยง๐Ÿšฆ Quick Start

Add the dependency to your Cargo.toml:

[dependencies]
oak-lua = { path = "..." }

ยงBasic Parsing Example

The following is a standard workflow for parsing Lua scripts, including support for tables, functions, and control flow:

use oak_lua::{LuaParser, SourceText, LuaLanguage};

fn main() {
    // 1. Prepare source code
    let code = r#"
        local function greet(name)
            local message = "Hello, " .. name
            print(message)
        end

        local user = {
            name = "Oak",
            age = 1
        }

        greet(user.name)
    "#;
    let source = SourceText::new(code);

    // 2. Initialize parser
    let config = LuaLanguage::new();
    let parser = LuaParser::new(&config);

    // 3. Execute parsing
    let result = parser.parse(&source);

    // 4. Handle results
    if result.is_success() {
        println!("Parsing successful! AST node count: {}", result.node_count());
    } else {
        eprintln!("Errors found during parsing.");
        for diag in result.diagnostics() {
            println!("[{}:{}] {}", diag.line, diag.column, diag.message);
        }
    }
}

ยง๐Ÿ” Core API Usage

ยง1. Syntax Tree Traversal

After a successful parse, you can use the built-in visitor pattern or manually traverse the Green/Red Tree to extract Lua specific constructs like function definitions, table constructors, local variable declarations, and loops.

ยง2. Incremental Parsing

Lua scripts can grow large in game development. oak-lua supports sub-millisecond incremental updates:

// Re-parse only the modified section
let new_result = parser.reparse(&new_source, &old_result);

ยง3. Error Recovery

The parser is designed for industrial-grade fault tolerance, recovering gracefully from missing end keywords or malformed table syntax to provide continuous feedback in IDEs.

ยง๐Ÿ—๏ธ Architecture Overview

  • Lexer: Tokenizes Lua source text, supporting Lua-specific long strings ([[ ... ]]), long comments, and numeric literals.
  • Parser: A high-performance recursive descent parser with Pratt parsing for expressions, handling Luaโ€™s operator precedence and table constructors.
  • AST: A strongly-typed, lossless syntax tree that preserves all trivia (comments/whitespace) for refactoring and formatting tools.

ยง๐Ÿ”— Advanced Resources

  • Full Examples: Check the examples/ folder in the project root.
  • API Documentation: Run cargo doc --open for detailed type definitions.
  • Test Cases: See tests/readme.md for details on our snapshot-based testing. Lua support for the Oak language framework.

Re-exportsยง

pub use crate::ast::LuaRoot;
pub use crate::builder::LuaBuilder;
pub use crate::language::LuaLanguage;
pub use crate::lexer::LuaLexer;
pub use crate::parser::LuaParser;
pub use crate::lsp::highlighter::LuaHighlighter;
pub use crate::lsp::LuaLanguageService;
pub use crate::mcp::serve_lua_mcp;
pub use lexer::token_type::LuaTokenType;
pub use parser::element_type::LuaElementType;

Modulesยง

ast
AST module.
builder
Builder module.
language
Type definitions module. Language configuration module.
lexer
Lexer module.
lsp
LSP module.
mcp
MCP module.
parser
Parser module.