| 1 |
/* |
| 2 |
Copyright 2012 Google Inc. All Rights Reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
Package shlex implements a simple lexer which splits input in to tokens using |
| 19 |
shell-style rules for quoting and commenting. |
| 20 |
|
| 21 |
The basic use case uses the default ASCII lexer to split a string into sub-strings: |
| 22 |
|
| 23 |
shlex.Split("one \"two three\" four") -> []string{"one", "two three", "four"} |
| 24 |
|
| 25 |
To process a stream of strings: |
| 26 |
|
| 27 |
l := NewLexer(os.Stdin) |
| 28 |
for ; token, err := l.Next(); err != nil { |
| 29 |
// process token |
| 30 |
} |
| 31 |
|
| 32 |
To access the raw token stream (which includes tokens for comments): |
| 33 |
|
| 34 |
t := NewTokenizer(os.Stdin) |
| 35 |
for ; token, err := t.Next(); err != nil { |
| 36 |
// process token |
| 37 |
} |
| 38 |
*/ |
| 39 |
package shlex |
| 40 |
|
| 41 |
import ( |
| 42 |
"bufio" |
| 43 |
"fmt" |
| 44 |
"io" |
| 45 |
"strings" |
| 46 |
) |
| 47 |
|
| 48 |
// TokenType is a top-level token classification: A word, space, comment, unknown. |
| 49 |
type TokenType int |
| 50 |
|
| 51 |
// runeTokenClass is the type of a UTF-8 character classification: A quote, space, escape. |
| 52 |
type runeTokenClass int |
| 53 |
|
| 54 |
// the internal state used by the lexer state machine |
| 55 |
type lexerState int |
| 56 |
|
| 57 |
// Token is a (type, value) pair representing a lexographical token. |
| 58 |
type Token struct { |
| 59 |
tokenType TokenType |
| 60 |
value string |
| 61 |
} |
| 62 |
|
| 63 |
// Equal reports whether tokens a, and b, are equal. |
| 64 |
// Two tokens are equal if both their types and values are equal. A nil token can |
| 65 |
// never be equal to another token. |
| 66 |
func (a *Token) Equal(b *Token) bool { |
| 67 |
if a == nil || b == nil { |
| 68 |
return false |
| 69 |
} |
| 70 |
if a.tokenType != b.tokenType { |
| 71 |
return false |
| 72 |
} |
| 73 |
return a.value == b.value |
| 74 |
} |
| 75 |
|
| 76 |
// Named classes of UTF-8 runes |
| 77 |
const ( |
| 78 |
spaceRunes = " \t\r\n" |
| 79 |
escapingQuoteRunes = `"` |
| 80 |
nonEscapingQuoteRunes = "'" |
| 81 |
escapeRunes = `\` |
| 82 |
commentRunes = "#" |
| 83 |
) |
| 84 |
|
| 85 |
// Classes of rune token |
| 86 |
const ( |
| 87 |
unknownRuneClass runeTokenClass = iota |
| 88 |
spaceRuneClass |
| 89 |
escapingQuoteRuneClass |
| 90 |
nonEscapingQuoteRuneClass |
| 91 |
escapeRuneClass |
| 92 |
commentRuneClass |
| 93 |
eofRuneClass |
| 94 |
) |
| 95 |
|
| 96 |
// Classes of lexographic token |
| 97 |
const ( |
| 98 |
UnknownToken TokenType = iota |
| 99 |
WordToken |
| 100 |
SpaceToken |
| 101 |
CommentToken |
| 102 |
) |
| 103 |
|
| 104 |
// Lexer state machine states |
| 105 |
const ( |
| 106 |
startState lexerState = iota // no runes have been seen |
| 107 |
inWordState // processing regular runes in a word |
| 108 |
escapingState // we have just consumed an escape rune; the next rune is literal |
| 109 |
escapingQuotedState // we have just consumed an escape rune within a quoted string |
| 110 |
quotingEscapingState // we are within a quoted string that supports escaping ("...") |
| 111 |
quotingState // we are within a string that does not support escaping ('...') |
| 112 |
commentState // we are within a comment (everything following an unquoted or unescaped # |
| 113 |
) |
| 114 |
|
| 115 |
// tokenClassifier is used for classifying rune characters. |
| 116 |
type tokenClassifier map[rune]runeTokenClass |
| 117 |
|
| 118 |
func (typeMap tokenClassifier) addRuneClass(runes string, tokenType runeTokenClass) { |
| 119 |
for _, runeChar := range runes { |
| 120 |
typeMap[runeChar] = tokenType |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// newDefaultClassifier creates a new classifier for ASCII characters. |
| 125 |
func newDefaultClassifier() tokenClassifier { |
| 126 |
t := tokenClassifier{} |
| 127 |
t.addRuneClass(spaceRunes, spaceRuneClass) |
| 128 |
t.addRuneClass(escapingQuoteRunes, escapingQuoteRuneClass) |
| 129 |
t.addRuneClass(nonEscapingQuoteRunes, nonEscapingQuoteRuneClass) |
| 130 |
t.addRuneClass(escapeRunes, escapeRuneClass) |
| 131 |
t.addRuneClass(commentRunes, commentRuneClass) |
| 132 |
return t |
| 133 |
} |
| 134 |
|
| 135 |
// ClassifyRune classifiees a rune |
| 136 |
func (t tokenClassifier) ClassifyRune(runeVal rune) runeTokenClass { |
| 137 |
return t[runeVal] |
| 138 |
} |
| 139 |
|
| 140 |
// Lexer turns an input stream into a sequence of tokens. Whitespace and comments are skipped. |
| 141 |
type Lexer Tokenizer |
| 142 |
|
| 143 |
// NewLexer creates a new lexer from an input stream. |
| 144 |
func NewLexer(r io.Reader) *Lexer { |
| 145 |
|
| 146 |
return (*Lexer)(NewTokenizer(r)) |
| 147 |
} |
| 148 |
|
| 149 |
// Next returns the next word, or an error. If there are no more words, |
| 150 |
// the error will be io.EOF. |
| 151 |
func (l *Lexer) Next() (string, error) { |
| 152 |
for { |
| 153 |
token, err := (*Tokenizer)(l).Next() |
| 154 |
if err != nil { |
| 155 |
return "", err |
| 156 |
} |
| 157 |
switch token.tokenType { |
| 158 |
case WordToken: |
| 159 |
return token.value, nil |
| 160 |
case CommentToken: |
| 161 |
// skip comments |
| 162 |
default: |
| 163 |
return "", fmt.Errorf("unknown token type: %v", token.tokenType) |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Tokenizer turns an input stream into a sequence of typed tokens |
| 169 |
type Tokenizer struct { |
| 170 |
input bufio.Reader |
| 171 |
classifier tokenClassifier |
| 172 |
} |
| 173 |
|
| 174 |
// NewTokenizer creates a new tokenizer from an input stream. |
| 175 |
func NewTokenizer(r io.Reader) *Tokenizer { |
| 176 |
input := bufio.NewReader(r) |
| 177 |
classifier := newDefaultClassifier() |
| 178 |
return &Tokenizer{ |
| 179 |
input: *input, |
| 180 |
classifier: classifier} |
| 181 |
} |
| 182 |
|
| 183 |
// scanStream scans the stream for the next token using the internal state machine. |
| 184 |
// It will panic if it encounters a rune which it does not know how to handle. |
| 185 |
func (t *Tokenizer) scanStream() (*Token, error) { |
| 186 |
state := startState |
| 187 |
var tokenType TokenType |
| 188 |
var value []rune |
| 189 |
var nextRune rune |
| 190 |
var nextRuneType runeTokenClass |
| 191 |
var err error |
| 192 |
|
| 193 |
for { |
| 194 |
nextRune, _, err = t.input.ReadRune() |
| 195 |
nextRuneType = t.classifier.ClassifyRune(nextRune) |
| 196 |
|
| 197 |
if err == io.EOF { |
| 198 |
nextRuneType = eofRuneClass |
| 199 |
err = nil |
| 200 |
} else if err != nil { |
| 201 |
return nil, err |
| 202 |
} |
| 203 |
|
| 204 |
switch state { |
| 205 |
case startState: // no runes read yet |
| 206 |
{ |
| 207 |
switch nextRuneType { |
| 208 |
case eofRuneClass: |
| 209 |
{ |
| 210 |
return nil, io.EOF |
| 211 |
} |
| 212 |
case spaceRuneClass: |
| 213 |
{ |
| 214 |
} |
| 215 |
case escapingQuoteRuneClass: |
| 216 |
{ |
| 217 |
tokenType = WordToken |
| 218 |
state = quotingEscapingState |
| 219 |
} |
| 220 |
case nonEscapingQuoteRuneClass: |
| 221 |
{ |
| 222 |
tokenType = WordToken |
| 223 |
state = quotingState |
| 224 |
} |
| 225 |
case escapeRuneClass: |
| 226 |
{ |
| 227 |
state = escapingState |
| 228 |
} |
| 229 |
case commentRuneClass: |
| 230 |
{ |
| 231 |
tokenType = CommentToken |
| 232 |
state = commentState |
| 233 |
} |
| 234 |
default: |
| 235 |
{ |
| 236 |
tokenType = WordToken |
| 237 |
value = append(value, nextRune) |
| 238 |
state = inWordState |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
case inWordState: // in a regular word |
| 243 |
{ |
| 244 |
switch nextRuneType { |
| 245 |
case eofRuneClass: |
| 246 |
{ |
| 247 |
token := &Token{ |
| 248 |
tokenType: tokenType, |
| 249 |
value: string(value)} |
| 250 |
return token, err |
| 251 |
} |
| 252 |
case spaceRuneClass: |
| 253 |
{ |
| 254 |
token := &Token{ |
| 255 |
tokenType: tokenType, |
| 256 |
value: string(value)} |
| 257 |
return token, err |
| 258 |
} |
| 259 |
case escapingQuoteRuneClass: |
| 260 |
{ |
| 261 |
state = quotingEscapingState |
| 262 |
} |
| 263 |
case nonEscapingQuoteRuneClass: |
| 264 |
{ |
| 265 |
state = quotingState |
| 266 |
} |
| 267 |
case escapeRuneClass: |
| 268 |
{ |
| 269 |
state = escapingState |
| 270 |
} |
| 271 |
default: |
| 272 |
{ |
| 273 |
value = append(value, nextRune) |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
case escapingState: // the rune after an escape character |
| 278 |
{ |
| 279 |
switch nextRuneType { |
| 280 |
case eofRuneClass: |
| 281 |
{ |
| 282 |
err = fmt.Errorf("EOF found after escape character") |
| 283 |
token := &Token{ |
| 284 |
tokenType: tokenType, |
| 285 |
value: string(value)} |
| 286 |
return token, err |
| 287 |
} |
| 288 |
default: |
| 289 |
{ |
| 290 |
// Backslash at end of line does not mean literal |
| 291 |
// newline, rather it means line continuation. If we |
| 292 |
// see this then we need to go back to the previous |
| 293 |
// state. If we had already been parsing a word |
| 294 |
// token then we can continue that, otherwise we |
| 295 |
// haven't seen any runes yet so we should go back |
| 296 |
// to the start. |
| 297 |
if nextRune == '\n' { |
| 298 |
if tokenType == WordToken { |
| 299 |
state = inWordState |
| 300 |
} else { |
| 301 |
state = startState |
| 302 |
} |
| 303 |
} else { |
| 304 |
tokenType = WordToken |
| 305 |
state = inWordState |
| 306 |
value = append(value, nextRune) |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
case escapingQuotedState: // the next rune after an escape character, in double quotes |
| 312 |
{ |
| 313 |
switch nextRuneType { |
| 314 |
case eofRuneClass: |
| 315 |
{ |
| 316 |
err = fmt.Errorf("EOF found after escape character") |
| 317 |
token := &Token{ |
| 318 |
tokenType: tokenType, |
| 319 |
value: string(value)} |
| 320 |
return token, err |
| 321 |
} |
| 322 |
default: |
| 323 |
{ |
| 324 |
state = quotingEscapingState |
| 325 |
value = append(value, nextRune) |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
case quotingEscapingState: // in escaping double quotes |
| 330 |
{ |
| 331 |
switch nextRuneType { |
| 332 |
case eofRuneClass: |
| 333 |
{ |
| 334 |
err = fmt.Errorf("EOF found when expecting closing quote") |
| 335 |
token := &Token{ |
| 336 |
tokenType: tokenType, |
| 337 |
value: string(value)} |
| 338 |
return token, err |
| 339 |
} |
| 340 |
case escapingQuoteRuneClass: |
| 341 |
{ |
| 342 |
state = inWordState |
| 343 |
} |
| 344 |
case escapeRuneClass: |
| 345 |
{ |
| 346 |
state = escapingQuotedState |
| 347 |
} |
| 348 |
default: |
| 349 |
{ |
| 350 |
value = append(value, nextRune) |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
case quotingState: // in non-escaping single quotes |
| 355 |
{ |
| 356 |
switch nextRuneType { |
| 357 |
case eofRuneClass: |
| 358 |
{ |
| 359 |
err = fmt.Errorf("EOF found when expecting closing quote") |
| 360 |
token := &Token{ |
| 361 |
tokenType: tokenType, |
| 362 |
value: string(value)} |
| 363 |
return token, err |
| 364 |
} |
| 365 |
case nonEscapingQuoteRuneClass: |
| 366 |
{ |
| 367 |
state = inWordState |
| 368 |
} |
| 369 |
default: |
| 370 |
{ |
| 371 |
value = append(value, nextRune) |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
case commentState: // in a comment |
| 376 |
{ |
| 377 |
switch nextRuneType { |
| 378 |
case eofRuneClass: |
| 379 |
{ |
| 380 |
token := &Token{ |
| 381 |
tokenType: tokenType, |
| 382 |
value: string(value)} |
| 383 |
return token, err |
| 384 |
} |
| 385 |
case spaceRuneClass: |
| 386 |
{ |
| 387 |
if nextRune == '\n' { |
| 388 |
state = startState |
| 389 |
token := &Token{ |
| 390 |
tokenType: tokenType, |
| 391 |
value: string(value)} |
| 392 |
return token, err |
| 393 |
} else { |
| 394 |
value = append(value, nextRune) |
| 395 |
} |
| 396 |
} |
| 397 |
default: |
| 398 |
{ |
| 399 |
value = append(value, nextRune) |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
default: |
| 404 |
{ |
| 405 |
return nil, fmt.Errorf("unexpected state: %v", state) |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// Next returns the next token in the stream. |
| 412 |
func (t *Tokenizer) Next() (*Token, error) { |
| 413 |
return t.scanStream() |
| 414 |
} |
| 415 |
|
| 416 |
// Split partitions a string into a slice of strings. |
| 417 |
func Split(s string) ([]string, error) { |
| 418 |
l := NewLexer(strings.NewReader(s)) |
| 419 |
subStrings := make([]string, 0) |
| 420 |
for { |
| 421 |
word, err := l.Next() |
| 422 |
if err != nil { |
| 423 |
if err == io.EOF { |
| 424 |
return subStrings, nil |
| 425 |
} |
| 426 |
return subStrings, err |
| 427 |
} |
| 428 |
subStrings = append(subStrings, word) |
| 429 |
} |
| 430 |
} |