-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.go
264 lines (212 loc) · 4.35 KB
/
tokens.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
package main
const (
ItemError ItemType = iota
ItemMetaKey
ItemMetaValue
ItemOverview
// keywords: headers
ItemTitleLevel1
ItemTitleLevel2
ItemTitleLevel3
ItemTitleLevel4
ItemTitleLevel5
ItemTitleLevel6
// Data structures section
ItemDataStructures // Section Title
ItemModel
ItemPropertyName
ItemPropertyType
ItemPropertyArrayType
ItemPropertyDesc
)
// LexMetaKey scans the Meta Section for the key in a key-value pair.
func LexMetaKey(l *Lexer) StateFn {
if l.Peek() == '#' {
return LexSectionTitle
}
l.AcceptUntil(":")
if l.Peek() == ':' {
l.Emit(ItemMetaKey)
l.Accept(":")
return LexMetaValue
}
l.Errorf("not valid meta key.")
return nil
}
// LexMetaValue scans the Meta Section for the value in a key-value pair.
func LexMetaValue(l *Lexer) StateFn {
// Ignore leading WS
l.AcceptRun(" ")
l.Ignore()
// Everything after initial WS to EOL is the value.
l.AcceptUntil("\n\r")
l.Emit(ItemMetaValue)
// Collapse WS to start MetaKey.
l.AcceptRun("\r\n\t ")
l.Ignore()
return LexMetaKey
}
// LexSectionTitle lexes the section title.
func LexSectionTitle(l *Lexer) StateFn {
start := l.Pos()
l.AcceptRun("#")
diff := l.Pos() - start
// consume WS
l.AcceptRun(" ")
l.Ignore()
// consume to EOL
l.AcceptUntil("\r\n")
if l.Peek() == EOF {
return nil
}
switch {
case l.HasPrefix("Data Structures"):
l.Emit(ItemDataStructures)
l.AcceptClasses(Whitespace)
l.Ignore()
return LexModel
}
titles := []ItemType{
ItemTitleLevel1,
ItemTitleLevel2,
ItemTitleLevel3,
ItemTitleLevel4,
ItemTitleLevel5,
ItemTitleLevel6,
}
d := titles[diff-1]
l.Emit(d)
l.AcceptClasses(Whitespace)
l.Ignore()
r := l.Peek()
if r == '#' {
return LexSectionTitle
}
return LexOverview
}
// LexOverview scans the for the Overview body.
func LexOverview(l *Lexer) StateFn {
for {
// consume to EOL
l.AcceptUntil("\n")
// consume WS
l.AcceptRun("\r\n ")
// peek if next line is a section title
r := l.Peek()
if r == '#' || r == EOF {
break
}
}
l.Emit(ItemOverview)
return LexSectionTitle
}
// LexModel scans for a models name.
func LexModel(l *Lexer) StateFn {
l.AcceptRun("#")
// consume WS
l.AcceptRun(" ")
l.Ignore()
// consume to EOL, WS or right-parenthesis.
l.AcceptUntil("\r\n (")
l.Emit(ItemModel)
l.AcceptClasses(Whitespace)
l.Ignore()
return LexPropertyName
}
// LexPropertyName scans for a properties name.
func LexPropertyName(l *Lexer) StateFn {
// consume + and WS
l.Accept("+")
l.AcceptRun("\t ")
l.Ignore()
// capture everything until WS or :
l.AcceptClasses(Letter, Number)
r := l.Peek()
if !(r == ':' || r == ' ') {
l.Errorf("unexpected character `%v`:0x%v for property name", string(r), r)
return nil
}
l.Emit(ItemPropertyName)
if l.Accept(":") {
return LexPropertyExample
}
// consume trailing whitespace
l.AcceptRun(" \t")
l.Ignore()
return LexPropertyType
}
// LexPropertyExample scans for a property example.
func LexPropertyExample(l *Lexer) StateFn {
l.AcceptUntil("(")
l.Ignore()
return LexPropertyType
}
// LexPropertyType scans for a type.
func LexPropertyType(l *Lexer) StateFn {
// consume and ignore (
l.Accept("(")
l.Ignore()
// capture letters
l.AcceptClasses(Letter)
r := l.Peek()
if r == ',' || r == ')' {
l.Emit(ItemPropertyType)
} else if l.Accept("[") {
l.Ignore()
l.AcceptClasses(Letter)
if l.Peek() == ']' {
l.Emit(ItemPropertyArrayType)
// consume ]
l.Next()
} else {
l.Errorf("missing closing brace in array type")
return nil
}
} else {
l.Errorf("unexpected character 0x%v for property type", l.Peek())
return nil
}
// consume boundary and ignore WS
l.Accept(",)")
l.AcceptClasses(Whitespace)
l.Ignore()
r = l.Peek()
if r == '#' {
return LexModel
} else if r == '-' {
return LexPropertyDesc
} else if r == EOF {
return nil
}
return LexPropertyName
}
func LexPropertyDesc(l *Lexer) StateFn {
l.AcceptUntil("\r\n")
l.Emit(ItemPropertyDesc)
l.AcceptClasses(Whitespace)
l.Ignore()
r := l.Peek()
if r == '#' {
return LexModel
} else {
return LexPropertyName
}
}
func Whitespace(ch rune) bool {
if ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' {
return true
}
return false
}
func Letter(ch rune) bool {
if ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' {
return true
}
return false
}
func Number(ch rune) bool {
if ch >= '0' && ch <= '9' {
return true
}
return false
}