4 legacyhcltoken "github.com/hashicorp/hcl/hcl/token"
5 "github.com/hashicorp/hcl2/hcl"
8 // SourcePos is a pointer to a particular location in a source file.
10 // This type is embedded into other structs to allow callers to locate the
11 // definition of each described module element. The SourcePos of an element
12 // is usually the first line of its definition, although the definition can
13 // be a little "fuzzy" with JSON-based config files.
14 type SourcePos struct {
15 Filename string `json:"filename"`
16 Line int `json:"line"`
19 func sourcePos(filename string, line int) SourcePos {
26 func sourcePosHCL(rng hcl.Range) SourcePos {
27 // We intentionally throw away the column information here because
28 // current and legacy HCL both disagree on the definition of a column
29 // and so a line-only reference is the best granularity we can do
30 // such that the result is consistent between both parsers.
32 Filename: rng.Filename,
37 func sourcePosLegacyHCL(pos legacyhcltoken.Pos, filename string) SourcePos {
38 useFilename := pos.Filename
39 // We'll try to use the filename given in legacy HCL position, but
40 // in practice there's no way to actually get this populated via
41 // the HCL API so it's usually empty except in some specialized
42 // situations, such as positions in error objects.
43 if useFilename == "" {
44 useFilename = filename
47 Filename: useFilename,