Documentation
¶
Overview ¶
Package hcl2 is a pure-Go (CGO-free) from-scratch parser and evaluator for the HCL2 native syntax (HashiCorp Configuration Language v2), exposing its result as a Ruby value model. Parse turns a document into a lazy *Body (attributes and blocks, expressions unevaluated); Eval parses and evaluates a document against a variables/functions *Context, returning a Ruby Hash (an insertion-ordered *Map). It is the deterministic, interpreter-independent HCL2 backend a host such as go-embedded-ruby binds (HCL2.parse / HCL2.eval → Hash), with no Ruby runtime.
Faithfulness ¶
There is no canonical Ruby HCL gem to mirror, so this package is faithful to the HCL2 native-syntax specification (the hashicorp/hcl v2 grammar). It is a clean-room pure-Go implementation mirroring the structure and semantics of the org's from-scratch C reference, github.com/libhcl/c-hcl2; nothing from hashicorp/hcl is vendored.
Ruby value model ¶
An evaluated document is an [any] drawn from a small, fixed set of Go types so a host can map its own object graph to and from this package:
HCL2 Go (Eval returns) Ruby (rbgo maps to) ---- ----------------- ------------------- string / template string String number (integral) int64 Integer number (fractional) float64 Float bool bool true / false null nil nil tuple []any Array object *Map (insertion order) Hash
HCL has a single number type; this package narrows an integral number to int64 and a fractional one to float64 so the host materialises Integer vs Float naturally, exactly as it does for the TOML and JSON backends.
Expression AST view ¶
Parse yields structure with expressions left unevaluated, and Eval yields fully-evaluated values; neither exposes the *shape* of an expression. For tools that read expression structure without evaluating it — a transpiler emitting another language, for instance — Attribute.Expression returns a read-only, exported Expr tree (LiteralExpr, BinaryExpr, ForObjectExpr, …). Expr is a sealed interface, so the concrete node types in this package are the exhaustive set a consumer can type-switch over. The exported tree is a fresh view; the internal nodes and the Eval path are unaffected.
Index ¶
- type AttrExpr
- type Attribute
- type BinaryExpr
- type Block
- type Body
- type CallExpr
- type CondExpr
- type Context
- type Diagnostic
- type Diagnostics
- type Expr
- type ForObjectExpr
- type ForTupleExpr
- type Func
- type IndexExpr
- type LiteralExpr
- type Map
- type ObjectExpr
- type Pair
- type Pos
- type TemplateExpr
- type TupleExpr
- type UnaryExpr
- type Value
- type VarExpr
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attribute ¶
Attribute is a `name = expr` body entry. Its expression is stored unevaluated.
func (*Attribute) Expression ¶
Expression returns the attribute's parsed expression as an exported Expr tree. The result is a fresh read-only view; mutating it does not affect the attribute or its evaluation.
type BinaryExpr ¶
BinaryExpr is any binary operator. Op is the operator source text, e.g. "+", "==", "&&", "<=".
type Body ¶
Body is a parsed HCL2 configuration body: an ordered list of attributes and blocks, expressions left unevaluated for lazy decoding. Parse returns the document root *Body.
func Parse ¶
Parse parses an HCL2 native-syntax document into a lazy *Body without evaluating any expression, mirroring hclsyntax.ParseConfig. A syntax error is returned as Diagnostics.
type CallExpr ¶
CallExpr is a function call. Expand marks a trailing `...` spread on the last argument.
type CondExpr ¶
type CondExpr struct{ Cond, Then, Else Expr }
CondExpr is the `Cond ? Then : Else` conditional.
type Context ¶
Context carries the variables and functions an expression may reference. The zero value is usable; use NewContext for an initialised one. Variables map bare identifiers to Ruby values; Functions map call names to Func.
type Diagnostic ¶
Diagnostic is a single error reported by the parser or evaluator, carrying the source position of the offending token — mirroring HCL's hcl.Diagnostic.
func (*Diagnostic) Error ¶
func (d *Diagnostic) Error() string
Error renders the message with its `line:col` position.
type Diagnostics ¶
type Diagnostics []*Diagnostic
Diagnostics is a non-empty collection of Diagnostic, itself an error so it can flow through the standard error channel — mirroring hcl.Diagnostics.
func (Diagnostics) Error ¶
func (ds Diagnostics) Error() string
Error renders the first diagnostic, plus a count of any further ones, matching the way HCL surfaces the leading error to a CLI.
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is the exported HCL2 expression AST: a read-only view intended for transpilers and other tools that inspect expression structure. It is sealed — only the concrete node types in this package implement it.
type ForObjectExpr ¶
type ForObjectExpr struct {
KeyVar, ValVar string
Coll, KeyExpr, ValExpr Expr
Cond Expr // may be nil
Group bool
}
ForObjectExpr is `{for KeyVar, ValVar in Coll : KeyExpr => ValExpr if Cond}`. KeyVar may be empty, Cond may be nil, and Group marks a trailing `...` grouping mode.
type ForTupleExpr ¶
ForTupleExpr is `[for ValVar in Coll : Body if Cond]`. KeyVar is the optional index variable (empty if absent) and Cond may be nil.
type Func ¶
Func is a function callable from HCL expressions. It receives already-evaluated arguments (with any trailing `...` spread already flattened) and returns a Ruby value or an error. Returning a *Diagnostic preserves source position.
type IndexExpr ¶
type IndexExpr struct{ Coll, Idx Expr }
IndexExpr is `Coll[Idx]` index/element access.
type LiteralExpr ¶
type LiteralExpr struct{ Value Value }
LiteralExpr is a number, bool, or null literal. Value is already a final Ruby value (int64, float64, bool, or nil).
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an insertion-ordered Ruby Hash. Eval returns the document root and every object value as a *Map so key order round-trips into Ruby.
func Eval ¶
Eval parses src and evaluates it against ctx, returning the document as a Ruby Hash (an insertion-ordered *Map). A nil ctx is treated as an empty context. A syntax or evaluation error is returned as Diagnostics.
type ObjectExpr ¶
type ObjectExpr struct{ Keys, Vals []Expr }
ObjectExpr is `{ k = v, "k2" = v2 }`. Keys and Vals are parallel slices.
type Pos ¶
Pos is a 1-based source position. Line and Col are 1-based; Offset is the 0-based byte index in the document.
type TemplateExpr ¶
TemplateExpr is a quoted-string or heredoc template. Raw holds the raw inner bytes (interpreted at evaluation time); Heredoc is true for a heredoc.
