refactor - build phase

This commit is contained in:
Arpad Ryszka 2017-11-04 22:24:55 +01:00
parent 60172b2f9f
commit 1a27c52e7c
2 changed files with 35 additions and 36 deletions

View File

@ -206,14 +206,17 @@ func (b *choiceBuilder) build(c *context) ([]*Node, bool) {
return nil, false return nil, false
} }
from := c.offset
parsed := to > from
if parsed {
c.results.dropMatchTo(c.offset, b.id, to)
} else {
if c.buildPending(c.offset, b.id, to) { if c.buildPending(c.offset, b.id, to) {
return nil, false return nil, false
} }
c.markBuildPending(c.offset, b.id, to) c.markBuildPending(c.offset, b.id, to)
if to-c.offset > 0 {
c.results.dropMatchTo(c.offset, b.id, to)
} }
var option builder var option builder
@ -228,14 +231,14 @@ func (b *choiceBuilder) build(c *context) ([]*Node, bool) {
panic("damaged parse result") panic("damaged parse result")
} }
from := c.offset
n, ok := option.build(c) n, ok := option.build(c)
if !ok { if !ok {
panic("damaged parse result") panic("damaged parse result")
} }
if !parsed {
c.unmarkBuildPending(from, b.id, to) c.unmarkBuildPending(from, b.id, to)
}
if b.commit&Alias != 0 { if b.commit&Alias != 0 {
return n, true return n, true

View File

@ -239,22 +239,11 @@ func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
return nil, false return nil, false
} }
if c.buildPending(c.offset, b.id, to) {
return nil, false
}
c.markBuildPending(c.offset, b.id, to)
if to-c.offset > 0 {
c.results.dropMatchTo(c.offset, b.id, to)
}
from := c.offset from := c.offset
parsed := to > from
if b.allChars { if b.allChars {
c.offset = to c.offset = to
c.unmarkBuildPending(from, b.id, to)
if b.commit&Alias != 0 { if b.commit&Alias != 0 {
return nil, true return nil, true
} }
@ -265,6 +254,14 @@ func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
To: to, To: to,
tokens: c.tokens, tokens: c.tokens,
}}, true }}, true
} else if parsed {
c.results.dropMatchTo(c.offset, b.id, to)
} else {
if c.buildPending(c.offset, b.id, to) {
return nil, false
}
c.markBuildPending(c.offset, b.id, to)
} }
var ( var (
@ -286,14 +283,18 @@ func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
continue continue
} }
parsed := c.offset > itemFrom if c.offset > itemFrom {
if parsed {
nodes = append(nodes, n...) nodes = append(nodes, n...)
currentCount++ currentCount++
if b.ranges[itemIndex][1] >= 0 && currentCount == b.ranges[itemIndex][1] {
itemIndex++
currentCount = 0
}
continue
} }
if !parsed {
if currentCount < b.ranges[itemIndex][0] { if currentCount < b.ranges[itemIndex][0] {
for i := 0; i < b.ranges[itemIndex][0]-currentCount; i++ { for i := 0; i < b.ranges[itemIndex][0]-currentCount; i++ {
nodes = append(nodes, n...) nodes = append(nodes, n...)
@ -302,16 +303,11 @@ func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
itemIndex++ itemIndex++
currentCount = 0 currentCount = 0
continue
}
if b.ranges[itemIndex][1] >= 0 && currentCount == b.ranges[itemIndex][1] {
itemIndex++
currentCount = 0
}
} }
if !parsed {
c.unmarkBuildPending(from, b.id, to) c.unmarkBuildPending(from, b.id, to)
}
if b.commit&Alias != 0 { if b.commit&Alias != 0 {
return nodes, true return nodes, true