-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalt.go
More file actions
53 lines (48 loc) · 1.01 KB
/
Copy pathalt.go
File metadata and controls
53 lines (48 loc) · 1.01 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
package parse
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"github.com/ohait/forego/ctx/log"
)
type Alts struct {
Grammar *Grammar
Name string
prods []*Prod
retType reflect.Type
}
// Add a production to the given list
func (this *Alts) Add(directives string, fn any) *Prod {
if this.Grammar.Log != nil {
this.Grammar.Log("adding %s: %s", this.Name, directives)
}
if this.Grammar.alts == nil {
this.Grammar.alts = map[string]*Alts{}
}
this.Grammar.Stats.Productions++
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
p := &Prod{
g: this.Grammar,
Name: this.Name,
Directive: directives,
src: fmt.Sprintf("%s:%d", file, line),
}
_, err := p.build("")
if err != nil {
log.Errorf(nil, "can't create prod %q: %v", this.Name, err)
panic(err)
}
this.append(p)
if fn == nil {
return p
}
return p.Return(fn)
}
func (this *Alts) append(p *Prod) {
this.prods = append(this.prods, p)
if len(this.prods) == 1 {
this.Grammar.Stats.Alternations++
}
}