matchigo-lua

Pattern matching for Lua — composable P.* primitives, optional Rust-style DSL, compiled dispatcher.

$ luarocks install matchigo-lua

matchigo-lua is a pattern-matching engine for Lua. Two API surfaces
share one compile model :

* P.* primitives — immutable, composable pattern descriptors
(P.string, P.between(0, 100), P.tuple(...), P.intersection(...),
P.array(...), P.shape{...}, P.select("label"), and more).
* DSL strings — Rust-style match arms parsed once and compiled to
the same P.* descriptors. Zero runtime overhead versus hand-written P.

Quick taste :

local m = require("matchigo")
local P = m.P

-- Predicate dispatch
m.isMatching(P.string, "hi") --> true
m.isMatching({ kind = "click" },
{ kind = "click", x = 5 }) --> true

-- Compiled dispatcher (O(1) hash on literal rules)
local route = m.compile({
{ with = "GET", handler = function() return list_handler end },
{ with = "POST", handler = function() return create_handler end },
{ otherwise = function() return method_not_allowed end },
})
route("GET")(request)

-- DSL via the chained matcher
local handle = m.matcher({ Num = P.number })
:with("{ user: { age: Num as a } } if a >= 18",
function(b) return "adult:" .. b.a end)
:with("[head, ...tail] if head == 'rm'",
function(b) return rm(b.tail) end)
:otherwise(function() return nil end)

How it stays fast :

* Tests baked at pattern construction (every P.* node carries its own
_test closure ; no central tag-dispatch table at call time).
* Literal-only rule lists collapse to a pure hash lookup at compile.
* Plain shape tests are weak-cached across rule reuse.
* Chained matcher lazy-compiles on first dispatch.
* DSL strings are AST-cached ; re-parsing the same source is free.

Tested on Lua 5.1 / 5.2 / 5.3 / 5.4 and LuaJIT 2.1. Zero external
dependencies. Reproducible benchmarks against native if/elseif chains
and t[key] lookup tables ship with the repo (see bench/results/matrix.md
on GitHub).

Full documentation, full bench results, the DSL grammar reference, and
worked examples live on GitHub — start at the homepage URL.

Versions

1.2.0-119 hours ago0 downloads

Dependencies

lua >= 5.1

Manifests