package shlex
import (
"regexp"
"strings"
)
type xonshFormat struct {}
func (xonshFormat ) Classifier () tokenClassifier {
t := newBaseClassifier (escapeRunes )
t .addWordbreaks ("|<>&;()" )
return t
}
func (xonshFormat ) ClassifyOperator (raw string ) WordbreakType {
return bashWordbreakType (raw )
}
func (xonshFormat ) KeywordOperators () map [string ]WordbreakType {
return map [string ]WordbreakType {
"and" : WORDBREAK_LIST_AND ,
"or" : WORDBREAK_LIST_OR ,
}
}
func (xonshFormat ) NonEscapingQuoteEscapes () bool { return false }
func (xonshFormat ) NonEscapingQuoteBackslashEscapes () bool { return true }
func (xonshFormat ) EscapeNotBareword () bool { return true }
func (xonshFormat ) EscapeNotInEscapingQuote () bool { return false }
func (xonshFormat ) EscapingQuoteEscapeChars () map [rune ]bool { return nil }
func (xonshFormat ) QuoteWord (s string ) string { return xonshQuoteWord (s ) }
func (xonshFormat ) TripleQuoteSupport () bool { return true }
func (xonshFormat ) RawPrefixSupport () bool { return true }
func (xonshFormat ) IsLineContinuation (r rune ) bool {
return r == '\n' || r == '\r'
}
var xonshStreamRedirects = map [string ]WordbreakType {
"e" : WORDBREAK_REDIRECT_OUTPUT ,
"err" : WORDBREAK_REDIRECT_OUTPUT ,
"o" : WORDBREAK_REDIRECT_OUTPUT ,
"out" : WORDBREAK_REDIRECT_OUTPUT ,
"a" : WORDBREAK_REDIRECT_OUTPUT_BOTH ,
"all" : WORDBREAK_REDIRECT_OUTPUT_BOTH ,
}
func (xonshFormat ) PostProcess (tokens TokenSlice ) TokenSlice {
result := make (TokenSlice , 0 , len (tokens ))
for i := 0 ; i < len (tokens ); i ++ {
t := tokens [i ]
if t .Type == WORD_TOKEN && t .Value == t .RawValue && i +1 < len (tokens ) {
next := tokens [i +1 ]
if next .Type == WORDBREAK_TOKEN && next .adjoins (t ) && len (next .RawValue ) > 0 &&
(next .RawValue [0 ] == '>' || next .RawValue [0 ] == '<' ) {
if wbType , ok := xonshStreamRedirects [t .Value ]; ok {
merged := Token {
Type : WORDBREAK_TOKEN ,
Value : t .Value + next .Value ,
RawValue : t .RawValue + next .RawValue ,
Span : Span {Start : t .Span .Start , End : next .Span .End },
State : next .State ,
WordbreakType : wbType ,
}
if i +2 < len (tokens ) {
afterNext := tokens [i +2 ]
if afterNext .Type == WORD_TOKEN && afterNext .Value == "p" &&
afterNext .Value == afterNext .RawValue && afterNext .adjoins (next ) {
merged .Value += "p"
merged .RawValue += "p"
merged .Span .End = afterNext .Span .End
result = append (result , merged )
i += 2
continue
}
}
result = append (result , merged )
i += 1
continue
}
}
}
result = append (result , t )
}
final := make (TokenSlice , 0 , len (result ))
for i := 0 ; i < len (result ); i ++ {
t := result [i ]
if t .Type == WORD_TOKEN && (t .Value == "$" || t .Value == "@" ) && i +1 < len (result ) {
next := result [i +1 ]
if next .Type == WORDBREAK_TOKEN && next .Value == "(" && t .adjoins (next ) {
merged := Token {
Type : WORDBREAK_TOKEN ,
Value : t .Value + "(" ,
RawValue : t .RawValue + next .RawValue ,
Span : Span {Start : t .Span .Start , End : next .Span .End },
State : next .State ,
WordbreakType : WORDBREAK_SUBSTITUTION_OPEN ,
}
final = append (final , merged )
i ++
continue
}
}
if t .Type == WORDBREAK_TOKEN && t .Value == "(" {
t .WordbreakType = WORDBREAK_SUBSTITUTION_OPEN
}
if t .Type == WORDBREAK_TOKEN && t .Value == ")" {
t .WordbreakType = WORDBREAK_SUBSTITUTION_CLOSE
}
final = append (final , t )
}
return final
}
func (xonshFormat ) Variable (word Token ) (Variable , bool ) {
return variableRef (word , variableRules {
brace : true ,
expands : func (state LexerState ) bool { return state == IN_WORD_STATE },
barewordEscapes : true ,
nameStart : isPosixNameStart ,
nameRune : isPosixNameRune ,
})
}
var xonshNumericWordRe = regexp .MustCompile (
`^0[xX][0-9a-fA-F_]+$` +
`|^0[oO][0-7_]+$` +
`|^0[bB][01_]+$` +
`|^[0-9][0-9_]*\.?[0-9_]*([eE][0-9]+)?$` ,
)
func (xonshFormat ) IsBareWord (s string ) bool {
if s != "" && s [0 ] >= '0' && s [0 ] <= '9' &&
(strings .ContainsAny (s , "+-*/%" ) || xonshNumericWordRe .MatchString (s )) {
return false
}
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 .