findPosInLine returns the parser position and the line number where the syntax error occurred when the expression is split over multiple lines.
(expr string, pos int)
| 41 | // where the syntax error occurred when the expression is split |
| 42 | // over multiple lines. |
| 43 | func findPosInLine(expr string, pos int) (int, int) { |
| 44 | ln := 1 |
| 45 | for i, c := range []rune(expr) { |
| 46 | if c == '\n' { |
| 47 | ln++ |
| 48 | } |
| 49 | if i == pos { |
| 50 | switch { |
| 51 | case ln > 1: |
| 52 | // multiline expression. Calculate |
| 53 | // the position relative to the line |
| 54 | // number by looking back for the |
| 55 | // previous newline terminator |
| 56 | j := pos |
| 57 | for expr[j] != '\n' { |
| 58 | j-- |
| 59 | // no newline found |
| 60 | if j == -1 { |
| 61 | break |
| 62 | } |
| 63 | } |
| 64 | return pos - j - 1, ln |
| 65 | default: |
| 66 | // single line expression |
| 67 | return pos, 1 |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return pos + 1, 1 |
| 72 | } |
| 73 | |
| 74 | type renderer struct { |
| 75 | strings.Builder |