-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrules.go
40 lines (33 loc) · 916 Bytes
/
rules.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
package srgs
import (
"fmt"
)
type Rules map[string]Expansion
type RuleRefs map[string][]*RuleRef
type RuleRef struct {
rule Expansion
ruleId string
}
func (r *RuleRef) Match(str string, mode MatchMode) {
r.rule.Match(str, mode)
}
func (r *RuleRef) Next() (string, error) {
return r.rule.Next()
}
func (r *RuleRef) Copy(rr RuleRefs) Expansion {
ref := new(RuleRef)
ref.ruleId = r.ruleId
if r.rule != nil {
ref.rule = r.rule.Copy(rr)
}
rr[ref.ruleId] = append(rr[ref.ruleId], ref)
return ref
}
func (r *RuleRef) Scan(p Processor) {
p.AppendTag("scopes.push({'rules':{}, 'out':undefined, 'raw':undefined});")
r.rule.Scan(p)
p.AppendTag(fmt.Sprintf(`var last = scopes.pop();
scopes[scopes.length-1]['rules']['%s'] = {'out': last.out, 'raw': last.raw};
scopes[scopes.length-1]['raw'] = scopes[scopes.length-1]['raw'] ? scopes[scopes.length-1]['raw'] + ' ' + last.raw : last.raw;
`, r.ruleId))
}