parser

package
v1.74.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package parser defines the AST node types for MX Script and the recursive-descent parser that builds an AST from a token stream.

Package parser implements a recursive-descent parser that converts a stream of tokens (produced by the lexer) into an MX Script AST.

visit.go — utilities for walking an AST. Used by tooling (formatter, coverage reporter, future LSP enhancements) that need to enumerate every statement / expression position.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecutableLines added in v0.31.0

func ExecutableLines(prog *Program) map[int]bool

ExecutableLines walks the program and returns the set of source-line numbers that carry an executable statement. The coverage reporter compares this against the lines that actually ran.

Types

type ArrayLit

type ArrayLit struct {
	Elements []Expr
	// contains filtered or unexported fields
}

func (ArrayLit) Pos

func (p ArrayLit) Pos() (int, int)

type AssignStmt

type AssignStmt struct {
	Target Expr
	Value  Expr
	// contains filtered or unexported fields
}

func (AssignStmt) Pos

func (p AssignStmt) Pos() (int, int)

type BenchDecl added in v1.32.0

type BenchDecl struct {
	Name string
	Body []Stmt
	// contains filtered or unexported fields
}

BenchDecl is `bench "name" { ... }` — the benchmarking sibling of TestDecl. `mx bench` discovers them, runs the body in a calibrated loop, and reports ops/sec.

func (BenchDecl) Pos added in v1.32.0

func (p BenchDecl) Pos() (int, int)

type BinaryExpr

type BinaryExpr struct {
	Op    string
	Left  Expr
	Right Expr
	// contains filtered or unexported fields
}

func (BinaryExpr) Pos

func (p BinaryExpr) Pos() (int, int)

type BoolLit

type BoolLit struct {
	Value bool
	// contains filtered or unexported fields
}

func (BoolLit) Pos

func (p BoolLit) Pos() (int, int)

type BreakStmt added in v0.2.0

type BreakStmt struct {
	// contains filtered or unexported fields
}

BreakStmt exits the nearest enclosing loop.

func (BreakStmt) Pos added in v0.2.0

func (p BreakStmt) Pos() (int, int)

type CallExpr

type CallExpr struct {
	Callee Expr
	Args   []Expr
	// contains filtered or unexported fields
}

func (CallExpr) Pos

func (p CallExpr) Pos() (int, int)

type ContinueStmt added in v0.2.0

type ContinueStmt struct {
	// contains filtered or unexported fields
}

ContinueStmt jumps to the next iteration of the nearest enclosing loop.

func (ContinueStmt) Pos added in v0.2.0

func (p ContinueStmt) Pos() (int, int)

type DestructureBinding added in v0.28.0

type DestructureBinding struct {
	Name    string
	Source  string // empty unless renamed
	Default Expr   // nil if no default
	Rest    bool   // array spread
}

DestructureBinding represents one position in a destructure pattern:

{ name }              -> Name="name", Source="name"
{ name: n }           -> Name="n",    Source="name"
{ name = "anon" }     -> Name="name", Source="name", Default=<expr>
{ name: n = "anon" }  -> Name="n",    Source="name", Default=<expr>
[a]                   -> Name="a"
[...rest]             -> Name="rest", Rest=true
[a = 0]               -> Name="a", Default=<expr>

type DestructurePattern added in v0.27.0

type DestructurePattern struct {
	IsArray bool                 // false = object, true = array
	Items   []DestructureBinding // each binding's source key + local name + optional default
}

DestructurePattern captures `let { a, b }` or `let [a, b]` style bindings.

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is the type implemented by all expression nodes.

type ExprStmt

type ExprStmt struct {
	Expr Expr
	// contains filtered or unexported fields
}

func (ExprStmt) Pos

func (p ExprStmt) Pos() (int, int)

type FnDecl

type FnDecl struct {
	Name   string
	Params []string
	Body   []Stmt
	// ParamTypes parallels Params; entries may be empty when no
	// annotation was given. ReturnType is `-> string` etc, also
	// optional.
	ParamTypes []string
	ReturnType string
	// contains filtered or unexported fields
}

func (FnDecl) Pos

func (p FnDecl) Pos() (int, int)

type FnLit

type FnLit struct {
	Params []string
	Body   []Stmt
	// contains filtered or unexported fields
}

FnLit is an anonymous function literal: `fn(x, y) { ... }`.

func (FnLit) Pos

func (p FnLit) Pos() (int, int)

type GroupStmt added in v0.39.0

type GroupStmt struct {
	Path string
	Body []Stmt
	// contains filtered or unexported fields
}

GroupStmt nests a set of routes under a shared path prefix and an optional shared list of middlewares. e.g.:

group /api/v1 {
  use require_auth
  get /users { ... }
  get /users/:id { ... }
}

func (GroupStmt) Pos added in v0.39.0

func (p GroupStmt) Pos() (int, int)

type Identifier

type Identifier struct {
	Name string
	// contains filtered or unexported fields
}

func (Identifier) Pos

func (p Identifier) Pos() (int, int)

type IfStmt

type IfStmt struct {
	Cond Expr
	Then []Stmt
	Else []Stmt
	// contains filtered or unexported fields
}

func (IfStmt) Pos

func (p IfStmt) Pos() (int, int)

type ImportStmt

type ImportStmt struct {
	Path string
	As   string // empty for flat import, non-empty for namespaced
	// contains filtered or unexported fields
}

ImportStmt represents either a flat import (everything dumped into the current scope) or a namespaced one:

import "./utils.mx"             // everything top-level becomes globally available
import "./auth.mx" as auth      // exports become auth.login(), auth.signup()

When `As` is non-empty, only top-level `let` bindings and `fn` declarations from the imported file are exposed, and they're hung off an object named `As` in the importing file's scope.

func (ImportStmt) Pos

func (p ImportStmt) Pos() (int, int)

type IndexExpr

type IndexExpr struct {
	Object Expr
	Index  Expr
	// contains filtered or unexported fields
}

func (IndexExpr) Pos

func (p IndexExpr) Pos() (int, int)

type LetStmt

type LetStmt struct {
	Name    string              // single-binding form: `let x = ...`
	Pattern *DestructurePattern // destructure form: `let {a,b} = ...` / `let [a,b] = ...`
	Value   Expr
	// Type is an optional annotation — `let x: int = 5`. Decorative
	// today; surfaced in LSP hover and `mx docs` so callers know what
	// shape to expect. Empty string means "no annotation".
	Type string
	// contains filtered or unexported fields
}

func (LetStmt) Pos

func (p LetStmt) Pos() (int, int)

type LoopStmt

type LoopStmt struct {
	Iterable Expr
	IndexVar string
	Var      string
	Body     []Stmt
	// contains filtered or unexported fields
}

LoopStmt is `loop iterable as item { ... }` or `loop iterable as i, item { ... }`. IndexVar is the empty string when no index is requested.

func (LoopStmt) Pos

func (p LoopStmt) Pos() (int, int)

type MatchArm added in v0.5.0

type MatchArm struct {
	Pattern Expr // nil means wildcard `_`
	Body    Expr
}

type MatchExpr added in v0.5.0

type MatchExpr struct {
	Subject Expr
	Arms    []MatchArm
	// contains filtered or unexported fields
}

MatchExpr is `match subject { p1 => e1, p2 => e2, _ => e3 }`. Arms are tested top-to-bottom; the first matching arm's body is the result. A pattern is either an expression (compared with `==`) or the bare identifier `_` which matches anything.

func (MatchExpr) Pos added in v0.5.0

func (p MatchExpr) Pos() (int, int)

type MemberExpr

type MemberExpr struct {
	Object   Expr
	Property string
	Optional bool // `?.` short-circuits if Object is null
	// contains filtered or unexported fields
}

func (MemberExpr) Pos

func (p MemberExpr) Pos() (int, int)

type MiddlewareDecl

type MiddlewareDecl struct {
	Name   string
	Params []string
	Body   []Stmt
	// contains filtered or unexported fields
}

func (MiddlewareDecl) Pos

func (p MiddlewareDecl) Pos() (int, int)

type Node

type Node interface {
	Pos() (line, col int)
}

Node is the base type for every AST node.

type NullLit

type NullLit struct {
	// contains filtered or unexported fields
}

func (NullLit) Pos

func (p NullLit) Pos() (int, int)

type NumberLit

type NumberLit struct {
	Value float64
	// contains filtered or unexported fields
}

func (NumberLit) Pos

func (p NumberLit) Pos() (int, int)

type ObjectLit

type ObjectLit struct {
	Pairs []ObjectPair
	// contains filtered or unexported fields
}

func (ObjectLit) Pos

func (p ObjectLit) Pos() (int, int)

type ObjectPair

type ObjectPair struct {
	Key   string
	Value Expr
}

type ParseError added in v0.2.0

type ParseError struct {
	Line    int
	Col     int
	Message string
}

ParseError carries a structured location so the CLI can render source-context errors with a caret pointer.

func (*ParseError) Error added in v0.2.0

func (e *ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func New

func New(tokens []lexer.Token) *Parser

func (*Parser) Parse

func (p *Parser) Parse() (*Program, error)

Parse consumes the full token stream and returns a Program AST. Any syntax error is returned with file-relative line/column information.

type Program

type Program struct {
	Stmts []Stmt
}

func (*Program) Pos

func (p *Program) Pos() (int, int)

type RangeExpr added in v1.37.0

type RangeExpr struct {
	Start     Expr
	End       Expr
	Inclusive bool
	// contains filtered or unexported fields
}

RangeExpr is `start..end` (exclusive) or `start..=end` (inclusive). Materialises to an array of integers — usable on the RHS of `loop`, `for`, anywhere an iterable is expected.

func (RangeExpr) Pos added in v1.37.0

func (p RangeExpr) Pos() (int, int)

type ReturnStmt

type ReturnStmt struct {
	Value Expr // nil if `return` with no value
	// contains filtered or unexported fields
}

func (ReturnStmt) Pos

func (p ReturnStmt) Pos() (int, int)

type RouteDecl

type RouteDecl struct {
	Method string
	Path   string
	Body   []Stmt
	// contains filtered or unexported fields
}

func (RouteDecl) Pos

func (p RouteDecl) Pos() (int, int)

type ServerBlock

type ServerBlock struct {
	Settings []ObjectPair
	// contains filtered or unexported fields
}

func (ServerBlock) Pos

func (p ServerBlock) Pos() (int, int)

type SpawnStmt added in v0.16.0

type SpawnStmt struct {
	Body []Stmt
	// contains filtered or unexported fields
}

SpawnStmt runs a block in a fresh goroutine. The body shares the enclosing closure (read-only by convention — writes from goroutines race with the main interpreter and other spawns). Use channels for inter-goroutine communication.

func (SpawnStmt) Pos added in v0.16.0

func (p SpawnStmt) Pos() (int, int)

type SpreadExpr added in v0.3.0

type SpreadExpr struct {
	Inner Expr
	// contains filtered or unexported fields
}

SpreadExpr wraps an expression that should be expanded inline inside an array literal, an object literal, or a call argument list.

func (SpreadExpr) Pos added in v0.3.0

func (p SpreadExpr) Pos() (int, int)

type StaticStmt added in v0.3.0

type StaticStmt struct {
	Dir   string
	Mount string // URL prefix; defaults to "/" if not specified
	// contains filtered or unexported fields
}

StaticStmt declares a static-file mount point.

static "./public"            // serves files from ./public at /
static "./assets" at "/cdn"  // serves files from ./assets at /cdn

func (StaticStmt) Pos added in v0.3.0

func (p StaticStmt) Pos() (int, int)

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

Stmt is the type implemented by all statement / top-level declaration nodes.

type StringLit

type StringLit struct {
	Value string
	// contains filtered or unexported fields
}

func (StringLit) Pos

func (p StringLit) Pos() (int, int)

type TestDecl added in v1.29.0

type TestDecl struct {
	Name string
	Body []Stmt
	// contains filtered or unexported fields
}

TestDecl is `test "name" { ... }` — an inline test block discovered by `mx test`. The body is evaluated in a fresh scope per test; failures are reported via assert() / panic. Position is the location of the `test` keyword for nice error messages.

func (TestDecl) Pos added in v1.29.0

func (p TestDecl) Pos() (int, int)

type TryExpr added in v0.7.0

type TryExpr struct {
	Try      []Stmt
	CatchVar string
	Catch    []Stmt
	// contains filtered or unexported fields
}

TryExpr is the expression form of try/catch. The value of the last expression statement in whichever block ran becomes the result.

let parsed = try { json_parse(input) } catch (e) { { error: e.message } }

func (TryExpr) Pos added in v0.7.0

func (p TryExpr) Pos() (int, int)

type TryStmt

type TryStmt struct {
	Try      []Stmt
	CatchVar string
	Catch    []Stmt
	// contains filtered or unexported fields
}

func (TryStmt) Pos

func (p TryStmt) Pos() (int, int)

type UnaryExpr

type UnaryExpr struct {
	Op      string
	Operand Expr
	// contains filtered or unexported fields
}

func (UnaryExpr) Pos

func (p UnaryExpr) Pos() (int, int)

type UseStmt

type UseStmt struct {
	Name string
	// contains filtered or unexported fields
}

UseStmt attaches a named middleware inside a route or globally.

func (UseStmt) Pos

func (p UseStmt) Pos() (int, int)

type WhileStmt added in v0.2.0

type WhileStmt struct {
	Cond Expr
	Body []Stmt
	// contains filtered or unexported fields
}

WhileStmt is `while (cond) { ... }`.

func (WhileStmt) Pos added in v0.2.0

func (p WhileStmt) Pos() (int, int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL