aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/tdewolff/parse/error.go
blob: 1e85f9b0b35443b16e1dc65222cceae812ad74ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package parse

import (
	"fmt"
	"io"

	"github.com/tdewolff/parse/buffer"
)

type Error struct {
	Message string
	Line    int
	Col     int
	Context string
}

func NewError(msg string, r io.Reader, offset int) *Error {
	line, col, context, _ := Position(r, offset)
	return &Error{
		msg,
		line,
		col,
		context,
	}
}

func NewErrorLexer(msg string, l *buffer.Lexer) *Error {
	r := buffer.NewReader(l.Bytes())
	offset := l.Offset()
	return NewError(msg, r, offset)
}

func (e *Error) Error() string {
	return fmt.Sprintf("parse error:%d:%d: %s\n%s", e.Line, e.Col, e.Message, e.Context)
}