2017-07-15 21:49:08 +02:00
|
|
|
package treerack
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
reader io.RuneReader
|
|
|
|
offset int
|
|
|
|
readOffset int
|
|
|
|
readErr error
|
|
|
|
eof bool
|
2017-11-02 20:49:49 +01:00
|
|
|
results *results
|
2017-06-25 17:51:08 +02:00
|
|
|
tokens []rune
|
2017-11-02 20:49:49 +01:00
|
|
|
matchLast bool
|
|
|
|
isPending [][]int
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newContext(r io.RuneReader) *context {
|
|
|
|
return &context{
|
2017-11-02 20:49:49 +01:00
|
|
|
reader: r,
|
|
|
|
results: &results{},
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) read() bool {
|
|
|
|
if c.eof || c.readErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:55:19 +01:00
|
|
|
token, n, err := c.reader.ReadRune()
|
2017-06-25 17:51:08 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if n == 0 {
|
|
|
|
c.eof = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.readErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.readOffset++
|
|
|
|
|
2017-11-02 22:55:19 +01:00
|
|
|
if token == unicode.ReplacementChar {
|
2017-06-26 02:20:23 +02:00
|
|
|
c.readErr = ErrInvalidUnicodeCharacter
|
2017-06-25 17:51:08 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:55:19 +01:00
|
|
|
c.tokens = append(c.tokens, token)
|
2017-06-25 17:51:08 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) token() (rune, bool) {
|
|
|
|
if c.offset == c.readOffset {
|
|
|
|
if !c.read() {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.tokens[c.offset], true
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:49:49 +01:00
|
|
|
func (c *context) fromResults(id int) bool {
|
2017-11-04 22:08:15 +01:00
|
|
|
to, m, ok := c.results.longestResult(c.offset, id)
|
2017-06-25 17:51:08 +02:00
|
|
|
if !ok {
|
2017-07-30 05:10:46 +02:00
|
|
|
return false
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m {
|
2017-07-17 04:23:29 +02:00
|
|
|
c.success(to)
|
2017-06-25 17:51:08 +02:00
|
|
|
} else {
|
|
|
|
c.fail(c.offset)
|
|
|
|
}
|
|
|
|
|
2017-07-30 05:10:46 +02:00
|
|
|
return true
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 11:14:28 +01:00
|
|
|
// TODO:
|
|
|
|
// - try to move this to the parsers
|
|
|
|
// - try to move more
|
|
|
|
// - if doens't help performance, try move more from there to here
|
|
|
|
|
2017-07-17 04:23:29 +02:00
|
|
|
func (c *context) success(to int) {
|
|
|
|
c.offset = to
|
2017-11-02 20:49:49 +01:00
|
|
|
c.matchLast = true
|
2017-07-17 01:41:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 17:51:08 +02:00
|
|
|
func (c *context) fail(offset int) {
|
|
|
|
c.offset = offset
|
2017-11-02 20:49:49 +01:00
|
|
|
c.matchLast = false
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:55:19 +01:00
|
|
|
func (c *context) finalizeParse(rootID int) error {
|
|
|
|
if !c.matchLast {
|
|
|
|
return ErrInvalidInput
|
|
|
|
}
|
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
to, match, found := c.results.longestResult(0, rootID)
|
2017-07-29 16:25:17 +02:00
|
|
|
if !found || !match || to < c.readOffset {
|
2017-06-25 17:51:08 +02:00
|
|
|
return ErrUnexpectedCharacter
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.eof {
|
|
|
|
c.read()
|
|
|
|
if !c.eof {
|
|
|
|
if c.readErr != nil {
|
|
|
|
return c.readErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrUnexpectedCharacter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|