syntax

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2016 License: BSD-3-Clause Imports: 6 Imported by: 174

Documentation

Overview

Package syntax implements parsing and formatting of shell programs. It supports both POSIX Shell and Bash.

Example
package main

import (
	"os"

	"github.com/mvdan/sh/syntax"
)

func main() {
	in := "{ foo;bar; }"
	f, err := syntax.Parse([]byte(in), "", 0)
	if err != nil {
		return
	}
	syntax.Fprint(os.Stdout, f)
}
Output:

{
	foo
	bar
}

Index

Examples

Constants

View Source
const (
	RdrOut = RedirOperator(rdrOut) + iota
	AppOut
	RdrIn
	RdrInOut
	DplIn
	DplOut
	ClbOut
	Hdoc
	DashHdoc
	WordHdoc
	RdrAll
	AppAll
)
View Source
const (
	CmdIn = ProcOperator(cmdIn) + iota
	CmdOut
)
View Source
const (
	GlobQuest = GlobOperator(globQuest) + iota
	GlobStar
	GlobPlus
	GlobAt
	GlobExcl
)
View Source
const (
	AndStmt = BinCmdOperator(andAnd) + iota
	OrStmt
	Pipe
	PipeAll
)
View Source
const (
	DblSemicolon = CaseOperator(dblSemicolon) + iota
	SemiFall
	DblSemiFall
)
View Source
const (
	SubstPlus = ParExpOperator(plus) + iota
	SubstColPlus
	SubstMinus
	SubstColMinus
	SubstQuest
	SubstColQuest
	SubstAssgn
	SubstColAssgn
	RemSmallSuffix
	RemLargeSuffix
	RemSmallPrefix
	RemLargePrefix
	UpperFirst
	UpperAll
	LowerFirst
	LowerAll
)
View Source
const (
	Not = UnAritOperator(exclMark) + iota
	Inc
	Dec
	Plus  = UnAritOperator(plus)
	Minus = UnAritOperator(minus)
)
View Source
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)
)
View Source
const (
	TsExists = UnTestOperator(tsExists) + iota
	TsRegFile
	TsDirect
	TsCharSp
	TsBlckSp
	TsNmPipe
	TsSocket
	TsSmbLink
	TsGIDSet
	TsUIDSet
	TsRead
	TsWrite
	TsExec
	TsNoEmpty
	TsFdTerm
	TsEmpStr
	TsNempStr
	TsOptSet
	TsVarSet
	TsRefVar
	TsNot = UnTestOperator(exclMark)
)
View Source
const (
	TsReMatch = BinTestOperator(tsReMatch) + iota
	TsNewer
	TsOlder
	TsDevIno
	TsEql
	TsNeq
	TsLeq
	TsGeq
	TsLss
	TsGtr
	AndTest  = BinTestOperator(andAnd)
	OrTest   = BinTestOperator(orOr)
	TsAssgn  = BinTestOperator(assgn)
	TsEqual  = BinTestOperator(equal)
	TsNequal = BinTestOperator(nequal)
	TsBefore = BinTestOperator(rdrIn)
	TsAfter  = BinTestOperator(rdrOut)
)

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, f *File) error

Fprint "pretty-prints" the given AST file to the given writer. It calls PrintConfig.Fprint with its default settings.

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

Example
package main

import (
	"os"
	"strings"

	"github.com/mvdan/sh/syntax"
)

type paramUpper struct{}

func (u paramUpper) Visit(node syntax.Node) syntax.Visitor {
	switch x := node.(type) {
	case *syntax.ParamExp:
		x.Param.Value = strings.ToUpper(x.Param.Value)
	}
	return u
}

func main() {
	in := `echo $foo "and $bar"`
	f, err := syntax.Parse([]byte(in), "", 0)
	if err != nil {
		return
	}
	syntax.Walk(paramUpper{}, f)
	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.

func (*ArithmCmd) End

func (a *ArithmCmd) End() Pos

func (*ArithmCmd) Pos

func (a *ArithmCmd) Pos() Pos

type ArithmExp

type ArithmExp struct {
	Left, Right Pos
	Bracket     bool
	X           ArithmExpr
}

ArithmExp represents an arithmetic expansion.

func (*ArithmExp) End

func (a *ArithmExp) End() Pos

func (*ArithmExp) Pos

func (a *ArithmExp) Pos() Pos

type ArithmExpr

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

ArithmExpr represents all nodes that form arithmetic expressions.

type ArrayExpr

type ArrayExpr struct {
	Lparen, Rparen Pos
	List           []*Word
}

ArrayExpr represents a Bash array expression.

This node will never appear when in PosixConformant mode.

func (*ArrayExpr) End

func (a *ArrayExpr) End() Pos

func (*ArrayExpr) Pos

func (a *ArrayExpr) Pos() Pos

type Assign

type Assign struct {
	Append bool
	Name   *Lit
	Value  *Word
}

Assign represents an assignment to a variable.

func (*Assign) End

func (a *Assign) End() Pos

func (*Assign) Pos

func (a *Assign) Pos() Pos

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.

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.

func (*BinaryCmd) End

func (b *BinaryCmd) End() Pos

func (*BinaryCmd) Pos

func (b *BinaryCmd) Pos() Pos

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 Block

type Block struct {
	Lbrace, Rbrace Pos
	Stmts          []*Stmt
}

Block represents a series of commands that should be executed in a nested scope.

func (*Block) End

func (b *Block) End() Pos

func (*Block) Pos

func (b *Block) 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.

func (*CallExpr) End

func (c *CallExpr) End() Pos

func (*CallExpr) Pos

func (c *CallExpr) Pos() Pos

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 CmdSubst

type CmdSubst struct {
	Left, Right Pos
	Stmts       []*Stmt
}

CmdSubst represents a command substitution.

func (*CmdSubst) End

func (c *CmdSubst) End() Pos

func (*CmdSubst) Pos

func (c *CmdSubst) Pos() Pos

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.

type Comment

type Comment struct {
	Hash Pos
	Text string
}

Comment represents a single comment on a single line.

func (*Comment) End

func (c *Comment) End() Pos

func (*Comment) Pos

func (c *Comment) Pos() Pos

type CoprocClause

type CoprocClause struct {
	Coproc Pos
	Name   *Lit
	Stmt   *Stmt
}

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 DblQuoted

type DblQuoted struct {
	Position Pos
	Dollar   bool
	Parts    []WordPart
}

DblQuoted represents a list of nodes within double quotes.

func (*DblQuoted) End

func (q *DblQuoted) End() Pos

func (*DblQuoted) Pos

func (q *DblQuoted) Pos() Pos

type DeclClause

type DeclClause struct {
	Position Pos
	Variant  string
	Opts     []*Word
	Assigns  []*Assign
}

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 Elif

type Elif struct {
	Elif, Then Pos
	CondStmts  []*Stmt
	ThenStmts  []*Stmt
}

Elif represents an "else if" case in an if clause.

type EvalClause

type EvalClause struct {
	Eval Pos
	Stmt *Stmt
}

EvalClause represents a Bash eval clause.

This node will never appear when in PosixConformant mode.

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.

func (*ExtGlob) End

func (e *ExtGlob) End() Pos

func (*ExtGlob) Pos

func (e *ExtGlob) Pos() Pos

type File

type File struct {
	Name string

	Stmts    []*Stmt
	Comments []*Comment

	// Lines contains the offset of the first character for each
	// line (the first entry is always 0)
	Lines []int
}

File is a shell program.

func Parse

func Parse(src []byte, name string, mode ParseMode) (*File, error)

Parse reads and parses a shell program with an optional name. It returns the parsed program if no issues were encountered. Otherwise, an error is returned.

func (*File) End

func (f *File) End() Pos

func (*File) Pos

func (f *File) Pos() Pos

func (*File) Position

func (f *File) Position(p Pos) (pos Position)

type ForClause

type ForClause struct {
	For, Do, Done Pos
	Loop          Loop
	DoStmts       []*Stmt
}

ForClause represents a for clause.

func (*ForClause) End

func (f *ForClause) End() Pos

func (*ForClause) Pos

func (f *ForClause) Pos() Pos

type FuncDecl

type FuncDecl struct {
	Position  Pos
	BashStyle bool
	Name      *Lit
	Body      *Stmt
}

FuncDecl represents the declaration of a function.

func (*FuncDecl) End

func (f *FuncDecl) End() Pos

func (*FuncDecl) Pos

func (f *FuncDecl) Pos() Pos

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.

func (*IfClause) End

func (c *IfClause) End() Pos

func (*IfClause) Pos

func (c *IfClause) Pos() Pos

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.

func (*LetClause) End

func (l *LetClause) End() Pos

func (*LetClause) Pos

func (l *LetClause) Pos() Pos

type Lit

type Lit struct {
	ValuePos, ValueEnd Pos
	Value              string
}

Lit represents an unquoted string consisting of characters that were not tokenized.

func (*Lit) End

func (l *Lit) End() Pos

func (*Lit) Pos

func (l *Lit) Pos() Pos

type Loop

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

Loop represents all nodes that can be loops in a for clause.

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, Length  bool
	Param          *Lit
	Ind            *Index
	Slice          *Slice
	Repl           *Replace
	Exp            *Expansion
}

ParamExp represents a parameter expansion.

func (*ParamExp) End

func (p *ParamExp) End() Pos

func (*ParamExp) Pos

func (p *ParamExp) Pos() Pos

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 ParenTest

type ParenTest struct {
	Lparen, Rparen Pos
	X              TestExpr
}

ParenTest represents an expression within parentheses inside an TestExp.

func (*ParenTest) End

func (p *ParenTest) End() Pos

func (*ParenTest) Pos

func (p *ParenTest) Pos() Pos

type ParseError

type ParseError struct {
	Position
	Filename, Text string
}

ParseError represents an error found when parsing a source file.

func (*ParseError) Error

func (e *ParseError) Error() string

type ParseMode

type ParseMode uint

ParseMode controls the parser behaviour via a set of flags.

const (
	ParseComments   ParseMode = 1 << iota // add comments to the AST
	PosixConformant                       // match the POSIX standard where it differs from bash
)

type PatternList

type PatternList struct {
	Op       CaseOperator
	OpPos    Pos
	Patterns []*Word
	Stmts    []*Stmt
}

PatternList represents a pattern list (case) within a CaseClause.

type Pos

type Pos uint32

Pos is the internal representation of a position within a source file.

type Position

type Position struct {
	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.

func (PrintConfig) Fprint

func (c PrintConfig) Fprint(w io.Writer, f *File) error

Fprint "pretty-prints" the given AST file to the given writer.

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.

func (*ProcSubst) End

func (s *ProcSubst) End() Pos

func (*ProcSubst) Pos

func (s *ProcSubst) Pos() Pos

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.

func (*Redirect) End

func (r *Redirect) End() Pos

func (*Redirect) Pos

func (r *Redirect) Pos() Pos

type Replace

type Replace struct {
	All        bool
	Orig, With *Word
}

Replace represents a search and replace inside a ParamExp.

type SglQuoted

type SglQuoted struct {
	Position Pos
	Dollar   bool
	Value    string
}

SglQuoted represents a string within single quotes.

func (*SglQuoted) End

func (q *SglQuoted) End() Pos

func (*SglQuoted) Pos

func (q *SglQuoted) Pos() Pos

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
	SemiPos    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.

func (*Stmt) End

func (s *Stmt) End() Pos

func (*Stmt) Pos

func (s *Stmt) Pos() Pos

type Subshell

type Subshell struct {
	Lparen, Rparen Pos
	Stmts          []*Stmt
}

Subshell represents a series of commands that should be executed in a nested shell environment.

func (*Subshell) End

func (s *Subshell) End() Pos

func (*Subshell) Pos

func (s *Subshell) Pos() Pos

type TestClause

type TestClause struct {
	Left, Right Pos
	X           TestExpr
}

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.

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.

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.

func (*UnaryTest) End

func (u *UnaryTest) End() Pos

func (*UnaryTest) Pos

func (u *UnaryTest) Pos() Pos

type UntilClause

type UntilClause struct {
	Until, Do, Done Pos
	CondStmts       []*Stmt
	DoStmts         []*Stmt
}

UntilClause represents an until clause.

func (*UntilClause) End

func (u *UntilClause) End() Pos

func (*UntilClause) Pos

func (u *UntilClause) Pos() Pos

type Visitor

type Visitor interface {
	Visit(node Node) (w Visitor)
}

Visitor holds a Visit method which is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).

type WhileClause

type WhileClause struct {
	While, Do, Done Pos
	CondStmts       []*Stmt
	DoStmts         []*Stmt
}

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.

func (*Word) End

func (w *Word) End() Pos

func (*Word) Pos

func (w *Word) Pos() Pos

type WordIter

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

WordIter represents the iteration of a variable over a series of words in a for clause.

func (*WordIter) End

func (w *WordIter) End() Pos

func (*WordIter) Pos

func (w *WordIter) Pos() Pos

type WordPart

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

WordPart represents all nodes that can form a word.

Jump to

Keyboard shortcuts

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