treerack/choice.go

159 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 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-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-06-25 17:51:08 +02:00
2017-12-31 17:34:10 +01:00
var (
match bool
optionIndex int
foundMatch bool
failingParser parser
)
2017-06-25 17:51:08 +02:00
2017-12-31 17:34:10 +01:00
from := c.offset
to := c.offset
2017-11-26 18:48:56 +01:00
initialFailOffset := c.failOffset
initialFailingParser := c.failingParser
failOffset := initialFailOffset
2017-11-26 18:48:56 +01:00
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
if !c.matchLast {
if c.failOffset > failOffset {
failOffset = c.failOffset
failingParser = c.failingParser
}
}
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-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-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-12-29 18:20:06 +01:00
if failOffset > to {
c.failOffset = failOffset
c.failingParser = failingParser
} else if to > initialFailOffset {
c.failOffset = -1
c.failingParser = nil
} else {
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
}
if failOffset > initialFailOffset {
c.failOffset = failOffset
c.failingParser = failingParser
if c.failingParser == nil &&
p.commitType()&userDefined != 0 &&
2017-12-29 18:36:58 +01:00
p.commitType()&Whitespace == 0 &&
p.commitType()&FailPass == 0 {
2017-11-26 18:48:56 +01:00
c.failingParser = p
}
}
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) {
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:24:55 +01:00
if parsed {
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-02 22:19:03 +01:00
var option builder
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-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
}