2022-07-10 00:48:38 +01:00
|
|
|
package asld
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
)
|
2022-07-10 00:48:38 +01:00
|
|
|
|
|
|
|
// Walker.... texas ranger.
|
|
|
|
// Except he's a cringe conservative.
|
|
|
|
//
|
|
|
|
// This is also cringe but not for cringe reasons.
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
type symbol byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
symbolOpenParen symbol = iota
|
|
|
|
symbolClosedParen
|
|
|
|
symbolOpenArray
|
|
|
|
symbolClosedArray
|
|
|
|
symbolString
|
|
|
|
symbolColon
|
|
|
|
symbolNullStart
|
|
|
|
symbolEOB // End-Of-Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusOK status = iota
|
|
|
|
statusError
|
|
|
|
statusWalkerNotAffected
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
stoppable = []SymbolInfo{
|
|
|
|
symbolOpenParen: {
|
|
|
|
self: '{',
|
|
|
|
closer: '}',
|
|
|
|
},
|
|
|
|
symbolClosedParen: {
|
|
|
|
self: '}',
|
|
|
|
},
|
|
|
|
symbolOpenArray: {
|
|
|
|
self: '[',
|
|
|
|
closer: ']',
|
|
|
|
},
|
|
|
|
symbolClosedArray: {
|
|
|
|
self: ']',
|
|
|
|
},
|
|
|
|
symbolString: {
|
|
|
|
self: '"',
|
|
|
|
closer: '"',
|
|
|
|
},
|
|
|
|
symbolColon: {
|
|
|
|
self: ':',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type status int
|
|
|
|
type walkerStatus struct {
|
|
|
|
walker
|
|
|
|
status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) Reset() walker {
|
|
|
|
w.position = 0
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) Debug() {
|
2022-07-10 00:48:38 +01:00
|
|
|
for index, b := range w.content {
|
|
|
|
out := string(b)
|
|
|
|
if w.position == index {
|
2022-07-10 21:23:00 +01:00
|
|
|
out = "<[_" + out + "_]>"
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
print(out)
|
|
|
|
}
|
|
|
|
print("\n")
|
2022-07-10 21:23:00 +01:00
|
|
|
print("globPos", w.globPos)
|
|
|
|
print("\n")
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) SliceInner() (walker, bool) {
|
|
|
|
// the !ok scenario here is only if the code is bad
|
2022-07-10 21:23:00 +01:00
|
|
|
s, ok := stoppableByByte[w.Current()]
|
2022-07-10 00:48:38 +01:00
|
|
|
// Debug
|
|
|
|
if !ok {
|
|
|
|
panic(w)
|
|
|
|
}
|
|
|
|
var height uint
|
|
|
|
for pos := w.position + 1; pos < w.len; pos++ {
|
|
|
|
curr := w.content[pos]
|
|
|
|
if curr == s.self && s.self != s.closer {
|
|
|
|
height++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if curr == s.closer {
|
|
|
|
if height == 0 {
|
2022-07-10 21:23:00 +01:00
|
|
|
return w.Between(w.position+1, pos), ok
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
height--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w, false
|
|
|
|
}
|
|
|
|
|
|
|
|
type walker struct {
|
|
|
|
content []byte
|
|
|
|
len int
|
|
|
|
position int
|
2022-07-10 21:23:00 +01:00
|
|
|
globPos int
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWalker(data []byte) walker {
|
|
|
|
return walker{
|
|
|
|
content: data,
|
|
|
|
len: len(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
func (w walker) Between(lower, upper int) walker {
|
|
|
|
// As lower, and upper, are offsets from the beginning
|
|
|
|
// of this walker's buffer, we can diff lower and w.position
|
|
|
|
// for an offset to set global position at
|
|
|
|
w.content = w.content[lower:upper]
|
|
|
|
offset := lower - w.position
|
|
|
|
w.position = 0
|
|
|
|
w.globPos += offset
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub returns a subwalker from the current position
|
|
|
|
func (w walker) Sub() walker {
|
|
|
|
w.content = w.content[w.position:]
|
|
|
|
w.len = len(w.content)
|
|
|
|
w.position = 0
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-07-10 00:48:38 +01:00
|
|
|
// Until returns a subwalker from position 0 to the current position
|
|
|
|
func (w walker) Until() walker {
|
|
|
|
w.content = w.content[:w.position]
|
|
|
|
w.len = len(w.content)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
func (w walker) Pos(v int) walker {
|
|
|
|
offset := v - w.position
|
|
|
|
w.position = v
|
|
|
|
w.globPos += offset
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-07-10 00:48:38 +01:00
|
|
|
// ToOrStay will stay at where the walker is if b is the
|
|
|
|
// same as the current walker position.
|
|
|
|
//
|
|
|
|
// Otherwise, calls To
|
|
|
|
func (w walker) ToOrStay(b byte) (walker, bool) {
|
|
|
|
if w.Current() == b {
|
|
|
|
return w, true
|
|
|
|
}
|
|
|
|
return w.To(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) To(b byte) (walker, bool) {
|
|
|
|
for pos := w.position + 1; pos < w.len; pos++ {
|
|
|
|
if w.content[pos] == b {
|
2022-07-10 21:23:00 +01:00
|
|
|
return w.Pos(pos), true
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w, false
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
func (w walker) WalkThroughSpaces() walker {
|
|
|
|
for pos := w.position; pos < w.len; pos++ {
|
|
|
|
b := w.content[pos]
|
|
|
|
if _, ok := stoppableByByte[b]; ok || (b >= '0' && b <= '9') || b == '-' || w.IsNullAt(pos) {
|
|
|
|
return w.Pos(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) StayOrNext() (walker, symbol) {
|
|
|
|
if sym, ok := stoppableByByte[w.content[w.position]]; ok {
|
|
|
|
return w, sym.enum
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) Next() (walker, symbol) {
|
|
|
|
w, s := w.ToNext()
|
|
|
|
return w.Sub(), s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) IsNullAt(pos int) bool {
|
|
|
|
return w.content[pos] == 'n' && w.len-pos >= 4 && bytes.Equal(w.content[pos:pos+4], null)
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
func (w walker) ToNext() (walker, symbol) {
|
2022-07-10 00:48:38 +01:00
|
|
|
for pos := w.position + 1; pos < w.len; pos++ {
|
2022-07-10 21:23:00 +01:00
|
|
|
if w.IsNullAt(pos) {
|
|
|
|
return w.Pos(pos), symbolNullStart
|
|
|
|
}
|
|
|
|
if s, ok := stoppableByByte[w.content[pos]]; ok {
|
|
|
|
return w.Pos(pos), s.enum
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-10 21:23:00 +01:00
|
|
|
return w, symbolEOB
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommaOrEnd walks to the next comma, or, if none
|
|
|
|
// is present in the current walker scope, returns
|
|
|
|
// itself with status statusWalkerNotAffected.
|
|
|
|
func (w walker) CommaOrEnd() (walkerStatus, error) {
|
|
|
|
if w.Current() == ',' {
|
2022-07-10 21:23:00 +01:00
|
|
|
w = w.Pos(w.position + 1)
|
2022-07-10 00:48:38 +01:00
|
|
|
}
|
|
|
|
for pos := w.position; pos < w.len; pos++ {
|
|
|
|
if in(openRaw, w.content[pos]) {
|
2022-07-10 21:23:00 +01:00
|
|
|
sb, ok := stoppableByByte[w.content[pos]]
|
2022-07-10 00:48:38 +01:00
|
|
|
if !ok {
|
|
|
|
panic("ok someone fucked up somewhere")
|
|
|
|
}
|
|
|
|
w, ok = w.To(sb.closer)
|
|
|
|
if !ok {
|
|
|
|
return walkerStatus{
|
|
|
|
status: statusError,
|
|
|
|
walker: w,
|
|
|
|
}, fmt.Errorf("%w %s", ErrNoMatching, string(sb.closer))
|
|
|
|
}
|
|
|
|
pos = w.position
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if w.content[pos] == ',' {
|
|
|
|
return walkerStatus{
|
|
|
|
status: statusOK,
|
2022-07-10 21:23:00 +01:00
|
|
|
walker: w.Pos(pos),
|
2022-07-10 00:48:38 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return walkerStatus{
|
|
|
|
status: statusWalkerNotAffected,
|
|
|
|
walker: w,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) Current() byte {
|
|
|
|
return w.content[w.position]
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:23:00 +01:00
|
|
|
func (w walker) CurrentSymbol() (SymbolInfo, bool) {
|
|
|
|
b, ok := stoppableByByte[w.Current()]
|
2022-07-10 00:48:38 +01:00
|
|
|
return b, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w walker) String() string {
|
|
|
|
if w.Current() == '"' {
|
|
|
|
w, _ = w.SliceInner()
|
|
|
|
}
|
|
|
|
return string(w.content)
|
|
|
|
}
|