| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package tfconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
legacyhcltoken "github.com/hashicorp/hcl/hcl/token" |
| 8 |
"github.com/hashicorp/hcl/v2" |
| 9 |
) |
| 10 |
|
| 11 |
// SourcePos is a pointer to a particular location in a source file. |
| 12 |
// |
| 13 |
// This type is embedded into other structs to allow callers to locate the |
| 14 |
// definition of each described module element. The SourcePos of an element |
| 15 |
// is usually the first line of its definition, although the definition can |
| 16 |
// be a little "fuzzy" with JSON-based config files. |
| 17 |
type SourcePos struct { |
| 18 |
Filename string `json:"filename"` |
| 19 |
Line int `json:"line"` |
| 20 |
} |
| 21 |
|
| 22 |
func sourcePos(filename string, line int) SourcePos { |
| 23 |
return SourcePos{ |
| 24 |
Filename: filename, |
| 25 |
Line: line, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func sourcePosHCL(rng hcl.Range) SourcePos { |
| 30 |
// We intentionally throw away the column information here because |
| 31 |
// current and legacy HCL both disagree on the definition of a column |
| 32 |
// and so a line-only reference is the best granularity we can do |
| 33 |
// such that the result is consistent between both parsers. |
| 34 |
return SourcePos{ |
| 35 |
Filename: rng.Filename, |
| 36 |
Line: rng.Start.Line, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func sourcePosLegacyHCL(pos legacyhcltoken.Pos, filename string) SourcePos { |
| 41 |
useFilename := pos.Filename |
| 42 |
// We'll try to use the filename given in legacy HCL position, but |
| 43 |
// in practice there's no way to actually get this populated via |
| 44 |
// the HCL API so it's usually empty except in some specialized |
| 45 |
// situations, such as positions in error objects. |
| 46 |
if useFilename == "" { |
| 47 |
useFilename = filename |
| 48 |
} |
| 49 |
return SourcePos{ |
| 50 |
Filename: useFilename, |
| 51 |
Line: pos.Line, |
| 52 |
} |
| 53 |
} |