parser

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsExpansion

func ContainsExpansion(w *Word) bool

ContainsExpansion checks if a word has any unquoted expansions.

Types

type ArithmCmd

type ArithmCmd struct{}

ArithmCmd represents (( ... )) (no-op in our shell).

type Assign

type Assign struct {
	Name  *Lit
	Value *Word
	Naked bool // true for bare names or flags like -p
}

Assign represents a variable assignment like NAME=VALUE.

type BinaryCmd

type BinaryCmd struct {
	Op BinaryOp
	X  *Stmt
	Y  *Stmt
}

BinaryCmd connects two statements with an operator.

type BinaryOp

type BinaryOp int

BinaryOp is the operator in a BinaryCmd.

const (
	Pipe    BinaryOp = iota // |
	PipeAll                 // |&
	AndStmt                 // &&
	OrStmt                  // ||
)

type Block

type Block struct{ Stmts []*Stmt }

Block wraps statements in { ... }.

type CallExpr

type CallExpr struct {
	Assigns  []*Assign
	Args     []*Word
	Heredocs []*Heredoc // <<DELIM heredocs attached to this command
	// MergeStderr is set when the command had a `2>&1` redirection,
	// causing the shell to send the command's stderr down its stdout.
	MergeStderr bool
	// Redirect is set when the command's stdout is redirected to a file via
	// `> file` or `>> file`. The shell buffers stdout and commits it as a
	// single atomic whole-object write (never a stream).
	Redirect *Redirect
}

CallExpr is a simple command: optional assignments followed by words.

type CmdSubst

type CmdSubst struct {
	Stmts []*Stmt
}

CmdSubst represents command substitution $(...).

type Command

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

Command is implemented by all command AST nodes.

type DblQuoted

type DblQuoted struct{ Parts []WordPart }

DblQuoted is a double-quoted string with nested expansions.

type DeclClause

type DeclClause struct {
	Variant *Lit
	Args    []*Assign
}

DeclClause represents export/local/declare/readonly.

type Expansion

type Expansion struct {
	Op   ParamExpOp
	Word *Word
}

Expansion holds the operator and word for parameter expansion modifiers.

type File

type File struct {
	Stmts []*Stmt
}

File is the top-level AST node representing a parsed shell input.

func Parse

func Parse(src string) (*File, error)

Parse parses a shell input string into an AST.

type ForClause

type ForClause struct {
	Loop *WordIter
	Do   []*Stmt
}

ForClause represents for-in loops.

type Heredoc

type Heredoc struct {
	Delimiter string // e.g. "EOF"
	StripTabs bool   // true when opener was `<<-DELIM` (strips leading TABs)
	Body      string // body text, filled in by the lexer
}

Heredoc holds the body of a `<<DELIM` here-document. Body is captured verbatim by the lexer at the first newline after the heredoc opener.

Variable expansion inside the body is intentionally NOT supported in this shell — heredocs are always treated as if the delimiter were quoted (i.e. `<<'EOF'`). This keeps the implementation small and matches the dominant use case: piping multi-line content into commands like `kb publish`.

type IfClause

type IfClause struct {
	Cond []*Stmt
	Then []*Stmt
	Else *IfClause
}

IfClause represents if/elif/else. A plain "else" is encoded as &IfClause{Then: body} with nil Cond.

type LetClause

type LetClause struct{}

LetClause represents let expressions (no-op in our shell).

type Lit

type Lit struct{ Value string }

Lit is a literal string.

type ParamExp

type ParamExp struct {
	Param  *Lit
	Length bool
	Exp    *Expansion
}

ParamExp represents parameter expansion: $VAR, ${VAR}, ${VAR:-default}, ${#VAR}.

type ParamExpOp

type ParamExpOp int

ParamExpOp is the operator in a parameter expansion.

const (
	DefaultUnset         ParamExpOp = iota // ${VAR-word}
	DefaultUnsetOrNull                     // ${VAR:-word}
	AlternateUnset                         // ${VAR+word}
	AlternateUnsetOrNull                   // ${VAR:+word}
	AssignUnset                            // ${VAR=word}
	AssignUnsetOrNull                      // ${VAR:=word}
	ErrorUnset                             // ${VAR?word}
	ErrorUnsetOrNull                       // ${VAR:?word}
)

type Redirect

type Redirect struct {
	Target *Word // the destination path
	Append bool  // true for `>>`
}

Redirect represents a stdout-to-file redirection (`>` or `>>`).

type SglQuoted

type SglQuoted struct{ Value string }

SglQuoted is a single-quoted string.

type Stmt

type Stmt struct {
	Negated bool
	Cmd     Command
}

Stmt is a single statement, optionally negated with !.

type Subshell

type Subshell struct{ Stmts []*Stmt }

Subshell wraps statements in ( ... ).

type TestClause

type TestClause struct {
	Words []*Word
}

TestClause represents [[ ... ]] test expressions.

type TimeClause

type TimeClause struct {
	Stmt *Stmt
}

TimeClause represents the time prefix.

type WhileClause

type WhileClause struct {
	Until bool
	Cond  []*Stmt
	Do    []*Stmt
}

WhileClause represents while/until loops.

type Word

type Word struct {
	Parts []WordPart
}

Word is a shell word composed of parts.

type WordIter

type WordIter struct {
	Name  *Lit
	Items []*Word
}

WordIter is the iterator for a for-in loop.

type WordPart

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

WordPart is implemented by all word-part AST nodes.

Jump to

Keyboard shortcuts

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