ToDisplayString decorates the error message with the source location.
(source Source)
| 44 | |
| 45 | // ToDisplayString decorates the error message with the source location. |
| 46 | func (e *Error) ToDisplayString(source Source) string { |
| 47 | var result = fmt.Sprintf("ERROR: %s:%d:%d: %s", |
| 48 | source.Description(), |
| 49 | e.Location.Line(), |
| 50 | e.Location.Column()+1, // add one to the 0-based column for display |
| 51 | e.Message) |
| 52 | if snippet, found := source.Snippet(e.Location.Line()); found && len(snippet) <= maxSnippetLength { |
| 53 | snippet := strings.Replace(snippet, "\t", " ", -1) |
| 54 | srcLine := "\n | " + snippet |
| 55 | var bytes = []byte(snippet) |
| 56 | var indLine = "\n | " |
| 57 | for i := 0; i < e.Location.Column() && len(bytes) > 0; i++ { |
| 58 | _, sz := utf8.DecodeRune(bytes) |
| 59 | bytes = bytes[sz:] |
| 60 | if sz > 1 { |
| 61 | indLine += wideDot |
| 62 | } else { |
| 63 | indLine += dot |
| 64 | } |
| 65 | } |
| 66 | if _, sz := utf8.DecodeRune(bytes); sz > 1 { |
| 67 | indLine += wideInd |
| 68 | } else { |
| 69 | indLine += ind |
| 70 | } |
| 71 | result += srcLine + indLine |
| 72 | } |
| 73 | return result |
| 74 | } |