Skip to content

Home › Cookbook

Parsing, Linting & Rewriting ​

Ouro's parser produces flat arenas with zero-based half-open [start, finish) byte offsets, built-in node slicing (:text), formatted caret diagnostics (:formatDiagnostic, :formatDiagnostics), and a lossless token tape (lossless = true).

Linting with visit() and text() ​

Walk the arena directly without allocating intermediate AST tables, and flag disallowed calls or patterns using Ouro.Kinds:

luau
--!strict
local Ouro = require("./ouro")
local K = Ouro.Kinds

local source = [[
local function run(x: number)
	local y = loadstring("return 1")
	return x + 1, y
end
]]

local parsed = Ouro.parse(source, { profile = "studio" })
assert(parsed.status == "ok", parsed:formatDiagnostics())

local warnings: { string } = {}
parsed:visit(function(nodeId, kind, _depth)
	if kind == K.AstExprCall then
		local node = parsed.arena.nodes[nodeId]
		local funcId = node.func :: number
		if parsed:kind(funcId) == K.AstExprGlobal and parsed:text(funcId) == "loadstring" then
			local start = parsed:range(nodeId)
			local line, col = parsed:position(start)
			table.insert(warnings, `{line + 1}:{col + 1}: avoid loadstring`)
		end
	end
	return true
end)

assert(#warnings == 1 and warnings[1] == "2:12: avoid loadstring")

Rendering Caret Diagnostics ​

Use parsed:formatDiagnostic(diag) or parsed:formatDiagnostics() to turn raw zero-based byte diagnostics into readable 1-based messages with source context:

luau
--!strict
local Ouro = require("./ouro")

local broken = "local a = 1\nlocal b = (2 + )\nreturn a"
local parsed = Ouro.parse(broken)
assert(parsed.status == "syntaxError")

local formatted = parsed:formatDiagnostics()
assert(string.find(formatted, "line 2, col 16", 1, true) ~= nil)
assert(string.find(formatted, "^", 1, true) ~= nil)

Lossless Source Rewriting ​

Because every node carries exact [start, finish) byte offsets, you can apply sorted non-overlapping span edits to parsed.source while preserving every comment, blank line, and indentation byte outside the edited spans:

luau
--!strict
local Ouro = require("./ouro")
local K = Ouro.Kinds

local source = "-- keep header comment\nlocal oldName = 10\nreturn oldName + 32\n"
local parsed = Ouro.parse(source, { lossless = true })
assert(parsed.status == "ok" and parsed:reconstruct() == source)

type SpanEdit = { start: number, finish: number, replacement: string }
local edits: { SpanEdit } = {}

-- Rename identifier occurrences of `oldName` using lossless lexical tokens
local tape = parsed.tape
assert(tape ~= nil)
local NAME_TOKEN = Ouro.Tokens.Name
for i = 1, tape.count do
	if tape.kinds[i] == NAME_TOKEN then
		local s = tape.starts[i]
		local f = tape.finishes[i]
		if string.sub(source, s + 1, f) == "oldName" then
			table.insert(edits, { start = s, finish = f, replacement = "newName" })
		end
	end
end

-- Apply edits back-to-front so earlier offsets stay valid
local rewritten = source
for i = #edits, 1, -1 do
	local edit = edits[i]
	rewritten = string.sub(rewritten, 1, edit.start) .. edit.replacement .. string.sub(rewritten, edit.finish + 1)
end

local reparsed = Ouro.parse(rewritten, { lossless = true })
assert(reparsed.status == "ok" and reparsed:kind(reparsed.root) == K.AstStatBlock)
assert(rewritten == "-- keep header comment\nlocal newName = 10\nreturn newName + 32\n")