gofront 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/README.md +14 -14
  2. package/package.json +10 -38
  3. package/src/backend/js/expressions.js +2060 -389
  4. package/src/backend/js/index.js +586 -174
  5. package/src/backend/js/natives.js +383 -0
  6. package/src/backend/js/runtime.js +936 -1644
  7. package/src/backend/js/statements.js +1349 -132
  8. package/src/backend/js/stdlib/index.js +51 -94
  9. package/src/backend/js/templ.js +67 -118
  10. package/src/backend/js/type-info.js +436 -0
  11. package/src/backend/shared/fixed-float.js +20 -0
  12. package/src/backend/shared/fmt-types.js +80 -0
  13. package/src/backend/shared/host-unicode.js +36 -0
  14. package/src/backend/wasm/boundary.js +753 -872
  15. package/src/backend/wasm/emit-builtins.js +914 -237
  16. package/src/backend/wasm/emit-exprs.js +1456 -195
  17. package/src/backend/wasm/emit-maps.js +78 -380
  18. package/src/backend/wasm/emit-stmts.js +1659 -214
  19. package/src/backend/wasm/emit.js +614 -88
  20. package/src/backend/wasm/encode.js +43 -13
  21. package/src/backend/wasm/globals.js +491 -0
  22. package/src/backend/wasm/glue.js +2 -4
  23. package/src/backend/wasm/index.js +406 -3195
  24. package/src/backend/wasm/method-expr.js +392 -0
  25. package/src/backend/wasm/module-emitter.js +1615 -0
  26. package/src/backend/wasm/monomorph.js +164 -9
  27. package/src/backend/wasm/natives.js +596 -0
  28. package/src/backend/wasm/prescan.js +249 -0
  29. package/src/backend/wasm/prune-funcs.js +89 -0
  30. package/src/backend/wasm/stdlib/index.js +2 -37
  31. package/src/backend/wasm/stdlib/simd.js +293 -28
  32. package/src/backend/wasm/types.js +260 -27
  33. package/src/backend/wasm/variadic.js +93 -0
  34. package/src/backend/wasm/wat.js +47 -20
  35. package/src/{cli-core.js → cli/cli-core.js} +241 -11
  36. package/src/{test-runner.js → cli/test-runner.js} +188 -122
  37. package/src/{vendor.js → cli/vendor.js} +3 -4
  38. package/src/compiler.js +202 -386
  39. package/src/index.js +15 -169
  40. package/src/{lexer.js → lexer/lexer.js} +203 -62
  41. package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
  42. package/src/{tokens.js → lexer/tokens.js} +2 -0
  43. package/src/link/sourcemap.js +97 -0
  44. package/src/link/stdlib.js +737 -0
  45. package/src/link/type-names.js +148 -0
  46. package/src/link/wasm.js +318 -0
  47. package/src/lower/bounds.js +547 -0
  48. package/src/lower/boxing.js +1 -1
  49. package/src/lower/captures.js +267 -64
  50. package/src/lower/embedding.js +32 -3
  51. package/src/lower/errors-as.js +196 -0
  52. package/src/lower/fmt-formatters.js +905 -0
  53. package/src/lower/functions.js +2 -0
  54. package/src/lower/gen-types.js +274 -0
  55. package/src/lower/index.js +33 -19
  56. package/src/lower/json-codecs.js +1379 -0
  57. package/src/lower/ownership.js +47 -8
  58. package/src/lower/range.js +86 -1
  59. package/src/lower/reachability.js +150 -0
  60. package/src/lower/walk.js +12 -0
  61. package/src/parser/declarations.js +83 -15
  62. package/src/parser/expressions.js +110 -18
  63. package/src/parser/index.js +2 -1
  64. package/src/parser/statements.js +38 -22
  65. package/src/{templ-parser.js → parser/templ.js} +179 -17
  66. package/src/parser/types.js +70 -9
  67. package/src/resolver.js +21 -1
  68. package/src/typechecker/assignability.js +291 -19
  69. package/src/typechecker/const-fold.js +399 -0
  70. package/src/typechecker/constants.js +282 -0
  71. package/src/typechecker/expressions.js +1220 -183
  72. package/src/typechecker/index.js +659 -92
  73. package/src/typechecker/init-order.js +426 -0
  74. package/src/typechecker/operand-text.js +40 -0
  75. package/src/typechecker/resolve.js +613 -62
  76. package/src/typechecker/statements.js +212 -20
  77. package/src/typechecker/stdlib/excluded.js +902 -0
  78. package/src/typechecker/stdlib/index.js +38 -0
  79. package/src/typechecker/stdlib/simd.js +65 -15
  80. package/src/typechecker/termination.js +11 -2
  81. package/src/typechecker/types.js +268 -514
  82. package/stdlib/bytes/buffer.go +479 -0
  83. package/stdlib/bytes/bytes.go +1010 -0
  84. package/stdlib/bytes/reader.go +161 -0
  85. package/stdlib/cmp/cmp.go +65 -0
  86. package/stdlib/context/context.go +115 -0
  87. package/stdlib/crypto/rand/rand.go +34 -0
  88. package/stdlib/crypto/sha256/sha256.go +249 -0
  89. package/stdlib/encoding/base64/base64.go +331 -0
  90. package/stdlib/encoding/binary/binary.go +332 -0
  91. package/stdlib/encoding/hex/hex.go +140 -0
  92. package/stdlib/encoding/json/errors.go +31 -0
  93. package/stdlib/encoding/json/indent.go +71 -0
  94. package/stdlib/encoding/json/json.go +62 -0
  95. package/stdlib/encoding/json/number.go +12 -0
  96. package/stdlib/encoding/json/stream.go +173 -0
  97. package/stdlib/errors/errors.go +174 -0
  98. package/stdlib/fmt/errors.go +110 -0
  99. package/stdlib/fmt/format.go +732 -0
  100. package/stdlib/fmt/print.go +1232 -0
  101. package/stdlib/gom/gom.go +293 -0
  102. package/stdlib/hash/hash.go +53 -0
  103. package/stdlib/html/html.go +251 -0
  104. package/stdlib/internal/fmtrt/fmtrt.go +163 -0
  105. package/stdlib/internal/jsonrt/dec.go +871 -0
  106. package/stdlib/internal/jsonrt/enc.go +516 -0
  107. package/stdlib/internal/jsonrt/indent.go +135 -0
  108. package/stdlib/internal/jsonrt/number.go +27 -0
  109. package/stdlib/internal/jsonrt/reader.go +73 -0
  110. package/stdlib/internal/jsonrt/scanner.go +658 -0
  111. package/stdlib/io/io.go +502 -0
  112. package/stdlib/iter/iter.go +18 -0
  113. package/stdlib/maps/maps.go +131 -0
  114. package/stdlib/math/bits/bits.go +507 -0
  115. package/stdlib/math/extra.go +272 -0
  116. package/stdlib/math/math.go +439 -0
  117. package/stdlib/math/rand/rand.go +297 -0
  118. package/stdlib/os/os.go +93 -0
  119. package/stdlib/path/filepath/filepath.go +47 -0
  120. package/stdlib/path/path.go +394 -0
  121. package/stdlib/regexp/regexp.go +231 -0
  122. package/stdlib/slices/slices.go +526 -0
  123. package/stdlib/slices/sort.go +209 -0
  124. package/stdlib/slices/zsort_cmp.go +480 -0
  125. package/stdlib/slices/zsort_ordered.go +482 -0
  126. package/stdlib/sort/sort.go +500 -0
  127. package/stdlib/sort/zsort_func.go +480 -0
  128. package/stdlib/sort/zsort_iface.go +480 -0
  129. package/stdlib/strconv/float.go +1699 -0
  130. package/stdlib/strconv/isprint.go +752 -0
  131. package/stdlib/strconv/quote.go +526 -0
  132. package/stdlib/strconv/strconv.go +616 -0
  133. package/stdlib/strings/builder.go +95 -0
  134. package/stdlib/strings/reader.go +163 -0
  135. package/stdlib/strings/replacer.go +102 -0
  136. package/stdlib/strings/strings.go +807 -0
  137. package/stdlib/sync/sync.go +268 -0
  138. package/stdlib/testing/async_js.go +61 -0
  139. package/stdlib/testing/benchmark.go +297 -0
  140. package/stdlib/testing/testing.go +523 -0
  141. package/stdlib/time/duration.go +233 -0
  142. package/stdlib/time/format.go +745 -0
  143. package/stdlib/time/parse.go +790 -0
  144. package/stdlib/time/sleep_js.go +63 -0
  145. package/stdlib/time/time.go +834 -0
  146. package/stdlib/unicode/unicode.go +250 -0
  147. package/stdlib/unicode/utf8/utf8.go +464 -0
  148. package/src/backend/js/stdlib/base64.js +0 -12
  149. package/src/backend/js/stdlib/binary.js +0 -102
  150. package/src/backend/js/stdlib/bits.js +0 -46
  151. package/src/backend/js/stdlib/builder.js +0 -63
  152. package/src/backend/js/stdlib/bytes.js +0 -122
  153. package/src/backend/js/stdlib/errors.js +0 -44
  154. package/src/backend/js/stdlib/fmt.js +0 -178
  155. package/src/backend/js/stdlib/gom.js +0 -155
  156. package/src/backend/js/stdlib/hex.js +0 -17
  157. package/src/backend/js/stdlib/html.js +0 -20
  158. package/src/backend/js/stdlib/io.js +0 -137
  159. package/src/backend/js/stdlib/json.js +0 -972
  160. package/src/backend/js/stdlib/maps.js +0 -45
  161. package/src/backend/js/stdlib/math.js +0 -69
  162. package/src/backend/js/stdlib/os.js +0 -13
  163. package/src/backend/js/stdlib/path.js +0 -48
  164. package/src/backend/js/stdlib/rand.js +0 -32
  165. package/src/backend/js/stdlib/regexp.js +0 -54
  166. package/src/backend/js/stdlib/sha256.js +0 -12
  167. package/src/backend/js/stdlib/shared.js +0 -17
  168. package/src/backend/js/stdlib/simd.js +0 -24
  169. package/src/backend/js/stdlib/slices.js +0 -77
  170. package/src/backend/js/stdlib/sort.js +0 -30
  171. package/src/backend/js/stdlib/strconv.js +0 -74
  172. package/src/backend/js/stdlib/strings.js +0 -222
  173. package/src/backend/js/stdlib/sync.js +0 -13
  174. package/src/backend/js/stdlib/testing.js +0 -34
  175. package/src/backend/js/stdlib/time.js +0 -128
  176. package/src/backend/js/stdlib/unicode.js +0 -25
  177. package/src/backend/js/stdlib/utf8.js +0 -26
  178. package/src/backend/wasm/const-fold.js +0 -205
  179. package/src/backend/wasm/emit-stdlib.js +0 -13
  180. package/src/backend/wasm/stdlib/bytes.js +0 -5039
  181. package/src/backend/wasm/stdlib/core.js +0 -8903
  182. package/src/backend/wasm/stdlib/io.js +0 -2161
  183. package/src/backend/wasm/stdlib/packages.js +0 -2400
  184. package/src/backend/wasm/stdlib/pure.js +0 -720
  185. package/src/backend/wasm/stdlib/time.js +0 -1200
  186. package/src/typechecker/stdlib/core.js +0 -801
  187. package/src/typechecker/stdlib/extended.js +0 -914
  188. package/src/typechecker/stdlib.js +0 -22
  189. /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
  190. /package/src/{colors.js → cli/colors.js} +0 -0
  191. /package/src/{dev-server.js → cli/dev-server.js} +0 -0
  192. /package/src/{minifier.js → cli/minifier.js} +0 -0
  193. /package/src/{project-config.js → cli/project-config.js} +0 -0
  194. /package/src/{pwa.js → cli/pwa.js} +0 -0
@@ -0,0 +1,658 @@
1
+ // Copyright 2010 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build gofront
6
+
7
+ //gofront:target both
8
+
9
+ // Package jsonrt is the runtime behind encoding/json in wasm packages: the
10
+ // JSON scanner (adapted from the Go standard library), the encoder state and
11
+ // the decoder state that the per-type codecs generated by the compiler call.
12
+ package jsonrt
13
+
14
+ import (
15
+ "io"
16
+ "strconv"
17
+ )
18
+
19
+ // Valid reports whether data is a valid JSON encoding.
20
+ func Valid(data []byte) bool {
21
+ scan := newScanner()
22
+ return checkValid(data, scan) == nil
23
+ }
24
+
25
+ // checkValid verifies that data is valid JSON-encoded data.
26
+ // scan is passed in for use by checkValid to avoid an allocation.
27
+ // checkValid returns nil or a SyntaxError.
28
+ func checkValid(data []byte, scan *scanner) error {
29
+ scan.reset()
30
+ for _, c := range data {
31
+ scan.bytes++
32
+ if scan.step(scan, c) == scanError {
33
+ return scan.err
34
+ }
35
+ }
36
+ if scan.eof() == scanError {
37
+ return scan.err
38
+ }
39
+ return nil
40
+ }
41
+
42
+ // A SyntaxError is a description of a JSON syntax error.
43
+ // [Unmarshal] will return a SyntaxError if the JSON can't be parsed.
44
+ type SyntaxError struct {
45
+ msg string // description of error
46
+ Offset int64 // error occurred after reading Offset bytes
47
+ }
48
+
49
+ func (e *SyntaxError) Error() string { return e.msg }
50
+
51
+ // A scanner is a JSON scanning state machine.
52
+ // Callers call scan.reset and then pass bytes in one at a time
53
+ // by calling scan.step(&scan, c) for each byte.
54
+ // The return value, referred to as an opcode, tells the
55
+ // caller about significant parsing events like beginning
56
+ // and ending literals, objects, and arrays, so that the
57
+ // caller can follow along if it wishes.
58
+ // The return value scanEnd indicates that a single top-level
59
+ // JSON value has been completed, *before* the byte that
60
+ // just got passed in. (The indication must be delayed in order
61
+ // to recognize the end of numbers: is 123 a whole value or
62
+ // the beginning of 12345e+6?).
63
+ type scanner struct {
64
+ // The step is a func to be called to execute the next transition.
65
+ // Also tried using an integer constant and a single func
66
+ // with a switch, but using the func directly was 10% faster
67
+ // on a 64-bit Mac Mini, and it's nicer to read.
68
+ step func(*scanner, byte) int
69
+
70
+ // Reached end of top-level value.
71
+ endTop bool
72
+
73
+ // Stack of what we're in the middle of - array values, object keys, object values.
74
+ // Only the first parseStateLen entries are live; the rest is reused room.
75
+ parseState []int
76
+ parseStateLen int
77
+
78
+ // Error that happened, if any.
79
+ err error
80
+
81
+ // total bytes consumed, updated by decoder.Decode (and deliberately
82
+ // not set to zero by scan.reset)
83
+ bytes int64
84
+ }
85
+
86
+ func newScanner() *scanner {
87
+ scan := &scanner{}
88
+ scan.reset()
89
+ return scan
90
+ }
91
+
92
+ // These values are returned by the state transition functions
93
+ // assigned to scanner.state and the method scanner.eof.
94
+ // They give details about the current state of the scan that
95
+ // callers might be interested to know about.
96
+ // It is okay to ignore the return value of any particular
97
+ // call to scanner.state: if one call returns scanError,
98
+ // every subsequent call will return scanError too.
99
+ const (
100
+ // Continue.
101
+ scanContinue = iota // uninteresting byte
102
+ scanBeginLiteral // end implied by next result != scanContinue
103
+ scanBeginObject // begin object
104
+ scanObjectKey // just finished object key (string)
105
+ scanObjectValue // just finished non-last object value
106
+ scanEndObject // end object (implies scanObjectValue if possible)
107
+ scanBeginArray // begin array
108
+ scanArrayValue // just finished array value
109
+ scanEndArray // end array (implies scanArrayValue if possible)
110
+ scanSkipSpace // space byte; can skip; known to be last "continue" result
111
+
112
+ // Stop.
113
+ scanEnd // top-level value ended *before* this byte; known to be first "stop" result
114
+ scanError // hit an error, scanner.err.
115
+ )
116
+
117
+ // These values are stored in the parseState stack.
118
+ // They give the current state of a composite value
119
+ // being scanned. If the parser is inside a nested value
120
+ // the parseState describes the nested state, outermost at entry 0.
121
+ const (
122
+ parseObjectKey = iota // parsing object key (before colon)
123
+ parseObjectValue // parsing object value (after colon)
124
+ parseArrayValue // parsing array value
125
+ )
126
+
127
+ // This limits the max nesting depth to prevent stack overflow.
128
+ // This is permitted by https://tools.ietf.org/html/rfc7159#section-9
129
+ const maxNestingDepth = 10000
130
+
131
+ // reset prepares the scanner for use.
132
+ // It must be called before calling s.step.
133
+ func (s *scanner) reset() {
134
+ s.step = stateBeginValue
135
+ s.parseStateLen = 0
136
+ s.err = nil
137
+ s.endTop = false
138
+ }
139
+
140
+ // eof tells the scanner that the end of input has been reached.
141
+ // It returns a scan status just as s.step does.
142
+ func (s *scanner) eof() int {
143
+ if s.err != nil {
144
+ return scanError
145
+ }
146
+ if s.endTop {
147
+ return scanEnd
148
+ }
149
+ s.step(s, ' ')
150
+ if s.endTop {
151
+ return scanEnd
152
+ }
153
+ if s.err == nil {
154
+ s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
155
+ }
156
+ return scanError
157
+ }
158
+
159
+ // pushParseState pushes a new parse state newParseState onto the parse stack.
160
+ // an error state is returned if maxNestingDepth was exceeded, otherwise successState is returned.
161
+ func (s *scanner) pushParseState(c byte, newParseState int, successState int) int {
162
+ if s.parseStateLen < len(s.parseState) {
163
+ s.parseState[s.parseStateLen] = newParseState
164
+ } else {
165
+ s.parseState = append(s.parseState, newParseState)
166
+ }
167
+ s.parseStateLen++
168
+ if s.parseStateLen <= maxNestingDepth {
169
+ return successState
170
+ }
171
+ return s.error(c, "exceeded max depth")
172
+ }
173
+
174
+ // popParseState pops a parse state (already obtained) off the stack
175
+ // and updates s.step accordingly.
176
+ func (s *scanner) popParseState() {
177
+ s.parseStateLen--
178
+ n := s.parseStateLen
179
+ if n == 0 {
180
+ s.step = stateEndTop
181
+ s.endTop = true
182
+ } else {
183
+ s.step = stateEndValue
184
+ }
185
+ }
186
+
187
+ func isSpace(c byte) bool {
188
+ return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n')
189
+ }
190
+
191
+ // stateBeginValueOrEmpty is the state after reading `[`.
192
+ func stateBeginValueOrEmpty(s *scanner, c byte) int {
193
+ if isSpace(c) {
194
+ return scanSkipSpace
195
+ }
196
+ if c == ']' {
197
+ return stateEndValue(s, c)
198
+ }
199
+ return stateBeginValue(s, c)
200
+ }
201
+
202
+ // stateBeginValue is the state at the beginning of the input.
203
+ func stateBeginValue(s *scanner, c byte) int {
204
+ if isSpace(c) {
205
+ return scanSkipSpace
206
+ }
207
+ switch c {
208
+ case '{':
209
+ s.step = stateBeginStringOrEmpty
210
+ return s.pushParseState(c, parseObjectKey, scanBeginObject)
211
+ case '[':
212
+ s.step = stateBeginValueOrEmpty
213
+ return s.pushParseState(c, parseArrayValue, scanBeginArray)
214
+ case '"':
215
+ s.step = stateInString
216
+ return scanBeginLiteral
217
+ case '-':
218
+ s.step = stateNeg
219
+ return scanBeginLiteral
220
+ case '0': // beginning of 0.123
221
+ s.step = state0
222
+ return scanBeginLiteral
223
+ case 't': // beginning of true
224
+ s.step = stateT
225
+ return scanBeginLiteral
226
+ case 'f': // beginning of false
227
+ s.step = stateF
228
+ return scanBeginLiteral
229
+ case 'n': // beginning of null
230
+ s.step = stateN
231
+ return scanBeginLiteral
232
+ }
233
+ if '1' <= c && c <= '9' { // beginning of 1234.5
234
+ s.step = state1
235
+ return scanBeginLiteral
236
+ }
237
+ return s.error(c, "looking for beginning of value")
238
+ }
239
+
240
+ // stateBeginStringOrEmpty is the state after reading `{`.
241
+ func stateBeginStringOrEmpty(s *scanner, c byte) int {
242
+ if isSpace(c) {
243
+ return scanSkipSpace
244
+ }
245
+ if c == '}' {
246
+ n := s.parseStateLen
247
+ s.parseState[n-1] = parseObjectValue
248
+ return stateEndValue(s, c)
249
+ }
250
+ return stateBeginString(s, c)
251
+ }
252
+
253
+ // stateBeginString is the state after reading `{"key": value,`.
254
+ func stateBeginString(s *scanner, c byte) int {
255
+ if isSpace(c) {
256
+ return scanSkipSpace
257
+ }
258
+ if c == '"' {
259
+ s.step = stateInString
260
+ return scanBeginLiteral
261
+ }
262
+ return s.error(c, "looking for beginning of object key string")
263
+ }
264
+
265
+ // stateEndValue is the state after completing a value,
266
+ // such as after reading `{}` or `true` or `["x"`.
267
+ func stateEndValue(s *scanner, c byte) int {
268
+ n := s.parseStateLen
269
+ if n == 0 {
270
+ // Completed top-level before the current byte.
271
+ s.step = stateEndTop
272
+ s.endTop = true
273
+ return stateEndTop(s, c)
274
+ }
275
+ if isSpace(c) {
276
+ s.step = stateEndValue
277
+ return scanSkipSpace
278
+ }
279
+ ps := s.parseState[n-1]
280
+ switch ps {
281
+ case parseObjectKey:
282
+ if c == ':' {
283
+ s.parseState[n-1] = parseObjectValue
284
+ s.step = stateBeginValue
285
+ return scanObjectKey
286
+ }
287
+ return s.error(c, "after object key")
288
+ case parseObjectValue:
289
+ if c == ',' {
290
+ s.parseState[n-1] = parseObjectKey
291
+ s.step = stateBeginString
292
+ return scanObjectValue
293
+ }
294
+ if c == '}' {
295
+ s.popParseState()
296
+ return scanEndObject
297
+ }
298
+ return s.error(c, "after object key:value pair")
299
+ case parseArrayValue:
300
+ if c == ',' {
301
+ s.step = stateBeginValue
302
+ return scanArrayValue
303
+ }
304
+ if c == ']' {
305
+ s.popParseState()
306
+ return scanEndArray
307
+ }
308
+ return s.error(c, "after array element")
309
+ }
310
+ return s.error(c, "")
311
+ }
312
+
313
+ // stateEndTop is the state after finishing the top-level value,
314
+ // such as after reading `{}` or `[1,2,3]`.
315
+ // Only space characters should be seen now.
316
+ func stateEndTop(s *scanner, c byte) int {
317
+ if !isSpace(c) {
318
+ // Complain about non-space byte on next call.
319
+ s.error(c, "after top-level value")
320
+ }
321
+ return scanEnd
322
+ }
323
+
324
+ // stateInString is the state after reading `"`.
325
+ func stateInString(s *scanner, c byte) int {
326
+ if c == '"' {
327
+ s.step = stateEndValue
328
+ return scanContinue
329
+ }
330
+ if c == '\\' {
331
+ s.step = stateInStringEsc
332
+ return scanContinue
333
+ }
334
+ if c < 0x20 {
335
+ return s.error(c, "in string literal")
336
+ }
337
+ return scanContinue
338
+ }
339
+
340
+ // stateInStringEsc is the state after reading `"\` during a quoted string.
341
+ func stateInStringEsc(s *scanner, c byte) int {
342
+ switch c {
343
+ case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
344
+ s.step = stateInString
345
+ return scanContinue
346
+ case 'u':
347
+ s.step = stateInStringEscU
348
+ return scanContinue
349
+ }
350
+ return s.error(c, "in string escape code")
351
+ }
352
+
353
+ // stateInStringEscU is the state after reading `"\u` during a quoted string.
354
+ func stateInStringEscU(s *scanner, c byte) int {
355
+ if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
356
+ s.step = stateInStringEscU1
357
+ return scanContinue
358
+ }
359
+ // numbers
360
+ return s.error(c, "in \\u hexadecimal character escape")
361
+ }
362
+
363
+ // stateInStringEscU1 is the state after reading `"\u1` during a quoted string.
364
+ func stateInStringEscU1(s *scanner, c byte) int {
365
+ if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
366
+ s.step = stateInStringEscU12
367
+ return scanContinue
368
+ }
369
+ // numbers
370
+ return s.error(c, "in \\u hexadecimal character escape")
371
+ }
372
+
373
+ // stateInStringEscU12 is the state after reading `"\u12` during a quoted string.
374
+ func stateInStringEscU12(s *scanner, c byte) int {
375
+ if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
376
+ s.step = stateInStringEscU123
377
+ return scanContinue
378
+ }
379
+ // numbers
380
+ return s.error(c, "in \\u hexadecimal character escape")
381
+ }
382
+
383
+ // stateInStringEscU123 is the state after reading `"\u123` during a quoted string.
384
+ func stateInStringEscU123(s *scanner, c byte) int {
385
+ if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
386
+ s.step = stateInString
387
+ return scanContinue
388
+ }
389
+ // numbers
390
+ return s.error(c, "in \\u hexadecimal character escape")
391
+ }
392
+
393
+ // stateNeg is the state after reading `-` during a number.
394
+ func stateNeg(s *scanner, c byte) int {
395
+ if c == '0' {
396
+ s.step = state0
397
+ return scanContinue
398
+ }
399
+ if '1' <= c && c <= '9' {
400
+ s.step = state1
401
+ return scanContinue
402
+ }
403
+ return s.error(c, "in numeric literal")
404
+ }
405
+
406
+ // state1 is the state after reading a non-zero integer during a number,
407
+ // such as after reading `1` or `100` but not `0`.
408
+ func state1(s *scanner, c byte) int {
409
+ if '0' <= c && c <= '9' {
410
+ s.step = state1
411
+ return scanContinue
412
+ }
413
+ return state0(s, c)
414
+ }
415
+
416
+ // state0 is the state after reading `0` during a number.
417
+ func state0(s *scanner, c byte) int {
418
+ if c == '.' {
419
+ s.step = stateDot
420
+ return scanContinue
421
+ }
422
+ if c == 'e' || c == 'E' {
423
+ s.step = stateE
424
+ return scanContinue
425
+ }
426
+ return stateEndValue(s, c)
427
+ }
428
+
429
+ // stateDot is the state after reading the integer and decimal point in a number,
430
+ // such as after reading `1.`.
431
+ func stateDot(s *scanner, c byte) int {
432
+ if '0' <= c && c <= '9' {
433
+ s.step = stateDot0
434
+ return scanContinue
435
+ }
436
+ return s.error(c, "after decimal point in numeric literal")
437
+ }
438
+
439
+ // stateDot0 is the state after reading the integer, decimal point, and subsequent
440
+ // digits of a number, such as after reading `3.14`.
441
+ func stateDot0(s *scanner, c byte) int {
442
+ if '0' <= c && c <= '9' {
443
+ return scanContinue
444
+ }
445
+ if c == 'e' || c == 'E' {
446
+ s.step = stateE
447
+ return scanContinue
448
+ }
449
+ return stateEndValue(s, c)
450
+ }
451
+
452
+ // stateE is the state after reading the mantissa and e in a number,
453
+ // such as after reading `314e` or `0.314e`.
454
+ func stateE(s *scanner, c byte) int {
455
+ if c == '+' || c == '-' {
456
+ s.step = stateESign
457
+ return scanContinue
458
+ }
459
+ return stateESign(s, c)
460
+ }
461
+
462
+ // stateESign is the state after reading the mantissa, e, and sign in a number,
463
+ // such as after reading `314e-` or `0.314e+`.
464
+ func stateESign(s *scanner, c byte) int {
465
+ if '0' <= c && c <= '9' {
466
+ s.step = stateE0
467
+ return scanContinue
468
+ }
469
+ return s.error(c, "in exponent of numeric literal")
470
+ }
471
+
472
+ // stateE0 is the state after reading the mantissa, e, optional sign,
473
+ // and at least one digit of the exponent in a number,
474
+ // such as after reading `314e-2` or `0.314e+1` or `3.14e0`.
475
+ func stateE0(s *scanner, c byte) int {
476
+ if '0' <= c && c <= '9' {
477
+ return scanContinue
478
+ }
479
+ return stateEndValue(s, c)
480
+ }
481
+
482
+ // stateT is the state after reading `t`.
483
+ func stateT(s *scanner, c byte) int {
484
+ if c == 'r' {
485
+ s.step = stateTr
486
+ return scanContinue
487
+ }
488
+ return s.error(c, "in literal true (expecting 'r')")
489
+ }
490
+
491
+ // stateTr is the state after reading `tr`.
492
+ func stateTr(s *scanner, c byte) int {
493
+ if c == 'u' {
494
+ s.step = stateTru
495
+ return scanContinue
496
+ }
497
+ return s.error(c, "in literal true (expecting 'u')")
498
+ }
499
+
500
+ // stateTru is the state after reading `tru`.
501
+ func stateTru(s *scanner, c byte) int {
502
+ if c == 'e' {
503
+ s.step = stateEndValue
504
+ return scanContinue
505
+ }
506
+ return s.error(c, "in literal true (expecting 'e')")
507
+ }
508
+
509
+ // stateF is the state after reading `f`.
510
+ func stateF(s *scanner, c byte) int {
511
+ if c == 'a' {
512
+ s.step = stateFa
513
+ return scanContinue
514
+ }
515
+ return s.error(c, "in literal false (expecting 'a')")
516
+ }
517
+
518
+ // stateFa is the state after reading `fa`.
519
+ func stateFa(s *scanner, c byte) int {
520
+ if c == 'l' {
521
+ s.step = stateFal
522
+ return scanContinue
523
+ }
524
+ return s.error(c, "in literal false (expecting 'l')")
525
+ }
526
+
527
+ // stateFal is the state after reading `fal`.
528
+ func stateFal(s *scanner, c byte) int {
529
+ if c == 's' {
530
+ s.step = stateFals
531
+ return scanContinue
532
+ }
533
+ return s.error(c, "in literal false (expecting 's')")
534
+ }
535
+
536
+ // stateFals is the state after reading `fals`.
537
+ func stateFals(s *scanner, c byte) int {
538
+ if c == 'e' {
539
+ s.step = stateEndValue
540
+ return scanContinue
541
+ }
542
+ return s.error(c, "in literal false (expecting 'e')")
543
+ }
544
+
545
+ // stateN is the state after reading `n`.
546
+ func stateN(s *scanner, c byte) int {
547
+ if c == 'u' {
548
+ s.step = stateNu
549
+ return scanContinue
550
+ }
551
+ return s.error(c, "in literal null (expecting 'u')")
552
+ }
553
+
554
+ // stateNu is the state after reading `nu`.
555
+ func stateNu(s *scanner, c byte) int {
556
+ if c == 'l' {
557
+ s.step = stateNul
558
+ return scanContinue
559
+ }
560
+ return s.error(c, "in literal null (expecting 'l')")
561
+ }
562
+
563
+ // stateNul is the state after reading `nul`.
564
+ func stateNul(s *scanner, c byte) int {
565
+ if c == 'l' {
566
+ s.step = stateEndValue
567
+ return scanContinue
568
+ }
569
+ return s.error(c, "in literal null (expecting 'l')")
570
+ }
571
+
572
+ // stateError is the state after reaching a syntax error,
573
+ // such as after reading `[1}` or `5.1.2`.
574
+ func stateError(s *scanner, c byte) int {
575
+ return scanError
576
+ }
577
+
578
+ // error records an error and switches to the error state.
579
+ func (s *scanner) error(c byte, context string) int {
580
+ s.step = stateError
581
+ s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes}
582
+ return scanError
583
+ }
584
+
585
+ // quoteChar formats c as a quoted character literal.
586
+ func quoteChar(c byte) string {
587
+ // special cases - different from quoted strings
588
+ if c == '\'' {
589
+ return `'\''`
590
+ }
591
+ if c == '"' {
592
+ return `'"'`
593
+ }
594
+
595
+ // use quoted string with different quotation marks
596
+ s := strconv.Quote(string(c))
597
+ return "'" + s[1:len(s)-1] + "'"
598
+ }
599
+
600
+ // CheckValid returns nil when data is one valid JSON value, else the
601
+ // *SyntaxError describing the first problem.
602
+ //
603
+ //gofront:native jsonrt.CheckValid
604
+ func CheckValid(data []byte) error {
605
+ return checkValid(data, newScanner())
606
+ }
607
+
608
+ // ScanNext reads and returns the bytes of the next single JSON value from r,
609
+ // along with the number of leading whitespace bytes that were skipped.
610
+ // If EOF is encountered before any non-whitespace bytes, it returns io.EOF.
611
+ // If the stream ends before the JSON value completes, it returns io.ErrUnexpectedEOF.
612
+ func ScanNext(r *Reader) ([]byte, int64, error) {
613
+ var skipped int64
614
+ var c byte
615
+ for {
616
+ b, err := r.ReadByte()
617
+ if err != nil {
618
+ return nil, skipped, err
619
+ }
620
+ if isSpace(b) {
621
+ skipped++
622
+ continue
623
+ }
624
+ c = b
625
+ break
626
+ }
627
+
628
+ scan := newScanner()
629
+ buf := []byte{c}
630
+ scan.bytes++
631
+ if op := scan.step(scan, c); op == scanError {
632
+ return nil, skipped, scan.err
633
+ }
634
+
635
+ for {
636
+ b, err := r.ReadByte()
637
+ if err == io.EOF {
638
+ op := scan.eof()
639
+ if op == scanEnd {
640
+ return buf, skipped, nil
641
+ }
642
+ return nil, skipped, io.ErrUnexpectedEOF
643
+ }
644
+ if err != nil {
645
+ return nil, skipped, err
646
+ }
647
+ scan.bytes++
648
+ op := scan.step(scan, b)
649
+ if op == scanEnd {
650
+ r.UnreadByte()
651
+ return buf, skipped, nil
652
+ }
653
+ if op == scanError {
654
+ return nil, skipped, scan.err
655
+ }
656
+ buf = append(buf, b)
657
+ }
658
+ }