summaryrefslogtreecommitdiff
path: root/decode_scan.go
blob: 7a5297508d6a968b43322f0db059dbcc27ea84a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"io"

	"git.lukeshu.com/go/lowmemjson/internal/jsonparse"
)

type runeTypeScanner struct {
	// everything that is not "initialized by constructor" starts
	// out as the zero value.

	inner io.RuneScanner // initialized by constructor

	parser jsonparse.Parser // initialized by constructor
	offset int64

	initialized bool
	repeat      bool

	rRune rune
	rSize int
	rType jsonparse.RuneType
	rErr  error
}

func (sc *runeTypeScanner) Reset() {
	sc.parser.Reset()
	if sc.repeat || (sc.rType == jsonparse.RuneTypeEOF && sc.rSize > 0) {
		sc.repeat = false
		// re-figure the rType and rErr
		var err error
		sc.rType, err = sc.parser.HandleRune(sc.rRune)
		if err != nil {
			sc.rErr = &DecodeSyntaxError{
				Offset: sc.offset - int64(sc.rSize),
				Err:    err,
			}
		} else {
			sc.rErr = nil
		}
		// tell it to use that rType and rErr
		_ = sc.UnreadRune() // we set it up to always succeed
	} else {
		sc.initialized = false
	}
}

// The returned error is a *ReadError, a *SyntaxError, or nil.
// An EOF condition is represented as one of:
//
//	end of value but not file:      (_, >0, RuneTypeEOF, nil)
//	end of both value and file:     (_, 0, RuneTypeEOF, nil)
//	end of file in middle of value: (_, 0, RuneTypeError, &DecodeSyntaxError{Offset: offset: Err: io.ErrUnexepctedEOF})
//	end of file at start of value:  (_, 0, RuneTypeError, &DecodeSyntaxError{Offset: offset: Err: io.EOF})
func (sc *runeTypeScanner) ReadRuneType() (rune, int, jsonparse.RuneType, error) {
	switch {
	case sc.initialized && (sc.rType == jsonparse.RuneTypeError || sc.rType == jsonparse.RuneTypeEOF):
		// do nothing
	case sc.repeat:
		_, _, _ = sc.inner.ReadRune()
	default:
		sc.initialized = true
	again:
		var err error
		sc.rRune, sc.rSize, err = sc.inner.ReadRune()
		sc.offset += int64(sc.rSize)
		switch err {
		case nil:
			sc.rType, err = sc.parser.HandleRune(sc.rRune)
			if err != nil {
				sc.rErr = &DecodeSyntaxError{
					Offset: sc.offset - int64(sc.rSize),
					Err:    err,
				}
			} else {
				sc.rErr = nil
			}
			if sc.rType == jsonparse.RuneTypeSpace {
				goto again
			}
		case io.EOF:
			sc.rType, err = sc.parser.HandleEOF()
			if err != nil {
				sc.rErr = &DecodeSyntaxError{
					Offset: sc.offset,
					Err:    err,
				}
			} else {
				sc.rErr = nil
			}
		default:
			sc.rType = 0
			sc.rErr = &DecodeReadError{
				Offset: sc.offset,
				Err:    err,
			}
		}
	}
	sc.repeat = false
	return sc.rRune, sc.rSize, sc.rType, sc.rErr
}

// UnreadRune undoes a call to .ReadRune() or .ReadRuneType().
//
// If the last call to .ReadRune() or .ReadRuneType() has already been
// unread, or if that call returned a rune with size 0, then
// ErrInvalidUnreadRune is returned.  Otherwise, nil is returned.
func (sc *runeTypeScanner) UnreadRune() error {
	if sc.repeat || sc.rSize == 0 {
		return ErrInvalidUnreadRune
	}
	sc.repeat = true
	_ = sc.inner.UnreadRune()
	return nil
}

func (sc *runeTypeScanner) InputOffset() int64 {
	ret := sc.offset
	if sc.repeat {
		ret -= int64(sc.rSize)
	}
	return ret
}

func (sc *runeTypeScanner) PushReadBarrier() {
	sc.parser.PushReadBarrier()
}

func (sc *runeTypeScanner) PopReadBarrier() {
	sc.parser.PopBarrier()
	if sc.repeat || (sc.rType == jsonparse.RuneTypeEOF && sc.rSize > 0) {
		// re-figure the rType and rErr
		var err error
		sc.rType, err = sc.parser.HandleRune(sc.rRune)
		if err != nil {
			sc.rErr = &DecodeSyntaxError{
				Offset: sc.offset - int64(sc.rSize),
				Err:    err,
			}
		} else {
			sc.rErr = nil
		}
		// tell it to use that rType and rErr
		_ = sc.UnreadRune() // we set it up to always succeed
	} else if sc.rType == jsonparse.RuneTypeEOF {
		// re-figure the rType and rErr
		var err error
		sc.rType, err = sc.parser.HandleEOF()
		if err != nil {
			sc.rErr = &DecodeSyntaxError{
				Offset: sc.offset,
				Err:    err,
			}
		} else {
			sc.rErr = nil
		}
	}
}