Documentation
¶
Overview ¶
Package bash implements a bash tokeniser and AST.
Index ¶
- Constants
- Variables
- func SetTokeniser(t *parser.Tokeniser) *parser.Tokeniser
- type ArithmeticExpansion
- type ArrayWord
- type Assignment
- type AssignmentOrWord
- type AssignmentType
- type BraceExpansion
- type BraceExpansionType
- type BraceWord
- type CaseCompound
- type CaseTerminationType
- type Command
- type CommandOrCompound
- type CommandSubstitution
- type Comments
- type Compound
- type Error
- type File
- type ForCompound
- type FunctionCompound
- type GroupingCompound
- type Heredoc
- type HeredocPartOrWord
- type IfCompound
- type JobControl
- type Line
- type LogicalOperator
- type LoopCompound
- type Parameter
- type ParameterAssign
- type ParameterExpansion
- type ParameterType
- type Pattern
- type PatternLines
- type Pipeline
- type PipelineTime
- type Redirection
- type SelectCompound
- type Statement
- type String
- type SubstitutionType
- type TestCompound
- type TestConsequence
- type TestOperator
- type Tests
- type Token
- type Tokeniser
- type Tokens
- type Type
- type Value
- type Word
- type WordOrOperator
- type WordOrToken
- type WordPart
Constants ¶
const ( TokenWhitespace parser.TokenType = iota TokenLineTerminator TokenComment TokenIdentifier TokenFunctionIdentifier TokenIdentifierAssign TokenLetIdentifierAssign TokenAssignment TokenKeyword TokenBuiltin TokenWord TokenNumberLiteral TokenString TokenStringStart TokenStringMid TokenStringEnd TokenBraceSequenceExpansion TokenBraceExpansion TokenBraceWord TokenPunctuator TokenHeredoc TokenHeredocIndent TokenHeredocEnd TokenOpenBacktick TokenCloseBacktick TokenPattern TokenOperator TokenBinaryOperator )
Variables ¶
var ( ErrInvalidCharacter = errors.New("invalid character") ErrInvalidParameterExpansion = errors.New("invalid parameter expansion") ErrInvalidNumber = errors.New("invalid number") ErrInvalidAssignment = errors.New("invalid assignment") ErrMissingClosingBracket = errors.New("missing closing bracket") ErrMissingClosingBrace = errors.New("missing closing brace") ErrMissingClosingParen = errors.New("missing closing paren") ErrMissingCloser = errors.New("missing closer") ErrInvalidEndOfStatement = errors.New("invalid end of statement") ErrIncorrectBacktick = errors.New("incorrect backtick depth") ErrMissingWord = errors.New("missing word") ErrMissingClosingIf = errors.New("missing if closing") ErrMissingThen = errors.New("missing then") ErrMissingIn = errors.New("missing in") ErrMissingDo = errors.New("missing do") ErrMissingClosingCase = errors.New("missing case closing") ErrMissingClosingPattern = errors.New("missing pattern closing") ErrInvalidKeyword = errors.New("invalid keyword") ErrInvalidIdentifier = errors.New("invalid identifier") ErrMissingOperator = errors.New("missing operator") ErrInvalidOperator = errors.New("invalid operator") )
Errors.
Functions ¶
Types ¶
type ArithmeticExpansion ¶
type ArithmeticExpansion struct { Expression bool WordsAndOperators []WordOrOperator Tokens Tokens }
ArithmeticExpansion represents either an expression ('((') or a compound ('$((').
For the expression, the returned number is the exit code, for the compound the returned value is a word.
type ArrayWord ¶
ArrayWord a word in a Values array value.
The first set of comments are from just before the word, and the second set are from just after.
type Assignment ¶
type Assignment struct { Identifier ParameterAssign Assignment AssignmentType Expression []WordOrOperator Value *Value Tokens Tokens }
Assignment represents a value assignment.
If Assignment is AssignmentAppend, Expression should be used, otherwise Value should be set.
type AssignmentOrWord ¶
type AssignmentOrWord struct { Assignment *Assignment Word *Word Tokens Tokens }
AssignmentOrWord represents either an Assignment or a Word in a command.
One, and only one, of Assignment or Word must be set.
type AssignmentType ¶
type AssignmentType uint8
AssignmentType represents the type of assignment, either a simple set or and append.
const ( AssignmentAssign AssignmentType = iota AssignmentAppend )
Assignment types.
func (AssignmentType) String ¶
func (a AssignmentType) String() string
type BraceExpansion ¶
type BraceExpansion struct { BraceExpansionType Words []Word Tokens Tokens }
BraceExpansion represents either a sequence expansion ('{a..b}', '{1..10..2}'), or a group of words ('{ab,cd,12}').
type BraceExpansionType ¶
type BraceExpansionType uint8
BraceExpansionType represents which type of BraceExpansion is being represented.
const ( BraceExpansionWords BraceExpansionType = iota BraceExpansionSequence )
Brace Expansion types.
func (BraceExpansionType) String ¶
func (b BraceExpansionType) String() string
type CaseCompound ¶
type CaseCompound struct { Word Word Matches []PatternLines Comments [3]Comments Tokens Tokens }
CaseCompound represents a case select compound.
The first two comment groups represent comments on either side on the 'in' keyword, and the third group represents comments from just before the closing 'esac' keyword.
type CaseTerminationType ¶
type CaseTerminationType uint8
CaseTerminationType represents the final punctuation of a case match.
Must be one of CaseTerminationNone, CaseTerminationEnd, CaseTerminationContinue, or CaseTerminationFallthrough.
const ( CaseTerminationNone CaseTerminationType = iota CaseTerminationEnd CaseTerminationContinue CaseTerminationFallthrough )
CaseTermination types.
func (CaseTerminationType) String ¶
func (c CaseTerminationType) String() string
type Command ¶
type Command struct { Vars []Assignment Redirections []Redirection AssignmentsOrWords []AssignmentOrWord Tokens Tokens }
Command represents an assignment or a call to a command or builtin.
At least one Var, Redirection, or Word must be set.
type CommandOrCompound ¶
CommandOrCompound represents either a Command or a Compound; one, and only one of which must be set.
type CommandSubstitution ¶
type CommandSubstitution struct { SubstitutionType SubstitutionType Backtick *Token Command File Tokens Tokens }
CommandSubstitution represents a subshell that returns some value.
For a SubstitutionNew or SubstitutionBacktick, the Standard Out is returned; for a SubstitutionProcessInput or SubstitutionProcessOutput a path is return.
For a SubstitutionBacktick, the Backtick must be set to the escaped backtick being used for the subshell.
The Command must contain at least one statement.
type Compound ¶
type Compound struct { IfCompound *IfCompound CaseCompound *CaseCompound LoopCompound *LoopCompound ForCompound *ForCompound SelectCompound *SelectCompound GroupingCompound *GroupingCompound TestCompound *TestCompound ArithmeticCompound *ArithmeticExpansion FunctionCompound *FunctionCompound Redirections []Redirection Tokens Tokens }
Compound represents one of the Bash compound statements. One, and only of the compounds must be set.
type File ¶
File represents a parsed Bash file, a subshell, or a compound body.
The first set of comments are from the start of the file/body, the second set from the end.
type ForCompound ¶
type ForCompound struct { Identifier *Token Words []Word ArithmeticExpansion *ArithmeticExpansion File File Comments [2]Comments Tokens Tokens }
ForCompound represents a For loop.
One, and only one, of Identifier and ArithmeticExpansion must be set.
The File must contain at least one statement.
The first set of comments are from after an Identifier; the second set of comments are from just before the 'do' keyword.
type FunctionCompound ¶
type FunctionCompound struct { HasKeyword bool Identifier *Token Body Compound Comments Comments Tokens Tokens }
Function compound represents a defined function, either with or without the 'function' keyword.
The Comments are from just before the Body.
type GroupingCompound ¶
GroupingCompound represents either a brace or parenthesized set of statements.
File must contain at least one statement.
type Heredoc ¶
type Heredoc struct { HeredocPartsOrWords []HeredocPartOrWord Tokens Tokens }
Heredoc represents the parts of a Here Document.
type HeredocPartOrWord ¶
HeredocPartOrWord represents either the string of Word part of a Here Document.
One of HeredocPart or Word must be set.
type IfCompound ¶
type IfCompound struct { If TestConsequence ElIf []TestConsequence Else *File Tokens Tokens }
IfCompound represents and if compound with optional elif and else sections.
type JobControl ¶
type JobControl uint8
JobControl determines whether a job starts in the foreground or background.
const ( JobControlForeground JobControl = iota JobControlBackground )
func (JobControl) String ¶
func (j JobControl) String() string
type Line ¶
Line represents a logical bash line; it may contain multiple statements.
The first set of Comments are from just before the line, the second set from the end of the line, and those on following lines not preceded by an empty line.
type LogicalOperator ¶
type LogicalOperator uint8
LogicalOperator represents how two statements are joined.
const ( LogicalOperatorNone LogicalOperator = iota LogicalOperatorAnd LogicalOperatorOr )
Logical Operators.
func (LogicalOperator) String ¶
func (l LogicalOperator) String() string
type LoopCompound ¶
type LoopCompound struct { Until bool Statement Statement File File Comments Comments Tokens Tokens }
LoopCompound represents either While or Until loops.
The File must contain at least one statement.
The comments are parsed after statement, before the 'do' keyword.
type Parameter ¶
type Parameter struct { Parameter *Token Array []WordOrOperator Tokens Tokens }
Parameter represents the Parameter, an Identifier with a possible Array subscript, used in a ParameterExpansion.
type ParameterAssign ¶
type ParameterAssign struct { Identifier *Token Subscript []WordOrOperator Tokens Tokens }
ParameterAssign represents an identifier being assigned to, with a possible subscript.
Identifier must be set.
type ParameterExpansion ¶
type ParameterExpansion struct { Indirect bool Parameter Parameter Type ParameterType SubstringStart *Token SubstringEnd *Token BraceWord *BraceWord Pattern *Token String *String Tokens Tokens }
ParameterExpansion represents the expansion of a parameter.
type ParameterType ¶
type ParameterType uint8
ParameterType represents the type of a ParameterExpansion.
const ( ParameterValue ParameterType = iota ParameterLength ParameterSubstitution ParameterAssignment ParameterMessage ParameterSetAssign ParameterUnsetSubstitution ParameterUnsetAssignment ParameterUnsetMessage ParameterUnsetSetAssign ParameterSubstring ParameterPrefix ParameterPrefixSeperate ParameterRemoveStartShortest ParameterRemoveStartLongest ParameterRemoveEndShortest ParameterRemoveEndLongest ParameterReplace ParameterReplaceAll ParameterReplaceStart ParameterReplaceEnd ParameterLowercaseFirstMatch ParameterLowercaseAllMatches ParameterUppercaseFirstMatch ParameterUppercaseAllMatches ParameterUppercase ParameterUppercaseFirst ParameterLowercase ParameterQuoted ParameterEscaped ParameterPrompt ParameterDeclare ParameterQuotedArrays ParameterQuotedArraysSeperate ParameterAttributes )
ParameterExpansion types.
func (ParameterType) String ¶
func (p ParameterType) String() string
type Pattern ¶
Pattern represents a pattern being matched against in a TestCompound test.
Must contain at least one WordPart.
type PatternLines ¶
type PatternLines struct { Patterns []Word Lines File CaseTerminationType Comments Comments Tokens Tokens }
PatternLines represents a CaseCompound pattern and the code to run for that match.
The Comments are parsed from just before the pattern.
type Pipeline ¶
type Pipeline struct { PipelineTime PipelineTime Not bool Coproc bool CoprocIdentifier *Token CommandOrCompound CommandOrCompound Pipeline *Pipeline Tokens Tokens }
Pipeline represents a command or compound, possibly connected to another pipeline by a pipe ('|').
type PipelineTime ¶
type PipelineTime uint8
PipelineTime represents a potential 'time' keyword prefixed to a pipeline.
const ( PipelineTimeNone PipelineTime = iota PipelineTimeBash PipelineTimePosix )
Pipeline Time options.
func (PipelineTime) String ¶
func (p PipelineTime) String() string
type Redirection ¶
type Redirection struct { Input *Token Redirector *Token Output Word Heredoc *Heredoc Tokens Tokens }
Redirection presents input/output redirection.
Redirector must be set to the redirection operator.
type SelectCompound ¶
type SelectCompound struct { Identifier *Token Words []Word File File Comments [2]Comments Tokens Tokens }
SelectCompound represents a Select loop.
The Identifier must be set and the File must contain at least one statement.
The first set of Comments is from just after the Identifier and the second are from just before the 'do' keyword.
type Statement ¶
type Statement struct { Pipeline Pipeline LogicalOperator LogicalOperator Statement *Statement JobControl JobControl Tokens }
Statement represents a statement or statements joined by '||' or '&&' operators.
With a LogicalOperator set to either LogicalOperatorAnd or LogicalOperatorOr, the Statement must be set.
type String ¶
type String struct { WordsOrTokens []WordOrToken Tokens Tokens }
String represents a collection of string or word parts that make up string.
type SubstitutionType ¶
type SubstitutionType uint8
SubstitutionType represents the type of a CommandSubstitution.
const ( SubstitutionNew SubstitutionType = iota SubstitutionBacktick SubstitutionProcessInput SubstitutionProcessOutput )
Substitution types.
func (SubstitutionType) String ¶
func (s SubstitutionType) String() string
type TestCompound ¶
TestCompound represents the wrapping of a '[[ ... ]]' compound.
The first set of comments are from just after the opening '[[' and the second set are from just before the closing ']]'.
type TestConsequence ¶
TestConsequence represents the conditional test and body of an if or elif section.
The Consequence must contain at least one statement.
The comments are parsed after the test statement, before the 'then' keyword.
type TestOperator ¶
type TestOperator uint8
TestOperator represents the type of test being represented.
const ( TestOperatorNone TestOperator = iota TestOperatorFileExists TestOperatorFileIsBlock TestOperatorFileIsCharacter TestOperatorDirectoryExists TestOperatorFileIsRegular TestOperatorFileHasSetGroupID TestOperatorFileIsSymbolic TestOperatorFileHasStickyBit TestOperatorFileIsPipe TestOperatorFileIsReadable TestOperatorFileIsNonZero TestOperatorFileIsTerminal TestOperatorFileHasSetUserID TestOperatorFileIsWritable TestOperatorFileIsExecutable TestOperatorFileIsOwnedByEffectiveGroup TestOperatorFileWasModifiedSinceLastRead TestOperatorFileIsOwnedByEffectiveUser TestOperatorFileIsSocket TestOperatorOptNameIsEnabled TestOperatorVarNameIsSet TestOperatorVarnameIsRef TestOperatorStringIsZero TestOperatorStringIsNonZero TestOperatorStringsEqual TestOperatorStringsMatch TestOperatorStringsNotEqual TestOperatorStringBefore TestOperatorStringAfter TestOperatorEqual TestOperatorNotEqual TestOperatorLessThan TestOperatorLessThanEqual TestOperatorGreaterThan TestOperatorGreaterThanEqual TestOperatorFilesAreSameInode TestOperatorFileIsNewerThan TestOperatorFileIsOlderThan )
func (TestOperator) String ¶
func (t TestOperator) String() string
type Tests ¶
type Tests struct { Not bool Test TestOperator Word *Word Pattern *Pattern Parens *Tests LogicalOperator LogicalOperator Tests *Tests Comments [5]Comments Tokens Tokens }
Tests represents the actual test conditions of a TestCompound.
type Tokeniser ¶
type Tokeniser interface { Iter(func(parser.Token) bool) TokeniserState(parser.TokenFunc) GetError() error }
Tokeniser represents the methods required by the bash tokeniser.
type Value ¶
Value represents the value to be assigned in an Assignment.
One, and only one, of Word or Array must be used.
When assigning an array, the first set of comments are from just after the opening paren, and the second set of comments are from just before the closing paren.
type WordOrOperator ¶
WordOrOperator represents either a Word or an Arithmetic Operator, one, and only one of which must be set.
type WordOrToken ¶
WordOrToken represents either a string token or a Word, one and only one of which must be set.
type WordPart ¶
type WordPart struct { Part *Token ParameterExpansion *ParameterExpansion CommandSubstitution *CommandSubstitution ArithmeticExpansion *ArithmeticExpansion BraceExpansion *BraceExpansion Tokens Tokens }
WordPart represents a single part of a word.
One and only one of Part, ParameterExpansion, CommandSubstitution, ArithmeticExpansion, or BraceExpansion must be set.