extractPositionFromError attempts to extract line and column from error message
(errMsg, content string, defaultLine int)
| 506 | |
| 507 | // extractPositionFromError attempts to extract line and column from error message |
| 508 | func extractPositionFromError(errMsg, content string, defaultLine int) (line, char int) { |
| 509 | line = defaultLine |
| 510 | char = 0 |
| 511 | |
| 512 | // Try line:column pattern first (e.g., "at line 5, column 10") |
| 513 | if matches := lineColPattern.FindStringSubmatch(errMsg); len(matches) >= 2 { |
| 514 | if l, err := strconv.Atoi(matches[1]); err == nil { |
| 515 | line = l - 1 // Convert to 0-based |
| 516 | if line < 0 { |
| 517 | line = 0 |
| 518 | } |
| 519 | } |
| 520 | if len(matches) >= 3 && matches[2] != "" { |
| 521 | if c, err := strconv.Atoi(matches[2]); err == nil { |
| 522 | char = c - 1 // Convert to 0-based |
| 523 | if char < 0 { |
| 524 | char = 0 |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | return |
| 529 | } |
| 530 | |
| 531 | // Try bracket pattern (e.g., "[1:5]") |
| 532 | if matches := bracketPattern.FindStringSubmatch(errMsg); len(matches) >= 3 { |
| 533 | if l, err := strconv.Atoi(matches[1]); err == nil { |
| 534 | line = l - 1 |
| 535 | if line < 0 { |
| 536 | line = 0 |
| 537 | } |
| 538 | } |
| 539 | if c, err := strconv.Atoi(matches[2]); err == nil { |
| 540 | char = c - 1 |
| 541 | if char < 0 { |
| 542 | char = 0 |
| 543 | } |
| 544 | } |
| 545 | return |
| 546 | } |
| 547 | |
| 548 | // Try absolute position pattern (e.g., "position 42") |
| 549 | if matches := positionPattern.FindStringSubmatch(errMsg); len(matches) >= 2 { |
| 550 | if pos, err := strconv.Atoi(matches[1]); err == nil { |
| 551 | line, char = offsetToLineColumn(content, pos) |
| 552 | } |
| 553 | return |
| 554 | } |
| 555 | |
| 556 | return |
| 557 | } |
| 558 | |
| 559 | // offsetToLineColumn converts an absolute offset to line and column |
| 560 | func offsetToLineColumn(content string, offset int) (line, col int) { |