2019-02-26 13:05:15 +04:00
|
|
|
package jsonpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
type ReaderLexer struct {
|
2019-02-26 13:05:15 +04:00
|
|
|
lex
|
|
|
|
bufInput *bufio.Reader
|
|
|
|
input io.Reader
|
|
|
|
pos Pos
|
|
|
|
nextByte int
|
|
|
|
lexeme *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func NewReaderLexer(rr io.Reader, initial stateFn) *ReaderLexer {
|
|
|
|
l := ReaderLexer{
|
2019-02-26 13:05:15 +04:00
|
|
|
input: rr,
|
|
|
|
bufInput: bufio.NewReader(rr),
|
|
|
|
nextByte: noValue,
|
|
|
|
lex: newLex(initial),
|
|
|
|
lexeme: bytes.NewBuffer(make([]byte, 0, 100)),
|
|
|
|
}
|
|
|
|
return &l
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) take() int {
|
2019-02-26 13:05:15 +04:00
|
|
|
if l.nextByte == noValue {
|
|
|
|
l.peek()
|
|
|
|
}
|
|
|
|
|
|
|
|
nr := l.nextByte
|
|
|
|
l.nextByte = noValue
|
|
|
|
l.lexeme.WriteByte(byte(nr))
|
|
|
|
return nr
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) takeString() error {
|
2019-02-26 13:05:15 +04:00
|
|
|
cur := l.take()
|
|
|
|
if cur != '"' {
|
2019-10-19 01:17:00 +04:00
|
|
|
return fmt.Errorf("expected \" as start of string instead of %#U", cur)
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var previous byte
|
|
|
|
looper:
|
|
|
|
for {
|
|
|
|
curByte, err := l.bufInput.ReadByte()
|
|
|
|
if err == io.EOF {
|
2019-10-19 01:17:00 +04:00
|
|
|
return errors.New("unexpected EOF in string")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
l.lexeme.WriteByte(curByte)
|
|
|
|
|
|
|
|
if curByte == '"' {
|
|
|
|
if previous != '\\' {
|
|
|
|
break looper
|
|
|
|
} else {
|
|
|
|
curByte, err = l.bufInput.ReadByte()
|
|
|
|
if err == io.EOF {
|
2019-10-19 01:17:00 +04:00
|
|
|
return errors.New("unexpected EOF in string")
|
2019-02-26 13:05:15 +04:00
|
|
|
}
|
|
|
|
l.lexeme.WriteByte(curByte)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previous = curByte
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) peek() int {
|
2019-02-26 13:05:15 +04:00
|
|
|
if l.nextByte != noValue {
|
|
|
|
return l.nextByte
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := l.bufInput.ReadByte()
|
|
|
|
if err == io.EOF {
|
|
|
|
l.nextByte = eof
|
|
|
|
return eof
|
|
|
|
}
|
|
|
|
|
|
|
|
l.nextByte = int(r)
|
|
|
|
return l.nextByte
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) emit(t int) {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.setItem(t, l.pos, l.lexeme.Bytes())
|
|
|
|
l.pos += Pos(l.lexeme.Len())
|
|
|
|
l.hasItem = true
|
|
|
|
|
|
|
|
if t == lexEOF {
|
|
|
|
// Do not capture eof character to match slice_lexer
|
|
|
|
l.item.val = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore whitespace after this token
|
|
|
|
if l.nextByte == noValue {
|
|
|
|
l.peek()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore white space
|
|
|
|
for l.nextByte != eof {
|
|
|
|
if l.nextByte == ' ' || l.nextByte == '\t' || l.nextByte == '\r' || l.nextByte == '\n' {
|
|
|
|
l.pos++
|
|
|
|
r, err := l.bufInput.ReadByte()
|
|
|
|
if err == io.EOF {
|
|
|
|
l.nextByte = eof
|
|
|
|
} else {
|
|
|
|
l.nextByte = int(r)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) setItem(typ int, pos Pos, val []byte) {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.item.typ = typ
|
|
|
|
l.item.pos = pos
|
|
|
|
l.item.val = val
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) ignore() {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.pos += Pos(l.lexeme.Len())
|
|
|
|
l.lexeme.Reset()
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) next() (*Item, bool) {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.lexeme.Reset()
|
|
|
|
for {
|
|
|
|
if l.currentStateFn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
l.currentStateFn = l.currentStateFn(l, &l.stack)
|
|
|
|
|
|
|
|
if l.hasItem {
|
|
|
|
l.hasItem = false
|
|
|
|
return &l.item, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &l.item, false
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) errorf(format string, args ...interface{}) stateFn {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.setItem(lexError, l.pos, []byte(fmt.Sprintf(format, args...)))
|
|
|
|
l.lexeme.Truncate(0)
|
|
|
|
l.hasItem = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-19 01:17:00 +04:00
|
|
|
func (l *ReaderLexer) reset() {
|
2019-02-26 13:05:15 +04:00
|
|
|
l.bufInput.Reset(l.input)
|
|
|
|
l.lexeme.Reset()
|
|
|
|
l.nextByte = noValue
|
|
|
|
l.pos = 0
|
|
|
|
l.lex = newLex(l.initialState)
|
|
|
|
}
|