You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.3 KiB
Go
161 lines
3.3 KiB
Go
package shell
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
)
|
|
|
|
type DollarParameters struct {
|
|
ignoreParams *IgnoreFirstCharParameters
|
|
subparsers []Parser
|
|
lastCreated *Dollar
|
|
}
|
|
|
|
func (p *DollarParameters) SubParsers() []Parser {
|
|
if p.subparsers == nil {
|
|
if p.ignoreParams == nil {
|
|
p.ignoreParams = &IgnoreFirstCharParameters{}
|
|
}
|
|
|
|
p.subparsers = []Parser{
|
|
&BaseParser{
|
|
parameters: p.ignoreParams,
|
|
},
|
|
&BaseParser{
|
|
parameters: &WordParameters{},
|
|
},
|
|
&BaseParser{
|
|
parameters: &SubshellParameters{},
|
|
},
|
|
&BaseParser{
|
|
parameters: &DollarBlockParameters{},
|
|
},
|
|
}
|
|
}
|
|
|
|
return p.subparsers
|
|
}
|
|
|
|
func (p *DollarParameters) Enter(_ *CharIterator) error {
|
|
if p.ignoreParams != nil {
|
|
p.ignoreParams.Reset()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *DollarParameters) Supports(charsBefore []rune, r rune) bool {
|
|
if r == '$' {
|
|
return len(charsBefore) == 0 ||
|
|
countBackslashSuffixes(charsBefore)%2 == 0
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (p *DollarParameters) ShouldLeave(charsBefore []rune, r rune) bool {
|
|
if p.lastCreated != nil {
|
|
if len(p.lastCreated.Tokens()) > 0 {
|
|
if _, isSub := p.lastCreated.Tokens()[0].(*Subshell); isSub {
|
|
return true
|
|
} else if _, isWord := p.lastCreated.Tokens()[0].(*Word); isWord {
|
|
return true
|
|
} else if _, isBlock := p.lastCreated.Tokens()[0].(*Block); isBlock {
|
|
return true
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if isWhitespace(r) || r == ';' || r == ')' {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (p *DollarParameters) Leave(i *CharIterator) error {
|
|
return i.Previous()
|
|
}
|
|
|
|
func (p *DollarParameters) MakeToken() Token {
|
|
dollar := &Dollar{}
|
|
|
|
p.lastCreated = dollar
|
|
|
|
return dollar
|
|
}
|
|
|
|
type Dollar struct {
|
|
BaseToken
|
|
}
|
|
|
|
func (d *Dollar) Expand(state State, stdin io.Reader, stderr io.Writer) ([]string, uint8) {
|
|
if len(d.tokens) > 1 {
|
|
logstr := "Unexpected tokens after dollar sign: " + d.tokens[1].String()
|
|
log.Println(logstr)
|
|
stderr.Write([]byte(logstr))
|
|
return []string{""}, 2
|
|
}
|
|
|
|
if word, ok := d.tokens[0].(*Word); ok {
|
|
return []string{state.Variable(word.String())}, 0
|
|
} else if block, ok := d.tokens[0].(*DollarBlock); ok && len(block.Tokens()) > 0 {
|
|
|
|
if word, ok := block.Tokens()[0].(*Word); ok {
|
|
return []string{state.Variable(word.String())}, 0
|
|
} else {
|
|
logstr := "Unexpected tokens after dollar sign: " + block.String()
|
|
log.Println(logstr)
|
|
stderr.Write([]byte(logstr))
|
|
return []string{}, 2
|
|
}
|
|
} else if subshell, ok := d.Tokens()[0].(*Subshell); ok {
|
|
stdout := bytes.NewBuffer([]byte{})
|
|
ret := subshell.Evaluate(state, stdin, stdout, stderr)
|
|
return []string{stdout.String()}, ret
|
|
}
|
|
logstr := "Unexpected tokens after dollar sign: " + d.String()
|
|
log.Println(logstr)
|
|
stderr.Write([]byte(logstr))
|
|
return []string{""}, 2
|
|
}
|
|
|
|
type IgnoreFirstCharParameters struct {
|
|
entered bool
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) Reset() {
|
|
p.entered = false
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) Enter(_ *CharIterator) error {
|
|
p.entered = true
|
|
return nil
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) SubParsers() []Parser {
|
|
return []Parser{
|
|
&BaseParser{
|
|
parameters: &CharParserParameters{},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) MakeToken() Token {
|
|
return nil
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) Supports(_ []rune, r rune) bool {
|
|
if !p.entered {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (p *IgnoreFirstCharParameters) ShouldLeave(_ []rune, r rune) bool {
|
|
return true
|
|
}
|