2017-07-15 21:49:08 +02:00
|
|
|
package treerack
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
type sequenceDefinition struct {
|
|
|
|
name string
|
2017-07-15 21:49:08 +02:00
|
|
|
id int
|
2017-06-25 17:51:08 +02:00
|
|
|
commit CommitType
|
2017-06-25 23:38:32 +02:00
|
|
|
items []SequenceItem
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type sequenceParser struct {
|
2017-06-26 00:04:16 +02:00
|
|
|
name string
|
2017-07-15 21:49:08 +02:00
|
|
|
id int
|
2017-06-26 00:04:16 +02:00
|
|
|
commit CommitType
|
|
|
|
items []parser
|
|
|
|
ranges [][]int
|
|
|
|
includedBy []parser
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
func newSequence(name string, ct CommitType, items []SequenceItem) *sequenceDefinition {
|
2017-06-25 17:51:08 +02:00
|
|
|
return &sequenceDefinition{
|
|
|
|
name: name,
|
|
|
|
commit: ct,
|
|
|
|
items: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sequenceDefinition) nodeName() string { return d.name }
|
2017-07-15 21:49:08 +02:00
|
|
|
func (d *sequenceDefinition) nodeID() int { return d.id }
|
|
|
|
func (d *sequenceDefinition) setID(id int) { d.id = id }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
func (d *sequenceDefinition) parser(r *registry, parsers []string) (parser, error) {
|
|
|
|
if stringsContainDeprecated(parsers, d.name) {
|
2017-06-25 23:38:32 +02:00
|
|
|
panic(cannotIncludeParsers(d.name))
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := r.parser(d.name)
|
|
|
|
if ok {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := &sequenceParser{
|
|
|
|
name: d.name,
|
2017-07-15 21:49:08 +02:00
|
|
|
id: d.id,
|
2017-06-25 17:51:08 +02:00
|
|
|
commit: d.commit,
|
|
|
|
}
|
|
|
|
|
|
|
|
r.setParser(sp)
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
var (
|
|
|
|
items []parser
|
|
|
|
ranges [][]int
|
|
|
|
)
|
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
parsers = append(parsers, d.name)
|
2017-06-25 23:38:32 +02:00
|
|
|
for _, item := range d.items {
|
|
|
|
if item.Min == 0 && item.Max == 0 {
|
|
|
|
item.Min, item.Max = 1, 1
|
|
|
|
} else if item.Max == 0 {
|
|
|
|
item.Max = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
pi, ok := r.parser(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
if ok {
|
2017-06-25 23:38:32 +02:00
|
|
|
items = append(items, pi)
|
|
|
|
ranges = append(ranges, []int{item.Min, item.Max})
|
2017-06-25 17:51:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
itemDefinition, ok := r.definition(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
if !ok {
|
2017-06-25 23:38:32 +02:00
|
|
|
return nil, parserNotFound(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
pi, err := itemDefinition.parser(r, parsers)
|
2017-06-25 17:51:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
items = append(items, pi)
|
|
|
|
ranges = append(ranges, []int{item.Min, item.Max})
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// for single items, acts like a choice
|
2017-06-25 23:38:32 +02:00
|
|
|
if len(items) == 1 && ranges[0][0] == 1 && ranges[0][1] == 1 {
|
2017-07-15 21:49:08 +02:00
|
|
|
items[0].setIncludedBy(sp, parsers)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sp.items = items
|
2017-06-25 23:38:32 +02:00
|
|
|
sp.ranges = ranges
|
2017-06-25 17:51:08 +02:00
|
|
|
return sp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sequenceDefinition) commitType() CommitType {
|
|
|
|
return d.commit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sequenceParser) nodeName() string { return p.name }
|
2017-07-15 21:49:08 +02:00
|
|
|
func (p *sequenceParser) nodeID() int { return p.id }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
func (p *sequenceParser) setIncludedBy(includedBy parser, parsers []string) {
|
|
|
|
if stringsContainDeprecated(parsers, p.name) {
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 00:04:16 +02:00
|
|
|
p.includedBy = append(p.includedBy, includedBy)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 02:20:23 +02:00
|
|
|
func (p *sequenceParser) storeIncluded(c *context, n *Node) {
|
2017-07-15 21:49:08 +02:00
|
|
|
if !c.excluded(n.From, p.id) {
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
nc := newNode(p.name, p.id, n.From, n.To, p.commit)
|
2017-06-25 17:51:08 +02:00
|
|
|
nc.append(n)
|
2017-06-26 02:20:23 +02:00
|
|
|
c.store.set(nc.From, p.name, nc)
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-06-26 00:04:16 +02:00
|
|
|
for _, includedBy := range p.includedBy {
|
2017-06-26 02:20:23 +02:00
|
|
|
includedBy.storeIncluded(c, nc)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sequenceParser) parse(t Trace, c *context) {
|
2017-06-26 00:20:54 +02:00
|
|
|
// t = t.Extend(p.name)
|
|
|
|
// t.Out1("parsing sequence", c.offset)
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
if p.commit&Documentation != 0 {
|
2017-06-26 00:20:54 +02:00
|
|
|
// t.Out1("fail, doc")
|
2017-06-25 17:51:08 +02:00
|
|
|
c.fail(c.offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
if c.excluded(c.offset, p.id) {
|
2017-06-26 00:20:54 +02:00
|
|
|
// t.Out1("excluded")
|
2017-06-25 17:51:08 +02:00
|
|
|
c.fail(c.offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-15 21:49:08 +02:00
|
|
|
c.exclude(c.offset, p.id)
|
|
|
|
defer c.include(c.offset, p.id)
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
items := p.items
|
2017-06-25 23:38:32 +02:00
|
|
|
ranges := p.ranges
|
|
|
|
var currentCount int
|
2017-07-15 21:49:08 +02:00
|
|
|
node := newNode(p.name, p.id, c.offset, c.offset, p.commit)
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
for len(items) > 0 {
|
2017-06-26 02:20:23 +02:00
|
|
|
m, ok := c.fromStore(items[0].nodeName())
|
2017-06-25 17:51:08 +02:00
|
|
|
if ok {
|
2017-06-26 02:20:23 +02:00
|
|
|
// t.Out1("sequence item found in store, match:", m, items[0].nodeName(), c.offset)
|
2017-06-25 23:58:25 +02:00
|
|
|
} else {
|
|
|
|
items[0].parse(t, c)
|
|
|
|
m = c.match
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:58:25 +02:00
|
|
|
if !m {
|
2017-06-25 23:38:32 +02:00
|
|
|
if currentCount < ranges[0][0] {
|
2017-06-26 00:20:54 +02:00
|
|
|
// t.Out1("fail, item failed")
|
2017-06-26 02:20:23 +02:00
|
|
|
c.store.set(node.From, p.name, nil)
|
2017-06-26 01:21:46 +02:00
|
|
|
c.fail(node.From)
|
2017-06-25 23:38:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
|
|
|
continue
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.node.tokenLength() > 0 {
|
|
|
|
node.append(c.node)
|
2017-06-25 23:38:32 +02:00
|
|
|
currentCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.node.tokenLength() == 0 || ranges[0][1] >= 0 && currentCount == ranges[0][1] {
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 00:20:54 +02:00
|
|
|
// t.Out1("success, items parsed")
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-06-26 02:20:23 +02:00
|
|
|
c.store.set(node.From, p.name, node)
|
2017-06-26 00:04:16 +02:00
|
|
|
for _, includedBy := range p.includedBy {
|
2017-06-26 02:20:23 +02:00
|
|
|
includedBy.storeIncluded(c, node)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.success(node)
|
|
|
|
}
|