aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hil/parser/error.go
blob: bacd696457d272b096865f6e05bb1af194f92db1 (plain) (blame)
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
36
37
38
package parser

import (
	"fmt"

	"github.com/hashicorp/hil/ast"
	"github.com/hashicorp/hil/scanner"
)

type ParseError struct {
	Message string
	Pos     ast.Pos
}

func Errorf(pos ast.Pos, format string, args ...interface{}) error {
	return &ParseError{
		Message: fmt.Sprintf(format, args...),
		Pos:     pos,
	}
}

// TokenErrorf is a convenient wrapper around Errorf that uses the
// position of the given token.
func TokenErrorf(token *scanner.Token, format string, args ...interface{}) error {
	return Errorf(token.Pos, format, args...)
}

func ExpectationError(wanted string, got *scanner.Token) error {
	return TokenErrorf(got, "expected %s but found %s", wanted, got)
}

func (e *ParseError) Error() string {
	return fmt.Sprintf("parse error at %s: %s", e.Pos, e.Message)
}

func (e *ParseError) String() string {
	return e.Error()
}