Help us shape the future of Go! Take the 2026 Go Developer Survey

ast

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package ast defines the typed Go AST that mirrors the just-bash TypeScript shape in src/ast/types.ts. It is the public AST surface produced by parser.Parse / parser.ParseString and consumed by the (future, Phase 13) transform plugin pipeline.

Stability

Add new node types freely; renaming or removing exported fields is a breaking change. Each compound-command type leaves room for parity-only fields that are not yet populated by the translator — Phase 4 ships the public shape; later phases (transform plugins in Phase 13, interpreter bridge in Phase 5) drive incremental fills.

Origin pointer

Script carries an Origin field pointing back at the mvdan.cc/sh/v3/syntax.File the parser produced. transform.Serialize uses Origin to byte-faithfully render the script back to bash source. Origin is nil for plugin-synthesized scripts (Phase 13+); the inverse translator in the transform package handles that case via best-effort reconstruction with default positions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alternative

type Alternative struct {
	Word      *Word
	ColonNull bool
}

Alternative: ${var+word} (or ${var:+word} when ColonNull).

type AnsiCQuoted

type AnsiCQuoted struct{ Value string }

AnsiCQuoted is the $'...' bash-specific quote form with C-style escapes. We keep the raw inner text and let the runtime decode.

type ArithCmd

type ArithCmd struct {
	Expr string
	Line int
}

ArithCmd: ((expr)).

type ArithmeticExpansion

type ArithmeticExpansion struct {
	Expr string
}

ArithmeticExpansion captures $((expr)). We keep the expression source verbatim and let the interpreter (or a runtime transform) evaluate it.

type ArrayElement

type ArrayElement struct {
	Index string // raw expression text inside [...]
	Value *Word
}

ArrayElement is one element of an array literal. Index is "" for positional entries.

type ArrayInit

type ArrayInit struct {
	Elements []*ArrayElement
}

ArrayInit holds an array literal: `(a b c)` or `([k]=v ...)`.

type Assign

type Assign struct {
	Word      *Word
	ColonNull bool
}

Assign: ${var=word} (or ${var:=word} when ColonNull).

type Assignment

type Assignment struct {
	Name   string
	Value  *Word
	Append bool
	Array  *ArrayInit
	Line   int
}

Assignment is a `NAME=value` or `NAME+=value` (Append) shell assignment. Array is non-nil for `NAME=(elem ...)` form.

type CStyleFor

type CStyleFor struct {
	Init string
	Cond string
	Post string
	Body []*Statement
	Line int
}

CStyleFor: for ((init; cond; post)); do body; done.

type CaseItem

type CaseItem struct {
	Patterns   []*Word
	Body       []*Statement
	Terminator string // ";;", ";&", ";;&"
}

CaseItem is one pattern → body branch.

type CaseModify

type CaseModify struct {
	Pattern *Word
	Op      string // "^", "^^", ",", ",,"
}

CaseModify: ${var^}, ${var^^}, ${var,}, ${var,,}.

type CaseStmt

type CaseStmt struct {
	Subject *Word
	Items   []CaseItem
	Line    int
}

CaseStmt: case word in pat) ... ;; esac.

type Command

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

Command is one element of a Pipeline. Every command type also implements Node.

type CommandSubstitution

type CommandSubstitution struct {
	Body     []*Statement
	Backtick bool
}

CommandSubstitution is $(...) or `...` (Backtick).

type CondCmd

type CondCmd struct {
	Expr string
	Line int
}

CondCmd: [[ expr ]]. The Expr text is kept verbatim for Phase 4 — a proper CondExpr tree lands when we wire conditional evaluation in Phase 5/6.

type DBracket

type DBracket = CondCmd

DBracket is the spec's alternate spelling for CondCmd. Defined as a type alias for API parity with src/ast/types.ts.

type DefaultValue

type DefaultValue struct {
	Word      *Word
	ColonNull bool
}

DefaultValue: ${var-word} (or ${var:-word} when ColonNull).

type DoubleQuoted

type DoubleQuoted struct{ Parts []WordPart }

DoubleQuoted holds the inner parts of a "..." word part.

type ErrorOp

type ErrorOp struct {
	Word      *Word
	ColonNull bool
}

ErrorOp: ${var?word} (or ${var:?word} when ColonNull).

type ExtGlob

type ExtGlob struct {
	Op      string
	Pattern string
}

ExtGlob captures bash extended glob patterns: ?(pat), *(pat), +(pat), @(pat), !(pat). Op is one of "?", "*", "+", "@", "!".

type ForStmt

type ForStmt struct {
	Var   string
	Words []*Word
	Body  []*Statement
	Line  int
}

ForStmt: for x in words; do body; done.

type FunctionDef

type FunctionDef struct {
	Name string
	Body Command
	Line int
}

FunctionDef: name() { ... } or function name { ... }.

type Group

type Group struct {
	Body         []*Statement
	Redirections []*Redirection
	Line         int
}

Group: { cmds; }.

type Heredoc

type Heredoc struct {
	Tag       string
	Body      string
	StripTabs bool // true for <<-
	Expand    bool // false when tag was quoted
}

Heredoc carries the body of a here-document.

type IfBranch

type IfBranch struct {
	Cond []*Statement
	Body []*Statement
}

IfBranch is one (if|elif) condition + body pair.

type IfStmt

type IfStmt struct {
	Branches []IfBranch
	Else     []*Statement
	Line     int
}

IfStmt: if/elif/else/fi.

type Keys

type Keys struct {
	SplitWords bool
}

Keys: ${!array[@]} or ${!array[*]}.

type Literal

type Literal struct{ Value string }

Literal is unquoted raw text inside a word.

type Names

type Names struct {
	SplitWords bool // true for @, false for *
}

Names: ${!prefix*} / ${!prefix@}.

type Node

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

Node is the closed type implemented by every AST node. The nodeMarker method is unexported to prevent external implementations.

type ParamOp

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

ParamOp is the closed interface implemented by every parameter expansion modifier.

type ParameterExpansion

type ParameterExpansion struct {
	Parameter string
	Operation ParamOp
	Indirect  bool   // ${!var}
	Length    bool   // ${#var}
	Index     string // raw text inside subscript [...]; "" if none
}

ParameterExpansion captures $var, ${var}, ${var-default}, etc. Parameter is the variable name (or special parameter like "?", "0", "@"). Operation is one of the concrete *ParamOp* types below, or nil for a bare ${var} reference.

type PatternRemove

type PatternRemove struct {
	Pattern *Word
	Op      string // "#", "##", "%", "%%"
}

PatternRemove: ${var#pat} ${var##pat} ${var%pat} ${var%%pat}.

type Pipeline

type Pipeline struct {
	Commands   []Command
	PipeStderr []bool // length = len(Commands) - 1
	Negated    bool
	Line       int
}

Pipeline is a sequence of Commands connected by | (or |& when the corresponding PipeStderr entry is true). Negated is true when the leftmost pipeline element carries a leading `!`.

type ProcessSubstitution

type ProcessSubstitution struct {
	Direction string // "<" or ">"
	Body      []*Statement
}

ProcessSubstitution captures <(cmds) and >(cmds).

type Redirection

type Redirection struct {
	FD      int      // -1 when not specified
	Op      string   // ">", ">>", "<", "<<", "<<-", "<<<", "<>", ">|", "&>", "&>>", "<&", ">&"
	Word    *Word    // target word
	Heredoc *Heredoc // non-nil for <<, <<-
}

Redirection is a single input/output redirection attached to a command.

type Replace

type Replace struct {
	Pattern     *Word
	Replacement *Word
	All         bool
}

Replace: ${var/pattern/replacement} (All for //, First for /).

type Script

type Script struct {
	Statements []*Statement
	Line       int

	// Origin is the back-reference to the original mvdan.cc/sh/v3
	// *syntax.File the parser walked. transform.Serialize uses it for
	// byte-identical round-trips. Nil for plugin-synthesized scripts.
	Origin *syntax.File
}

Script is the root of a parsed bash file.

type SimpleCommand

type SimpleCommand struct {
	Assignments  []*Assignment
	Name         *Word // may be nil for assignment-only statements
	Args         []*Word
	Redirections []*Redirection
	Line         int
}

SimpleCommand is a leaf command: zero or more inline assignments followed by a command name and its arguments, plus any redirections.

type SingleQuoted

type SingleQuoted struct{ Value string }

SingleQuoted holds a single-quoted string: '...'.

type Statement

type Statement struct {
	Pipelines  []*Pipeline
	Operators  []string // each "&&" or "||"; len = len(Pipelines)-1
	Background bool
	Line       int
}

Statement is one and-or list: a sequence of pipelines joined by && or || (Operators), optionally terminated with & (Background).

Note: the just-bash AST overloads Operators to include ";" for in-source separators between pipelines on the same line; the Go port reserves Operators for "&&" and "||" and represents ";"-separated commands as distinct top-level Statements. This preserves the natural 1:1 mapping with mvdan.cc/sh/v3's *syntax.Stmt and is a deliberate resolved decision.

type Subshell

type Subshell struct {
	Body         []*Statement
	Redirections []*Redirection
	Line         int
}

Subshell: (cmds).

type SubstringRange

type SubstringRange struct {
	Offset string
	Length string
}

SubstringRange: ${var:offset:length}. Length is "" when omitted.

type Transform

type Transform struct {
	Op string // single char after '@'
}

Transform: ${var@op}.

type WhileStmt

type WhileStmt struct {
	Cond  []*Statement
	Body  []*Statement
	Until bool
	Line  int
}

WhileStmt: while cond; do body; done (Until=true → until).

type Word

type Word struct{ Parts []WordPart }

Word is a contiguous shell word: a sequence of parts that the expander joins.

type WordPart

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

WordPart is one piece of a Word. Closed via wordPartMarker().

Jump to

Keyboard shortcuts

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