-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtokenzier.go
166 lines (127 loc) · 3.23 KB
/
tokenzier.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
package mockingbird
import (
"regexp"
"strings"
"github.com/lazywei/mockingbird/scanner"
)
// Start state on token, ignore anything till the next newline
func singleLineCmtPtrn() string {
// Why would we need the trailing space here?
return `\s*\/\/ |\s*\-\- |\s*# |\s*% |\s*" `
}
func multiLineCmtPtrn() string {
// Start state on opening token, ignore anything until the closing
// token is reached.
return `\/\*|<!--|{-|\(\*|"""|'''`
}
func multiLineCmtPairs() map[string]string {
return map[string]string{
`/*`: `\*\/`, // C
`<!--`: `-->`, // XML
`{-`: `-}`, // Haskell
`(*`: `\*\)`, // Coq
`"""`: `"""`, // Python
`'''`: `'''`, // Python
}
}
func ExtractTokens(data string) []string {
s := scanner.NewScanner(data)
tokens := []string{}
for s.IsEos() != true {
if token, ok := s.Scan(`^#!.+`); ok {
name, ok := extractShebang(token)
if ok {
tokens = append(tokens, "SHEBANG#!"+name)
}
} else if s.IsBol() && scanOrNot(s, singleLineCmtPtrn()) {
s.SkipUntil(`\n|\z`)
} else if startToken, ok := s.Scan(multiLineCmtPtrn()); ok {
closeToken := multiLineCmtPairs()[startToken]
s.SkipUntil(closeToken)
} else if scanOrNot(s, `"`) {
if s.Peek(1) == `"` {
s.Getch()
} else {
s.ScanUntil(`[^\\]"`)
}
} else if scanOrNot(s, `'`) {
if s.Peek(1) == `'` {
s.Getch()
} else {
s.ScanUntil(`[^\\]'`)
}
} else if scanOrNot(s, `(0x)?\d(\d|\.)*`) {
// Skip number literals
} else if rtn, ok := s.Scan(`<[^\s<>][^<>]*>`); ok {
for _, tkn := range extractSgmlTokens(rtn) {
tokens = append(tokens, tkn)
}
} else if rtn, ok := s.Scan(`;|\{|\}|\(|\)|\[|\]`); ok {
// Common programming punctuation
tokens = append(tokens, rtn)
} else if rtn, ok := s.Scan(`[\w\.@#\/\*]+`); ok {
// Regular token
tokens = append(tokens, rtn)
} else if rtn, ok := s.Scan(`<<?|\+|\-|\*|\/|%|&&?|\|\|?`); ok {
// Common operators
tokens = append(tokens, rtn)
} else {
s.Getch()
}
}
return tokens
}
// This function is silly ... silly Go ...
func scanOrNot(s *scanner.Scanner, ptrn string) bool {
_, ok := s.Scan(ptrn)
return ok
}
func extractSgmlTokens(token string) []string {
s := scanner.NewScanner(token)
tokens := []string{}
for s.IsEos() != true {
if token, ok := s.Scan(`<\/?[^\s>]+`); ok {
tokens = append(tokens, token+">")
} else if token, ok := s.Scan(`\w+=`); ok {
tokens = append(tokens, token)
// Then skip over attribute value
if scanOrNot(s, `"`) {
s.SkipUntil(`[^\\]"`)
} else if scanOrNot(s, `'`) {
s.SkipUntil(`[^\\]'`)
} else {
s.SkipUntil(`\w+`)
}
} else if token, ok := s.Scan(`\w+`); ok {
tokens = append(tokens, token)
} else if scanOrNot(s, `\w+`) {
s.Terminate()
} else {
s.Getch()
}
}
return tokens
}
func extractShebang(token string) (string, bool) {
s := scanner.NewScanner(token)
path, ok := s.Scan(`^#!\s*\S+`)
if !ok {
return "", false
}
paths := strings.Split(path, `/`)
if len(paths) == 0 {
return "", false
}
name := paths[len(paths)-1]
if name == `env` {
s.Scan(`\s+`)
s.Scan(`.*=[^\s]+\s+`)
name, ok = s.Scan(`\S+`)
}
if ok {
name = regexp.MustCompile(`[^\d]+`).FindString(name)
return name, true
} else {
return "", false
}
}