-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
222 lines (211 loc) · 6.37 KB
/
parser.go
File metadata and controls
222 lines (211 loc) · 6.37 KB
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
package resrap
import (
"fmt"
"math"
"slices"
"strconv"
)
type parser struct {
func_ptr uint32
print_ptr uint32
name_map map[string]uint32 //Maps func names to their ids
rev_name_map map[uint32]string //Help in debugging
def_check map[uint32]bool //To check if a function exists
charmap map[uint32]string //To store the print values corresponding to ids
inter_rep map[uint32][]token //Intermediate Representation
tokens []token
errors []error
index int
graph syntaxGraph
regexhandler regexer
}
func new_parser() parser {
return parser{
print_ptr: math.MaxInt32, // grows downward
func_ptr: 1000, // IDs below 1000 reserved for core graph nodes
// allocate maps to avoid nil panics
name_map: make(map[string]uint32),
def_check: make(map[uint32]bool),
charmap: make(map[uint32]string),
inter_rep: make(map[uint32][]token),
rev_name_map: make(map[uint32]string),
tokens: []token{},
errors: []error{},
graph: newSyntaxGraph(),
regexhandler: newRegexer(),
}
}
func (i *parser) get_print_ptr() uint32 {
i.print_ptr--
return i.print_ptr
}
func (i *parser) get_func_ptr() uint32 {
i.func_ptr++
return i.func_ptr
}
func (i *parser) curr() token {
return i.tokens[i.index]
}
func (i *parser) match(word tokenType, expec []tokenType) bool {
return slices.Contains(expec, word)
}
func (i *parser) expect(expected []tokenType, errmsg string) bool {
if !i.match(i.curr().typ, expected) {
i.errors = append(i.errors, fmt.Errorf("%s", errmsg))
i.index++
return true
}
i.index++
return false
}
func (i *parser) get_index(name string) uint32 {
value, ok := i.name_map[name]
if ok {
return value
}
value = i.get_func_ptr()
i.def_check[value] = false //tb set back to true in the subject, else remain false
i.name_map[name] = value
i.rev_name_map[value] = name
return value
}
func (i *parser) parse_grammar() {
for i.index < len(i.tokens) {
i.parse_subject()
if len(i.errors) > 0 {
return //Crash on errors for now
}
}
}
func (i *parser) parse_subject() {
subject := i.curr()
if i.expect([]tokenType{identifier}, "Expected Subject at start of statement") {
return
}
if i.expect([]tokenType{colon}, "Expected Colon after Subject") {
return
}
id := i.get_index(subject.text)
if i.def_check[id] { //If map is already set to true
i.errors = append(i.errors, fmt.Errorf("Multiple definitions for %s", subject.text))
}
i.def_check[id] = true
startnode := i.graph.GetNode(uint32(start), start)
startnode.AddEdgeNext(&i.graph, i.graph.GetNode(id, header), 1)
//Send here only if current is col else crash code
if i.match(i.tokens[i.index-1].typ, []tokenType{colon}) {
i.parse_rules(id, false)
} else {
return
}
}
func (i *parser) parse_rules(root uint32, isDeep bool) (*syntaxNode, *syntaxNode) {
rootnode := i.graph.GetNode(root, idk)
bufferNode := rootnode
endNode := i.graph.GetNode(uint32(end), end)
var startBuffer *syntaxNode
if isDeep { //Means called from a backet so a pseudo end branch
endNode = i.graph.GetNode(i.get_func_ptr(), end)
}
for {
switch i.curr().typ {
case identifier:
//Means its a reference to a different Subject(presumably)
pointerid := i.get_index(i.tokens[i.index].text)
node := i.graph.GetNode(i.get_func_ptr(), pointer)
node.pointer = pointerid
bufferNode.AddEdgeNext(&i.graph, node, i.get_probability())
jumpNode := i.graph.GetNode(i.get_func_ptr(), jump)
node.AddEdgeNext(&i.graph, jumpNode, 1)
startBuffer = bufferNode
bufferNode = jumpNode
//Basically just add the word and next to it its jump node
// So when generating, the control will pass to the node at the location and save the exit in a stack
// Then when it reached its local collapse node, then the control will automatically come back to default
case character, regex:
index := i.get_print_ptr()
i.charmap[index] = i.tokens[i.index].text
var leafnode *syntaxNode
if i.tokens[i.index].typ == character {
leafnode = i.graph.GetNode(index, ch)
} else {
leafnode = i.graph.GetNode(index, rx)
i.regexhandler.CacheRegex(i.curr().text)
}
bufferNode.AddEdgeNext(&i.graph, leafnode, i.get_probability())
jumpNode := i.graph.GetNode(i.get_func_ptr(), jump)
leafnode.AddEdgeNext(&i.graph, jumpNode, 1)
startBuffer = bufferNode
bufferNode = jumpNode
case colon:
//Colon is not allowed here
i.errors = append(i.errors, fmt.Errorf("Missing Semicolon"))
return nil, nil
case maybe:
startBuffer.AddEdgeNext(&i.graph, bufferNode, 1-i.get_probability()) //An option to skip to the end
case oneormore:
bufferNode.AddEdgeNext(&i.graph, startBuffer, i.get_probability()) //An option to go to the start
case anyno:
startBuffer.AddEdgeNext(&i.graph, bufferNode, 1-i.get_probability()) //Well both of them combined
bufferNode.AddEdgeNext(&i.graph, startBuffer, i.get_probability())
case option:
//in Case of an option, no need to really do anything, just set the buffer settings back to the parent
bufferNode.AddEdgeNext(&i.graph, endNode, i.get_probability())
bufferNode = rootnode
startBuffer = nil
case padding:
bufferNode.AddEdgeNext(&i.graph, endNode, 1)
if isDeep {
i.errors = append(i.errors, fmt.Errorf("Stray '('"))
}
i.index++
return nil, nil //End of this statement
case bracopen:
i.index++
startBuffer, bufferNode = i.parse_rules(bufferNode.id, true)
case bracclose:
if isDeep {
bufferNode.AddEdgeNext(&i.graph, endNode, 1)
return rootnode, endNode
}
i.errors = append(i.errors, fmt.Errorf("Stray ')' found"))
case infinite:
//Now at the end it will loop back to this case
endNode.AddEdgeNext(&i.graph, startBuffer, 1)
default:
continue
}
i.index++
}
}
func (i *parser) get_probability() float32 {
i.index++
if i.tokens[i.index].typ == probability {
num := i.tokens[i.index].text
numf, err := strconv.ParseFloat(num, 32)
if err != nil {
i.index--
i.errors = append(i.errors, err)
return 0
}
if numf < 0 {
i.errors = append(i.errors, fmt.Errorf("Negative Probability Found"))
return 0
}
return float32(numf)
}
i.index-- //Reverting
return 0.5
}
func (p *parser) ValidateGraph() []error {
if len(p.errors) > 0 {
return nil
}
var errors []error
for key, val := range p.def_check {
if !val {
errors = append(errors, fmt.Errorf("Definition of '%s' not found", p.rev_name_map[key]))
}
}
return errors
}