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
|
2017-07-30 02:35:51 +02:00
|
|
|
includedBy *idSet
|
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-10-28 22:54:15 +02:00
|
|
|
func (d *choiceDefinition) nodeName() string { return d.name }
|
|
|
|
func (d *choiceDefinition) setNodeName(n string) { d.name = n }
|
|
|
|
func (d *choiceDefinition) nodeID() int { return d.id }
|
|
|
|
func (d *choiceDefinition) setID(id int) { d.id = id }
|
|
|
|
func (d *choiceDefinition) commitType() CommitType { return d.commit }
|
|
|
|
func (d *choiceDefinition) setCommitType(ct CommitType) { d.commit = ct }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-08-06 20:43:52 +02:00
|
|
|
func (d *choiceDefinition) validate(r *registry, path *idSet) error {
|
|
|
|
for i := range d.elements {
|
2017-08-06 21:47:53 +02:00
|
|
|
if _, ok := r.definitions[d.elements[i]]; !ok {
|
2017-08-06 20:43:52 +02:00
|
|
|
return parserNotFound(d.elements[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-06 20:32:59 +02:00
|
|
|
func (d *choiceDefinition) normalize(r *registry, path *idSet) error {
|
|
|
|
if path.has(d.id) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path.set(d.id)
|
|
|
|
for i := range d.elements {
|
2017-08-06 21:47:53 +02:00
|
|
|
r.definitions[d.elements[i]].normalize(r, path)
|
2017-08-06 20:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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{
|
2017-07-30 02:35:51 +02:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
includedBy: &idSet{},
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range d.elements {
|
2017-08-06 21:47:53 +02:00
|
|
|
d.cbuilder.elements = append(d.cbuilder.elements, r.definitions[e].builder())
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
|
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{
|
2017-07-30 02:35:51 +02:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
includedBy: &idSet{},
|
2017-07-29 20:04:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 02:35:51 +02:00
|
|
|
d.cbuilder.includedBy.set(includedBy)
|
2017-07-29 20:04:22 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:47:53 +02:00
|
|
|
element, err := r.definitions[e].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{
|
2017-07-30 02:35:51 +02:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
includedBy: &idSet{},
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-10-27 16:23:17 +02:00
|
|
|
// t = t.Extend(p.name)
|
|
|
|
// t.Out1("parsing choice", c.offset)
|
2017-07-29 22:31:16 +02:00
|
|
|
|
|
|
|
// TODO: don't add documentation
|
2017-07-30 05:10:46 +02:00
|
|
|
// if p.commit&Documentation != 0 {
|
|
|
|
// // t.Out1("fail, doc")
|
|
|
|
// c.fail(c.offset)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
if c.fromStore(p.id) {
|
2017-10-27 16:23:17 +02:00
|
|
|
// t.Out1("found in store, match:")
|
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-10-27 16:23:17 +02:00
|
|
|
// t.Out1("fail, 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)
|
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 elementIndex int
|
2017-07-30 02:35:51 +02:00
|
|
|
var foundMatch bool
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
for {
|
2017-07-30 02:35:51 +02:00
|
|
|
foundMatch = false
|
2017-07-18 00:38:44 +02:00
|
|
|
elementIndex = 0
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-10-27 16:04:09 +02:00
|
|
|
// TODO:
|
|
|
|
// - avoid double parsing by setting first-from-store in the context, prepare in advance to
|
|
|
|
// know whether it can be it's own item
|
|
|
|
// - it is also important to figure why disabling the failed elements breaks the parsing
|
2017-08-06 21:47:53 +02:00
|
|
|
|
2017-07-17 04:23:29 +02:00
|
|
|
for elementIndex < len(p.elements) {
|
|
|
|
p.elements[elementIndex].parse(t, c)
|
|
|
|
elementIndex++
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-07-30 05:10:46 +02:00
|
|
|
if !c.match || match && c.offset <= to {
|
|
|
|
c.offset = from
|
2017-06-25 17:51:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
match = true
|
|
|
|
foundMatch = true
|
2017-07-30 05:10:46 +02:00
|
|
|
to = c.offset
|
|
|
|
c.offset = from
|
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-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-10-27 16:23:17 +02:00
|
|
|
// t.Out1("choice, success")
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 16:23:17 +02:00
|
|
|
// t.Out1("fail")
|
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) {
|
2017-07-30 02:35:51 +02:00
|
|
|
to, ok := c.store.takeMatch(c.offset, b.id, b.includedBy)
|
2017-07-29 18:40:22 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var element builder
|
|
|
|
for _, e := range b.elements {
|
2017-07-30 02:35:51 +02:00
|
|
|
if c.store.hasMatchTo(c.offset, e.nodeID(), to) {
|
2017-07-29 18:40:22 +02:00
|
|
|
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
|
|
|
}
|