-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphbuilder.go
More file actions
126 lines (113 loc) · 2.34 KB
/
graphbuilder.go
File metadata and controls
126 lines (113 loc) · 2.34 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
package resrap
import (
"errors"
"fmt"
)
type token struct {
id uint32 //Will be generated by the parser
typ tokenType
text string //Generated by the Scanner
}
type tokenType int8
const (
character tokenType = iota //Normal words
//'...'
maybe //?
oneormore //+
anyno //*
bracks //(...)
option // |
padding //just to account for my bad indexing skills
regexrange //[...]
infinite //^
probability //<...>
identifier //Normal words
str //'...'
regex //[...]
bracopen //(
bracclose //)
colon
semicolon
)
func (t tokenType) String() string {
switch t {
case character:
return "character"
case maybe:
return "maybe"
case oneormore:
return "oneormore"
case anyno:
return "anyno"
case bracks:
return "bracks"
case option:
return "option"
case padding:
return "padding"
case regexrange:
return "regexrange"
case infinite:
return "infinite"
case probability:
return "probability"
case identifier:
return "identifier"
case str:
return "str"
case regex:
return "regex"
case bracopen:
return "bracopen"
case bracclose:
return "bracclose"
case colon:
return "colon"
case semicolon:
return "semicolon"
default:
return fmt.Sprintf("tokenType(%d)", int(t))
}
}
type graphbuilder struct {
grammar string
pars parser
tokens []token
}
func newGraphBuilder() graphbuilder {
return graphbuilder{
pars: new_parser(),
}
}
func (g *graphbuilder) start_generation(grammar string) error {
g.grammar = grammar
return g.generate_graph()
}
func (g *graphbuilder) generate_graph() error {
tokens, scanErrs := extracttokens(g.grammar)
if len(scanErrs) != 0 {
var all []error
for _, err := range scanErrs {
all = append(all, fmt.Errorf("ERROR Scanning grammar >>> %s", err.Msg))
}
return errors.Join(all...)
}
g.pars.tokens = tokens
g.pars.graph.charmap = g.pars.charmap
g.pars.graph.namemap = g.pars.name_map
g.pars.graph.regexhandler = g.pars.regexhandler
g.pars.parse_grammar()
var all []error
// collect parse errors
for _, err := range g.pars.errors {
all = append(all, fmt.Errorf("ERROR Parsing >>> %s", err.Error()))
}
// collect validation errors
for _, err := range g.pars.ValidateGraph() {
all = append(all, fmt.Errorf("ERROR Validating >>> %s", err.Error()))
}
if len(all) > 0 {
return errors.Join(all...)
}
return nil
}