treerack/choice.go

162 lines
2.9 KiB
Go
Raw Normal View History

2017-07-15 21:49:08 +02:00
package treerack
2017-06-25 17:51:08 +02:00
type choiceDefinition struct {
name string
2017-07-15 21:49:08 +02:00
id int
2017-06-25 17:51:08 +02:00
commit CommitType
elements []string
}
type choiceParser 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
elements []parser
includedBy []parser
2017-06-25 17:51:08 +02:00
}
func newChoice(name string, ct CommitType, elements []string) *choiceDefinition {
return &choiceDefinition{
name: name,
commit: ct,
elements: elements,
}
}
func (d *choiceDefinition) nodeName() string { return d.name }
2017-07-15 21:49:08 +02:00
func (d *choiceDefinition) nodeID() int { return d.id }
func (d *choiceDefinition) setID(id int) { d.id = id }
2017-06-25 17:51:08 +02:00
2017-07-15 22:12:01 +02:00
func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
2017-06-25 17:51:08 +02:00
p, ok := r.parser(d.name)
if ok {
return p, nil
}
cp := &choiceParser{
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(cp)
var elements []parser
2017-07-15 22:12:01 +02:00
parsers.set(d.id)
defer parsers.unset(d.id)
2017-06-25 17:51:08 +02:00
for _, e := range d.elements {
element, ok := r.parser(e)
if ok {
elements = append(elements, element)
2017-07-15 21:49:08 +02:00
element.setIncludedBy(cp, parsers)
2017-06-25 17:51:08 +02:00
continue
}
elementDefinition, ok := r.definition(e)
if !ok {
return nil, parserNotFound(e)
}
2017-07-15 21:49:08 +02:00
element, err := elementDefinition.parser(r, parsers)
2017-06-25 17:51:08 +02:00
if err != nil {
return nil, err
}
2017-07-15 21:49:08 +02:00
element.setIncludedBy(cp, parsers)
2017-06-25 17:51:08 +02:00
elements = append(elements, element)
}
cp.elements = elements
return cp, nil
}
func (d *choiceDefinition) commitType() CommitType {
return d.commit
}
func (p *choiceParser) nodeName() string { return p.name }
2017-07-15 21:49:08 +02:00
func (p *choiceParser) nodeID() int { return p.id }
2017-06-25 17:51:08 +02:00
2017-07-15 22:12:01 +02:00
func (p *choiceParser) setIncludedBy(includedBy parser, parsers *idSet) {
if parsers.has(p.id) {
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-07-17 04:23:29 +02:00
func (p *choiceParser) storeIncluded(c *context, from, to int) {
if !c.excluded(from, p.id) {
2017-06-25 17:51:08 +02:00
return
}
2017-07-17 21:58:03 +02:00
c.store.setMatch(from, p.id, to)
2017-06-25 17:51:08 +02:00
2017-06-26 00:04:16 +02:00
for _, includedBy := range p.includedBy {
2017-07-17 04:23:29 +02:00
includedBy.storeIncluded(c, from, to)
2017-06-25 17:51:08 +02:00
}
}
func (p *choiceParser) parse(t Trace, c *context) {
if p.commit&Documentation != 0 {
c.fail(c.offset)
return
}
2017-07-15 23:00:43 +02:00
if _, ok := c.fromStore(p.id); ok {
2017-06-25 17:51:08 +02:00
return
}
2017-07-15 21:49:08 +02:00
if c.excluded(c.offset, p.id) {
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)
2017-07-17 04:23:29 +02:00
from := c.offset
to := c.offset
2017-06-25 17:51:08 +02:00
var match bool
var nextTo int
var elementIndex int
2017-06-25 17:51:08 +02:00
for {
var foundMatch bool
elementIndex = 0
2017-06-25 17:51:08 +02:00
2017-07-17 04:23:29 +02:00
for elementIndex < len(p.elements) {
p.elements[elementIndex].parse(t, c)
elementIndex++
nextTo = c.offset
2017-07-17 04:23:29 +02:00
c.offset = from
2017-06-25 17:51:08 +02:00
2017-07-17 04:23:29 +02:00
if !c.match || match && nextTo <= to {
2017-06-25 17:51:08 +02:00
continue
}
match = true
foundMatch = true
2017-07-17 04:23:29 +02:00
to = nextTo
2017-06-25 17:51:08 +02:00
2017-07-17 21:58:03 +02:00
c.store.setMatch(from, p.id, to)
2017-06-26 00:04:16 +02:00
for _, includedBy := range p.includedBy {
2017-07-17 04:23:29 +02:00
includedBy.storeIncluded(c, from, to)
2017-06-25 17:51:08 +02:00
}
}
if !foundMatch {
break
}
}
if match {
2017-07-17 04:23:29 +02:00
c.success(to)
c.include(from, p.id)
2017-06-25 17:51:08 +02:00
return
}
2017-07-17 21:58:03 +02:00
c.store.setNoMatch(from, p.id)
2017-07-17 04:23:29 +02:00
c.fail(from)
c.include(from, p.id)
2017-06-25 17:51:08 +02:00
}