package shlex
type elvishFormat struct {}
func (elvishFormat ) Classifier () tokenClassifier {
t := newBaseClassifier (escapeRunes )
t .addWordbreaks ("|><;()[]" )
return t
}
func (elvishFormat ) ClassifyOperator (raw string ) WordbreakType {
switch raw {
case "|" :
return WORDBREAK_PIPE
case ">" , ">>" , "<>" , "<" :
return WORDBREAK_REDIRECT_OUTPUT
case ";" :
return WORDBREAK_LIST_SEQUENTIAL
case "(" , ")" :
return WORDBREAK_OUTPUT_CAPTURE
case "[" , "]" :
return WORDBREAK_BRACKET
default :
return WORDBREAK_UNKNOWN
}
}
func (elvishFormat ) KeywordOperators () map [string ]WordbreakType { return nil }
func (elvishFormat ) NonEscapingQuoteEscapes () bool { return true }
func (elvishFormat ) NonEscapingQuoteBackslashEscapes () bool { return false }
func (elvishFormat ) EscapeNotBareword () bool { return false }
func (elvishFormat ) EscapeNotInEscapingQuote () bool { return false }
func (elvishFormat ) EscapingQuoteEscapeChars () map [rune ]bool { return nil }
func (elvishFormat ) QuoteWord (s string ) string { return elvishQuoteWord (s ) }
func (elvishFormat ) TripleQuoteSupport () bool { return false }
func (elvishFormat ) RawPrefixSupport () bool { return false }
func (elvishFormat ) LineContinuationChar () rune { return '^' }
func (elvishFormat ) IsLineContinuationWhitespace (r rune ) bool {
return r == '\n' || r == '\r'
}
type braceState int
const (
braceOutside braceState = iota
braceLambdaOpen
braceParams
braceLambdaBody
braceBraced
)
func (elvishFormat ) PostProcess (tokens TokenSlice ) TokenSlice {
var stack []braceState
for i := range tokens {
t := &tokens [i ]
if t .Type == WORDBREAK_TOKEN && t .WordbreakType == WORDBREAK_OUTPUT_CAPTURE {
if t .Value == "(" {
t .WordbreakType = WORDBREAK_SUBSTITUTION_OPEN
} else if t .Value == ")" {
t .WordbreakType = WORDBREAK_SUBSTITUTION_CLOSE
}
}
if t .Type == WORD_TOKEN && t .Value == "{" {
isLambda := false
isLambdaBody := false
if i +1 < len (tokens ) {
next := tokens [i +1 ]
if next .Type == WORDBREAK_TOKEN && next .Value == "|" {
isLambda = true
} else if !t .adjoins (next ) {
isLambdaBody = true
}
} else {
isLambda = true
}
if isLambda {
stack = append (stack , braceLambdaOpen )
} else if isLambdaBody {
stack = append (stack , braceLambdaBody )
} else {
stack = append (stack , braceBraced )
}
continue
}
if t .Type == WORD_TOKEN {
if t .Value != "{" && t .Value != "}" {
for _ , r := range t .Value {
switch r {
case '{' :
stack = append (stack , braceBraced )
case '}' :
if len (stack ) > 0 {
stack = stack [:len (stack )-1 ]
}
}
}
} else if t .Value == "}" {
if len (stack ) > 0 {
stack = stack [:len (stack )-1 ]
}
continue
}
}
if t .Type == WORDBREAK_TOKEN && t .Value == "|" && len (stack ) > 0 {
scope := &stack [len (stack )-1 ]
switch *scope {
case braceLambdaOpen :
*scope = braceParams
t .WordbreakType = WORDBREAK_LAMBDA_PIPE
case braceParams :
*scope = braceLambdaBody
t .WordbreakType = WORDBREAK_LAMBDA_PIPE
case braceBraced :
t .WordbreakType = WORDBREAK_LAMBDA_PIPE
}
}
}
return tokens
}
func (elvishFormat ) Variable (word Token ) (Variable , bool ) {
return variableRef (word , variableRules {
expands : func (state LexerState ) bool { return state != QUOTING_STATE },
barewordEscapes : false ,
nameStart : isPosixNameStart ,
nameRune : isPosixNameRune ,
})
}
func (elvishFormat ) IsBareWord (s string ) bool {
return bareWordRe .MatchString (s )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .