Documentation ¶
Overview ¶
Package syntax implements parsing and formatting of shell programs. It supports both POSIX Shell and Bash.
Example ¶
package main import ( "os" "strings" "github.com/mvdan/sh/syntax" ) func main() { in := strings.NewReader("{ foo; bar; }") f, err := syntax.Parse(in, "", 0) if err != nil { return } syntax.Fprint(os.Stdout, f) }
Output: { foo bar }
Index ¶
- Constants
- func Fprint(w io.Writer, f *File) error
- func Walk(node Node, f func(Node) bool)
- type ArithmCmd
- type ArithmExp
- type ArithmExpr
- type ArrayExpr
- type Assign
- type BinAritOperator
- type BinCmdOperator
- type BinTestOperator
- type BinaryArithm
- type BinaryCmd
- type BinaryTest
- type Block
- type CStyleLoop
- type CallExpr
- type CaseClause
- type CaseOperator
- type CmdSubst
- type Command
- type Comment
- type CoprocClause
- type DblQuoted
- type DeclClause
- type Elif
- type EvalClause
- type Expansion
- type ExtGlob
- type File
- type ForClause
- type FuncDecl
- type GlobOperator
- type IfClause
- type Index
- type LetClause
- type Lit
- type Loop
- type Node
- type ParExpOperator
- type ParamExp
- type ParenArithm
- type ParenTest
- type ParseError
- type ParseMode
- type PatternList
- type Pos
- type Position
- type PrintConfig
- type ProcOperator
- type ProcSubst
- type RedirOperator
- type Redirect
- type Replace
- type SglQuoted
- type Slice
- type Stmt
- type Subshell
- type TestClause
- type TestExpr
- type UnAritOperator
- type UnTestOperator
- type UnaryArithm
- type UnaryTest
- type UntilClause
- type WhileClause
- type Word
- type WordIter
- type WordPart
Examples ¶
Constants ¶
const ( RdrOut = RedirOperator(rdrOut) + iota AppOut RdrIn RdrInOut DplIn DplOut ClbOut Hdoc DashHdoc WordHdoc RdrAll AppAll )
const ( CmdIn = ProcOperator(cmdIn) + iota CmdOut )
const ( GlobQuest = GlobOperator(globQuest) + iota GlobStar GlobPlus GlobAt GlobExcl )
const ( AndStmt = BinCmdOperator(andAnd) + iota OrStmt Pipe PipeAll )
const ( DblSemicolon = CaseOperator(dblSemicolon) + iota SemiFall DblSemiFall )
const ( SubstPlus = ParExpOperator(plus) + iota SubstColPlus SubstMinus SubstColMinus SubstQuest SubstColQuest SubstAssgn SubstColAssgn RemSmallSuffix RemLargeSuffix RemSmallPrefix RemLargePrefix UpperFirst UpperAll LowerFirst LowerAll OtherParamOps )
const ( Not = UnAritOperator(exclMark) + iota Inc Dec Plus = UnAritOperator(plus) Minus = UnAritOperator(minus) )
const ( Add = BinAritOperator(plus) Sub = BinAritOperator(minus) Mul = BinAritOperator(star) Quo = BinAritOperator(slash) Rem = BinAritOperator(perc) Pow = BinAritOperator(power) Eql = BinAritOperator(equal) Gtr = BinAritOperator(rdrOut) Lss = BinAritOperator(rdrIn) Neq = BinAritOperator(nequal) Leq = BinAritOperator(lequal) Geq = BinAritOperator(gequal) And = BinAritOperator(and) Or = BinAritOperator(or) Xor = BinAritOperator(caret) Shr = BinAritOperator(appOut) Shl = BinAritOperator(hdoc) AndArit = BinAritOperator(andAnd) OrArit = BinAritOperator(orOr) Comma = BinAritOperator(comma) Quest = BinAritOperator(quest) Colon = BinAritOperator(colon) Assgn = BinAritOperator(assgn) AddAssgn = BinAritOperator(addAssgn) SubAssgn = BinAritOperator(subAssgn) MulAssgn = BinAritOperator(mulAssgn) QuoAssgn = BinAritOperator(quoAssgn) RemAssgn = BinAritOperator(remAssgn) AndAssgn = BinAritOperator(andAssgn) OrAssgn = BinAritOperator(orAssgn) XorAssgn = BinAritOperator(xorAssgn) ShlAssgn = BinAritOperator(shlAssgn) ShrAssgn = BinAritOperator(shrAssgn) )
const ( TsExists = UnTestOperator(tsExists) + iota TsRegFile TsDirect TsCharSp TsBlckSp TsNmPipe TsSocket TsSmbLink TsSticky TsGIDSet TsUIDSet TsGrpOwn TsUsrOwn TsModif TsRead TsWrite TsExec TsNoEmpty TsFdTerm TsEmpStr TsNempStr TsOptSet TsVarSet TsRefVar TsNot = UnTestOperator(exclMark) )
const ( TsReMatch = BinTestOperator(tsReMatch) + iota TsNewer TsOlder TsDevIno TsEql TsNeq TsLeq TsGeq TsLss TsGtr AndTest = BinTestOperator(andAnd) OrTest = BinTestOperator(orOr) // TODO(mvdan): == and != are pattern matches; use more // appropriate names like TsMatch and TsNoMatch in 2.0 TsEqual = BinTestOperator(equal) TsNequal = BinTestOperator(nequal) TsBefore = BinTestOperator(rdrIn) TsAfter = BinTestOperator(rdrOut) // Deprecated: now parses as TsEqual TsAssgn = BinTestOperator(assgn) // TODO(mvdan): remove in 2.0 )
Variables ¶
This section is empty.
Functions ¶
func Fprint ¶
Fprint "pretty-prints" the given AST file to the given writer. It calls PrintConfig.Fprint with its default settings.
func Walk ¶
Walk traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Walk invokes f recursively for each of the non-nil children of node, followed by f(nil).
Example ¶
package main import ( "os" "strings" "github.com/mvdan/sh/syntax" ) func main() { in := strings.NewReader(`echo $foo "and $bar"`) f, err := syntax.Parse(in, "", 0) if err != nil { return } syntax.Walk(f, func(node syntax.Node) bool { switch x := node.(type) { case *syntax.ParamExp: x.Param.Value = strings.ToUpper(x.Param.Value) } return true }) syntax.Fprint(os.Stdout, f) }
Output: echo $FOO "and $BAR"
Types ¶
type ArithmCmd ¶
type ArithmCmd struct {
Left, Right Pos
X ArithmExpr
}
ArithmCmd represents an arithmetic command.
This node will never appear when in PosixConformant mode.
type ArithmExp ¶
type ArithmExp struct {
Left, Right Pos
Bracket bool
X ArithmExpr
}
ArithmExp represents an arithmetic expansion.
type ArithmExpr ¶
type ArithmExpr interface { Node // contains filtered or unexported methods }
ArithmExpr represents all nodes that form arithmetic expressions.
These are *BinaryArithm, *UnaryArithm, *ParenArithm, and *Word.
If it contains a *Word, it will contain a single *Lit or a single *ParamExp.
TODO(mvdan): replace *Word with *Lit/*ParamExp in 2.0.
type ArrayExpr ¶
ArrayExpr represents a Bash array expression.
This node will never appear when in PosixConformant mode.
type BinAritOperator ¶
type BinAritOperator token
func (BinAritOperator) String ¶
func (o BinAritOperator) String() string
type BinCmdOperator ¶
type BinCmdOperator token
func (BinCmdOperator) String ¶
func (o BinCmdOperator) String() string
type BinTestOperator ¶
type BinTestOperator token
func (BinTestOperator) String ¶
func (o BinTestOperator) String() string
type BinaryArithm ¶
type BinaryArithm struct { OpPos Pos Op BinAritOperator X, Y ArithmExpr }
BinaryArithm represents a binary expression between two arithmetic expression.
If Op is any assign operator, X will be a *Word with a single *Lit whose value is a valid name.
Ternary operators like "a ? b : c" are fit into this structure. Thus, if Op == Quest, Y will be a *BinaryArithm with Op == Colon. Op can only be Colon in that scenario.
TODO(mvdan): we might want to split up assigns in 2.0 (X would be a *Lit) to make the structure easier.
func (*BinaryArithm) End ¶
func (b *BinaryArithm) End() Pos
func (*BinaryArithm) Pos ¶
func (b *BinaryArithm) Pos() Pos
type BinaryCmd ¶
type BinaryCmd struct { OpPos Pos Op BinCmdOperator X, Y *Stmt }
BinaryCmd represents a binary expression between two statements.
type BinaryTest ¶
type BinaryTest struct { OpPos Pos Op BinTestOperator X, Y TestExpr }
BinaryTest represents a binary expression between two arithmetic expression.
func (*BinaryTest) End ¶
func (b *BinaryTest) End() Pos
func (*BinaryTest) Pos ¶
func (b *BinaryTest) Pos() Pos
type CStyleLoop ¶
type CStyleLoop struct {
Lparen, Rparen Pos
Init, Cond, Post ArithmExpr
}
CStyleLoop represents the behaviour of a for clause similar to the C language.
This node will never appear when in PosixConformant mode.
func (*CStyleLoop) End ¶
func (c *CStyleLoop) End() Pos
func (*CStyleLoop) Pos ¶
func (c *CStyleLoop) Pos() Pos
type CallExpr ¶
type CallExpr struct {
Args []*Word
}
CallExpr represents a command execution or function call.
type CaseClause ¶
type CaseClause struct {
Case, Esac Pos
Word *Word
List []*PatternList
}
CaseClause represents a case (switch) clause.
func (*CaseClause) End ¶
func (c *CaseClause) End() Pos
func (*CaseClause) Pos ¶
func (c *CaseClause) Pos() Pos
type CaseOperator ¶
type CaseOperator token
func (CaseOperator) String ¶
func (o CaseOperator) String() string
type Command ¶
type Command interface { Node // contains filtered or unexported methods }
Command represents all nodes that are simple commands, which are directly placed in a Stmt.
These are *CallExpr, *IfClause, *WhileClause, *UntilClause, *ForClause, *CaseClause, *Block, *Subshell, *BinaryCmd, *FuncDecl, *ArithmCmd, *TestClause, *DeclClause, *EvalClause, *LetClause, and *CoprocClause.
type CoprocClause ¶
CoprocClause represents a Bash coproc clause.
This node will never appear when in PosixConformant mode.
func (*CoprocClause) End ¶
func (c *CoprocClause) End() Pos
func (*CoprocClause) Pos ¶
func (c *CoprocClause) Pos() Pos
type DeclClause ¶
DeclClause represents a Bash declare clause.
This node will never appear when in PosixConformant mode.
func (*DeclClause) End ¶
func (d *DeclClause) End() Pos
func (*DeclClause) Pos ¶
func (d *DeclClause) Pos() Pos
type EvalClause ¶
EvalClause represents a Bash eval clause.
This node will never appear when in PosixConformant mode.
TODO(mvdan): EvalClause is actually pointless, as any non-trivial use of eval will involve parsing the program at run-time. Remove in 2.0.
func (*EvalClause) End ¶
func (e *EvalClause) End() Pos
func (*EvalClause) Pos ¶
func (e *EvalClause) Pos() Pos
type Expansion ¶
type Expansion struct { Op ParExpOperator Word *Word }
Expansion represents string manipulation in a ParamExp other than those covered by Replace.
type ExtGlob ¶
type ExtGlob struct { OpPos Pos Op GlobOperator Pattern *Lit }
ExtGlob represents a Bash extended globbing expression. Note that these are parsed independently of whether shopt has been called or not.
This node will never appear when in PosixConformant mode.
type File ¶
type File struct { Name string Stmts []*Stmt Comments []*Comment // contains filtered or unexported fields }
File is a shell program.
type GlobOperator ¶
type GlobOperator token
func (GlobOperator) String ¶
func (o GlobOperator) String() string
type IfClause ¶
type IfClause struct {
If, Then, Else, Fi Pos
CondStmts []*Stmt
ThenStmts []*Stmt
Elifs []*Elif
ElseStmts []*Stmt
}
IfClause represents an if statement.
type Index ¶
type Index struct {
Expr ArithmExpr
}
Index represents access to an array via an index inside a ParamExp.
This node will never appear when in PosixConformant mode.
type LetClause ¶
type LetClause struct { Let Pos Exprs []ArithmExpr }
LetClause represents a Bash let clause.
This node will never appear when in PosixConformant mode.
type Loop ¶
type Loop interface { Node // contains filtered or unexported methods }
Loop holds either *WordIter or *CStyleLoop.
type Node ¶
type Node interface { // Pos returns the first character of the node Pos() Pos // End returns the character immediately after the node End() Pos }
Node represents an AST node.
type ParExpOperator ¶
type ParExpOperator token
func (ParExpOperator) String ¶
func (o ParExpOperator) String() string
type ParamExp ¶
type ParamExp struct {
Dollar, Rbrace Pos
Short bool
Length, Excl bool // TODO(mvdan): rename Excl in 2.0 (Indirect, etc)
Param *Lit
Ind *Index
Slice *Slice
Repl *Replace
Exp *Expansion
}
ParamExp represents a parameter expansion.
type ParenArithm ¶
type ParenArithm struct {
Lparen, Rparen Pos
X ArithmExpr
}
ParenArithm represents an expression within parentheses inside an ArithmExp.
func (*ParenArithm) End ¶
func (p *ParenArithm) End() Pos
func (*ParenArithm) Pos ¶
func (p *ParenArithm) Pos() Pos
type ParseError ¶
ParseError represents an error found when parsing a source file.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type PatternList ¶
type PatternList struct { Op CaseOperator OpPos Pos Patterns []*Word Stmts []*Stmt }
PatternList represents a pattern list (case) within a CaseClause.
type Position ¶
type Position struct { Filename string // if any Offset int // byte offset, starting at 0 Line int // line number, starting at 1 Column int // column number, starting at 1 (in bytes) }
Position describes a position within a source file including the line and column location. A Position is valid if the line number is > 0.
type PrintConfig ¶
type PrintConfig struct {
Spaces int // 0 (default) for tabs, >0 for number of spaces
}
PrintConfig controls how the printing of an AST node will behave.
type ProcOperator ¶
type ProcOperator token
func (ProcOperator) String ¶
func (o ProcOperator) String() string
type ProcSubst ¶
type ProcSubst struct {
OpPos, Rparen Pos
Op ProcOperator
Stmts []*Stmt
}
ProcSubst represents a Bash process substitution.
This node will never appear when in PosixConformant mode.
type RedirOperator ¶
type RedirOperator token
func (RedirOperator) String ¶
func (o RedirOperator) String() string
type Redirect ¶
type Redirect struct { OpPos Pos Op RedirOperator N *Lit Word, Hdoc *Word }
Redirect represents an input/output redirection.
type Slice ¶
type Slice struct {
Offset, Length ArithmExpr
}
Slice represents character slicing inside a ParamExp.
This node will never appear when in PosixConformant mode.
type Stmt ¶
type Stmt struct { Cmd Command Position Pos Semicolon Pos Negated bool Background bool Assigns []*Assign Redirs []*Redirect }
Stmt represents a statement, otherwise known as a compound command. It is compromised of a command and other components that may come before or after it.
type Subshell ¶
Subshell represents a series of commands that should be executed in a nested shell environment.
type TestClause ¶
TestClause represents a Bash extended test clause.
This node will never appear when in PosixConformant mode.
func (*TestClause) End ¶
func (t *TestClause) End() Pos
func (*TestClause) Pos ¶
func (t *TestClause) Pos() Pos
type TestExpr ¶
type TestExpr interface { Node // contains filtered or unexported methods }
TestExpr represents all nodes that form arithmetic expressions.
These are *BinaryTest, *UnaryTest, *ParenTest, and *Word.
type UnAritOperator ¶
type UnAritOperator token
func (UnAritOperator) String ¶
func (o UnAritOperator) String() string
type UnTestOperator ¶
type UnTestOperator token
func (UnTestOperator) String ¶
func (o UnTestOperator) String() string
type UnaryArithm ¶
type UnaryArithm struct { OpPos Pos Op UnAritOperator Post bool X ArithmExpr }
UnaryArithm represents an unary expression over a node, either before or after it.
If Op is Inc or Dec, X will be a *Word with a single *Lit whose value is a valid name.
TODO(mvdan): consider splitting up Inc/Dec like the assigns above in 2.0.
func (*UnaryArithm) End ¶
func (u *UnaryArithm) End() Pos
func (*UnaryArithm) Pos ¶
func (u *UnaryArithm) Pos() Pos
type UnaryTest ¶
type UnaryTest struct { OpPos Pos Op UnTestOperator X TestExpr }
UnaryTest represents an unary expression over a node, either before or after it.
type UntilClause ¶
UntilClause represents an until clause.
func (*UntilClause) End ¶
func (u *UntilClause) End() Pos
func (*UntilClause) Pos ¶
func (u *UntilClause) Pos() Pos
type WhileClause ¶
WhileClause represents a while clause.
func (*WhileClause) End ¶
func (w *WhileClause) End() Pos
func (*WhileClause) Pos ¶
func (w *WhileClause) Pos() Pos
type Word ¶
type Word struct {
Parts []WordPart
}
Word represents a non-empty list of nodes that are contiguous to each other. The word is delimeted by word boundaries.