-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphbuilder.go
More file actions
124 lines (112 loc) · 2.38 KB
/
Copy pathgraphbuilder.go
File metadata and controls
124 lines (112 loc) · 2.38 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
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
}
func newGraphBuilder() graphbuilder {
return graphbuilder{
pars: newParser(),
}
}
func (g *graphbuilder) startGeneration(grammar string) error {
g.grammar = grammar
return g.generateGraph()
}
func (g *graphbuilder) generateGraph() error {
tokens, scanErrs := extracttokens(g.grammar)
if len(scanErrs) != 0 {
all := make([]error, 0, len(scanErrs))
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.nameMap
g.pars.graph.regexhandler = g.pars.regexhandler
g.pars.parseGrammar()
// collect parse errors
all := make([]error, 0, len(g.pars.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
}