| 1 | // Package shlex provides a simple lexical analysis like Unix shell. |
| 2 | package shlexscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/ |
| 3 | |
| 4 | import ( |
| 5 | "bufioscip-go gomod github.com/golang/go/src go1.13 bufio/" |
| 6 | "errorsscip-go gomod github.com/golang/go/src go1.13 errors/" |
| 7 | "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 9 | "unicodescip-go gomod github.com/golang/go/src go1.13 unicode/" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | ErrNoClosingscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/ErrNoClosing. = errorsscip-go gomod github.com/golang/go/src go1.13 errors/.Newscip-go gomod github.com/golang/go/src go1.13 errors/New().("No closing quotation") |
| 14 | ErrNoEscapedscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/ErrNoEscaped. = errorsscip-go gomod github.com/golang/go/src go1.13 errors/.Newscip-go gomod github.com/golang/go/src go1.13 errors/New().("No escaped character") |
| 15 | ) |
| 16 | |
| 17 | // Tokenizer is the interface that classifies a token according to |
| 18 | // words, whitespaces, quotations, escapes and escaped quotations. |
| 19 | type Tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer# interface { |
| 20 | IsWordscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWord.(rune) bool |
| 21 | IsWhitespacescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWhitespace.(rune) bool |
| 22 | IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(rune) bool |
| 23 | IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(rune) bool |
| 24 | IsEscapedQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscapedQuote.(rune) bool |
| 25 | } |
| 26 | |
| 27 | // DefaultTokenizer implements a simple tokenizer like Unix shell. |
| 28 | type DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer# struct{} |
| 29 | |
| 30 | func (t *DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#) IsWordscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#IsWord().(r rune) bool { |
| 31 | return r == '_' || unicodescip-go gomod github.com/golang/go/src go1.13 unicode/.IsLetterscip-go gomod github.com/golang/go/src go1.13 unicode/IsLetter().(r) || unicodescip-go gomod github.com/golang/go/src go1.13 unicode/.IsNumberscip-go gomod github.com/golang/go/src go1.13 unicode/IsNumber().(r) |
| 32 | } |
| 33 | func (t *DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#) IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#IsQuote().(r rune) bool { |
| 34 | switch r { |
| 35 | case '\'', '"': |
| 36 | return true |
| 37 | default: |
| 38 | return false |
| 39 | } |
| 40 | } |
| 41 | func (t *DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#) IsWhitespacescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#IsWhitespace().(r rune) bool { |
| 42 | return unicodescip-go gomod github.com/golang/go/src go1.13 unicode/.IsSpacescip-go gomod github.com/golang/go/src go1.13 unicode/IsSpace().(r) |
| 43 | } |
| 44 | func (t *DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#) IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#IsEscape().(r rune) bool { |
| 45 | return r == '\\' |
| 46 | } |
| 47 | func (t *DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#) IsEscapedQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#IsEscapedQuote().(r rune) bool { |
| 48 | return r == '"' |
| 49 | } |
| 50 | |
| 51 | // Lexer represents a lexical analyzer. |
| 52 | type Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer# struct { |
| 53 | readerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#reader. *bufioscip-go gomod github.com/golang/go/src go1.13 bufio/.Readerscip-go gomod github.com/golang/go/src go1.13 bufio/Reader# |
| 54 | tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#tokenizer. Tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer# |
| 55 | posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. bool |
| 56 | whitespacesplitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#whitespacesplit. bool |
| 57 | } |
| 58 | |
| 59 | // NewLexer creates a new Lexer reading from io.Reader. This Lexer |
| 60 | // has a DefaultTokenizer according to posix and whitespacesplit |
| 61 | // rules. |
| 62 | func NewLexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/NewLexer().(r ioscip-go gomod github.com/golang/go/src go1.13 io/.Readerscip-go gomod github.com/golang/go/src go1.13 io/Reader#, posix, whitespacesplit bool) *Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer# { |
| 63 | return &Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#{ |
| 64 | readerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#reader.: bufioscip-go gomod github.com/golang/go/src go1.13 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.13 bufio/NewReader().(r), |
| 65 | tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#tokenizer.: &DefaultTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/DefaultTokenizer#{}, |
| 66 | posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix.: posix, |
| 67 | whitespacesplitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#whitespacesplit.: whitespacesplit, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // NewLexerString creates a new Lexer reading from a string. This |
| 72 | // Lexer has a DefaultTokenizer according to posix and whitespacesplit |
| 73 | // rules. |
| 74 | func NewLexerStringscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/NewLexerString().(s string, posix, whitespacesplit bool) *Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer# { |
| 75 | return NewLexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/NewLexer().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.13 strings/NewReader().(s), posix, whitespacesplit) |
| 76 | } |
| 77 | |
| 78 | // Split splits a string according to posix or non-posix rules. |
| 79 | func Splitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Split().(s string, posix bool) ([]string, error) { |
| 80 | return NewLexerStringscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/NewLexerString().(s, posix, true).Splitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#Split().() |
| 81 | } |
| 82 | |
| 83 | // SetTokenizer sets a Tokenizer. |
| 84 | func (l *Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#) SetTokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#SetTokenizer().(t Tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#) { |
| 85 | l.tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#tokenizer. = t |
| 86 | } |
| 87 | |
| 88 | func (l *Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#) Splitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#Split().() ([]string, error) { |
| 89 | result := make([]string, 0) |
| 90 | for { |
| 91 | token, err := l.readTokenscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#readToken().() |
| 92 | if token != "" { |
| 93 | result = append(result, token) |
| 94 | } |
| 95 | |
| 96 | if err == ioscip-go gomod github.com/golang/go/src go1.13 io/.EOFscip-go gomod github.com/golang/go/src go1.13 io/EOF. { |
| 97 | break |
| 98 | } else if err != nil { |
| 99 | return result, err |
| 100 | } |
| 101 | } |
| 102 | return result, nil |
| 103 | } |
| 104 | |
| 105 | func (l *Lexerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#) readTokenscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#readToken().() (string, error) { |
| 106 | t := l.tokenizerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#tokenizer. |
| 107 | token := "" |
| 108 | quoted := false |
| 109 | state := ' ' |
| 110 | escapedstate := ' ' |
| 111 | scanning: |
| 112 | for { |
| 113 | next, _, err := l.readerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#reader..ReadRunescip-go gomod github.com/golang/go/src go1.13 bufio/Reader#ReadRune().() |
| 114 | if err != nil { |
| 115 | if t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(state) { |
| 116 | return token, ErrNoClosingscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/ErrNoClosing. |
| 117 | } else if t.IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(state) { |
| 118 | return token, ErrNoEscapedscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/ErrNoEscaped. |
| 119 | } |
| 120 | return token, err |
| 121 | } |
| 122 | |
| 123 | switch { |
| 124 | case t.IsWhitespacescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWhitespace.(state): |
| 125 | switch { |
| 126 | case t.IsWhitespacescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWhitespace.(next): |
| 127 | break scanning |
| 128 | case l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && t.IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(next): |
| 129 | escapedstate = 'a' |
| 130 | state = next |
| 131 | case t.IsWordscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWord.(next): |
| 132 | token += string(next) |
| 133 | state = 'a' |
| 134 | case t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(next): |
| 135 | if !l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. { |
| 136 | token += string(next) |
| 137 | } |
| 138 | state = next |
| 139 | default: |
| 140 | token = string(next) |
| 141 | if l.whitespacesplitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#whitespacesplit. { |
| 142 | state = 'a' |
| 143 | } else if token != "" || (l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && quoted) { |
| 144 | break scanning |
| 145 | } |
| 146 | } |
| 147 | case t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(state): |
| 148 | quoted = true |
| 149 | switch { |
| 150 | case next == state: |
| 151 | if !l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. { |
| 152 | token += string(next) |
| 153 | break scanning |
| 154 | } else { |
| 155 | state = 'a' |
| 156 | } |
| 157 | case l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && t.IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(next) && t.IsEscapedQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscapedQuote.(state): |
| 158 | escapedstate = state |
| 159 | state = next |
| 160 | default: |
| 161 | token += string(next) |
| 162 | } |
| 163 | case t.IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(state): |
| 164 | if t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(escapedstate) && next != state && next != escapedstate { |
| 165 | token += string(state) |
| 166 | } |
| 167 | token += string(next) |
| 168 | state = escapedstate |
| 169 | case t.IsWordscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWord.(state): |
| 170 | switch { |
| 171 | case t.IsWhitespacescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWhitespace.(next): |
| 172 | if token != "" || (l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && quoted) { |
| 173 | break scanning |
| 174 | } |
| 175 | case l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(next): |
| 176 | state = next |
| 177 | case l.posixscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#posix. && t.IsEscapescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsEscape.(next): |
| 178 | escapedstate = 'a' |
| 179 | state = next |
| 180 | case t.IsWordscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsWord.(next) || t.IsQuotescip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Tokenizer#IsQuote.(next): |
| 181 | token += string(next) |
| 182 | default: |
| 183 | if l.whitespacesplitscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#whitespacesplit. { |
| 184 | token += string(next) |
| 185 | } else if token != "" { |
| 186 | l.readerscip-go gomod github.com/anmitsu/go-shlex . `github.com/anmitsu/go-shlex`/Lexer#reader..UnreadRunescip-go gomod github.com/golang/go/src go1.13 bufio/Reader#UnreadRune().() |
| 187 | break scanning |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return token, nil |
| 193 | } |