-
Notifications
You must be signed in to change notification settings - Fork 15
/
cfgrammar_special.go
89 lines (84 loc) · 1.65 KB
/
cfgrammar_special.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fmr
import "fmt"
func (p *parser) special() (*Term, error) {
if err := p.eat('('); err != nil {
return nil, err
}
p.ws()
name, err := p.text()
if err != nil {
return nil, err
}
p.ws()
switch name {
case "any":
return p.any()
case "list":
return p.list()
default:
return nil, fmt.Errorf(
"%s: special rule:(%s) not supported", p.posInfo(), name)
}
}
func (p *parser) specialMeta() (map[string]int, error) {
p.ws()
var err error
var meta map[string]int
if p.peek() == '{' {
// contains range
meta = make(map[string]int)
p.eat('{')
p.ws()
if meta["min"], err = p.getInt(); err != nil {
return nil, err
}
p.ws()
if err = p.eat(','); err != nil {
return nil, err
}
p.ws()
if meta["max"], err = p.getInt(); err != nil {
return nil, err
}
if meta["max"] < meta["min"] {
return nil, fmt.Errorf("%s : max:%d less than min:%d",
p.posInfo(), meta["max"], meta["min"])
}
p.ws()
if err = p.eat('}'); err != nil {
return nil, err
}
}
p.ws()
return meta, nil
}
func (p *parser) list() (*Term, error) {
name, err := p.nonterminal()
if err != nil {
return nil, err
}
meta, err := p.specialMeta()
if err != nil {
return nil, err
}
if err = p.eat(')'); err != nil {
return nil, err
}
if len(meta) > 0 {
return &Term{Type: List, Value: name, Meta: meta}, nil
}
return &Term{Type: List, Value: name}, nil
}
func (p *parser) any() (*Term, error) {
meta, err := p.specialMeta()
if err != nil {
return nil, err
}
if err = p.eat(')'); err != nil {
return nil, err
}
if len(meta) > 0 {
return &Term{Value: "any", Type: Any, Meta: meta}, nil
}
return &Term{Value: "any", Type: Any}, nil
}