diff --git a/char.go b/char.go index 2a4099a..3bb0ae6 100644 --- a/char.go +++ b/char.go @@ -83,6 +83,6 @@ func (p *charParser) parse(t Trace, c *context) { c.success(c.offset + 1) for _, includedBy := range p.includedBy { - includedBy.storeIncluded(c, c.offset, c.offset + 1) + includedBy.storeIncluded(c, c.offset, c.offset+1) } } diff --git a/choice.go b/choice.go index c0f3d2a..55df20b 100644 --- a/choice.go +++ b/choice.go @@ -117,15 +117,17 @@ func (p *choiceParser) parse(t Trace, c *context) { to := c.offset var match bool + var nextTo int + var elementIndex int for { - elementIndex := 0 var foundMatch bool + elementIndex = 0 for elementIndex < len(p.elements) { p.elements[elementIndex].parse(t, c) elementIndex++ - nextTo := c.offset + nextTo = c.offset c.offset = from if !c.match || match && nextTo <= to { diff --git a/context.go b/context.go index 137f9c8..150e7e7 100644 --- a/context.go +++ b/context.go @@ -15,7 +15,7 @@ type context struct { tokens []rune match bool node *Node - isExcluded []*idSet + isExcluded [][]int } func newContext(r io.RuneReader) *context { @@ -65,43 +65,41 @@ func (c *context) token() (rune, bool) { } func (c *context) excluded(offset int, id int) bool { - if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil { + if len(c.isExcluded) <= id { return false } - return c.isExcluded[offset].has(id) -} - -func (c *context) exclude(offset int, id int) { - if c.excluded(offset, id) { - return - } - - if len(c.isExcluded) <= offset { - c.isExcluded = append(c.isExcluded, nil) - if cap(c.isExcluded) > offset { - c.isExcluded = c.isExcluded[:offset+1] - } else { - c.isExcluded = append( - c.isExcluded[:cap(c.isExcluded)], - make([]*idSet, offset+1-cap(c.isExcluded))..., - ) + for i := range c.isExcluded[id] { + if c.isExcluded[id][i] == offset { + return true } } - if c.isExcluded[offset] == nil { - c.isExcluded[offset] = &idSet{} + return false +} + +func (c *context) exclude(offset int, id int) { + if len(c.isExcluded) <= id { + if cap(c.isExcluded) > id { + c.isExcluded = c.isExcluded[:id+1] + } else { + c.isExcluded = c.isExcluded[:cap(c.isExcluded)] + for i := cap(c.isExcluded); i <= id; i++ { + c.isExcluded = append(c.isExcluded, nil) + } + } } - c.isExcluded[offset].set(id) + c.isExcluded[id] = append(c.isExcluded[id], offset) } func (c *context) include(offset int, id int) { - if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil { - return + for i := range c.isExcluded[id] { + if c.isExcluded[id][i] == offset { + c.isExcluded[id] = append(c.isExcluded[id][:i], c.isExcluded[id][i+1:]...) + break + } } - - c.isExcluded[offset].unset(id) } func (c *context) fromStore(id int) (bool, bool) { diff --git a/sequence.go b/sequence.go index 9f179c2..eecaa1d 100644 --- a/sequence.go +++ b/sequence.go @@ -129,9 +129,9 @@ func (p *sequenceParser) parse(t Trace, c *context) { return } - if c.store.hasNoMatch(c.offset, p.id) { - c.fail(c.offset) - } + // if c.store.hasNoMatch(c.offset, p.id) { + // c.fail(c.offset) + // } c.exclude(c.offset, p.id) @@ -144,7 +144,7 @@ func (p *sequenceParser) parse(t Trace, c *context) { p.items[itemIndex].parse(t, c) if !c.match { if currentCount < p.ranges[itemIndex][0] { - c.store.setNoMatch(from, p.id) + // c.store.setNoMatch(from, p.id) c.fail(from) c.include(from, p.id) return diff --git a/store.go b/store.go index 36a7458..cd0e3e0 100644 --- a/store.go +++ b/store.go @@ -2,7 +2,7 @@ package treerack type store struct { noMatch []*idSet - match [][]int + match [][]int } func (s *store) hasNoMatch(offset, id int) bool { @@ -23,7 +23,7 @@ func (s *store) getMatch(offset, id int) (int, bool, bool) { } var ( - found bool + found bool length int ) @@ -33,8 +33,8 @@ func (s *store) getMatch(offset, id int) (int, bool, bool) { } found = true - if s.match[offset][i + 1] > length { - length = s.match[offset][i + 1] + if s.match[offset][i+1] > length { + length = s.match[offset][i+1] } } @@ -52,7 +52,7 @@ func (s *store) ensureOffset(offset int) { } s.match = s.match[:cap(s.match)] - for i := len(s.match); i <= offset; i++ { + for i := cap(s.match); i <= offset; i++ { s.match = append(s.match, nil) } } @@ -65,10 +65,10 @@ func (s *store) setMatch(offset, id, to int) { func (s *store) setNoMatch(offset, id int) { if len(s.noMatch) <= offset { if cap(s.noMatch) > offset { - s.noMatch = s.noMatch[:offset + 1] + s.noMatch = s.noMatch[:offset+1] } else { s.noMatch = s.noMatch[:cap(s.noMatch)] - for i := len(s.noMatch); i <= offset; i++ { + for i := cap(s.noMatch); i <= offset; i++ { s.noMatch = append(s.noMatch, nil) } }