hclparse

package
v2.0.0-rc6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2024 License: MPL-2.0 Imports: 5 Imported by: 7

Documentation

Overview

Package hclparse has the main API entry point for parsing both HCL native syntax and HCL JSON.

The main HCL package also includes SimpleParse and SimpleParseFile which can be a simpler interface for the common case where an application just needs to parse a single file. The gohcl package simplifies that further in its SimpleDecode function, which combines hcl.SimpleParse with decoding into Go struct values

Package hclparse, then, is useful for applications that require more fine control over parsing or which need to load many separate files and keep track of them for possible error reporting or other analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is the main interface for parsing configuration files. As well as parsing files, a parser also retains a registry of all of the files it has parsed so that multiple attempts to parse the same file will return the same object and so the collected files can be used when printing diagnostics.

Any diagnostics for parsing a file are only returned once on the first call to parse that file. Callers are expected to collect up diagnostics and present them together, so returning diagnostics for the same file multiple times would create a confusing result.

func NewParser

func NewParser() *Parser

NewParser creates a new parser, ready to parse configuration files.

func (*Parser) AddFile

func (p *Parser) AddFile(filename string, file *hcl.File)

AddFile allows a caller to record in a parser a file that was parsed some other way, thus allowing it to be included in the registry of sources.

func (*Parser) Files

func (p *Parser) Files() map[string]*hcl.File

Files returns a map from filenames to the File objects produced from them. This is intended to be used, for example, to print diagnostics with contextual information.

The returned map and all of the objects it refers to directly or indirectly must not be modified.

func (*Parser) ParseHCL

func (p *Parser) ParseHCL(src []byte, filename string) (*hcl.File, hcl.Diagnostics)

ParseHCL parses the given buffer (which is assumed to have been loaded from the given filename) as a native-syntax configuration file and returns the hcl.File object representing it.

func (*Parser) ParseHCLFile

func (p *Parser) ParseHCLFile(filename string) (*hcl.File, hcl.Diagnostics)

ParseHCLFile reads the given filename and parses it as a native-syntax HCL configuration file. An error diagnostic is returned if the given file cannot be read.

func (*Parser) ParseJSON

func (p *Parser) ParseJSON(src []byte, filename string) (*hcl.File, hcl.Diagnostics)

ParseJSON parses the given JSON buffer (which is assumed to have been loaded from the given filename) and returns the hcl.File object representing it.

func (*Parser) ParseJSONFile

func (p *Parser) ParseJSONFile(filename string) (*hcl.File, hcl.Diagnostics)

ParseJSONFile reads the given filename and parses it as JSON, similarly to ParseJSON. An error diagnostic is returned if the given file cannot be read.

func (*Parser) Sources

func (p *Parser) Sources() map[string][]byte

Sources returns a map from filenames to the raw source code that was read from them. This is intended to be used, for example, to print diagnostics with contextual information.

The arrays underlying the returned slices should not be modified.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL