forked from google/shlex
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shlex.go
441 lines (403 loc) · 11.7 KB
/
shlex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package shlex
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
// TokenType is a top-level token classification: A word, space, comment, unknown.
type TokenType int
func (t TokenType) MarshalJSON() ([]byte, error) {
return json.Marshal(tokenTypes[t])
}
// runeTokenClass is the type of a UTF-8 character classification: A quote, space, escape.
type runeTokenClass int
// the internal state used by the lexer state machine
type LexerState int
func (l LexerState) MarshalJSON() ([]byte, error) {
return json.Marshal(lexerStates[l])
}
// Token is a (type, value) pair representing a lexographical token.
type Token struct {
Type TokenType
Value string
RawValue string
Index int
State LexerState
WordbreakType WordbreakType `json:",omitempty"`
WordbreakIndex int // index of last opening quote in Value (only correct when in quoting state)
}
func (t *Token) add(r rune) {
t.Value += string(r)
}
func (t *Token) removeLastRaw() {
runes := []rune(t.RawValue)
t.RawValue = string(runes[:len(runes)-1])
}
func (t Token) adjoins(other Token) bool {
return t.Index+len(t.RawValue) == other.Index || t.Index == other.Index+len(other.RawValue)
}
// Equal reports whether tokens a, and b, are equal.
// Two tokens are equal if both their types and values are equal. A nil token can
// never be equal to another token.
func (t *Token) Equal(other *Token) bool {
switch {
case t == nil,
other == nil,
t.Type != other.Type,
t.Value != other.Value,
t.RawValue != other.RawValue,
t.Index != other.Index,
t.State != other.State,
t.WordbreakType != other.WordbreakType,
t.WordbreakIndex != other.WordbreakIndex:
return false
default:
return true
}
}
// Named classes of UTF-8 runes
const (
spaceRunes = " \t\r\n"
escapingQuoteRunes = `"`
nonEscapingQuoteRunes = "'"
escapeRunes = `\`
commentRunes = "#"
)
// Classes of rune token
const (
unknownRuneClass runeTokenClass = iota
spaceRuneClass
escapingQuoteRuneClass
nonEscapingQuoteRuneClass
escapeRuneClass
commentRuneClass
wordbreakRuneClass
eofRuneClass
)
// Classes of lexographic token
const (
UNKNOWN_TOKEN TokenType = iota
WORD_TOKEN
SPACE_TOKEN
COMMENT_TOKEN
WORDBREAK_TOKEN
)
var tokenTypes = map[TokenType]string{
UNKNOWN_TOKEN: "UNKNOWN_TOKEN",
WORD_TOKEN: "WORD_TOKEN",
SPACE_TOKEN: "SPACE_TOKEN",
COMMENT_TOKEN: "COMMENT_TOKEN",
WORDBREAK_TOKEN: "WORDBREAK_TOKEN",
}
// Lexer state machine states
const (
START_STATE LexerState = iota // no runes have been seen
IN_WORD_STATE // processing regular runes in a word
ESCAPING_STATE // we have just consumed an escape rune; the next rune is literal
ESCAPING_QUOTED_STATE // we have just consumed an escape rune within a quoted string
QUOTING_ESCAPING_STATE // we are within a quoted string that supports escaping ("...")
QUOTING_STATE // we are within a string that does not support escaping ('...')
COMMENT_STATE // we are within a comment (everything following an unquoted or unescaped #
WORDBREAK_STATE // we have just consumed a wordbreak rune
)
var lexerStates = map[LexerState]string{
START_STATE: "START_STATE",
IN_WORD_STATE: "IN_WORD_STATE",
ESCAPING_STATE: "ESCAPING_STATE",
ESCAPING_QUOTED_STATE: "ESCAPING_QUOTED_STATE",
QUOTING_ESCAPING_STATE: "QUOTING_ESCAPING_STATE",
QUOTING_STATE: "QUOTING_STATE",
COMMENT_STATE: "COMMENT_STATE",
WORDBREAK_STATE: "WORDBREAK_STATE",
}
// tokenClassifier is used for classifying rune characters.
type tokenClassifier map[rune]runeTokenClass
func (typeMap tokenClassifier) addRuneClass(runes string, tokenType runeTokenClass) {
for _, runeChar := range runes {
typeMap[runeChar] = tokenType
}
}
// newDefaultClassifier creates a new classifier for ASCII characters.
func newDefaultClassifier() tokenClassifier {
t := tokenClassifier{}
t.addRuneClass(spaceRunes, spaceRuneClass)
t.addRuneClass(escapingQuoteRunes, escapingQuoteRuneClass)
t.addRuneClass(nonEscapingQuoteRunes, nonEscapingQuoteRuneClass)
t.addRuneClass(escapeRunes, escapeRuneClass)
t.addRuneClass(commentRunes, commentRuneClass)
wordbreakRunes := BASH_WORDBREAKS
if wordbreaks := os.Getenv("COMP_WORDBREAKS"); wordbreaks != "" {
wordbreakRunes = wordbreaks
}
filtered := make([]rune, 0)
for _, r := range wordbreakRunes {
if t.ClassifyRune(r) == unknownRuneClass {
filtered = append(filtered, r)
}
}
t.addRuneClass(string(filtered), wordbreakRuneClass)
return t
}
// ClassifyRune classifiees a rune
func (t tokenClassifier) ClassifyRune(runeVal rune) runeTokenClass {
return t[runeVal]
}
// lexer turns an input stream into a sequence of tokens. Whitespace and comments are skipped.
type lexer tokenizer
// newLexer creates a new lexer from an input stream.
func newLexer(r io.Reader) *lexer {
return (*lexer)(newTokenizer(r))
}
// Next returns the next token, or an error. If there are no more tokens,
// the error will be io.EOF.
func (l *lexer) Next() (*Token, error) {
for {
token, err := (*tokenizer)(l).Next()
if err != nil {
return token, err
}
switch token.Type {
case WORD_TOKEN, WORDBREAK_TOKEN:
return token, nil
case COMMENT_TOKEN:
// skip comments
default:
return nil, fmt.Errorf("unknown token type: %v", token.Type)
}
}
}
// tokenizer turns an input stream into a sequence of typed tokens
type tokenizer struct {
input bufio.Reader
classifier tokenClassifier
index int
state LexerState
}
func (t *tokenizer) ReadRune() (r rune, size int, err error) {
if r, size, err = t.input.ReadRune(); err == nil {
t.index += 1
}
return
}
func (t *tokenizer) UnreadRune() (err error) {
if err = t.input.UnreadRune(); err == nil {
t.index -= 1
}
return
}
// newTokenizer creates a new tokenizer from an input stream.
func newTokenizer(r io.Reader) *tokenizer {
input := bufio.NewReader(r)
classifier := newDefaultClassifier()
return &tokenizer{
input: *input,
classifier: classifier}
}
// scanStream scans the stream for the next token using the internal state machine.
// It will panic if it encounters a rune which it does not know how to handle.
func (t *tokenizer) scanStream() (*Token, error) {
previousState := t.state
t.state = START_STATE
token := &Token{}
var nextRune rune
var nextRuneType runeTokenClass
var err error
consumed := 0
for {
nextRune, _, err = t.ReadRune()
nextRuneType = t.classifier.ClassifyRune(nextRune)
token.RawValue += string(nextRune)
consumed += 1 // TODO find a nicer solution for this
switch {
case err == io.EOF:
nextRuneType = eofRuneClass
err = nil
case err != nil:
return nil, err
}
switch t.state {
case START_STATE: // no runes read yet
{
if nextRuneType != spaceRuneClass {
token.Index = t.index - 1
}
switch nextRuneType {
case eofRuneClass:
switch {
case t.index == 0: // tonkenizer contains an empty string
token.removeLastRaw()
token.Type = WORD_TOKEN
token.Index = t.index
t.index += 1
return token, nil // return an additional empty token for current cursor position
case previousState == WORDBREAK_STATE, consumed > 1: // consumed is greater than 1 when when there were spaceRunes before
token.removeLastRaw()
token.Type = WORD_TOKEN
token.Index = t.index
return token, nil // return an additional empty token for current cursor position
default:
return nil, io.EOF
}
case spaceRuneClass:
token.removeLastRaw()
case escapingQuoteRuneClass:
token.Type = WORD_TOKEN
t.state = QUOTING_ESCAPING_STATE
token.WordbreakIndex = len(token.Value)
case nonEscapingQuoteRuneClass:
token.Type = WORD_TOKEN
t.state = QUOTING_STATE
token.WordbreakIndex = len(token.Value)
case escapeRuneClass:
token.Type = WORD_TOKEN
t.state = ESCAPING_STATE
case commentRuneClass:
token.Type = COMMENT_TOKEN
t.state = COMMENT_STATE
case wordbreakRuneClass:
token.Type = WORDBREAK_TOKEN
token.add(nextRune)
t.state = WORDBREAK_STATE
default:
token.Type = WORD_TOKEN
token.add(nextRune)
t.state = IN_WORD_STATE
}
}
case WORDBREAK_STATE:
switch nextRuneType {
case wordbreakRuneClass:
token.add(nextRune)
default:
token.removeLastRaw()
t.UnreadRune()
return token, err
}
case IN_WORD_STATE: // in a regular word
switch nextRuneType {
case wordbreakRuneClass:
token.removeLastRaw()
t.UnreadRune()
return token, err
case eofRuneClass, spaceRuneClass:
token.removeLastRaw()
t.UnreadRune()
return token, err
case escapingQuoteRuneClass:
t.state = QUOTING_ESCAPING_STATE
token.WordbreakIndex = len(token.Value)
case nonEscapingQuoteRuneClass:
t.state = QUOTING_STATE
token.WordbreakIndex = len(token.Value)
case escapeRuneClass:
t.state = ESCAPING_STATE
default:
token.add(nextRune)
}
case ESCAPING_STATE: // the rune after an escape character
switch nextRuneType {
case eofRuneClass: // EOF found after escape character
token.removeLastRaw()
return token, err
default:
t.state = IN_WORD_STATE
token.add(nextRune)
}
case ESCAPING_QUOTED_STATE: // the next rune after an escape character, in double quotes
switch nextRuneType {
case eofRuneClass: // EOF found after escape character
token.removeLastRaw()
return token, err
default:
t.state = QUOTING_ESCAPING_STATE
token.add(nextRune)
}
case QUOTING_ESCAPING_STATE: // in escaping double quotes
switch nextRuneType {
case eofRuneClass: // EOF found when expecting closing quote
token.removeLastRaw()
return token, err
case escapingQuoteRuneClass:
t.state = IN_WORD_STATE
case escapeRuneClass:
t.state = ESCAPING_QUOTED_STATE
default:
token.add(nextRune)
}
case QUOTING_STATE: // in non-escaping single quotes
switch nextRuneType {
case eofRuneClass: // EOF found when expecting closing quote
token.removeLastRaw()
return token, err
case nonEscapingQuoteRuneClass:
t.state = IN_WORD_STATE
default:
token.add(nextRune)
}
case COMMENT_STATE: // in a comment
switch nextRuneType {
case eofRuneClass:
return token, err
case spaceRuneClass:
if nextRune == '\n' {
token.removeLastRaw()
t.state = START_STATE
return token, err
} else {
token.add(nextRune)
}
default:
token.add(nextRune)
}
default:
return nil, fmt.Errorf("unexpected state: %v", t.state)
}
}
}
// Next returns the next token in the stream.
func (t *tokenizer) Next() (*Token, error) {
token, err := t.scanStream()
if err == nil {
token.State = t.state // TODO should be done in scanStream
token.WordbreakType = wordbreakType(*token)
}
return token, err
}
// Split partitions of a string into tokens.
func Split(s string) (TokenSlice, error) {
l := newLexer(strings.NewReader(s))
tokens := make(TokenSlice, 0)
for {
token, err := l.Next()
if err != nil {
if err == io.EOF {
return tokens, nil
}
return nil, err
}
tokens = append(tokens, *token)
}
}
// Join concatenates words to create a single string.
// It quotes and escapes where appropriate.
// TODO experimental
func Join(s []string) string {
replacer := strings.NewReplacer(
"$", "\\$",
"`", "\\`",
)
formatted := make([]string, 0, len(s))
for _, arg := range s {
switch {
case arg == "",
strings.ContainsAny(arg, `"' `+"\n\r\t"):
formatted = append(formatted, replacer.Replace(fmt.Sprintf("%#v", arg)))
default:
formatted = append(formatted, arg)
}
}
return strings.Join(formatted, " ")
}