README

#ast

Abstract Syntax Tree definitions and streaming interpreter for jq expressions.

#Overview

This package provides:

  1. AST Types: Enum definitions for jq expressions, literals, and operators
  2. Interpreter: A streaming evaluator that processes expressions against JSON input
  3. Error Types: Structured errors for runtime evaluation failures

The interpreter uses lazy evaluation via Iter[Json] to enable efficient streaming of results without loading everything into memory.

#Public Functions

#eval

pub fn eval(expr : Expr, input : Json) -> Iter[Json] raise

Evaluate a jq expression against JSON input, returning an iterator of results.

Parameters:
  • expr: The parsed jq expression (from @parser.parse)
  • input: The JSON value to query

Returns:
  • An Iter[Json] that lazily produces results

Raises:
  • InterpreterError on evaluation failures (type mismatches, missing keys, etc.)

Example:
let expr = @parser.parse(".users[] | .name")
let input = @json.parse("{\"users\": [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]}")
for name in @ast.eval(expr, input) {
println(name) // "Alice", then "Bob"
}

#Types

#Expr

pub(all) enum Expr

The expression AST representing all jq constructs. Key variants include:

#Core Expressions

Variantjq SyntaxDescription
Identity.Returns input unchanged
Literal(Literal)null, true, 123, "str"Literal values
Pipe(Expr, Expr)expr \| exprPipeline composition
Comma(Expr, Expr)expr, exprMultiple outputs

#Access Expressions

Variantjq SyntaxDescription
Key(String).fooObject field access
Index(Array[Int]).[0], .[]Array indexing/iteration
Slice(Int?, Int?).[2:4]Array slicing
Optional(Expr)expr?Suppress errors
Recurse..Recursive descent

#Constructors

Variantjq SyntaxDescription
ArrayConstruct(Expr?)[expr], []Build arrays
ObjectConstruct(Array[(Expr, Expr?)]){key: value}Build objects

#Binary Operations

Variantjq SyntaxDescription
Operation(Expr, BinaryOp, Expr)a + b, a == bBinary operations
Alternative(Expr, Expr)a // bAlternative (default)

#Built-in Functions

Variantjq SyntaxDescription
LengthlengthString/array/object length
KeyskeysObject keys or array indices
ValuesvaluesObject values
TypetypeType name as string
EmptyemptyProduce no output
NotnotBoolean negation

#Array Functions

Variantjq SyntaxDescription
Map(Expr)map(expr)Transform each element
Select(Expr)select(expr)Filter elements
SortsortSort array
SortBy(Expr)sort_by(expr)Sort by key
ReversereverseReverse array
Flatten(Int?)flatten, flatten(n)Flatten nested arrays
UniqueuniqueRemove duplicates
UniqueBy(Expr)unique_by(expr)Unique by key
GroupBy(Expr)group_by(expr)Group by key
CombinationscombinationsCartesian product
TransposetransposeMatrix transpose

#Numeric Functions

Variantjq SyntaxDescription
AddaddSum array elements
Min / Maxmin, maxArray min/max
MinBy(Expr) / MaxBy(Expr)min_by(expr), max_by(expr)Min/max by key
Floor / Round / Ceilfloor, round, ceilRounding
AbsabsAbsolute value
SqrtsqrtSquare root
Pow(Expr)pow(exp)Exponentiation
Log / Explog, expNatural log/exponential
Sin / Cos / Tansin, cos, tanTrigonometry
Asin / Acos / Atanasin, acos, atanInverse trig

#String Functions

Variantjq SyntaxDescription
Split(String)split(sep)Split string
Join(String)join(sep)Join array
StartsWith(String)startswith(str)Prefix check
EndsWith(String)endswith(str)Suffix check
Contains(Expr)contains(val)Containment check
LTrimStr(String)ltrimstr(str)Remove prefix
RTrimStr(String)rtrimstr(str)Remove suffix
AsciiUpcase / AsciiDowncaseascii_upcase, ascii_downcaseCase conversion
ExplodeexplodeString to codepoints
ImplodeimplodeCodepoints to string

#Object Functions

Variantjq SyntaxDescription
Has(String)has(key)Key existence check
In(Expr)in(obj)Check if input is key in obj
ToEntriesto_entriesObject to key-value pairs
FromEntriesfrom_entriesKey-value pairs to object
WithEntries(Expr)with_entries(expr)Transform entries
MapValues(Expr)map_values(expr)Transform values

#Control Flow

Variantjq SyntaxDescription
IfThenElse(Expr, Expr, Expr)if c then a else b endConditional
TryCatch(Expr, Expr?)try expr catch handlerError handling
Limit(Int, Expr)limit(n; expr)Limit output count
Until(Expr, Expr)until(cond; update)Loop until condition
While(Expr, Expr)while(cond; update)Loop while condition

#Variables and Iteration

Variantjq SyntaxDescription
Variable(String)$varVariable reference
As(Expr, String, Expr)expr as $v \| bodyVariable binding
Reduce(Expr, String, Expr, Expr)reduce expr as $v (init; update)Fold/reduce
Foreach(...)foreach expr as $v (init; update; extract)Stateful iteration

#Path Functions

Variantjq SyntaxDescription
Path(Expr)path(expr)Get path to value
PathspathsAll paths in input
PathsWithFilter(Expr)paths(filter)Filtered paths
LeafPathsleaf_pathsPaths to leaf values
GetPath(Expr)getpath(path)Get value at path
SetPath(Expr, Expr)setpath(path; value)Set value at path
DelPaths(Expr)delpaths(paths)Delete paths

#Assignment

Variantjq SyntaxDescription
Update(Expr, Expr)path \|= exprUpdate in place
Assign(Expr, Expr)path = exprAssign value
AddAssign(Expr, Expr)path += exprAdd and assign
SubAssign(Expr, Expr)path -= exprSubtract and assign
MulAssign(Expr, Expr)path *= exprMultiply and assign
DivAssign(Expr, Expr)path /= exprDivide and assign
ModAssign(Expr, Expr)path %= exprModulo and assign
AltAssign(Expr, Expr)path //= exprAlternative assign

#Format Strings

Variantjq SyntaxDescription
Format(String)@base64, @uri, @csv, @htmlFormat encodings
StringInterpolation(...)"text \(expr) more"String interpolation

#User-defined Functions

Variantjq SyntaxDescription
FunctionDef(String, Array[String], Expr)def name(params): body;Define function
FunctionCall(String, Array[Expr])name(args)Call function

#Regex (Simple Implementation)

Variantjq SyntaxDescription
Test(String)test(regex)Test if matches
Match(String)match(regex)Get match info
Capture(String)capture(regex)Capture groups
Scan(String)scan(regex)Extract all matches
Splits(String)splits(regex)Split by regex
Sub(String, String)sub(regex; repl)Replace first
GSub(String, String)gsub(regex; repl)Replace all

#Literal

///|
pub(all) enum Literal {
Null
Bool(Bool)
Number(Double)
String(String)
}

Literal values that can appear in jq expressions.

#BinaryOp

///|
pub(all) enum BinaryOp {
Add // +
Subtract // -
Multiply // *
Divide // /
Modulo // %
Equal // ==
NotEqual // !=
LessThan // <
LessEq // <=
GreaterThan // >
GreaterEq // >=
And // and
Or // or
}

Binary operators for arithmetic, comparison, and logical operations.

#InterpreterError

///|
pub(all) suberror InterpreterError {
TypeMismatch(String, String) // (expected, got)
KeyNotFound(String)
IndexOutOfBounds(Int)
InvalidOperation(String)
DivisionByZero
TypeError(String)
EvalError(String)
}

Runtime errors during expression evaluation:

VariantDescription
TypeMismatch(expected, got)Operation received wrong type
KeyNotFound(key)Object key does not exist
IndexOutOfBounds(idx)Array index out of range
InvalidOperation(msg)Operation not supported for types
DivisionByZeroDivision or modulo by zero
TypeError(msg)General type error
EvalError(msg)General evaluation error

#Streaming Semantics

The interpreter uses Iter[Json] for lazy evaluation:

// Results are computed on-demand
let results = @ast.eval(expr, input)

// Only computes what's needed
for r in results.take(5) {
println(r)
}

// Collect all results when needed
let all_results = results.collect()

This enables efficient processing of queries that produce many results without loading everything into memory.

#Internal Structure

The package is organized into multiple files:

  • expression.mbt - Expr enum definition
  • literal.mbt - Literal enum and conversion
  • operator.mbt - BinaryOp enum
  • interpreter.mbt - Main eval function and dispatch
  • interpreter_*.mbt - Specialized evaluators for different expression types
  • interpreter_error.mbt - Error type definitions

#
InterpreterError

pub(all) suberror InterpreterError {
TypeMismatch(String, String)
KeyNotFound(String)
IndexOutOfBounds(Int)
InvalidOperation(String)
DivisionByZero
TypeError(String)
EvalError(String)
} derive(Eq,
Debug
)

Interpreter error types

#
InterpreterError::equal

Preserve method-style access to derived equality operations.

#
InterpreterError::not_equal

fn InterpreterError::not_equal(x : InterpreterError, y : InterpreterError) -> Bool

Preserve method-style access to derived equality operations.

#
InterpreterError::output

fn InterpreterError::output(self : InterpreterError, logger : &Logger) -> Unit

Preserve method-style access to display operations.

#
InterpreterError::to_repr

Preserve method-style access to derived debug representation.

#
InterpreterError::to_string

fn InterpreterError::to_string(self : InterpreterError) -> String

Preserve method-style access to display operations.

#
BinaryOp

pub(all) enum BinaryOp {
Add
Subtract
Multiply
Divide
Modulo
Equal
NotEqual
LessThan
LessEq
GreaterThan
GreaterEq
And
Or
} derive(Eq, ToJson,
Debug
)

Binary operators in jq
impl Show for BinaryOp

#
BinaryOp::equal

fn BinaryOp::equal(BinaryOp, BinaryOp) -> Bool

Preserve method-style access to derived equality operations.

#
BinaryOp::not_equal

fn BinaryOp::not_equal(x : BinaryOp, y : BinaryOp) -> Bool

Preserve method-style access to derived equality operations.

#
BinaryOp::output

fn BinaryOp::output(self : BinaryOp, logger : &Logger) -> Unit

Preserve method-style access to display operations.

#
BinaryOp::to_json

fn BinaryOp::to_json(BinaryOp) -> Json

Preserve method-style access to derived JSON conversion.

#
BinaryOp::to_repr

Preserve method-style access to derived debug representation.

#
BinaryOp::to_string

fn BinaryOp::to_string(self : BinaryOp) -> String

Preserve method-style access to display operations.

#
Expr

pub(all) enum Expr {
Identity
Literal(Literal)
Pipe(Expr, Expr)
Comma(Expr, Expr)
Key(String)
Index(Array[Int])
Slice(Int?, Int?)
Optional(Expr)
ArrayConstruct(Expr?)
ObjectConstruct(Array[(Expr, Expr?)])
Operation(Expr, BinaryOp, Expr)
Length
Keys
Values
Type
Empty
Not
Map(Expr)
Select(Expr)
Sort
SortBy(Expr)
Reverse
Flatten(Int?)
Unique
UniqueBy(Expr)
GroupBy(Expr)
Combinations
Transpose
Add
Floor
Sqrt
Min
MinBy(Expr)
Max
MaxBy(Expr)
Round
Ceil
Abs
Split(String)
Join(String)
StartsWith(String)
EndsWith(String)
Contains(Expr)
Inside(Expr)
Has(String)
In(Expr)
ToEntries
FromEntries
WithEntries(Expr)
MapValues(Expr)
Range(Int)
RangeFromTo(Expr, Expr)
RangeWithStep(Expr, Expr, Expr)
First
FirstGen(Expr)
Last
LastGen(Expr)
Repeat(Expr)
IndicesOf(Expr)
IndexOf(Expr)
Any
AnyGen(Expr, Expr)
All
AllGen(Expr, Expr)
IfThenElse(Expr, Expr, Expr)
TryCatch(Expr, Expr?)
Variable(String)
As(Expr, String, Expr)
Reduce(Expr, String, Expr, Expr)
Foreach(Expr, String, Expr, Expr, Expr?)
Recurse
RecurseWith(Expr, Expr)
Walk(Expr)
Path(Expr)
Update(Expr, Expr)
Assign(Expr, Expr)
Alternative(Expr, Expr)
AddAssign(Expr, Expr)
SubAssign(Expr, Expr)
MulAssign(Expr, Expr)
DivAssign(Expr, Expr)
ModAssign(Expr, Expr)
AltAssign(Expr, Expr)
Format(String)
StringInterpolation(Array[(String, Expr?)])
FunctionDef(String, Array[String], Expr)
FunctionCall(String, Array[Expr])
LTrimStr(String)
RTrimStr(String)
AsciiUpcase
AsciiDowncase
Explode
Implode
ToJsonString
FromJsonString
Nth(Int)
RIndex(Expr)
Paths
PathsWithFilter(Expr)
LeafPaths
GetPath(Expr)
SetPath(Expr, Expr)
DelPaths(Expr)
Limit(Int, Expr)
Until(Expr, Expr)
While(Expr, Expr)
Pow(Expr)
Log
Exp
Sin
Cos
Tan
Asin
Acos
Atan
Test(String)
Match(String)
Capture(String)
Scan(String)
Splits(String)
Sub(String, String)
GSub(String, String)
} derive(Eq, ToJson,
Debug
)

jq expression AST
impl Show for Expr

#
Expr::equal

fn Expr::equal(Expr, Expr) -> Bool

Preserve method-style access to derived equality operations.

#
Expr::not_equal

fn Expr::not_equal(x : Expr, y : Expr) -> Bool

Preserve method-style access to derived equality operations.

#
Expr::output

fn Expr::output(self : Expr, logger : &Logger) -> Unit

Preserve method-style access to display operations.

#
Expr::to_json

fn Expr::to_json(Expr) -> Json

Preserve method-style access to derived JSON conversion.

#
Expr::to_repr

Preserve method-style access to derived debug representation.

#
Expr::to_string

fn Expr::to_string(self : Expr) -> String

Preserve method-style access to display operations.

#
Literal

pub(all) enum Literal {
Null
Bool(Bool)
Number(Double)
String(String)
} derive(Eq, ToJson,
Debug
)

Literal values in jq expressions
impl Show for Literal

#
Literal::equal

fn Literal::equal(Literal, Literal) -> Bool

Preserve method-style access to derived equality operations.

#
Literal::not_equal

fn Literal::not_equal(x : Literal, y : Literal) -> Bool

Preserve method-style access to derived equality operations.

#
Literal::output

fn Literal::output(self : Literal, logger : &Logger) -> Unit

Preserve method-style access to display operations.

#
Literal::to_json

fn Literal::to_json(Literal) -> Json

Preserve method-style access to derived JSON conversion.

#
Literal::to_repr

Preserve method-style access to derived debug representation.

#
Literal::to_string

fn Literal::to_string(self : Literal) -> String

Preserve method-style access to display operations.

#
eval

fn eval(expr : Expr, input : Json) -> Iter[Json] raise

Evaluate an expression with input JSON, returns iterator of results

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io