-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.go
More file actions
90 lines (78 loc) · 1.85 KB
/
Copy pathlexer.go
File metadata and controls
90 lines (78 loc) · 1.85 KB
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
package strut
import "strings"
// Lexer tokenises the argument text of a prefix command. Tokens are
// whitespace separated, with double quotes grouping a token that contains
// spaces. A backslash escapes the next character inside quotes.
type Lexer struct {
src string
pos int
}
// NewLexer returns a Lexer over the argument text following a command name.
func NewLexer(s string) *Lexer { return &Lexer{src: s} }
// Next consumes and returns the next token.
func (l *Lexer) Next() (string, bool) {
l.skipSpace()
if l.pos >= len(l.src) {
return "", false
}
if l.src[l.pos] == '"' {
return l.quoted()
}
start := l.pos
for l.pos < len(l.src) && !isSpace(l.src[l.pos]) {
l.pos++
}
return l.src[start:l.pos], true
}
// Peek returns the next token without consuming it.
func (l *Lexer) Peek() (string, bool) {
save := l.pos
tok, ok := l.Next()
l.pos = save
return tok, ok
}
// Rest consumes and returns everything left, unquoted and untrimmed. Used by
// options tagged rest.
func (l *Lexer) Rest() string {
l.skipSpace()
s := l.src[l.pos:]
l.pos = len(l.src)
return s
}
// Empty reports whether any tokens remain.
func (l *Lexer) Empty() bool {
l.skipSpace()
return l.pos >= len(l.src)
}
func (l *Lexer) quoted() (string, bool) {
l.pos++ // opening quote
var b strings.Builder
for l.pos < len(l.src) {
c := l.src[l.pos]
switch c {
case '\\':
// An escape at the very end is a literal backslash.
if l.pos+1 < len(l.src) {
l.pos++
b.WriteByte(l.src[l.pos])
l.pos++
continue
}
case '"':
l.pos++
return b.String(), true
}
b.WriteByte(c)
l.pos++
}
// Unterminated quote: treat the remainder as the token.
return b.String(), true
}
func (l *Lexer) skipSpace() {
for l.pos < len(l.src) && isSpace(l.src[l.pos]) {
l.pos++
}
}
func isSpace(c byte) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}