@eryx/luau/parse Module

Luau source code parser and comment utilities.

Wraps the native Luau parser to produce a fully typed AST. Also provides helpers for extracting comment text from surrounding-text annotations and for round-trip pretty-printing.

local parse = require("@eryx/luau/parse")

local result = parse.parse(source, { captureComments = true })
print("Lines:", result.lines)
for _, stat in result.root.body do
    print(stat.type, stat.location.beginline)
end

-- Extract attached comments from surrounding text
local text = result.surroundingText and result.surroundingText[result.root.body[1]]
if text then
    local comments = parse.extractComments(text.leading, { stripLineLeadingWhitespace = true })
    print(comments[1])
end

Summary

Functions

parse.parse(source: string, options: ParseOptions?)ParseResult

API Reference

Functions

parse.parse

Parses Luau source code and returns a structured AST.

The returned root is a Block node whose body contains an array of statement nodes. Every node carries a type string discriminator and a location with 1-based line/column positions.

Pass { captureComments = true } to include a comments array in the result. Pass { collectSurroundingText = true } to include a sparse surroundingText side table keyed by AST node.

local result = parse.parse(source, { captureComments = true })
print("Lines:", result.lines)
print("Errors:", #result.errors)
print("Top-level statements:", #result.root.body)
parse.parse(source: string, options: ParseOptions?)ParseResult

Parameters

source: string

The Luau source code string to parse.

options: ParseOptions?

Parse options (e.g. comment capture).

Returns

The parse result containing the AST root, errors, and optional comments.

parse.prettyPrint

Pretty-prints Luau source code by round-tripping through the parser.

parse.prettyPrint(source: string)string

Parameters

source: string

The Luau source code to format.

Returns

The reformatted source code.

parse.extractComments

Extracts comment content from raw surrounding text.

This is intended for use with strings from result.surroundingText[node].leading and .trailing. The parse result keeps those strings lossless; this helper turns them into comment payloads on demand.

When stripLineLeadingWhitespace is true, line comments like -- hello are returned as "hello" instead of " hello".

parse.extractComments(text: string, options: ExtractCommentsOptions?){ string }

Parameters

text: string

Raw surrounding text.

Comment extraction options.

Returns

{ string }

Extracted comment strings.

Types

Position

type Position = ast.Position
Implements: ast.Position

Location

type Location = ast.Location
Implements: ast.Location

ParseErrorInfo

Implements: ast.ParseErrorInfo

CommentInfo

Implements: ast.CommentInfo

SurroundingText

Implements: ast.SurroundingText

ParseOptions

Implements: ast.ParseOptions

AstNode

type AstNode = ast.AstNode
Implements: ast.AstNode

ParseResult

Implements: ast.ParseResult

ExtractCommentsOptions

type ExtractCommentsOptions = { unindentMultiLineContent: boolean?, stripLineLeadingWhitespace: boolean?, merge: ("none" | "same" | "all")? }
unindentMultiLineContent: boolean?

If true, strip the common leading indentation from block comment content.

stripLineLeadingWhitespace: boolean?

If true, strip leading whitespace from line comment content (e.g. -- hello -> "hello").

merge: ("none" | "same" | "all")?

Controls merging of adjacent comments. "none" (default) keeps each comment separate, "same" merges adjacent same-kind comments, "all" merges all adjacent comments.