package shlex
func posixSubstitutionPostProcess(tokens TokenSlice ) TokenSlice {
result := make (TokenSlice , 0 , len (tokens ))
depth := 0
arithmeticDepth := 0
i := 0
for i < len (tokens ) {
t := tokens [i ]
if t .Type == WORD_TOKEN && t .Value == "$" && i +1 < len (tokens ) {
next := tokens [i +1 ]
if next .Type == WORDBREAK_TOKEN && next .Value == "(" && t .adjoins (next ) {
if i +2 < len (tokens ) && tokens [i +2 ].Type == WORDBREAK_TOKEN &&
tokens [i +2 ].Value == "(" && next .adjoins (tokens [i +2 ]) {
merged := Token {
Type : WORDBREAK_TOKEN ,
Value : "$(" ,
RawValue : t .RawValue + next .RawValue + tokens [i +2 ].RawValue ,
Span : Span {Start : t .Span .Start , End : tokens [i +2 ].Span .End },
State : tokens [i +2 ].State ,
WordbreakType : WORDBREAK_SUBSTITUTION_OPEN ,
}
result = append (result , merged )
depth ++
arithmeticDepth ++
i += 3
continue
}
merged := Token {
Type : WORDBREAK_TOKEN ,
Value : "$(" ,
RawValue : t .RawValue + next .RawValue ,
Span : Span {Start : t .Span .Start , End : next .Span .End },
State : next .State ,
WordbreakType : WORDBREAK_SUBSTITUTION_OPEN ,
}
result = append (result , merged )
depth ++
i += 2
continue
}
}
if t .Type == WORDBREAK_TOKEN && t .WordbreakType .IsRedirect () &&
(t .Value == "<" || t .Value == ">" || t .Value == ">>" ) && i +1 < len (tokens ) {
next := tokens [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 ,
}
result = append (result , merged )
depth ++
i += 2
continue
}
}
if t .Type == WORDBREAK_TOKEN && t .Value == "(" {
merged := Token {
Type : t .Type ,
Value : t .Value ,
RawValue : t .RawValue ,
Span : t .Span ,
State : t .State ,
WordbreakType : WORDBREAK_SUBSTITUTION_OPEN ,
}
result = append (result , merged )
depth ++
i ++
continue
}
if t .Type == WORDBREAK_TOKEN && t .Value == ")" {
if arithmeticDepth > 0 {
if i +1 < len (tokens ) && tokens [i +1 ].Type == WORDBREAK_TOKEN &&
tokens [i +1 ].Value == ")" && t .adjoins (tokens [i +1 ]) {
merged := Token {
Type : WORDBREAK_TOKEN ,
Value : "))" ,
RawValue : t .RawValue + tokens [i +1 ].RawValue ,
Span : Span {Start : t .Span .Start , End : tokens [i +1 ].Span .End },
State : tokens [i +1 ].State ,
WordbreakType : WORDBREAK_SUBSTITUTION_CLOSE ,
}
result = append (result , merged )
depth --
arithmeticDepth --
i += 2
continue
}
}
merged := Token {
Type : t .Type ,
Value : t .Value ,
RawValue : t .RawValue ,
Span : t .Span ,
State : t .State ,
WordbreakType : WORDBREAK_SUBSTITUTION_CLOSE ,
}
result = append (result , merged )
if depth > 0 {
depth --
}
i ++
continue
}
result = append (result , t )
i ++
}
return result
}
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 .