-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.go
66 lines (57 loc) · 1.27 KB
/
rule.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
package lr0
import (
"github.com/pkg/errors"
)
func newRule(s Symbol, main bool, d nonTerminalDefinition, l NamedHiddenRegistry) *rule {
defer func() {
e := recover()
if e == nil {
return
}
err, ok := e.(error)
if !ok || !errors.Is(err, ErrDefine) {
panic(e)
}
panic(errors.Wrapf(err, "rule for %s", dumpSymbol(s)))
}()
hidden := make(map[int]struct{})
for i, id := range d.items {
if l.IsHidden(id) {
hidden[i] = struct{}{}
}
}
return &rule{
subject: s.Id(),
eof: main,
definition: d.items,
calc: newCalcFunc(d.calcHandler, len(d.items)-len(hidden)),
hidden: hidden,
nameReg: l,
}
}
type rule struct {
subject Id
eof bool
definition []Id
calc calcFunc
hidden map[int]struct{}
nameReg SymbolRegistry
}
func (r *rule) Subject() Id { return r.subject }
func (r *rule) HasEOF() bool { return r.eof }
func (r *rule) Definition() []Id { return r.definition }
func (r *rule) String() string {
s := dumpId(r.subject, r.nameReg) + " :"
for _, id := range r.definition {
s += " " + dumpId(id, r.nameReg)
}
if r.eof {
s += " $"
}
return s
}
func (r *rule) Value(v []any) (any, error) { return r.calc(v) }
func (r *rule) IsHidden(index int) bool {
_, ok := r.hidden[index]
return ok
}