package shlex
import (
"strconv"
)
type TokenSlice []Token
func (t TokenSlice ) Strings () []string {
s := make ([]string , 0 , len (t ))
for _ , token := range t {
s = append (s , token .Value )
}
return s
}
func (t TokenSlice ) pipelines () []TokenSlice {
pipelines := make ([]TokenSlice , 0 )
pipeline := make (TokenSlice , 0 )
depth := 0
for _ , token := range t {
switch {
case token .WordbreakType == WORDBREAK_SUBSTITUTION_OPEN :
depth ++
pipeline = append (pipeline , token )
case token .WordbreakType == WORDBREAK_SUBSTITUTION_CLOSE :
if depth > 0 {
depth --
}
pipeline = append (pipeline , token )
case depth > 0 :
pipeline = append (pipeline , token )
case token .Type == WORDBREAK_TOKEN && token .WordbreakType .IsPipelineDelimiter ():
pipelines = append (pipelines , pipeline )
pipeline = make (TokenSlice , 0 )
default :
pipeline = append (pipeline , token )
}
}
return append (pipelines , pipeline )
}
func (t TokenSlice ) CurrentPipeline () TokenSlice {
pipelines := t .pipelines ()
return pipelines [len (pipelines )-1 ]
}
func (t TokenSlice ) Words () TokenSlice {
words := make (TokenSlice , 0 )
for index , token := range t {
switch {
case index == 0 :
words = append (words , token )
case t [index -1 ].adjoins (token ):
words [len (words )-1 ].Value += token .Value
words [len (words )-1 ].RawValue += token .RawValue
words [len (words )-1 ].Span .End = token .Span .End
words [len (words )-1 ].State = token .State
default :
words = append (words , token )
}
}
return words
}
func (t TokenSlice ) FilterRedirects () TokenSlice {
filtered := make (TokenSlice , 0 )
for index , token := range t {
switch token .Type {
case WORDBREAK_TOKEN :
if token .WordbreakType .IsRedirect () {
continue
}
}
if index > 0 {
if t [index -1 ].WordbreakType .IsRedirect () {
continue
}
}
if index < len (t )-1 {
next := t [index +1 ]
if token .adjoins (next ) {
if _ , err := strconv .Atoi (token .RawValue ); err == nil {
if t [index +1 ].WordbreakType .IsRedirect () {
continue
}
}
}
}
filtered = append (filtered , token )
}
return filtered
}
func (t TokenSlice ) wordsWithSubstitutions () TokenSlice {
words := make (TokenSlice , 0 )
depth := 0
var sub *Token
for index , token := range t {
switch {
case token .WordbreakType == WORDBREAK_SUBSTITUTION_OPEN :
if depth == 0 {
sub = &Token {
Type : WORD_TOKEN ,
Span : token .Span ,
}
}
depth ++
if sub != nil {
if sub .Span .End != token .Span .Start && sub .RawValue != "" {
sub .RawValue += " "
sub .Value += " "
}
sub .RawValue += token .RawValue
sub .Value += token .RawValue
sub .Span .End = token .Span .End
sub .State = token .State
}
case token .WordbreakType == WORDBREAK_SUBSTITUTION_CLOSE :
if depth > 0 {
if sub != nil {
if sub .Span .End != token .Span .Start && sub .RawValue != "" {
sub .RawValue += " "
sub .Value += " "
}
sub .RawValue += token .RawValue
sub .Value += token .RawValue
sub .Span .End = token .Span .End
sub .State = token .State
}
depth --
if depth == 0 {
words = append (words , *sub )
sub = nil
}
}
case depth > 0 :
if sub != nil {
if sub .Span .End != token .Span .Start && sub .RawValue != "" {
sub .RawValue += " "
sub .Value += " "
}
sub .RawValue += token .RawValue
sub .Value += token .RawValue
sub .Span .End = token .Span .End
sub .State = token .State
}
default :
if index == 0 {
words = append (words , token )
} else if t [index -1 ].adjoins (token ) {
words [len (words )-1 ].Value += token .Value
words [len (words )-1 ].RawValue += token .RawValue
words [len (words )-1 ].Span .End = token .Span .End
words [len (words )-1 ].State = token .State
} else {
words = append (words , token )
}
}
}
if depth > 0 {
return words
}
return words
}
func (t TokenSlice ) currentToken () (token Token ) {
if len (t ) > 0 {
token = t [len (t )-1 ]
}
return
}
func (t TokenSlice ) WordbreakPrefix () string {
if len (t ) == 0 {
return ""
}
found := false
prefix := ""
last := t [len (t )-1 ]
switch last .State {
case QUOTING_STATE , QUOTING_ESCAPING_STATE , ESCAPING_QUOTED_STATE ,
QUOTING_TRIPLE_STATE , QUOTING_TRIPLE_ESCAPING_STATE :
found = true
prefix = last .Value [:last .WordbreakIndex ]
}
for i := len (t ) - 2 ; i >= 0 ; i -- {
token := t [i ]
if !token .adjoins (t [i +1 ]) {
break
}
if token .Type == WORDBREAK_TOKEN && token .Value == "@" {
if !found {
found = true
continue
}
prevIsWB := i > 0 && t [i -1 ].Type == WORDBREAK_TOKEN
nextIsWB := i +1 < len (t ) && t [i +1 ].Type == WORDBREAK_TOKEN
if prevIsWB || nextIsWB {
continue
}
}
if !found && token .Type == WORDBREAK_TOKEN {
found = true
}
if found {
prefix = token .Value + prefix
}
}
return prefix
}
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 .