A pattern has emerged of wrapping Expression instances with other Expressions in order to subtly modify their behavior. A key example of this is in ext/dynblock, where wrap an expression in order to introduce our additional iteration variable for expressions in dynamic blocks. Rather than having each wrapper expression implement wrapping implementations for our various syntax-level-analysis functions (like ExprList and AbsTraversalForExpr), instead we define a standard mechanism to unwrap expressions back to the lowest-level object -- usually an AST node -- and then use this in all of our analyses that look at the expression's structure rather than its value.
38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package hcl
|
|
|
|
// ExprList tests if the given expression is a static list construct and,
|
|
// if so, extracts the expressions that represent the list elements.
|
|
// If the given expression is not a static list, error diagnostics are
|
|
// returned.
|
|
//
|
|
// A particular Expression implementation can support this function by
|
|
// offering a method called ExprList that takes no arguments and returns
|
|
// []Expression. This method should return nil if a static list cannot
|
|
// be extracted. Alternatively, an implementation can support
|
|
// UnwrapExpression to delegate handling of this function to a wrapped
|
|
// Expression object.
|
|
func ExprList(expr Expression) ([]Expression, Diagnostics) {
|
|
type exprList interface {
|
|
ExprList() []Expression
|
|
}
|
|
|
|
physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool {
|
|
_, supported := expr.(exprList)
|
|
return supported
|
|
})
|
|
|
|
if exL, supported := physExpr.(exprList); supported {
|
|
if list := exL.ExprList(); list != nil {
|
|
return list, nil
|
|
}
|
|
}
|
|
return nil, Diagnostics{
|
|
&Diagnostic{
|
|
Severity: DiagError,
|
|
Summary: "Invalid expression",
|
|
Detail: "A static list expression is required.",
|
|
Subject: expr.StartRange().Ptr(),
|
|
},
|
|
}
|
|
}
|