-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
191 lines (165 loc) · 4.01 KB
/
parse.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
package runic
import (
"fmt"
"slices"
)
type parser struct {
input string
tree *Node
lexer *lexer
error string
currentNode *Node
ctx int
tagDepth int
collectedTokens []token
}
func New() *parser {
return &parser{}
}
func (p *parser) Parse(input string) *Node {
p.lexer = lex(input)
p.tree = &Node{Typ: nodeRoot, Val: ""}
p.currentNode = p.tree
p.collectedTokens = []token{}
p.parseGlobal()
return p.tree
}
func (p *parser) addNewNode(typ, val string) {
newNode := &Node{
Typ: typ,
Val: val,
parent: p.currentNode,
}
p.currentNode.Children = append(p.currentNode.Children, newNode)
p.currentNode = newNode
}
func (p *parser) previousSibling() *Node {
numOfChildren := len(p.currentNode.Children)
if numOfChildren > 0 {
return p.currentNode.Children[numOfChildren-1]
}
return &Node{}
}
func (p *parser) nextToken() {
p.lexer.nextToken()
p.collectedTokens = append(p.collectedTokens, p.lexer.token)
}
func (p *parser) returnNode() {
p.currentNode = p.currentNode.parent
}
func (p *parser) isOneOf(t ...tokenType) bool {
return slices.Contains(t, p.lexer.token.Typ)
}
func (p *parser) parseGlobal() {
p.tagDepth = 0
p.nextToken()
for !p.isOneOf(typeEOF) {
switch p.lexer.token.Typ {
case typeHeading:
p.parseHeading()
case typeBulletpoint:
p.parseList(0)
default:
p.parseParagraph()
}
p.nextToken()
}
}
func (p *parser) parseParagraph() {
p.addNewNode(nodeParagraph, "")
p.parseRichText()
p.returnNode()
}
func (p *parser) parseText() {
previousSibling := p.previousSibling()
// merge consecutive text nodes into one
if previousSibling.Typ == nodeText {
previousSibling.Val += " " + p.lexer.token.Val
return
}
p.addNewNode(nodeText, p.lexer.token.Val)
p.returnNode()
}
func (p *parser) parseRichText() {
for !p.isOneOf(typeBulletpoint, typeTerminator, typeEOF) {
switch p.lexer.token.Typ {
case typeText:
p.parseText()
case typeTag:
p.parseTag()
if p.tagDepth > 0 && p.isOneOf(typeBulletpoint, typeTerminator) {
return
}
case typeClosingSquare:
if p.tagDepth > 0 {
p.tagDepth--
return
}
}
p.nextToken()
}
}
func (p *parser) parseHeading() {
switch p.lexer.token.Val {
case nodeHeadingOneValue:
p.addNewNode(nodeHeadingOne, nodeHeadingOneValue)
case nodeHeadingTwoValue:
p.addNewNode(nodeHeadingTwo, nodeHeadingTwoValue)
case nodeHeadingThreeValue:
p.addNewNode(nodeHeadingThree, nodeHeadingThreeValue)
case nodeHeadingFourValue:
p.addNewNode(nodeHeadingFour, nodeHeadingFourValue)
case nodeHeadingFiveValue:
p.addNewNode(nodeHeadingFive, nodeHeadingFiveValue)
case nodeHeadingSixValue:
p.addNewNode(nodeHeadingSix, nodeHeadingSixValue)
default:
p.addNewNode(nodeError, fmt.Sprintf("%s: %s", errInvalidHeading, p.lexer.token.Val))
}
p.nextToken()
p.parseRichText()
p.returnNode()
}
func (p *parser) parseTag() {
switch p.lexer.token.Val {
case "bold":
p.addNewNode(nodeBoldTag, "")
case "italic":
p.addNewNode(nodeItalicTag, "")
default:
p.addNewNode(nodeError, fmt.Sprintf("%s: %s", errInvalidTag, p.lexer.token.Val))
}
// skip over openSquare token
p.nextToken()
p.tagDepth++
p.parseRichText()
p.returnNode()
}
func (p *parser) parseList(currentListDepth int) {
if p.isOneOf(typeBulletpoint) && getListItemDepth(p.lexer.token) < currentListDepth {
p.returnNode()
return
}
p.addNewNode(nodeList, "")
for p.isOneOf(typeBulletpoint) {
currentListDepth = getListItemDepth(p.lexer.token)
p.nextToken()
p.parseListItem()
p.tagDepth = 0
// bulletpoint is at a lower depth, create nested list
if p.isOneOf(typeBulletpoint) && getListItemDepth(p.lexer.token) > currentListDepth {
p.parseList(getListItemDepth(p.lexer.token))
}
// bulletpoint is a higher depth, return until no longer shallower
if p.isOneOf(typeBulletpoint) && getListItemDepth(p.lexer.token) < currentListDepth {
p.returnNode()
return
}
}
p.returnNode()
}
func (p *parser) parseListItem() {
p.addNewNode(nodeListItem, "")
p.parseRichText()
p.returnNode()
}