| 1 |
// Copyright 2013-2024 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Customized heavily from |
| 15 |
// https://github.com/BurntSushi/toml/blob/master/lex.go, which is based on |
| 16 |
// Rob Pike's talk: http://cuddle.googlecode.com/hg/talk/lex.html |
| 17 |
|
| 18 |
// The format supported is less restrictive than today's formats. |
| 19 |
// Supports mixed Arrays [], nested Maps {}, multiple comment types (# and //) |
| 20 |
// Also supports key value assignments using '=' or ':' or whiteSpace() |
| 21 |
// e.g. foo = 2, foo : 2, foo 2 |
| 22 |
// maps can be assigned with no key separator as well |
| 23 |
// semicolons as value terminators in key/value assignments are optional |
| 24 |
// |
| 25 |
// see lex_test.go for more examples. |
| 26 |
|
| 27 |
package conf |
| 28 |
|
| 29 |
import ( |
| 30 |
"encoding/hex" |
| 31 |
"fmt" |
| 32 |
"strings" |
| 33 |
"unicode" |
| 34 |
"unicode/utf8" |
| 35 |
) |
| 36 |
|
| 37 |
type itemType int |
| 38 |
|
| 39 |
const ( |
| 40 |
itemError itemType = iota |
| 41 |
itemNIL // used in the parser to indicate no type |
| 42 |
itemEOF |
| 43 |
itemKey |
| 44 |
itemText |
| 45 |
itemString |
| 46 |
itemBool |
| 47 |
itemInteger |
| 48 |
itemFloat |
| 49 |
itemDatetime |
| 50 |
itemArrayStart |
| 51 |
itemArrayEnd |
| 52 |
itemMapStart |
| 53 |
itemMapEnd |
| 54 |
itemCommentStart |
| 55 |
itemVariable |
| 56 |
itemInclude |
| 57 |
) |
| 58 |
|
| 59 |
const ( |
| 60 |
eof = 0 |
| 61 |
mapStart = '{' |
| 62 |
mapEnd = '}' |
| 63 |
keySepEqual = '=' |
| 64 |
keySepColon = ':' |
| 65 |
arrayStart = '[' |
| 66 |
arrayEnd = ']' |
| 67 |
arrayValTerm = ',' |
| 68 |
mapValTerm = ',' |
| 69 |
commentHashStart = '#' |
| 70 |
commentSlashStart = '/' |
| 71 |
dqStringStart = '"' |
| 72 |
dqStringEnd = '"' |
| 73 |
sqStringStart = '\'' |
| 74 |
sqStringEnd = '\'' |
| 75 |
optValTerm = ';' |
| 76 |
topOptStart = '{' |
| 77 |
topOptValTerm = ',' |
| 78 |
topOptTerm = '}' |
| 79 |
blockStart = '(' |
| 80 |
blockEnd = ')' |
| 81 |
mapEndString = string(mapEnd) |
| 82 |
) |
| 83 |
|
| 84 |
type stateFn func(lx *lexer) stateFn |
| 85 |
|
| 86 |
type lexer struct { |
| 87 |
input string |
| 88 |
start int |
| 89 |
pos int |
| 90 |
width int |
| 91 |
line int |
| 92 |
state stateFn |
| 93 |
items chan item |
| 94 |
|
| 95 |
// A stack of state functions used to maintain context. |
| 96 |
// The idea is to reuse parts of the state machine in various places. |
| 97 |
// For example, values can appear at the top level or within arbitrarily |
| 98 |
// nested arrays. The last state on the stack is used after a value has |
| 99 |
// been lexed. Similarly for comments. |
| 100 |
stack []stateFn |
| 101 |
|
| 102 |
// Used for processing escapable substrings in double-quoted and raw strings |
| 103 |
stringParts []string |
| 104 |
stringStateFn stateFn |
| 105 |
|
| 106 |
// lstart is the start position of the current line. |
| 107 |
lstart int |
| 108 |
|
| 109 |
// ilstart is the start position of the line from the current item. |
| 110 |
ilstart int |
| 111 |
} |
| 112 |
|
| 113 |
type item struct { |
| 114 |
typ itemType |
| 115 |
val string |
| 116 |
line int |
| 117 |
pos int |
| 118 |
} |
| 119 |
|
| 120 |
func (lx *lexer) nextItem() item { |
| 121 |
for { |
| 122 |
select { |
| 123 |
case item := <-lx.items: |
| 124 |
return item |
| 125 |
default: |
| 126 |
lx.state = lx.state(lx) |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func lex(input string) *lexer { |
| 132 |
lx := &lexer{ |
| 133 |
input: input, |
| 134 |
state: lexTop, |
| 135 |
line: 1, |
| 136 |
items: make(chan item, 10), |
| 137 |
stack: make([]stateFn, 0, 10), |
| 138 |
stringParts: []string{}, |
| 139 |
} |
| 140 |
return lx |
| 141 |
} |
| 142 |
|
| 143 |
func (lx *lexer) push(state stateFn) { |
| 144 |
lx.stack = append(lx.stack, state) |
| 145 |
} |
| 146 |
|
| 147 |
func (lx *lexer) pop() stateFn { |
| 148 |
if len(lx.stack) == 0 { |
| 149 |
return lx.errorf("BUG in lexer: no states to pop.") |
| 150 |
} |
| 151 |
li := len(lx.stack) - 1 |
| 152 |
last := lx.stack[li] |
| 153 |
lx.stack = lx.stack[0:li] |
| 154 |
return last |
| 155 |
} |
| 156 |
|
| 157 |
func (lx *lexer) emit(typ itemType) { |
| 158 |
val := strings.Join(lx.stringParts, "") + lx.input[lx.start:lx.pos] |
| 159 |
// Position of item in line where it started. |
| 160 |
pos := lx.pos - lx.ilstart - len(val) |
| 161 |
lx.items <- item{typ, val, lx.line, pos} |
| 162 |
lx.start = lx.pos |
| 163 |
lx.ilstart = lx.lstart |
| 164 |
} |
| 165 |
|
| 166 |
func (lx *lexer) emitString() { |
| 167 |
var finalString string |
| 168 |
if len(lx.stringParts) > 0 { |
| 169 |
finalString = strings.Join(lx.stringParts, "") + lx.input[lx.start:lx.pos] |
| 170 |
lx.stringParts = []string{} |
| 171 |
} else { |
| 172 |
finalString = lx.input[lx.start:lx.pos] |
| 173 |
} |
| 174 |
// Position of string in line where it started. |
| 175 |
pos := lx.pos - lx.ilstart - len(finalString) |
| 176 |
lx.items <- item{itemString, finalString, lx.line, pos} |
| 177 |
lx.start = lx.pos |
| 178 |
lx.ilstart = lx.lstart |
| 179 |
} |
| 180 |
|
| 181 |
func (lx *lexer) addCurrentStringPart(offset int) { |
| 182 |
lx.stringParts = append(lx.stringParts, lx.input[lx.start:lx.pos-offset]) |
| 183 |
lx.start = lx.pos |
| 184 |
} |
| 185 |
|
| 186 |
func (lx *lexer) addStringPart(s string) stateFn { |
| 187 |
lx.stringParts = append(lx.stringParts, s) |
| 188 |
lx.start = lx.pos |
| 189 |
return lx.stringStateFn |
| 190 |
} |
| 191 |
|
| 192 |
func (lx *lexer) hasEscapedParts() bool { |
| 193 |
return len(lx.stringParts) > 0 |
| 194 |
} |
| 195 |
|
| 196 |
func (lx *lexer) next() (r rune) { |
| 197 |
if lx.pos >= len(lx.input) { |
| 198 |
lx.width = 0 |
| 199 |
return eof |
| 200 |
} |
| 201 |
|
| 202 |
if lx.input[lx.pos] == '\n' { |
| 203 |
lx.line++ |
| 204 |
|
| 205 |
// Mark start position of current line. |
| 206 |
lx.lstart = lx.pos |
| 207 |
} |
| 208 |
r, lx.width = utf8.DecodeRuneInString(lx.input[lx.pos:]) |
| 209 |
lx.pos += lx.width |
| 210 |
|
| 211 |
return r |
| 212 |
} |
| 213 |
|
| 214 |
// ignore skips over the pending input before this point. |
| 215 |
func (lx *lexer) ignore() { |
| 216 |
lx.start = lx.pos |
| 217 |
lx.ilstart = lx.lstart |
| 218 |
} |
| 219 |
|
| 220 |
// backup steps back one rune. Can be called only once per call of next. |
| 221 |
func (lx *lexer) backup() { |
| 222 |
lx.pos -= lx.width |
| 223 |
if lx.pos < len(lx.input) && lx.input[lx.pos] == '\n' { |
| 224 |
lx.line-- |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// peek returns but does not consume the next rune in the input. |
| 229 |
func (lx *lexer) peek() rune { |
| 230 |
r := lx.next() |
| 231 |
lx.backup() |
| 232 |
return r |
| 233 |
} |
| 234 |
|
| 235 |
// errorf stops all lexing by emitting an error and returning `nil`. |
| 236 |
// Note that any value that is a character is escaped if it's a special |
| 237 |
// character (new lines, tabs, etc.). |
| 238 |
func (lx *lexer) errorf(format string, values ...any) stateFn { |
| 239 |
for i, value := range values { |
| 240 |
if v, ok := value.(rune); ok { |
| 241 |
values[i] = escapeSpecial(v) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Position of error in current line. |
| 246 |
pos := lx.pos - lx.lstart |
| 247 |
lx.items <- item{ |
| 248 |
itemError, |
| 249 |
fmt.Sprintf(format, values...), |
| 250 |
lx.line, |
| 251 |
pos, |
| 252 |
} |
| 253 |
return nil |
| 254 |
} |
| 255 |
|
| 256 |
// lexTop consumes elements at the top level of data structure. |
| 257 |
func lexTop(lx *lexer) stateFn { |
| 258 |
r := lx.next() |
| 259 |
if unicode.IsSpace(r) { |
| 260 |
return lexSkip(lx, lexTop) |
| 261 |
} |
| 262 |
|
| 263 |
switch r { |
| 264 |
case topOptStart: |
| 265 |
lx.push(lexTop) |
| 266 |
return lexSkip(lx, lexBlockStart) |
| 267 |
case commentHashStart: |
| 268 |
lx.push(lexTop) |
| 269 |
return lexCommentStart |
| 270 |
case commentSlashStart: |
| 271 |
rn := lx.next() |
| 272 |
if rn == commentSlashStart { |
| 273 |
lx.push(lexTop) |
| 274 |
return lexCommentStart |
| 275 |
} |
| 276 |
lx.backup() |
| 277 |
fallthrough |
| 278 |
case eof: |
| 279 |
if lx.pos > lx.start { |
| 280 |
return lx.errorf("Unexpected EOF.") |
| 281 |
} |
| 282 |
lx.emit(itemEOF) |
| 283 |
return nil |
| 284 |
} |
| 285 |
|
| 286 |
// At this point, the only valid item can be a key, so we back up |
| 287 |
// and let the key lexer do the rest. |
| 288 |
lx.backup() |
| 289 |
lx.push(lexTopValueEnd) |
| 290 |
return lexKeyStart |
| 291 |
} |
| 292 |
|
| 293 |
// lexTopValueEnd is entered whenever a top-level value has been consumed. |
| 294 |
// It must see only whitespace, and will turn back to lexTop upon a new line. |
| 295 |
// If it sees EOF, it will quit the lexer successfully. |
| 296 |
func lexTopValueEnd(lx *lexer) stateFn { |
| 297 |
r := lx.next() |
| 298 |
switch { |
| 299 |
case r == commentHashStart: |
| 300 |
// a comment will read to a new line for us. |
| 301 |
lx.push(lexTop) |
| 302 |
return lexCommentStart |
| 303 |
case r == commentSlashStart: |
| 304 |
rn := lx.next() |
| 305 |
if rn == commentSlashStart { |
| 306 |
lx.push(lexTop) |
| 307 |
return lexCommentStart |
| 308 |
} |
| 309 |
lx.backup() |
| 310 |
fallthrough |
| 311 |
case isWhitespace(r): |
| 312 |
return lexTopValueEnd |
| 313 |
case isNL(r) || r == eof || r == optValTerm || r == topOptValTerm || r == topOptTerm: |
| 314 |
lx.ignore() |
| 315 |
return lexTop |
| 316 |
} |
| 317 |
return lx.errorf("Expected a top-level value to end with a new line, "+ |
| 318 |
"comment or EOF, but got '%v' instead.", r) |
| 319 |
} |
| 320 |
|
| 321 |
func lexBlockStart(lx *lexer) stateFn { |
| 322 |
r := lx.next() |
| 323 |
if unicode.IsSpace(r) { |
| 324 |
return lexSkip(lx, lexBlockStart) |
| 325 |
} |
| 326 |
|
| 327 |
switch r { |
| 328 |
case topOptStart: |
| 329 |
lx.push(lexBlockEnd) |
| 330 |
return lexSkip(lx, lexBlockStart) |
| 331 |
case topOptTerm: |
| 332 |
lx.ignore() |
| 333 |
return lx.pop() |
| 334 |
case commentHashStart: |
| 335 |
lx.push(lexBlockStart) |
| 336 |
return lexCommentStart |
| 337 |
case commentSlashStart: |
| 338 |
rn := lx.next() |
| 339 |
if rn == commentSlashStart { |
| 340 |
lx.push(lexBlockStart) |
| 341 |
return lexCommentStart |
| 342 |
} |
| 343 |
lx.backup() |
| 344 |
fallthrough |
| 345 |
case eof: |
| 346 |
if lx.pos > lx.start { |
| 347 |
return lx.errorf("Unexpected EOF.") |
| 348 |
} |
| 349 |
lx.emit(itemEOF) |
| 350 |
return nil |
| 351 |
} |
| 352 |
|
| 353 |
// At this point, the only valid item can be a key, so we back up |
| 354 |
// and let the key lexer do the rest. |
| 355 |
lx.backup() |
| 356 |
lx.push(lexBlockValueEnd) |
| 357 |
return lexKeyStart |
| 358 |
} |
| 359 |
|
| 360 |
// lexBlockValueEnd is entered whenever a block-level value has been consumed. |
| 361 |
// It must see only whitespace, and will turn back to lexBlockStart upon a new line. |
| 362 |
// If it sees EOF, it will quit the lexer successfully. |
| 363 |
func lexBlockValueEnd(lx *lexer) stateFn { |
| 364 |
r := lx.next() |
| 365 |
switch { |
| 366 |
case r == commentHashStart: |
| 367 |
// a comment will read to a new line for us. |
| 368 |
lx.push(lexBlockValueEnd) |
| 369 |
return lexCommentStart |
| 370 |
case r == commentSlashStart: |
| 371 |
rn := lx.next() |
| 372 |
if rn == commentSlashStart { |
| 373 |
lx.push(lexBlockValueEnd) |
| 374 |
return lexCommentStart |
| 375 |
} |
| 376 |
lx.backup() |
| 377 |
fallthrough |
| 378 |
case isWhitespace(r): |
| 379 |
return lexBlockValueEnd |
| 380 |
case isNL(r) || r == optValTerm || r == topOptValTerm: |
| 381 |
lx.ignore() |
| 382 |
return lexBlockStart |
| 383 |
case r == topOptTerm: |
| 384 |
lx.backup() |
| 385 |
return lexBlockEnd |
| 386 |
} |
| 387 |
return lx.errorf("Expected a block-level value to end with a new line, "+ |
| 388 |
"comment or EOF, but got '%v' instead.", r) |
| 389 |
} |
| 390 |
|
| 391 |
// lexBlockEnd is entered whenever a block-level value has been consumed. |
| 392 |
// It must see only whitespace, and will turn back to lexTop upon a "}". |
| 393 |
func lexBlockEnd(lx *lexer) stateFn { |
| 394 |
r := lx.next() |
| 395 |
switch { |
| 396 |
case r == commentHashStart: |
| 397 |
// a comment will read to a new line for us. |
| 398 |
lx.push(lexBlockStart) |
| 399 |
return lexCommentStart |
| 400 |
case r == commentSlashStart: |
| 401 |
rn := lx.next() |
| 402 |
if rn == commentSlashStart { |
| 403 |
lx.push(lexBlockStart) |
| 404 |
return lexCommentStart |
| 405 |
} |
| 406 |
lx.backup() |
| 407 |
fallthrough |
| 408 |
case isNL(r) || isWhitespace(r): |
| 409 |
return lexBlockEnd |
| 410 |
case r == optValTerm || r == topOptValTerm: |
| 411 |
lx.ignore() |
| 412 |
return lexBlockStart |
| 413 |
case r == topOptTerm: |
| 414 |
lx.ignore() |
| 415 |
return lx.pop() |
| 416 |
} |
| 417 |
return lx.errorf("Expected a block-level to end with a '}', but got '%v' instead.", r) |
| 418 |
} |
| 419 |
|
| 420 |
// lexKeyStart consumes a key name up until the first non-whitespace character. |
| 421 |
// lexKeyStart will ignore whitespace. It will also eat enclosing quotes. |
| 422 |
func lexKeyStart(lx *lexer) stateFn { |
| 423 |
r := lx.peek() |
| 424 |
switch { |
| 425 |
case isKeySeparator(r): |
| 426 |
return lx.errorf("Unexpected key separator '%v'", r) |
| 427 |
case unicode.IsSpace(r): |
| 428 |
lx.next() |
| 429 |
return lexSkip(lx, lexKeyStart) |
| 430 |
case r == dqStringStart: |
| 431 |
lx.next() |
| 432 |
return lexSkip(lx, lexDubQuotedKey) |
| 433 |
case r == sqStringStart: |
| 434 |
lx.next() |
| 435 |
return lexSkip(lx, lexQuotedKey) |
| 436 |
} |
| 437 |
lx.ignore() |
| 438 |
lx.next() |
| 439 |
return lexKey |
| 440 |
} |
| 441 |
|
| 442 |
// lexDubQuotedKey consumes the text of a key between quotes. |
| 443 |
func lexDubQuotedKey(lx *lexer) stateFn { |
| 444 |
r := lx.peek() |
| 445 |
if r == dqStringEnd { |
| 446 |
lx.emit(itemKey) |
| 447 |
lx.next() |
| 448 |
return lexSkip(lx, lexKeyEnd) |
| 449 |
} else if r == eof { |
| 450 |
if lx.pos > lx.start { |
| 451 |
return lx.errorf("Unexpected EOF.") |
| 452 |
} |
| 453 |
lx.emit(itemEOF) |
| 454 |
return nil |
| 455 |
} |
| 456 |
lx.next() |
| 457 |
return lexDubQuotedKey |
| 458 |
} |
| 459 |
|
| 460 |
// lexQuotedKey consumes the text of a key between quotes. |
| 461 |
func lexQuotedKey(lx *lexer) stateFn { |
| 462 |
r := lx.peek() |
| 463 |
if r == sqStringEnd { |
| 464 |
lx.emit(itemKey) |
| 465 |
lx.next() |
| 466 |
return lexSkip(lx, lexKeyEnd) |
| 467 |
} else if r == eof { |
| 468 |
if lx.pos > lx.start { |
| 469 |
return lx.errorf("Unexpected EOF.") |
| 470 |
} |
| 471 |
lx.emit(itemEOF) |
| 472 |
return nil |
| 473 |
} |
| 474 |
lx.next() |
| 475 |
return lexQuotedKey |
| 476 |
} |
| 477 |
|
| 478 |
// keyCheckKeyword will check for reserved keywords as the key value when the key is |
| 479 |
// separated with a space. |
| 480 |
func (lx *lexer) keyCheckKeyword(fallThrough, push stateFn) stateFn { |
| 481 |
key := strings.ToLower(lx.input[lx.start:lx.pos]) |
| 482 |
switch key { |
| 483 |
case "include": |
| 484 |
lx.ignore() |
| 485 |
if push != nil { |
| 486 |
lx.push(push) |
| 487 |
} |
| 488 |
return lexIncludeStart |
| 489 |
} |
| 490 |
lx.emit(itemKey) |
| 491 |
return fallThrough |
| 492 |
} |
| 493 |
|
| 494 |
// lexIncludeStart will consume the whitespace til the start of the value. |
| 495 |
func lexIncludeStart(lx *lexer) stateFn { |
| 496 |
r := lx.next() |
| 497 |
if isWhitespace(r) { |
| 498 |
return lexSkip(lx, lexIncludeStart) |
| 499 |
} |
| 500 |
lx.backup() |
| 501 |
return lexInclude |
| 502 |
} |
| 503 |
|
| 504 |
// lexIncludeQuotedString consumes the inner contents of a string. It assumes that the |
| 505 |
// beginning '"' has already been consumed and ignored. It will not interpret any |
| 506 |
// internal contents. |
| 507 |
func lexIncludeQuotedString(lx *lexer) stateFn { |
| 508 |
r := lx.next() |
| 509 |
switch { |
| 510 |
case r == sqStringEnd: |
| 511 |
lx.backup() |
| 512 |
lx.emit(itemInclude) |
| 513 |
lx.next() |
| 514 |
lx.ignore() |
| 515 |
return lx.pop() |
| 516 |
case r == eof: |
| 517 |
return lx.errorf("Unexpected EOF in quoted include") |
| 518 |
} |
| 519 |
return lexIncludeQuotedString |
| 520 |
} |
| 521 |
|
| 522 |
// lexIncludeDubQuotedString consumes the inner contents of a string. It assumes that the |
| 523 |
// beginning '"' has already been consumed and ignored. It will not interpret any |
| 524 |
// internal contents. |
| 525 |
func lexIncludeDubQuotedString(lx *lexer) stateFn { |
| 526 |
r := lx.next() |
| 527 |
switch { |
| 528 |
case r == dqStringEnd: |
| 529 |
lx.backup() |
| 530 |
lx.emit(itemInclude) |
| 531 |
lx.next() |
| 532 |
lx.ignore() |
| 533 |
return lx.pop() |
| 534 |
case r == eof: |
| 535 |
return lx.errorf("Unexpected EOF in double quoted include") |
| 536 |
} |
| 537 |
return lexIncludeDubQuotedString |
| 538 |
} |
| 539 |
|
| 540 |
// lexIncludeString consumes the inner contents of a raw string. |
| 541 |
func lexIncludeString(lx *lexer) stateFn { |
| 542 |
r := lx.next() |
| 543 |
switch { |
| 544 |
case isNL(r) || r == eof || r == optValTerm || r == mapEnd || isWhitespace(r): |
| 545 |
lx.backup() |
| 546 |
lx.emit(itemInclude) |
| 547 |
return lx.pop() |
| 548 |
case r == sqStringEnd: |
| 549 |
lx.backup() |
| 550 |
lx.emit(itemInclude) |
| 551 |
lx.next() |
| 552 |
lx.ignore() |
| 553 |
return lx.pop() |
| 554 |
} |
| 555 |
return lexIncludeString |
| 556 |
} |
| 557 |
|
| 558 |
// lexInclude will consume the include value. |
| 559 |
func lexInclude(lx *lexer) stateFn { |
| 560 |
r := lx.next() |
| 561 |
switch { |
| 562 |
case r == sqStringStart: |
| 563 |
lx.ignore() // ignore the " or ' |
| 564 |
return lexIncludeQuotedString |
| 565 |
case r == dqStringStart: |
| 566 |
lx.ignore() // ignore the " or ' |
| 567 |
return lexIncludeDubQuotedString |
| 568 |
case r == arrayStart: |
| 569 |
return lx.errorf("Expected include value but found start of an array") |
| 570 |
case r == mapStart: |
| 571 |
return lx.errorf("Expected include value but found start of a map") |
| 572 |
case r == blockStart: |
| 573 |
return lx.errorf("Expected include value but found start of a block") |
| 574 |
case unicode.IsDigit(r), r == '-': |
| 575 |
return lx.errorf("Expected include value but found start of a number") |
| 576 |
case r == '\\': |
| 577 |
return lx.errorf("Expected include value but found escape sequence") |
| 578 |
case isNL(r): |
| 579 |
return lx.errorf("Expected include value but found new line") |
| 580 |
} |
| 581 |
lx.backup() |
| 582 |
return lexIncludeString |
| 583 |
} |
| 584 |
|
| 585 |
// lexKey consumes the text of a key. Assumes that the first character (which |
| 586 |
// is not whitespace) has already been consumed. |
| 587 |
func lexKey(lx *lexer) stateFn { |
| 588 |
r := lx.peek() |
| 589 |
if unicode.IsSpace(r) { |
| 590 |
// Spaces signal we could be looking at a keyword, e.g. include. |
| 591 |
// Keywords will eat the keyword and set the appropriate return stateFn. |
| 592 |
return lx.keyCheckKeyword(lexKeyEnd, nil) |
| 593 |
} else if isKeySeparator(r) || r == eof { |
| 594 |
lx.emit(itemKey) |
| 595 |
return lexKeyEnd |
| 596 |
} |
| 597 |
lx.next() |
| 598 |
return lexKey |
| 599 |
} |
| 600 |
|
| 601 |
// lexKeyEnd consumes the end of a key (up to the key separator). |
| 602 |
// Assumes that the first whitespace character after a key (or the '=' or ':' |
| 603 |
// separator) has NOT been consumed. |
| 604 |
func lexKeyEnd(lx *lexer) stateFn { |
| 605 |
r := lx.next() |
| 606 |
switch { |
| 607 |
case unicode.IsSpace(r): |
| 608 |
return lexSkip(lx, lexKeyEnd) |
| 609 |
case isKeySeparator(r): |
| 610 |
return lexSkip(lx, lexValue) |
| 611 |
case r == eof: |
| 612 |
lx.emit(itemEOF) |
| 613 |
return nil |
| 614 |
} |
| 615 |
// We start the value here |
| 616 |
lx.backup() |
| 617 |
return lexValue |
| 618 |
} |
| 619 |
|
| 620 |
// lexValue starts the consumption of a value anywhere a value is expected. |
| 621 |
// lexValue will ignore whitespace. |
| 622 |
// After a value is lexed, the last state on the next is popped and returned. |
| 623 |
func lexValue(lx *lexer) stateFn { |
| 624 |
// We allow whitespace to precede a value, but NOT new lines. |
| 625 |
// In array syntax, the array states are responsible for ignoring new lines. |
| 626 |
r := lx.next() |
| 627 |
if isWhitespace(r) { |
| 628 |
return lexSkip(lx, lexValue) |
| 629 |
} |
| 630 |
|
| 631 |
switch { |
| 632 |
case r == arrayStart: |
| 633 |
lx.ignore() |
| 634 |
lx.emit(itemArrayStart) |
| 635 |
return lexArrayValue |
| 636 |
case r == mapStart: |
| 637 |
lx.ignore() |
| 638 |
lx.emit(itemMapStart) |
| 639 |
return lexMapKeyStart |
| 640 |
case r == sqStringStart: |
| 641 |
lx.ignore() // ignore the " or ' |
| 642 |
return lexQuotedString |
| 643 |
case r == dqStringStart: |
| 644 |
lx.ignore() // ignore the " or ' |
| 645 |
lx.stringStateFn = lexDubQuotedString |
| 646 |
return lexDubQuotedString |
| 647 |
case r == '-': |
| 648 |
return lexNegNumberStart |
| 649 |
case r == blockStart: |
| 650 |
lx.ignore() |
| 651 |
return lexBlock |
| 652 |
case unicode.IsDigit(r): |
| 653 |
lx.backup() // avoid an extra state and use the same as above |
| 654 |
return lexNumberOrDateOrStringOrIPStart |
| 655 |
case r == '.': // special error case, be kind to users |
| 656 |
return lx.errorf("Floats must start with a digit") |
| 657 |
case isNL(r): |
| 658 |
return lx.errorf("Expected value but found new line") |
| 659 |
} |
| 660 |
lx.backup() |
| 661 |
lx.stringStateFn = lexString |
| 662 |
return lexString |
| 663 |
} |
| 664 |
|
| 665 |
// lexArrayValue consumes one value in an array. It assumes that '[' or ',' |
| 666 |
// have already been consumed. All whitespace and new lines are ignored. |
| 667 |
func lexArrayValue(lx *lexer) stateFn { |
| 668 |
r := lx.next() |
| 669 |
switch { |
| 670 |
case unicode.IsSpace(r): |
| 671 |
return lexSkip(lx, lexArrayValue) |
| 672 |
case r == commentHashStart: |
| 673 |
lx.push(lexArrayValue) |
| 674 |
return lexCommentStart |
| 675 |
case r == commentSlashStart: |
| 676 |
rn := lx.next() |
| 677 |
if rn == commentSlashStart { |
| 678 |
lx.push(lexArrayValue) |
| 679 |
return lexCommentStart |
| 680 |
} |
| 681 |
lx.backup() |
| 682 |
fallthrough |
| 683 |
case r == arrayValTerm: |
| 684 |
return lx.errorf("Unexpected array value terminator '%v'.", arrayValTerm) |
| 685 |
case r == arrayEnd: |
| 686 |
return lexArrayEnd |
| 687 |
} |
| 688 |
|
| 689 |
lx.backup() |
| 690 |
lx.push(lexArrayValueEnd) |
| 691 |
return lexValue |
| 692 |
} |
| 693 |
|
| 694 |
// lexArrayValueEnd consumes the cruft between values of an array. Namely, |
| 695 |
// it ignores whitespace and expects either a ',' or a ']'. |
| 696 |
func lexArrayValueEnd(lx *lexer) stateFn { |
| 697 |
r := lx.next() |
| 698 |
switch { |
| 699 |
case isWhitespace(r): |
| 700 |
return lexSkip(lx, lexArrayValueEnd) |
| 701 |
case r == commentHashStart: |
| 702 |
lx.push(lexArrayValueEnd) |
| 703 |
return lexCommentStart |
| 704 |
case r == commentSlashStart: |
| 705 |
rn := lx.next() |
| 706 |
if rn == commentSlashStart { |
| 707 |
lx.push(lexArrayValueEnd) |
| 708 |
return lexCommentStart |
| 709 |
} |
| 710 |
lx.backup() |
| 711 |
fallthrough |
| 712 |
case r == arrayValTerm || isNL(r): |
| 713 |
return lexSkip(lx, lexArrayValue) // Move onto next |
| 714 |
case r == arrayEnd: |
| 715 |
return lexArrayEnd |
| 716 |
} |
| 717 |
return lx.errorf("Expected an array value terminator %q or an array "+ |
| 718 |
"terminator %q, but got '%v' instead.", arrayValTerm, arrayEnd, r) |
| 719 |
} |
| 720 |
|
| 721 |
// lexArrayEnd finishes the lexing of an array. It assumes that a ']' has |
| 722 |
// just been consumed. |
| 723 |
func lexArrayEnd(lx *lexer) stateFn { |
| 724 |
lx.ignore() |
| 725 |
lx.emit(itemArrayEnd) |
| 726 |
return lx.pop() |
| 727 |
} |
| 728 |
|
| 729 |
// lexMapKeyStart consumes a key name up until the first non-whitespace |
| 730 |
// character. |
| 731 |
// lexMapKeyStart will ignore whitespace. |
| 732 |
func lexMapKeyStart(lx *lexer) stateFn { |
| 733 |
r := lx.peek() |
| 734 |
switch { |
| 735 |
case isKeySeparator(r): |
| 736 |
return lx.errorf("Unexpected key separator '%v'.", r) |
| 737 |
case r == arrayEnd: |
| 738 |
return lx.errorf("Unexpected array end '%v' processing map.", r) |
| 739 |
case unicode.IsSpace(r): |
| 740 |
lx.next() |
| 741 |
return lexSkip(lx, lexMapKeyStart) |
| 742 |
case r == mapEnd: |
| 743 |
lx.next() |
| 744 |
return lexSkip(lx, lexMapEnd) |
| 745 |
case r == commentHashStart: |
| 746 |
lx.next() |
| 747 |
lx.push(lexMapKeyStart) |
| 748 |
return lexCommentStart |
| 749 |
case r == commentSlashStart: |
| 750 |
lx.next() |
| 751 |
rn := lx.next() |
| 752 |
if rn == commentSlashStart { |
| 753 |
lx.push(lexMapKeyStart) |
| 754 |
return lexCommentStart |
| 755 |
} |
| 756 |
lx.backup() |
| 757 |
case r == sqStringStart: |
| 758 |
lx.next() |
| 759 |
return lexSkip(lx, lexMapQuotedKey) |
| 760 |
case r == dqStringStart: |
| 761 |
lx.next() |
| 762 |
return lexSkip(lx, lexMapDubQuotedKey) |
| 763 |
case r == eof: |
| 764 |
return lx.errorf("Unexpected EOF processing map.") |
| 765 |
} |
| 766 |
lx.ignore() |
| 767 |
lx.next() |
| 768 |
return lexMapKey |
| 769 |
} |
| 770 |
|
| 771 |
// lexMapQuotedKey consumes the text of a key between quotes. |
| 772 |
func lexMapQuotedKey(lx *lexer) stateFn { |
| 773 |
if r := lx.peek(); r == eof { |
| 774 |
return lx.errorf("Unexpected EOF processing quoted map key.") |
| 775 |
} else if r == sqStringEnd { |
| 776 |
lx.emit(itemKey) |
| 777 |
lx.next() |
| 778 |
return lexSkip(lx, lexMapKeyEnd) |
| 779 |
} |
| 780 |
lx.next() |
| 781 |
return lexMapQuotedKey |
| 782 |
} |
| 783 |
|
| 784 |
// lexMapDubQuotedKey consumes the text of a key between quotes. |
| 785 |
func lexMapDubQuotedKey(lx *lexer) stateFn { |
| 786 |
if r := lx.peek(); r == eof { |
| 787 |
return lx.errorf("Unexpected EOF processing double quoted map key.") |
| 788 |
} else if r == dqStringEnd { |
| 789 |
lx.emit(itemKey) |
| 790 |
lx.next() |
| 791 |
return lexSkip(lx, lexMapKeyEnd) |
| 792 |
} |
| 793 |
lx.next() |
| 794 |
return lexMapDubQuotedKey |
| 795 |
} |
| 796 |
|
| 797 |
// lexMapKey consumes the text of a key. Assumes that the first character (which |
| 798 |
// is not whitespace) has already been consumed. |
| 799 |
func lexMapKey(lx *lexer) stateFn { |
| 800 |
if r := lx.peek(); r == eof { |
| 801 |
return lx.errorf("Unexpected EOF processing map key.") |
| 802 |
} else if unicode.IsSpace(r) { |
| 803 |
// Spaces signal we could be looking at a keyword, e.g. include. |
| 804 |
// Keywords will eat the keyword and set the appropriate return stateFn. |
| 805 |
return lx.keyCheckKeyword(lexMapKeyEnd, lexMapValueEnd) |
| 806 |
} else if isKeySeparator(r) { |
| 807 |
lx.emit(itemKey) |
| 808 |
return lexMapKeyEnd |
| 809 |
} |
| 810 |
lx.next() |
| 811 |
return lexMapKey |
| 812 |
} |
| 813 |
|
| 814 |
// lexMapKeyEnd consumes the end of a key (up to the key separator). |
| 815 |
// Assumes that the first whitespace character after a key (or the '=' |
| 816 |
// separator) has NOT been consumed. |
| 817 |
func lexMapKeyEnd(lx *lexer) stateFn { |
| 818 |
r := lx.next() |
| 819 |
switch { |
| 820 |
case unicode.IsSpace(r): |
| 821 |
return lexSkip(lx, lexMapKeyEnd) |
| 822 |
case isKeySeparator(r): |
| 823 |
return lexSkip(lx, lexMapValue) |
| 824 |
} |
| 825 |
// We start the value here |
| 826 |
lx.backup() |
| 827 |
return lexMapValue |
| 828 |
} |
| 829 |
|
| 830 |
// lexMapValue consumes one value in a map. It assumes that '{' or ',' |
| 831 |
// have already been consumed. All whitespace and new lines are ignored. |
| 832 |
// Map values can be separated by ',' or simple NLs. |
| 833 |
func lexMapValue(lx *lexer) stateFn { |
| 834 |
r := lx.next() |
| 835 |
switch { |
| 836 |
case unicode.IsSpace(r): |
| 837 |
return lexSkip(lx, lexMapValue) |
| 838 |
case r == mapValTerm: |
| 839 |
return lx.errorf("Unexpected map value terminator %q.", mapValTerm) |
| 840 |
case r == mapEnd: |
| 841 |
return lexSkip(lx, lexMapEnd) |
| 842 |
} |
| 843 |
lx.backup() |
| 844 |
lx.push(lexMapValueEnd) |
| 845 |
return lexValue |
| 846 |
} |
| 847 |
|
| 848 |
// lexMapValueEnd consumes the cruft between values of a map. Namely, |
| 849 |
// it ignores whitespace and expects either a ',' or a '}'. |
| 850 |
func lexMapValueEnd(lx *lexer) stateFn { |
| 851 |
r := lx.next() |
| 852 |
switch { |
| 853 |
case isWhitespace(r): |
| 854 |
return lexSkip(lx, lexMapValueEnd) |
| 855 |
case r == commentHashStart: |
| 856 |
lx.push(lexMapValueEnd) |
| 857 |
return lexCommentStart |
| 858 |
case r == commentSlashStart: |
| 859 |
rn := lx.next() |
| 860 |
if rn == commentSlashStart { |
| 861 |
lx.push(lexMapValueEnd) |
| 862 |
return lexCommentStart |
| 863 |
} |
| 864 |
lx.backup() |
| 865 |
fallthrough |
| 866 |
case r == optValTerm || r == mapValTerm || isNL(r): |
| 867 |
return lexSkip(lx, lexMapKeyStart) // Move onto next |
| 868 |
case r == mapEnd: |
| 869 |
return lexSkip(lx, lexMapEnd) |
| 870 |
} |
| 871 |
return lx.errorf("Expected a map value terminator %q or a map "+ |
| 872 |
"terminator %q, but got '%v' instead.", mapValTerm, mapEnd, r) |
| 873 |
} |
| 874 |
|
| 875 |
// lexMapEnd finishes the lexing of a map. It assumes that a '}' has |
| 876 |
// just been consumed. |
| 877 |
func lexMapEnd(lx *lexer) stateFn { |
| 878 |
lx.ignore() |
| 879 |
lx.emit(itemMapEnd) |
| 880 |
return lx.pop() |
| 881 |
} |
| 882 |
|
| 883 |
// Checks if the unquoted string was actually a boolean |
| 884 |
func (lx *lexer) isBool() bool { |
| 885 |
str := strings.ToLower(lx.input[lx.start:lx.pos]) |
| 886 |
return str == "true" || str == "false" || |
| 887 |
str == "on" || str == "off" || |
| 888 |
str == "yes" || str == "no" |
| 889 |
} |
| 890 |
|
| 891 |
// Check if the unquoted string is a variable reference, starting with $. |
| 892 |
func (lx *lexer) isVariable() bool { |
| 893 |
if lx.start >= len(lx.input) { |
| 894 |
return false |
| 895 |
} |
| 896 |
if lx.input[lx.start] == '$' { |
| 897 |
lx.start += 1 |
| 898 |
return true |
| 899 |
} |
| 900 |
return false |
| 901 |
} |
| 902 |
|
| 903 |
// lexQuotedString consumes the inner contents of a string. It assumes that the |
| 904 |
// beginning '"' has already been consumed and ignored. It will not interpret any |
| 905 |
// internal contents. |
| 906 |
func lexQuotedString(lx *lexer) stateFn { |
| 907 |
r := lx.next() |
| 908 |
switch { |
| 909 |
case r == sqStringEnd: |
| 910 |
lx.backup() |
| 911 |
lx.emit(itemString) |
| 912 |
lx.next() |
| 913 |
lx.ignore() |
| 914 |
return lx.pop() |
| 915 |
case r == eof: |
| 916 |
if lx.pos > lx.start { |
| 917 |
return lx.errorf("Unexpected EOF.") |
| 918 |
} |
| 919 |
lx.emit(itemEOF) |
| 920 |
return nil |
| 921 |
} |
| 922 |
return lexQuotedString |
| 923 |
} |
| 924 |
|
| 925 |
// lexDubQuotedString consumes the inner contents of a string. It assumes that the |
| 926 |
// beginning '"' has already been consumed and ignored. It will not interpret any |
| 927 |
// internal contents. |
| 928 |
func lexDubQuotedString(lx *lexer) stateFn { |
| 929 |
r := lx.next() |
| 930 |
switch { |
| 931 |
case r == '\\': |
| 932 |
lx.addCurrentStringPart(1) |
| 933 |
return lexStringEscape |
| 934 |
case r == dqStringEnd: |
| 935 |
lx.backup() |
| 936 |
lx.emitString() |
| 937 |
lx.next() |
| 938 |
lx.ignore() |
| 939 |
return lx.pop() |
| 940 |
case r == eof: |
| 941 |
if lx.pos > lx.start { |
| 942 |
return lx.errorf("Unexpected EOF.") |
| 943 |
} |
| 944 |
lx.emit(itemEOF) |
| 945 |
return nil |
| 946 |
} |
| 947 |
return lexDubQuotedString |
| 948 |
} |
| 949 |
|
| 950 |
// lexString consumes the inner contents of a raw string. |
| 951 |
func lexString(lx *lexer) stateFn { |
| 952 |
r := lx.next() |
| 953 |
switch { |
| 954 |
case r == '\\': |
| 955 |
lx.addCurrentStringPart(1) |
| 956 |
return lexStringEscape |
| 957 |
// Termination of non-quoted strings |
| 958 |
case isNL(r) || r == eof || r == optValTerm || |
| 959 |
r == arrayValTerm || r == arrayEnd || r == mapEnd || |
| 960 |
isWhitespace(r): |
| 961 |
|
| 962 |
lx.backup() |
| 963 |
if lx.hasEscapedParts() { |
| 964 |
lx.emitString() |
| 965 |
} else if lx.isBool() { |
| 966 |
lx.emit(itemBool) |
| 967 |
} else if lx.isVariable() { |
| 968 |
lx.emit(itemVariable) |
| 969 |
} else { |
| 970 |
lx.emitString() |
| 971 |
} |
| 972 |
return lx.pop() |
| 973 |
case r == sqStringEnd: |
| 974 |
lx.backup() |
| 975 |
lx.emitString() |
| 976 |
lx.next() |
| 977 |
lx.ignore() |
| 978 |
return lx.pop() |
| 979 |
} |
| 980 |
return lexString |
| 981 |
} |
| 982 |
|
| 983 |
// lexBlock consumes the inner contents as a string. It assumes that the |
| 984 |
// beginning '(' has already been consumed and ignored. It will continue |
| 985 |
// processing until it finds a ')' on a new line by itself. |
| 986 |
func lexBlock(lx *lexer) stateFn { |
| 987 |
r := lx.next() |
| 988 |
switch { |
| 989 |
case r == blockEnd: |
| 990 |
lx.backup() |
| 991 |
lx.backup() |
| 992 |
|
| 993 |
// Looking for a ')' character on a line by itself, if the previous |
| 994 |
// character isn't a new line, then break so we keep processing the block. |
| 995 |
if lx.next() != '\n' { |
| 996 |
lx.next() |
| 997 |
break |
| 998 |
} |
| 999 |
lx.next() |
| 1000 |
|
| 1001 |
// Make sure the next character is a new line or an eof. We want a ')' on a |
| 1002 |
// bare line by itself. |
| 1003 |
switch lx.next() { |
| 1004 |
case '\n', eof: |
| 1005 |
lx.backup() |
| 1006 |
lx.backup() |
| 1007 |
lx.emit(itemString) |
| 1008 |
lx.next() |
| 1009 |
lx.ignore() |
| 1010 |
return lx.pop() |
| 1011 |
} |
| 1012 |
lx.backup() |
| 1013 |
case r == eof: |
| 1014 |
return lx.errorf("Unexpected EOF processing block.") |
| 1015 |
} |
| 1016 |
return lexBlock |
| 1017 |
} |
| 1018 |
|
| 1019 |
// lexStringEscape consumes an escaped character. It assumes that the preceding |
| 1020 |
// '\\' has already been consumed. |
| 1021 |
func lexStringEscape(lx *lexer) stateFn { |
| 1022 |
r := lx.next() |
| 1023 |
switch r { |
| 1024 |
case 'x': |
| 1025 |
return lexStringBinary |
| 1026 |
case 't': |
| 1027 |
return lx.addStringPart("\t") |
| 1028 |
case 'n': |
| 1029 |
return lx.addStringPart("\n") |
| 1030 |
case 'r': |
| 1031 |
return lx.addStringPart("\r") |
| 1032 |
case '"': |
| 1033 |
return lx.addStringPart("\"") |
| 1034 |
case '\\': |
| 1035 |
return lx.addStringPart("\\") |
| 1036 |
} |
| 1037 |
return lx.errorf("Invalid escape character '%v'. Only the following "+ |
| 1038 |
"escape characters are allowed: \\xXX, \\t, \\n, \\r, \\\", \\\\.", r) |
| 1039 |
} |
| 1040 |
|
| 1041 |
// lexStringBinary consumes two hexadecimal digits following '\x'. It assumes |
| 1042 |
// that the '\x' has already been consumed. |
| 1043 |
func lexStringBinary(lx *lexer) stateFn { |
| 1044 |
r := lx.next() |
| 1045 |
if isNL(r) { |
| 1046 |
return lx.errorf("Expected two hexadecimal digits after '\\x', but hit end of line") |
| 1047 |
} |
| 1048 |
r = lx.next() |
| 1049 |
if isNL(r) { |
| 1050 |
return lx.errorf("Expected two hexadecimal digits after '\\x', but hit end of line") |
| 1051 |
} |
| 1052 |
offset := lx.pos - 2 |
| 1053 |
byteString, err := hex.DecodeString(lx.input[offset:lx.pos]) |
| 1054 |
if err != nil { |
| 1055 |
return lx.errorf("Expected two hexadecimal digits after '\\x', but got '%s'", lx.input[offset:lx.pos]) |
| 1056 |
} |
| 1057 |
lx.addStringPart(string(byteString)) |
| 1058 |
return lx.stringStateFn |
| 1059 |
} |
| 1060 |
|
| 1061 |
// lexNumberOrDateOrStringOrIPStart consumes either a (positive) |
| 1062 |
// integer, a float, a datetime, or IP, or String that started with a |
| 1063 |
// number. It assumes that NO negative sign has been consumed, that |
| 1064 |
// is triggered above. |
| 1065 |
func lexNumberOrDateOrStringOrIPStart(lx *lexer) stateFn { |
| 1066 |
r := lx.next() |
| 1067 |
if !unicode.IsDigit(r) { |
| 1068 |
if r == '.' { |
| 1069 |
return lx.errorf("Floats must start with a digit, not '.'.") |
| 1070 |
} |
| 1071 |
return lx.errorf("Expected a digit but got '%v'.", r) |
| 1072 |
} |
| 1073 |
return lexNumberOrDateOrStringOrIP |
| 1074 |
} |
| 1075 |
|
| 1076 |
// lexNumberOrDateOrStringOrIP consumes either a (positive) integer, |
| 1077 |
// float, datetime, IP or string without quotes that starts with a |
| 1078 |
// number. |
| 1079 |
func lexNumberOrDateOrStringOrIP(lx *lexer) stateFn { |
| 1080 |
r := lx.next() |
| 1081 |
switch { |
| 1082 |
case r == '-': |
| 1083 |
if lx.pos-lx.start != 5 { |
| 1084 |
return lx.errorf("All ISO8601 dates must be in full Zulu form.") |
| 1085 |
} |
| 1086 |
return lexDateAfterYear |
| 1087 |
case unicode.IsDigit(r): |
| 1088 |
return lexNumberOrDateOrStringOrIP |
| 1089 |
case r == '.': |
| 1090 |
// Assume float at first, but could be IP |
| 1091 |
return lexFloatStart |
| 1092 |
case isNumberSuffix(r): |
| 1093 |
return lexConvenientNumber |
| 1094 |
case !(isNL(r) || r == eof || r == mapEnd || r == optValTerm || r == mapValTerm || isWhitespace(r) || unicode.IsDigit(r)): |
| 1095 |
// Treat it as a string value once we get a rune that |
| 1096 |
// is not a number. |
| 1097 |
lx.stringStateFn = lexString |
| 1098 |
return lexString |
| 1099 |
} |
| 1100 |
lx.backup() |
| 1101 |
lx.emit(itemInteger) |
| 1102 |
return lx.pop() |
| 1103 |
} |
| 1104 |
|
| 1105 |
// lexConvenientNumber is when we have a suffix, e.g. 1k or 1Mb |
| 1106 |
func lexConvenientNumber(lx *lexer) stateFn { |
| 1107 |
r := lx.next() |
| 1108 |
switch { |
| 1109 |
case r == 'b' || r == 'B' || r == 'i' || r == 'I': |
| 1110 |
return lexConvenientNumber |
| 1111 |
} |
| 1112 |
lx.backup() |
| 1113 |
if isNL(r) || r == eof || r == mapEnd || r == optValTerm || r == mapValTerm || isWhitespace(r) || unicode.IsDigit(r) { |
| 1114 |
lx.emit(itemInteger) |
| 1115 |
return lx.pop() |
| 1116 |
} |
| 1117 |
// This is not a number, so treat it as a string. |
| 1118 |
lx.stringStateFn = lexString |
| 1119 |
return lexString |
| 1120 |
} |
| 1121 |
|
| 1122 |
// lexDateAfterYear consumes a full Zulu Datetime in ISO8601 format. |
| 1123 |
// It assumes that "YYYY-" has already been consumed. |
| 1124 |
func lexDateAfterYear(lx *lexer) stateFn { |
| 1125 |
formats := []rune{ |
| 1126 |
// digits are '0'. |
| 1127 |
// everything else is direct equality. |
| 1128 |
'0', '0', '-', '0', '0', |
| 1129 |
'T', |
| 1130 |
'0', '0', ':', '0', '0', ':', '0', '0', |
| 1131 |
'Z', |
| 1132 |
} |
| 1133 |
for _, f := range formats { |
| 1134 |
r := lx.next() |
| 1135 |
if f == '0' { |
| 1136 |
if !unicode.IsDigit(r) { |
| 1137 |
return lx.errorf("Expected digit in ISO8601 datetime, "+ |
| 1138 |
"but found '%v' instead.", r) |
| 1139 |
} |
| 1140 |
} else if f != r { |
| 1141 |
return lx.errorf("Expected '%v' in ISO8601 datetime, "+ |
| 1142 |
"but found '%v' instead.", f, r) |
| 1143 |
} |
| 1144 |
} |
| 1145 |
lx.emit(itemDatetime) |
| 1146 |
return lx.pop() |
| 1147 |
} |
| 1148 |
|
| 1149 |
// lexNegNumberStart consumes either an integer or a float. It assumes that a |
| 1150 |
// negative sign has already been read, but that *no* digits have been consumed. |
| 1151 |
// lexNegNumberStart will move to the appropriate integer or float states. |
| 1152 |
func lexNegNumberStart(lx *lexer) stateFn { |
| 1153 |
// we MUST see a digit. Even floats have to start with a digit. |
| 1154 |
r := lx.next() |
| 1155 |
if !unicode.IsDigit(r) { |
| 1156 |
if r == '.' { |
| 1157 |
return lx.errorf("Floats must start with a digit, not '.'.") |
| 1158 |
} |
| 1159 |
return lx.errorf("Expected a digit but got '%v'.", r) |
| 1160 |
} |
| 1161 |
return lexNegNumber |
| 1162 |
} |
| 1163 |
|
| 1164 |
// lexNegNumber consumes a negative integer or a float after seeing the first digit. |
| 1165 |
func lexNegNumber(lx *lexer) stateFn { |
| 1166 |
r := lx.next() |
| 1167 |
switch { |
| 1168 |
case unicode.IsDigit(r): |
| 1169 |
return lexNegNumber |
| 1170 |
case r == '.': |
| 1171 |
return lexFloatStart |
| 1172 |
case isNumberSuffix(r): |
| 1173 |
return lexConvenientNumber |
| 1174 |
} |
| 1175 |
lx.backup() |
| 1176 |
lx.emit(itemInteger) |
| 1177 |
return lx.pop() |
| 1178 |
} |
| 1179 |
|
| 1180 |
// lexFloatStart starts the consumption of digits of a float after a '.'. |
| 1181 |
// Namely, at least one digit is required. |
| 1182 |
func lexFloatStart(lx *lexer) stateFn { |
| 1183 |
r := lx.next() |
| 1184 |
if !unicode.IsDigit(r) { |
| 1185 |
return lx.errorf("Floats must have a digit after the '.', but got "+ |
| 1186 |
"'%v' instead.", r) |
| 1187 |
} |
| 1188 |
return lexFloat |
| 1189 |
} |
| 1190 |
|
| 1191 |
// lexFloat consumes the digits of a float after a '.'. |
| 1192 |
// Assumes that one digit has been consumed after a '.' already. |
| 1193 |
func lexFloat(lx *lexer) stateFn { |
| 1194 |
r := lx.next() |
| 1195 |
if unicode.IsDigit(r) { |
| 1196 |
return lexFloat |
| 1197 |
} |
| 1198 |
|
| 1199 |
// Not a digit, if its another '.', need to see if we falsely assumed a float. |
| 1200 |
if r == '.' { |
| 1201 |
return lexIPAddr |
| 1202 |
} |
| 1203 |
|
| 1204 |
lx.backup() |
| 1205 |
lx.emit(itemFloat) |
| 1206 |
return lx.pop() |
| 1207 |
} |
| 1208 |
|
| 1209 |
// lexIPAddr consumes IP addrs, like 127.0.0.1:4222 |
| 1210 |
func lexIPAddr(lx *lexer) stateFn { |
| 1211 |
r := lx.next() |
| 1212 |
if unicode.IsDigit(r) || r == '.' || r == ':' || r == '-' { |
| 1213 |
return lexIPAddr |
| 1214 |
} |
| 1215 |
lx.backup() |
| 1216 |
lx.emit(itemString) |
| 1217 |
return lx.pop() |
| 1218 |
} |
| 1219 |
|
| 1220 |
// lexCommentStart begins the lexing of a comment. It will emit |
| 1221 |
// itemCommentStart and consume no characters, passing control to lexComment. |
| 1222 |
func lexCommentStart(lx *lexer) stateFn { |
| 1223 |
lx.ignore() |
| 1224 |
lx.emit(itemCommentStart) |
| 1225 |
return lexComment |
| 1226 |
} |
| 1227 |
|
| 1228 |
// lexComment lexes an entire comment. It assumes that '#' has been consumed. |
| 1229 |
// It will consume *up to* the first new line character, and pass control |
| 1230 |
// back to the last state on the stack. |
| 1231 |
func lexComment(lx *lexer) stateFn { |
| 1232 |
r := lx.peek() |
| 1233 |
if isNL(r) || r == eof { |
| 1234 |
lx.emit(itemText) |
| 1235 |
return lx.pop() |
| 1236 |
} |
| 1237 |
lx.next() |
| 1238 |
return lexComment |
| 1239 |
} |
| 1240 |
|
| 1241 |
// lexSkip ignores all slurped input and moves on to the next state. |
| 1242 |
func lexSkip(lx *lexer, nextState stateFn) stateFn { |
| 1243 |
return func(lx *lexer) stateFn { |
| 1244 |
lx.ignore() |
| 1245 |
return nextState |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
// Tests to see if we have a number suffix |
| 1250 |
func isNumberSuffix(r rune) bool { |
| 1251 |
return r == 'k' || r == 'K' || r == 'm' || r == 'M' || r == 'g' || r == 'G' || r == 't' || r == 'T' || r == 'p' || r == 'P' || r == 'e' || r == 'E' |
| 1252 |
} |
| 1253 |
|
| 1254 |
// Tests for both key separators |
| 1255 |
func isKeySeparator(r rune) bool { |
| 1256 |
return r == keySepEqual || r == keySepColon |
| 1257 |
} |
| 1258 |
|
| 1259 |
// isWhitespace returns true if `r` is a whitespace character according |
| 1260 |
// to the spec. |
| 1261 |
func isWhitespace(r rune) bool { |
| 1262 |
return r == '\t' || r == ' ' |
| 1263 |
} |
| 1264 |
|
| 1265 |
func isNL(r rune) bool { |
| 1266 |
return r == '\n' || r == '\r' |
| 1267 |
} |
| 1268 |
|
| 1269 |
func (itype itemType) String() string { |
| 1270 |
switch itype { |
| 1271 |
case itemError: |
| 1272 |
return "Error" |
| 1273 |
case itemNIL: |
| 1274 |
return "NIL" |
| 1275 |
case itemEOF: |
| 1276 |
return "EOF" |
| 1277 |
case itemText: |
| 1278 |
return "Text" |
| 1279 |
case itemString: |
| 1280 |
return "String" |
| 1281 |
case itemBool: |
| 1282 |
return "Bool" |
| 1283 |
case itemInteger: |
| 1284 |
return "Integer" |
| 1285 |
case itemFloat: |
| 1286 |
return "Float" |
| 1287 |
case itemDatetime: |
| 1288 |
return "DateTime" |
| 1289 |
case itemKey: |
| 1290 |
return "Key" |
| 1291 |
case itemArrayStart: |
| 1292 |
return "ArrayStart" |
| 1293 |
case itemArrayEnd: |
| 1294 |
return "ArrayEnd" |
| 1295 |
case itemMapStart: |
| 1296 |
return "MapStart" |
| 1297 |
case itemMapEnd: |
| 1298 |
return "MapEnd" |
| 1299 |
case itemCommentStart: |
| 1300 |
return "CommentStart" |
| 1301 |
case itemVariable: |
| 1302 |
return "Variable" |
| 1303 |
case itemInclude: |
| 1304 |
return "Include" |
| 1305 |
} |
| 1306 |
panic(fmt.Sprintf("BUG: Unknown type '%s'.", itype.String())) |
| 1307 |
} |
| 1308 |
|
| 1309 |
func (item item) String() string { |
| 1310 |
return fmt.Sprintf("(%s, '%s', %d, %d)", item.typ.String(), item.val, item.line, item.pos) |
| 1311 |
} |
| 1312 |
|
| 1313 |
func escapeSpecial(c rune) string { |
| 1314 |
switch c { |
| 1315 |
case '\n': |
| 1316 |
return "\\n" |
| 1317 |
} |
| 1318 |
return string(c) |
| 1319 |
} |