lex returns all the tokens and diagnostics after lexing source.
(source string)
| 352 | |
| 353 | // lex returns all the tokens and diagnostics after lexing source. |
| 354 | func lex(source string) ([]*Token, []Diagnostic, error) { |
| 355 | l := lexer{source: source, lexerState: lexerState{pos: Position{1, 1}}} |
| 356 | |
| 357 | lastPos := Position{} |
| 358 | for l.e == nil { |
| 359 | // Integrity check that the parser is making progress |
| 360 | if l.pos == lastPos { |
| 361 | log.Panicf("Parsing stuck at %v", l.pos) |
| 362 | } |
| 363 | lastPos = l.pos |
| 364 | |
| 365 | s := l.save() |
| 366 | r := l.next() |
| 367 | switch { |
| 368 | case r == '%': |
| 369 | l.restore(s) |
| 370 | l.pident() |
| 371 | case r == '+' || r == '-' || r == '_' || isAlphaNumeric(r): |
| 372 | l.restore(s) |
| 373 | l.numberOrIdent() |
| 374 | case r == '"': |
| 375 | l.restore(s) |
| 376 | l.string() |
| 377 | case r == '=', r == '|': |
| 378 | l.restore(s) |
| 379 | l.operator() |
| 380 | case r == ';': |
| 381 | l.restore(s) |
| 382 | l.lineComment() |
| 383 | case r == '\n': |
| 384 | l.restore(s) |
| 385 | l.newline() |
| 386 | } |
| 387 | } |
| 388 | if l.e != nil && l.e != io.EOF { |
| 389 | return nil, nil, l.e |
| 390 | } |
| 391 | return l.toks, l.diags, nil |
| 392 | } |
| 393 | |
| 394 | func isNumeric(r rune) bool { return unicode.IsDigit(r) } |
| 395 | func isAlpha(r rune) bool { return unicode.IsLetter(r) } |
no test coverage detected