2017-07-15 21:49:08 +02:00
|
|
|
package treerack
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
type choiceDefinition struct {
|
2017-07-27 01:48:16 +02:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
elements []string
|
|
|
|
includedBy []int
|
2017-07-29 18:40:22 +02:00
|
|
|
cbuilder *choiceBuilder
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-07-27 01:48:16 +02:00
|
|
|
includedBy []int
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 16:25:17 +02:00
|
|
|
type choiceBuilder struct {
|
2017-07-29 20:04:22 +02:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
elements []builder
|
|
|
|
includedBy []int
|
2017-07-29 16:25:17 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
func (d *choiceDefinition) nodeName() string { return d.name }
|
|
|
|
func (d *choiceDefinition) nodeID() int { return d.id }
|
|
|
|
func (d *choiceDefinition) setID(id int) { d.id = id }
|
2017-07-29 16:25:17 +02:00
|
|
|
func (d *choiceDefinition) commitType() CommitType { return d.commit }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-07-27 01:48:16 +02:00
|
|
|
func (d *choiceDefinition) init(r *registry) error {
|
2017-07-29 18:40:22 +02:00
|
|
|
if d.cbuilder == nil {
|
|
|
|
d.cbuilder = &choiceBuilder{
|
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range d.elements {
|
|
|
|
def, ok := r.definition(e)
|
|
|
|
if !ok {
|
|
|
|
return parserNotFound(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.cbuilder.elements = append(d.cbuilder.elements, def.builder())
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:48:16 +02:00
|
|
|
parsers := &idSet{}
|
|
|
|
parsers.set(d.id)
|
|
|
|
return setItemsIncludedBy(r, d.elements, d.id, parsers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *choiceDefinition) setIncludedBy(r *registry, includedBy int, parsers *idSet) error {
|
|
|
|
if parsers.has(d.id) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.includedBy = appendIfMissing(d.includedBy, includedBy)
|
2017-07-29 20:04:22 +02:00
|
|
|
|
|
|
|
if d.cbuilder == nil {
|
|
|
|
d.cbuilder = &choiceBuilder{
|
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.cbuilder.includedBy = appendIfMissing(d.cbuilder.includedBy, includedBy)
|
|
|
|
|
2017-07-27 01:48:16 +02:00
|
|
|
parsers.set(d.id)
|
|
|
|
return setItemsIncludedBy(r, d.elements, includedBy, parsers)
|
|
|
|
}
|
|
|
|
|
2017-07-29 16:25:17 +02:00
|
|
|
// TODO:
|
|
|
|
// - it may be possible to initialize the parsers non-recursively
|
|
|
|
// - maybe the whole definition, parser and builder can be united
|
|
|
|
|
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{
|
2017-07-27 01:48:16 +02:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
includedBy: d.includedBy,
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
elements = append(elements, element)
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.elements = elements
|
|
|
|
return cp, nil
|
|
|
|
}
|
|
|
|
|
2017-07-29 16:25:17 +02:00
|
|
|
func (d *choiceDefinition) builder() builder {
|
2017-07-29 18:40:22 +02:00
|
|
|
if d.cbuilder == nil {
|
|
|
|
d.cbuilder = &choiceBuilder{
|
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.cbuilder
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2017-07-18 00:38:44 +02:00
|
|
|
var nextTo int
|
|
|
|
var elementIndex int
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
var foundMatch bool
|
2017-07-18 00:38:44 +02:00
|
|
|
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++
|
2017-07-18 00:38:44 +02:00
|
|
|
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-27 01:48:16 +02:00
|
|
|
c.store.setMatch(from, includedBy, 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
|
|
|
}
|
2017-07-29 16:25:17 +02:00
|
|
|
|
|
|
|
func (b *choiceBuilder) nodeName() string { return b.name }
|
|
|
|
func (b *choiceBuilder) nodeID() int { return b.id }
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
func (b *choiceBuilder) build(c *context) ([]*Node, bool) {
|
|
|
|
to, ok := c.store.takeMatch(c.offset, b.id)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:04:22 +02:00
|
|
|
for _, ib := range b.includedBy {
|
|
|
|
c.store.takeMatchLength(c.offset, ib, to)
|
|
|
|
}
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
var element builder
|
|
|
|
for _, e := range b.elements {
|
|
|
|
elementTo, match, _ := c.store.getMatch(c.offset, e.nodeID())
|
|
|
|
if match && elementTo == to {
|
|
|
|
element = e
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if element == nil {
|
|
|
|
panic("damaged parse result")
|
|
|
|
}
|
|
|
|
|
|
|
|
from := c.offset
|
|
|
|
|
|
|
|
n, ok := element.build(c)
|
|
|
|
if !ok {
|
|
|
|
panic("damaged parse result")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.commit&Alias != 0 {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*Node{{
|
|
|
|
Name: b.name,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Nodes: n,
|
|
|
|
tokens: c.tokens,
|
|
|
|
}}, true
|
2017-07-29 16:25:17 +02:00
|
|
|
}
|