-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.go
291 lines (243 loc) · 6.56 KB
/
parser.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
package mm
import (
"io"
"strings"
)
type Parser struct {
s *Scanner
// Temporary storage of values.
tempSlice []string
tempString string
tempBulletSlice []string
tempHeader string
Formatter []string
Stringlit []string
}
func NewParser(r io.Reader) *Parser {
return &Parser{s: NewScanner(r)}
}
type stateFn func(*Parser) stateFn
// Start state machine.
func (p *Parser) Parse() {
p.run(p.stateParse())
}
// State machine: Chain states until EOF.
func (p *Parser) run(state stateFn) stateFn {
newstate := state
if newstate != nil {
p.run(newstate)
}
return nil
}
//
// State functions.
//
func (p *Parser) stateParse() stateFn {
// t stands for token, and l stands for string literal returned by Scan() functions.
t, l := p.s.Scan()
switch t {
case HEX:
p.tempHeader = l
return p.stateHeader()
case SINGLESTAR:
return p.stateSingleStar()
case DOUBLESTAR:
return p.stateDoubleStar()
case STRINGLIT:
p.tempString = l
return p.statePara()
case EOF:
return nil
case NEWLINE:
return p.stateParse()
}
return nil
}
// Accept # Header 1. If #Header 1, return as string in paragraph.
func (p *Parser) stateHeader() stateFn {
t1, l1 := p.s.Scan()
switch t1 {
// Check WS after Hex token.
case WS:
_, l2 := p.s.Scan()
if len(p.tempHeader) < 7 {
p.Formatter = append(p.Formatter, p.tempHeader)
} else {
// Presume h6 if more than 6#.
p.Formatter = append(p.Formatter, "######")
}
p.Stringlit = append(p.Stringlit, l2)
// Always reset temp storage after appending to Formatter or Stringlit.
p.tempHeader = ""
return p.stateParse()
}
// If not followed by WS, store string in tempHeader and send to paragraph state.
// FIXME: not redirected here when ###noWS after **string*** line. Returns WS as next token instead.
p.tempSlice = append(p.tempSlice, p.tempHeader+l1)
return p.statePara()
}
// stateDoubleStar() signal either bold text or plain string.
func (p *Parser) stateDoubleStar() stateFn {
t1, l1 := p.s.Scan()
switch t1 {
// If WS, not bold text.
// >> ** not bold text.
// Send to statePara().
case WS:
_, l2 := p.s.Scan()
p.tempSlice = append(p.tempSlice, "** "+l2)
return p.statePara()
case STRINGLIT:
// Check if stringliteral is followed by **, in which case it is bold text.
// >> **bold text**
if ta, _ := p.s.Scan(); ta == DOUBLESTAR {
p.tempSlice = append(p.tempSlice, "<b>"+l1+"</b>")
} else {
// stringlit not followed by **
// >> **stringlit
p.tempSlice = append(p.tempSlice, "** "+l1)
}
// >> **\n
case NEWLINE:
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, "**")
return p.stateParse()
// >> **#
default:
p.tempSlice = append(p.tempSlice, "**"+l1)
}
return p.statePara()
}
// This state can mean bullets, italics, or string.
func (p *Parser) stateSingleStar() stateFn {
t1, l1 := p.s.Scan()
switch t1 {
// If followed by WS, then not followed by NEWLINE, that is a bullet string.
// >> * bullet 1
case WS:
t2, l2 := p.s.Scan()
if t2 != NEWLINE {
bulletstr := p.consumeAllToks()
p.tempSlice = append(p.tempSlice, "<li>"+l2+bulletstr+"</li>\n")
// Check if next line is *. If it is, return stateSingleStar() and find out context again.
t3, l3 := p.s.Scan()
switch t3 {
case SINGLESTAR:
return p.stateSingleStar()
case DOUBLESTAR:
p.Formatter = append(p.Formatter, "bullet")
p.Stringlit = append(p.Stringlit, strings.Join(p.tempSlice, ""))
p.tempSlice = []string{}
return p.stateDoubleStar()
case HEX:
p.Formatter = append(p.Formatter, "bullet")
p.Stringlit = append(p.Stringlit, strings.Join(p.tempSlice, ""))
p.tempSlice = []string{}
p.tempHeader = l3
return p.stateHeader()
case STRINGLIT:
p.Formatter = append(p.Formatter, "bullet")
p.Stringlit = append(p.Stringlit, strings.Join(p.tempSlice, ""))
p.tempSlice = []string{}
p.tempString = l3
return p.statePara()
}
} else {
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, "* ")
return p.stateParse()
}
case STRINGLIT:
if ta, _ := p.s.Scan(); ta == SINGLESTAR {
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, "<i>"+l1+"</i>")
return p.stateParse()
}
joinSlice := strings.Join(p.tempSlice, "")
if joinSlice != "" {
p.Formatter = append(p.Formatter, "bullet")
p.Stringlit = append(p.Stringlit, joinSlice)
p.tempSlice = []string{}
}
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, "*"+l1)
return p.stateParse()
case NEWLINE:
p.tempSlice = append(p.tempSlice, p.tempString)
}
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, strings.Join(p.tempSlice, ""))
p.tempSlice = []string{}
return p.stateParse()
}
func (p *Parser) statePara() stateFn {
t1, l1 := p.s.Scan()
p.tempSlice = append(p.tempSlice, p.tempString)
p.tempString = ""
switch t1 {
case WS:
p.tempSlice = append(p.tempSlice, " ")
return p.statePara()
case SINGLESTAR:
typ, inlineString := p.checkIfItalics(t1, l1)
if typ == "italics" {
p.tempSlice = append(p.tempSlice, "<i>"+inlineString+"</i>")
return p.statePara()
} else if typ == "new line" {
p.tempSlice = append(p.tempSlice, l1+inlineString)
} else {
p.tempSlice = append(p.tempSlice, l1+inlineString)
return p.statePara()
}
case DOUBLESTAR:
typ, inlineString := p.checkIfBold(t1, l1)
if typ == "bold" {
p.tempSlice = append(p.tempSlice, "<b>"+inlineString+"</b>")
return p.statePara()
} else {
p.tempSlice = append(p.tempSlice, l1+inlineString)
}
case STRINGLIT, HEX:
p.tempSlice = append(p.tempSlice, l1)
return p.statePara()
}
p.Formatter = append(p.Formatter, "para")
p.Stringlit = append(p.Stringlit, strings.Join(p.tempSlice, ""))
p.tempSlice = []string{}
return p.stateParse()
}
//
// Helper functions.
//
// Consume all tokens until NEWLINE. Returns bullet string.
func (p *Parser) consumeAllToks() string {
ta, la := p.s.Scan()
if ta != NEWLINE {
p.tempBulletSlice = append(p.tempBulletSlice, la)
p.consumeAllToks()
}
return strings.Join(p.tempBulletSlice, "")
}
func (p *Parser) checkIfItalics(t ItemType, l string) (string, string) {
t2, l2 := p.s.Scan()
t3, _ := p.s.Scan()
if t3 != SINGLESTAR || t2 == WS {
if t3 == NEWLINE {
return "new line", l2
}
return "not italics", l2
}
if t2 == NEWLINE || t3 == NEWLINE {
return "new line", ""
}
return "italics", l2
}
func (p *Parser) checkIfBold(t1 ItemType, l string) (string, string) {
_, l2 := p.s.Scan()
t3, _ := p.s.Scan()
switch t3 {
case DOUBLESTAR:
return "bold", l2
}
return "not bold", l2
}