GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — hujson/parse.go at master · tailscale/hujson Expand file tree/ Copy pathparse.goMore file actions 302 lines (278 loc) · 7.63 KB / Copy pathparse.goFile metadata and controls302 lines (278 loc) · 7.63 KB 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package hujson import ( "bytes" "errors" "fmt" "io" "unicode" "unicode/utf8" ) func lineColumn(b []byte, n int) (line, column int) { line = 1 + bytes.Count(b[:n], []byte("\n")) column = 1 + n - (bytes.LastIndexByte(b[:n], '\n') + len("\n")) return line, column } // Parse parses a HuJSON value as a Value. // Extra and Literal values in v will alias the provided input buffer. func Parse(b []byte) (Value, error) { v, n, err := parseNext(0, b) if err == nil && n < len(b) { err = newInvalidCharacterError(b[n:], "after top-level value") } if err != nil { line, column := lineColumn(b, n) err = fmt.Errorf("hujson: line %d, column %d: %w", line, column, err) return v, err } return v, nil } // parseNext parses the next value with surrounding whitespace and comments. func parseNext(n int, b []byte) (v Value, _ int, err error) { n0 := n // Consume leading whitespace and comments. if n, err = consumeExtra(n, b); err != nil { return v, n, err } if n > n0 { v.BeforeExtra = b[n0:n:n] } // Parse the next value. v.StartOffset = n if v.Value, n, err = parseNextTrimmed(n, b); err != nil { return v, n, err } v.EndOffset = n // Consume trailing whitespace and comments. if n, err = consumeExtra(n, b); err != nil { return v, n, err } if n > v.EndOffset { v.AfterExtra = b[v.EndOffset:n:n] } return v, n, nil } var ( errInvalidObjectEnd = errors.New("invalid character '}' at start of value") errInvalidArrayEnd = errors.New("invalid character ']' at start of value") ) // parseNextTrimmed parses the next value without surrounding whitespace and comments. func parseNextTrimmed(n int, b []byte) (ValueTrimmed, int, error) { if len(b) == n { return nil, n, fmt.Errorf("parsing value: %w", io.ErrUnexpectedEOF) } switch b[n] { // Parse objects. case '{': n++ var obj Object for { var vk, vv Value var err error // Parse the name. if vk, n, err = parseNext(n, b); err != nil { if err == errInvalidObjectEnd && vk.Value == nil { setTrailingComma(&obj, len(obj.Members) > 0) obj.AfterExtra = vk.BeforeExtra return &obj, n + len(`}`), nil } return &obj, n, err } if vk.Value.Kind() != '"' { return &obj, vk.StartOffset, newInvalidCharacterError(b[vk.StartOffset:], "at start of object name") } // Parse the colon. switch { case len(b) == n: return &obj, n, fmt.Errorf("parsing object after name: %w", io.ErrUnexpectedEOF) case b[n] != ':': return &obj, n, newInvalidCharacterError(b[n:], "after object name") } n++ // Parse the value. if vv, n, err = parseNext(n, b); err != nil { return &obj, n, err } obj.Members = append(obj.Members, ObjectMember{vk, vv}) switch { case len(b) == n: return &obj, n, fmt.Errorf("parsing object after value: %w", io.ErrUnexpectedEOF) case b[n] == ',': n++ case b[n] == '}': // Move AfterExtra from last value to AfterExtra of the object. obj.AfterExtra = obj.Members[len(obj.Members)-1].Value.AfterExtra obj.Members[len(obj.Members)-1].Value.AfterExtra = nil return &obj, n + len(`}`), nil default: return &obj, n, newInvalidCharacterError(b[n:], "after object value (expecting ',' or '}')") } } case '}': return nil, n, errInvalidObjectEnd // Parse arrays. case '[': n++ var arr Array for { var v Value var err error if v, n, err = parseNext(n, b); err != nil { if err == errInvalidArrayEnd && v.Value == nil { setTrailingComma(&arr, len(arr.Elements) > 0) arr.AfterExtra = v.BeforeExtra return &arr, n + len(`]`), nil } return &arr, n, err } arr.Elements = append(arr.Elements, v) switch { case len(b) == n: return &arr, n, fmt.Errorf("parsing array after value: %w", io.ErrUnexpectedEOF) case b[n] == ',': n++ case b[n] == ']': // Move AfterExtra from last value to AfterExtra of the array. arr.AfterExtra = arr.Elements[len(arr.Elements)-1].AfterExtra arr.Elements[len(arr.Elements)-1].AfterExtra = nil return &arr, n + len(`]`), nil default: return &arr, n, newInvalidCharacterError(b[n:], "after array value (expecting ',' or ']')") } } case ']': return nil, n, errInvalidArrayEnd // Parse strings. case '"': n0 := n n++ var inEscape bool for { switch { case len(b) == n: return nil, n, fmt.Errorf("parsing string: %w", io.ErrUnexpectedEOF) case inEscape: inEscape = false case b[n] == '\\': inEscape = true case b[n] == '"': n++ lit := Literal(b[n0:n:n]) if !lit.IsValid() { return nil, n0, fmt.Errorf("invalid literal: %s", lit) } return lit, n, nil } n++ } // Parse null, booleans, and numbers. default: n0 := n for len(b) > n && (b[n] == '-' || b[n] == '+' || b[n] == '.' || ('a' <= b[n] && b[n] <= 'z') || ('A' <= b[n] && b[n] <= 'Z') || ('0' <= b[n] && b[n] <= '9')) { n++ } switch lit := Literal(b[n0:n:n]); { case len(lit) == 0: return nil, n0, newInvalidCharacterError(b[n0:], "at start of value") case !lit.IsValid(): return nil, n0, fmt.Errorf("invalid literal: %s", lit) default: return lit, n, nil } } } var ( lineCommentStart = []byte("//") lineCommentEnd = []byte("\n") blockCommentStart = []byte("/*") blockCommentEnd = []byte("*/") ) // consumeExtra consumes leading whitespace and comments. func consumeExtra(n int, b []byte) (int, error) { for len(b) > n { switch b[n] { // Skip past whitespace. case ' ', '\t', '\r', '\n': n += consumeWhitespace(b[n:]) // Skip past comments. case '/': isLineComment := bytes.HasPrefix(b[n:], lineCommentStart) switch nc := consumeComment(b[n:]); { case nc == 0: return n, nil case nc < 0: if isLineComment { if i := indexLineSeparator(b[n:]); i >= 0 { return n + i, newInvalidCharacterError(b[n+i:], "in line comment") } } return n, fmt.Errorf("parsing comment: %w", io.ErrUnexpectedEOF) case !utf8.Valid(b[n : n+nc]): return n, fmt.Errorf("invalid UTF-8 in comment") case isLineComment: if i := indexLineSeparator(b[n : n+nc]); i >= 0 { return n + i, newInvalidCharacterError(b[n+i:], "in line comment") } n += nc default: n += nc } default: return n, nil } } return n, nil } func consumeWhitespace(b []byte) (n int) { for len(b) > n && (b[n] == ' ' || b[n] == '\t' || b[n] == '\r' || b[n] == '\n') { n++ } return n } func indexLineSeparator(b []byte) int { i := bytes.IndexRune(b, '\u2028') j := bytes.IndexRune(b, '\u2029') if i < 0 || (j >= 0 && j < i) { return j } return i } // consumeComment consumes a line or block comment start in b. // It returns the length of the comment if valid, otherwise // it returns 0 if it is not a comment and -1 if it is invalid. func consumeComment(b []byte) (n int) { var start, end []byte switch { case bytes.HasPrefix(b, lineCommentStart): start, end = lineCommentStart, lineCommentEnd case bytes.HasPrefix(b, blockCommentStart): start, end = blockCommentStart, blockCommentEnd default: return 0 } i := bytes.Index(b[len(start):], end) if i < 0 { return -1 } return len(start) + i + len(end) } func newInvalidCharacterError(prefix []byte, where string) error { var what string r, n := utf8.DecodeRune(prefix) switch { case r == utf8.RuneError && n == 1: what = fmt.Sprintf(`'\x%02x'`, prefix[0]) case unicode.IsPrint(r): what = fmt.Sprintf(`%q`, r) case r <= '\uffff': what = fmt.Sprintf(`'\u%04x'`, r) default: what = fmt.Sprintf(`'\U%08x'`, r) } return errors.New("invalid character " + what + " " + where) } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |