2017-07-15 21:49:08 +02:00
|
|
|
package treerack
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
type choiceDefinition struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
options []string
|
|
|
|
optionDefs []definition
|
|
|
|
generalizations []int
|
|
|
|
cparser *choiceParser
|
|
|
|
cbuilder *choiceBuilder
|
|
|
|
validated bool
|
|
|
|
initialized bool
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type choiceParser struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
options []parser
|
|
|
|
generalizations []int
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 16:25:17 +02:00
|
|
|
type choiceBuilder struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
options []builder
|
2017-07-29 16:25:17 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func newChoice(name string, ct CommitType, options []string) *choiceDefinition {
|
2017-06-25 17:51:08 +02:00
|
|
|
return &choiceDefinition{
|
2017-11-02 22:19:03 +01:00
|
|
|
name: name,
|
|
|
|
commit: ct,
|
|
|
|
options: options,
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 22:54:15 +02:00
|
|
|
func (d *choiceDefinition) nodeName() string { return d.name }
|
2017-11-05 03:28:36 +01:00
|
|
|
func (d *choiceDefinition) setName(n string) { d.name = n }
|
2017-10-28 22:54:15 +02:00
|
|
|
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-11-04 22:49:42 +01:00
|
|
|
func (d *choiceDefinition) preinit() {}
|
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-11-02 22:19:03 +01:00
|
|
|
for i := range d.options {
|
2017-11-04 22:08:15 +01:00
|
|
|
o, ok := r.definitions[d.options[i]]
|
2017-11-01 02:43:46 +01:00
|
|
|
if !ok {
|
2017-11-02 22:19:03 +01:00
|
|
|
return parserNotFound(d.options[i])
|
2017-08-06 20:43:52 +02:00
|
|
|
}
|
2017-11-01 02:43:46 +01:00
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
if err := o.validate(r); err != nil {
|
2017-11-01 02:43:46 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-08-06 20:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:46:07 +01:00
|
|
|
func (d *choiceDefinition) createBuilder() {
|
2017-11-01 03:54:53 +01:00
|
|
|
d.cbuilder = &choiceBuilder{
|
2017-11-02 22:19:03 +01:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-07-29 18:40:22 +02:00
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func (d *choiceDefinition) initOptions(r *registry) {
|
2017-11-04 22:08:15 +01:00
|
|
|
for _, o := range d.options {
|
|
|
|
def := r.definitions[o]
|
2017-11-02 22:19:03 +01:00
|
|
|
d.optionDefs = append(d.optionDefs, def)
|
2017-11-01 02:43:46 +01:00
|
|
|
def.init(r)
|
2017-11-02 22:19:03 +01:00
|
|
|
d.cbuilder.options = append(d.cbuilder.options, def.builder())
|
|
|
|
def.addGeneralization(d.id)
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *choiceDefinition) init(r *registry) {
|
|
|
|
if d.initialized {
|
2017-11-01 02:43:46 +01:00
|
|
|
return
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
d.initialized = true
|
2017-11-02 22:46:07 +01:00
|
|
|
d.createBuilder()
|
2017-11-02 22:19:03 +01:00
|
|
|
d.initOptions(r)
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-07-29 20:04:22 +02:00
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func (d *choiceDefinition) addGeneralization(g int) {
|
|
|
|
if intsContain(d.generalizations, g) {
|
2017-11-01 03:54:53 +01:00
|
|
|
return
|
2017-07-29 20:04:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
d.generalizations = append(d.generalizations, g)
|
2017-11-04 22:08:15 +01:00
|
|
|
for _, o := range d.optionDefs {
|
|
|
|
o.addGeneralization(g)
|
2017-11-01 02:43:46 +01:00
|
|
|
}
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *choiceDefinition) createParser() {
|
|
|
|
d.cparser = &choiceParser{
|
2017-11-02 22:19:03 +01:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
generalizations: d.generalizations,
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func (d *choiceDefinition) createOptionParsers() {
|
|
|
|
for _, def := range d.optionDefs {
|
|
|
|
option := def.parser()
|
|
|
|
d.cparser.options = append(d.cparser.options, option)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *choiceDefinition) parser() parser {
|
|
|
|
if d.cparser != nil {
|
|
|
|
return d.cparser
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
d.createParser()
|
2017-11-02 22:19:03 +01:00
|
|
|
d.createOptionParsers()
|
2017-11-01 03:54:53 +01:00
|
|
|
return d.cparser
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *choiceDefinition) builder() builder { return d.cbuilder }
|
2017-11-02 22:19:03 +01:00
|
|
|
|
2017-11-26 01:49:22 +01:00
|
|
|
func (d *choiceDefinition) format(r *registry, f formatFlags) string {
|
|
|
|
var chars []rune
|
|
|
|
for i := range d.options {
|
|
|
|
if i > 0 {
|
|
|
|
chars = append(chars, []rune(" | ")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
optionDef, _ := r.definition(d.options[i])
|
|
|
|
if optionDef.commitType()&userDefined != 0 {
|
|
|
|
chars = append(chars, []rune(optionDef.nodeName())...)
|
|
|
|
} else {
|
|
|
|
chars = append(chars, []rune(optionDef.format(r, f))...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(chars)
|
|
|
|
}
|
|
|
|
|
2017-11-25 17:37:05 +01:00
|
|
|
func (p *choiceParser) nodeName() string { return p.name }
|
|
|
|
func (p *choiceParser) nodeID() int { return p.id }
|
|
|
|
func (p *choiceParser) commitType() CommitType { return p.commit }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-10-31 21:09:30 +01:00
|
|
|
func (p *choiceParser) parse(c *context) {
|
2017-11-25 17:37:05 +01:00
|
|
|
if c.fromResults(p) {
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:41:00 +01:00
|
|
|
if c.results.pending(c.offset, p.id) {
|
2017-11-25 22:01:02 +01:00
|
|
|
c.fail(c.offset)
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:41:00 +01:00
|
|
|
c.results.markPending(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-11-02 22:19:03 +01:00
|
|
|
var optionIndex int
|
2017-07-30 02:35:51 +02:00
|
|
|
var foundMatch bool
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-26 18:48:56 +01:00
|
|
|
// TODO:
|
|
|
|
// - if there is a failure already, it should be left alone
|
|
|
|
// - what if reading more means that the previous failurs don't count
|
|
|
|
initialFailOffset := c.failOffset
|
|
|
|
initialFailingParser := c.failingParser
|
|
|
|
c.failingParser = nil
|
|
|
|
var (
|
|
|
|
failOffset int
|
|
|
|
failingParser parser
|
|
|
|
)
|
|
|
|
|
2017-06-25 17:51:08 +02:00
|
|
|
for {
|
2017-07-30 02:35:51 +02:00
|
|
|
foundMatch = false
|
2017-11-02 22:19:03 +01:00
|
|
|
optionIndex = 0
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
for optionIndex < len(p.options) {
|
|
|
|
p.options[optionIndex].parse(c)
|
|
|
|
optionIndex++
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-02 20:49:49 +01:00
|
|
|
if !c.matchLast || match && c.offset <= to {
|
2017-07-30 05:10:46 +02:00
|
|
|
c.offset = from
|
2017-11-26 18:48:56 +01:00
|
|
|
if c.failOffset > failOffset {
|
|
|
|
failOffset = c.failOffset
|
|
|
|
failingParser = c.failingParser
|
|
|
|
}
|
|
|
|
|
|
|
|
c.failOffset = initialFailOffset
|
|
|
|
c.failingParser = nil
|
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-11-26 18:48:56 +01:00
|
|
|
c.failOffset = initialFailOffset
|
|
|
|
c.failingParser = nil
|
2017-11-02 20:49:49 +01:00
|
|
|
c.results.setMatch(from, p.id, to)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !foundMatch {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match {
|
2017-11-26 18:48:56 +01:00
|
|
|
c.failOffset = initialFailOffset
|
|
|
|
c.failingParser = initialFailingParser
|
2017-07-17 04:23:29 +02:00
|
|
|
c.success(to)
|
2017-11-06 11:41:00 +01:00
|
|
|
c.results.unmarkPending(from, p.id)
|
2017-06-25 17:51:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:48:56 +01:00
|
|
|
if failOffset > initialFailOffset {
|
|
|
|
// println("recording choice failure", p.name, failOffset)
|
|
|
|
c.failOffset = failOffset
|
|
|
|
if failingParser == nil && p.commit&userDefined != 0 && p.commit&Whitespace == 0 {
|
|
|
|
// println("setting failing choice parser", p.nodeName(), failOffset)
|
|
|
|
c.failingParser = p
|
|
|
|
} else {
|
|
|
|
c.failingParser = failingParser
|
|
|
|
}
|
|
|
|
} else if failOffset == initialFailOffset && initialFailingParser == nil {
|
|
|
|
// println("recording choice failure", p.name, failOffset)
|
|
|
|
c.failOffset = failOffset
|
|
|
|
if failingParser == nil && p.commit&userDefined != 0 && p.commit&Whitespace == 0 {
|
|
|
|
// println("setting failing choice parser", p.nodeName(), failOffset)
|
|
|
|
c.failingParser = p
|
|
|
|
} else {
|
|
|
|
c.failingParser = failingParser
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.failOffset = initialFailOffset
|
|
|
|
c.failingParser = initialFailingParser
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:49:49 +01:00
|
|
|
c.results.setNoMatch(from, p.id)
|
2017-11-25 22:01:02 +01:00
|
|
|
c.fail(from)
|
2017-11-06 11:41:00 +01:00
|
|
|
c.results.unmarkPending(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-11-04 22:08:15 +01:00
|
|
|
to, ok := c.results.longestMatch(c.offset, b.id)
|
2017-07-29 18:40:22 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-11-04 22:24:55 +01:00
|
|
|
from := c.offset
|
|
|
|
parsed := to > from
|
2017-11-04 22:08:15 +01:00
|
|
|
|
2017-11-04 22:24:55 +01:00
|
|
|
if parsed {
|
2017-11-04 22:08:15 +01:00
|
|
|
c.results.dropMatchTo(c.offset, b.id, to)
|
2017-11-04 22:24:55 +01:00
|
|
|
} else {
|
2017-11-06 11:41:00 +01:00
|
|
|
if c.results.pending(c.offset, b.id) {
|
2017-11-04 22:24:55 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:41:00 +01:00
|
|
|
c.results.markPending(c.offset, b.id)
|
2017-11-04 22:08:15 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
var option builder
|
2017-11-04 22:08:15 +01:00
|
|
|
for _, o := range b.options {
|
|
|
|
if c.results.hasMatchTo(c.offset, o.nodeID(), to) {
|
|
|
|
option = o
|
2017-07-29 18:40:22 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 03:28:36 +01:00
|
|
|
n, _ := option.build(c)
|
2017-11-04 22:24:55 +01:00
|
|
|
if !parsed {
|
2017-11-06 11:41:00 +01:00
|
|
|
c.results.unmarkPending(from, b.id)
|
2017-11-04 22:24:55 +01:00
|
|
|
}
|
2017-11-04 22:08:15 +01:00
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
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
|
|
|
}
|