forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_lexer.go
89 lines (73 loc) · 1.29 KB
/
func_lexer.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
package methodfunc
import (
"unicode"
)
const (
tokenBy = "By"
tokenWildcard = "Wildcard" // should be followed by "By",
)
// word lexer, not characters.
type lexer struct {
words []string
cur int
}
func newLexer(s string) *lexer {
l := new(lexer)
l.reset(s)
return l
}
func (l *lexer) reset(trailing string) {
l.cur = -1
var words []string
if trailing != "" {
end := len(trailing)
start := -1
for i, n := 0, end; i < n; i++ {
c := rune(trailing[i])
if unicode.IsUpper(c) {
// it doesn't count the last uppercase
if start != -1 {
end = i
words = append(words, trailing[start:end])
}
start = i
continue
}
end = i + 1
}
if end > 0 && len(trailing) >= end {
words = append(words, trailing[start:end])
}
}
l.words = words
}
func (l *lexer) next() (w string) {
cur := l.cur + 1
if w = l.peek(cur); w != "" {
l.cur++
}
return
}
func (l *lexer) skip() {
if cur := l.cur + 1; cur < len(l.words) {
l.cur = cur
} else {
l.cur = len(l.words) - 1
}
}
func (l *lexer) peek(idx int) string {
if idx < len(l.words) {
return l.words[idx]
}
return ""
}
func (l *lexer) peekNext() (w string) {
return l.peek(l.cur + 1)
}
func (l *lexer) peekPrev() (w string) {
if l.cur > 0 {
cur := l.cur - 1
w = l.words[cur]
}
return w
}