|
6 | 6 | package parser
|
7 | 7 |
|
8 | 8 | import (
|
9 |
| - "bytes" |
10 | 9 | "fmt"
|
11 | 10 | "strings"
|
12 | 11 |
|
| 12 | + "github.com/cockroachdb/cockroach/pkg/sql/parserutils" |
13 | 13 | "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
|
14 | 14 | "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
|
15 | 15 | "github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
|
@@ -428,49 +428,9 @@ func (l *lexer) Error(e string) {
|
428 | 428 | l.populateErrorDetails()
|
429 | 429 | }
|
430 | 430 |
|
431 |
| -// PopulateErrorDetails properly wraps the "last error" field in the lexer. |
432 |
| -func PopulateErrorDetails( |
433 |
| - tokID int32, lastTokStr string, lastTokPos int32, lastErr error, lIn string, |
434 |
| -) error { |
435 |
| - var retErr error |
436 |
| - |
437 |
| - if tokID == ERROR { |
438 |
| - // This is a tokenizer (lexical) error: the scanner |
439 |
| - // will have stored the error message in the string field. |
440 |
| - err := pgerror.WithCandidateCode(errors.Newf("lexical error: %s", lastTokStr), pgcode.Syntax) |
441 |
| - retErr = errors.WithSecondaryError(err, lastErr) |
442 |
| - } else { |
443 |
| - // This is a contextual error. Print the provided error message |
444 |
| - // and the error context. |
445 |
| - if !strings.Contains(lastErr.Error(), "syntax error") { |
446 |
| - // "syntax error" is already prepended when the yacc-generated |
447 |
| - // parser encounters a parsing error. |
448 |
| - lastErr = errors.Wrap(lastErr, "syntax error") |
449 |
| - } |
450 |
| - retErr = errors.Wrapf(lastErr, "at or near \"%s\"", lastTokStr) |
451 |
| - } |
452 |
| - |
453 |
| - // Find the end of the line containing the last token. |
454 |
| - i := strings.IndexByte(lIn[lastTokPos:], '\n') |
455 |
| - if i == -1 { |
456 |
| - i = len(lIn) |
457 |
| - } else { |
458 |
| - i += int(lastTokPos) |
459 |
| - } |
460 |
| - // Find the beginning of the line containing the last token. Note that |
461 |
| - // LastIndexByte returns -1 if '\n' could not be found. |
462 |
| - j := strings.LastIndexByte(lIn[:lastTokPos], '\n') + 1 |
463 |
| - // Output everything up to and including the line containing the last token. |
464 |
| - var buf bytes.Buffer |
465 |
| - fmt.Fprintf(&buf, "source SQL:\n%s\n", lIn[:i]) |
466 |
| - // Output a caret indicating where the last token starts. |
467 |
| - fmt.Fprintf(&buf, "%s^", strings.Repeat(" ", int(lastTokPos)-j)) |
468 |
| - return errors.WithDetail(retErr, buf.String()) |
469 |
| -} |
470 |
| - |
471 | 431 | func (l *lexer) populateErrorDetails() {
|
472 | 432 | lastTok := l.lastToken()
|
473 |
| - l.lastError = PopulateErrorDetails(lastTok.id, lastTok.str, lastTok.pos, l.lastError, l.in) |
| 433 | + l.lastError = parserutils.PopulateErrorDetails(lastTok.id, ERROR, lastTok.str, lastTok.pos, l.lastError, l.in) |
474 | 434 | }
|
475 | 435 |
|
476 | 436 | // SetHelp marks the "last error" field in the lexer to become a
|
|
0 commit comments