treerack/choice.go

264 lines
4.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 {
2017-11-01 02:43:46 +01:00
name string
id int
commit CommitType
elements []string
includedBy []int
cbuilder *choiceBuilder
validated bool
initialized bool
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-11-01 02:43:46 +01:00
func (d *choiceDefinition) validate(r *registry) error {
if d.validated {
return nil
}
d.validated = true
2017-08-06 20:43:52 +02:00
for i := range d.elements {
2017-11-01 02:43:46 +01:00
e, ok := r.definitions[d.elements[i]]
if !ok {
2017-08-06 20:43:52 +02:00
return parserNotFound(d.elements[i])
}
2017-11-01 02:43:46 +01:00
if err := e.validate(r); err != nil {
return err
}
2017-08-06 20:43:52 +02:00
}
return nil
}
2017-11-01 02:43:46 +01:00
func (d *choiceDefinition) init(r *registry) {
if d.initialized {
return
2017-08-06 20:32:59 +02:00
}
2017-11-01 02:43:46 +01:00
d.initialized = true
2017-08-06 20:32:59 +02:00
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-11-01 02:43:46 +01:00
def := r.definitions[e]
d.cbuilder.elements = append(d.cbuilder.elements, def.builder())
def.init(r)
def.setIncludedBy(r, d.id)
2017-07-29 18:40:22 +02:00
}
2017-07-27 01:48:16 +02:00
}
2017-11-01 02:43:46 +01:00
func (d *choiceDefinition) setIncludedBy(r *registry, includedBy int) {
if intsContain(d.includedBy, includedBy) {
return
2017-07-27 01:48:16 +02:00
}
2017-11-01 02:43:46 +01:00
d.includedBy = append(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-11-01 02:43:46 +01:00
for _, e := range d.elements {
r.definitions[e].setIncludedBy(r, includedBy)
}
2017-07-27 01:48:16 +02:00
}
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-11-01 02:43:46 +01:00
func (d *choiceDefinition) parser(r *registry) parser {
2017-06-25 17:51:08 +02:00
p, ok := r.parser(d.name)
if ok {
2017-11-01 02:43:46 +01:00
return p
2017-06-25 17:51:08 +02:00
}
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
for _, e := range d.elements {
element, ok := r.parser(e)
if ok {
elements = append(elements, element)
continue
}
2017-11-01 02:43:46 +01:00
element = r.definitions[e].parser(r)
2017-06-25 17:51:08 +02:00
elements = append(elements, element)
}
cp.elements = elements
2017-11-01 02:43:46 +01:00
return cp
2017-06-25 17:51:08 +02:00
}
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
2017-10-31 21:09:30 +01:00
func (p *choiceParser) parse(c *context) {
2017-07-30 05:10:46 +02:00
if c.fromStore(p.id) {
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 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
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-07-17 04:23:29 +02:00
for elementIndex < len(p.elements) {
2017-10-31 21:09:30 +01:00
p.elements[elementIndex].parse(c)
2017-07-17 04:23:29 +02:00
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-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) {
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
}