diff options
author | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
---|---|---|
committer | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
commit | 15c0b25d011f37e7c20aeca9eaf461f78285b8d9 (patch) | |
tree | 255c250a5c9d4801c74092d33b7337d8c14438ff /vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md | |
parent | 07971ca38143c5faf951d152fba370ddcbe26ad5 (diff) | |
download | terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.gz terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.zst terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.zip |
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
Updated via: go get github.com/hashicorp/terraform@sdk-v0.11-with-go-modules and go mod tidy
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md | 923 |
1 files changed, 923 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md new file mode 100644 index 0000000..49b9a3e --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/spec.md | |||
@@ -0,0 +1,923 @@ | |||
1 | # HCL Native Syntax Specification | ||
2 | |||
3 | This is the specification of the syntax and semantics of the native syntax | ||
4 | for HCL. HCL is a system for defining configuration languages for applications. | ||
5 | The HCL information model is designed to support multiple concrete syntaxes | ||
6 | for configuration, but this native syntax is considered the primary format | ||
7 | and is optimized for human authoring and maintenence, as opposed to machine | ||
8 | generation of configuration. | ||
9 | |||
10 | The language consists of three integrated sub-languages: | ||
11 | |||
12 | * The _structural_ language defines the overall heirarchical configuration | ||
13 | structure, and is a serialization of HCL bodies, blocks and attributes. | ||
14 | |||
15 | * The _expression_ language is used to express attribute values, either as | ||
16 | literals or as derivations of other values. | ||
17 | |||
18 | * The _template_ language is used to compose values together into strings, | ||
19 | as one of several types of expression in the expression language. | ||
20 | |||
21 | In normal use these three sub-languages are used together within configuration | ||
22 | files to describe an overall configuration, with the structural language | ||
23 | being used at the top level. The expression and template languages can also | ||
24 | be used in isolation, to implement features such as REPLs, debuggers, and | ||
25 | integration into more limited HCL syntaxes such as the JSON profile. | ||
26 | |||
27 | ## Syntax Notation | ||
28 | |||
29 | Within this specification a semi-formal notation is used to illustrate the | ||
30 | details of syntax. This notation is intended for human consumption rather | ||
31 | than machine consumption, with the following conventions: | ||
32 | |||
33 | * A naked name starting with an uppercase letter is a global production, | ||
34 | common to all of the syntax specifications in this document. | ||
35 | * A naked name starting with a lowercase letter is a local production, | ||
36 | meaningful only within the specification where it is defined. | ||
37 | * Double and single quotes (`"` and `'`) are used to mark literal character | ||
38 | sequences, which may be either punctuation markers or keywords. | ||
39 | * The default operator for combining items, which has no punctuation, | ||
40 | is concatenation. | ||
41 | * The symbol `|` indicates that any one of its left and right operands may | ||
42 | be present. | ||
43 | * The `*` symbol indicates zero or more repetitions of the item to its left. | ||
44 | * The `?` symbol indicates zero or one of the item to its left. | ||
45 | * Parentheses (`(` and `)`) are used to group items together to apply | ||
46 | the `|`, `*` and `?` operators to them collectively. | ||
47 | |||
48 | The grammar notation does not fully describe the language. The prose may | ||
49 | augment or conflict with the illustrated grammar. In case of conflict, prose | ||
50 | has priority. | ||
51 | |||
52 | ## Source Code Representation | ||
53 | |||
54 | Source code is unicode text expressed in the UTF-8 encoding. The language | ||
55 | itself does not perform unicode normalization, so syntax features such as | ||
56 | identifiers are sequences of unicode code points and so e.g. a precombined | ||
57 | accented character is distinct from a letter associated with a combining | ||
58 | accent. (String literals have some special handling with regard to Unicode | ||
59 | normalization which will be covered later in the relevant section.) | ||
60 | |||
61 | UTF-8 encoded Unicode byte order marks are not permitted. Invalid or | ||
62 | non-normalized UTF-8 encoding is always a parse error. | ||
63 | |||
64 | ## Lexical Elements | ||
65 | |||
66 | ### Comments and Whitespace | ||
67 | |||
68 | Comments and Whitespace are recognized as lexical elements but are ignored | ||
69 | except as described below. | ||
70 | |||
71 | Whitespace is defined as a sequence of zero or more space characters | ||
72 | (U+0020). Newline sequences (either U+000A or U+000D followed by U+000A) | ||
73 | are _not_ considered whitespace but are ignored as such in certain contexts. | ||
74 | |||
75 | Horizontal tab characters (U+0009) are not considered to be whitespace and | ||
76 | are not valid within HCL native syntax. | ||
77 | |||
78 | Comments serve as program documentation and come in two forms: | ||
79 | |||
80 | * _Line comments_ start with either the `//` or `#` sequences and end with | ||
81 | the next newline sequence. A line comments is considered equivalent to a | ||
82 | newline sequence. | ||
83 | |||
84 | * _Inline comments_ start with the `/*` sequence and end with the `*/` | ||
85 | sequence, and may have any characters within except the ending sequence. | ||
86 | An inline comments is considered equivalent to a whitespace sequence. | ||
87 | |||
88 | Comments and whitespace cannot begin within within other comments, or within | ||
89 | template literals except inside an interpolation sequence or template directive. | ||
90 | |||
91 | ### Identifiers | ||
92 | |||
93 | Identifiers name entities such as blocks, attributes and expression variables. | ||
94 | Identifiers are interpreted as per [UAX #31][UAX31] Section 2. Specifically, | ||
95 | their syntax is defined in terms of the `ID_Start` and `ID_Continue` | ||
96 | character properties as follows: | ||
97 | |||
98 | ```ebnf | ||
99 | Identifier = ID_Start (ID_Continue | '-')*; | ||
100 | ``` | ||
101 | |||
102 | The Unicode specification provides the normative requirements for identifier | ||
103 | parsing. Non-normatively, the spirit of this specification is that `ID_Start` | ||
104 | consists of Unicode letter and certain unambiguous punctuation tokens, while | ||
105 | `ID_Continue` augments that set with Unicode digits, combining marks, etc. | ||
106 | |||
107 | The dash character `-` is additionally allowed in identifiers, even though | ||
108 | that is not part of the unicode `ID_Continue` definition. This is to allow | ||
109 | attribute names and block type names to contain dashes, although underscores | ||
110 | as word separators are considered the idiomatic usage. | ||
111 | |||
112 | [UAX31]: http://unicode.org/reports/tr31/ "Unicode Identifier and Pattern Syntax" | ||
113 | |||
114 | ### Keywords | ||
115 | |||
116 | There are no globally-reserved words, but in some contexts certain identifiers | ||
117 | are reserved to function as keywords. These are discussed further in the | ||
118 | relevant documentation sections that follow. In such situations, the | ||
119 | identifier's role as a keyword supersedes any other valid interpretation that | ||
120 | may be possible. Outside of these specific situations, the keywords have no | ||
121 | special meaning and are interpreted as regular identifiers. | ||
122 | |||
123 | ### Operators and Delimiters | ||
124 | |||
125 | The following character sequences represent operators, delimiters, and other | ||
126 | special tokens: | ||
127 | |||
128 | ``` | ||
129 | + && == < : { [ ( ${ | ||
130 | - || != > ? } ] ) %{ | ||
131 | * ! <= = . | ||
132 | / >= => , | ||
133 | % ... | ||
134 | ``` | ||
135 | |||
136 | ### Numeric Literals | ||
137 | |||
138 | A numeric literal is a decimal representation of a | ||
139 | real number. It has an integer part, a fractional part, | ||
140 | and an exponent part. | ||
141 | |||
142 | ```ebnf | ||
143 | NumericLit = decimal+ ("." decimal+)? (expmark decimal+)?; | ||
144 | decimal = '0' .. '9'; | ||
145 | expmark = ('e' | 'E') ("+" | "-")?; | ||
146 | ``` | ||
147 | |||
148 | ## Structural Elements | ||
149 | |||
150 | The structural language consists of syntax representing the following | ||
151 | constructs: | ||
152 | |||
153 | * _Attributes_, which assign a value to a specified name. | ||
154 | * _Blocks_, which create a child body annotated by a type and optional labels. | ||
155 | * _Body Content_, which consists of a collection of attributes and blocks. | ||
156 | |||
157 | These constructs correspond to the similarly-named concepts in the | ||
158 | language-agnostic HCL information model. | ||
159 | |||
160 | ```ebnf | ||
161 | ConfigFile = Body; | ||
162 | Body = (Attribute | Block)*; | ||
163 | Attribute = Identifier "=" Expression Newline; | ||
164 | Block = Identifier (StringLit|Identifier)* "{" Newline Body "}" Newline; | ||
165 | ``` | ||
166 | |||
167 | ### Configuration Files | ||
168 | |||
169 | A _configuration file_ is a sequence of characters whose top-level is | ||
170 | interpreted as a Body. | ||
171 | |||
172 | ### Bodies | ||
173 | |||
174 | A _body_ is a collection of associated attributes and blocks. The meaning of | ||
175 | this association is defined by the calling application. | ||
176 | |||
177 | ### Attribute Definitions | ||
178 | |||
179 | An _attribute definition_ assigns a value to a particular attribute name within | ||
180 | a body. Each distinct attribute name may be defined no more than once within a | ||
181 | single body. | ||
182 | |||
183 | The attribute value is given as an expression, which is retained literally | ||
184 | for later evaluation by the calling application. | ||
185 | |||
186 | ### Blocks | ||
187 | |||
188 | A _block_ creates a child body that is annotated with a block _type_ and | ||
189 | zero or more block _labels_. Blocks create a structural heirachy which can be | ||
190 | interpreted by the calling application. | ||
191 | |||
192 | Block labels can either be quoted literal strings or naked identifiers. | ||
193 | |||
194 | ## Expressions | ||
195 | |||
196 | The expression sub-language is used within attribute definitions to specify | ||
197 | values. | ||
198 | |||
199 | ```ebnf | ||
200 | Expression = ( | ||
201 | ExprTerm | | ||
202 | Operation | | ||
203 | Conditional | ||
204 | ); | ||
205 | ``` | ||
206 | |||
207 | ### Types | ||
208 | |||
209 | The value types used within the expression language are those defined by the | ||
210 | syntax-agnostic HCL information model. An expression may return any valid | ||
211 | type, but only a subset of the available types have first-class syntax. | ||
212 | A calling application may make other types available via _variables_ and | ||
213 | _functions_. | ||
214 | |||
215 | ### Expression Terms | ||
216 | |||
217 | Expression _terms_ are the operands for unary and binary expressions, as well | ||
218 | as acting as expressions in their own right. | ||
219 | |||
220 | ```ebnf | ||
221 | ExprTerm = ( | ||
222 | LiteralValue | | ||
223 | CollectionValue | | ||
224 | TemplateExpr | | ||
225 | VariableExpr | | ||
226 | FunctionCall | | ||
227 | ForExpr | | ||
228 | ExprTerm Index | | ||
229 | ExprTerm GetAttr | | ||
230 | ExprTerm Splat | | ||
231 | "(" Expression ")" | ||
232 | ); | ||
233 | ``` | ||
234 | |||
235 | The productions for these different term types are given in their corresponding | ||
236 | sections. | ||
237 | |||
238 | Between the `(` and `)` characters denoting a sub-expression, newline | ||
239 | characters are ignored as whitespace. | ||
240 | |||
241 | ### Literal Values | ||
242 | |||
243 | A _literal value_ immediately represents a particular value of a primitive | ||
244 | type. | ||
245 | |||
246 | ```ebnf | ||
247 | LiteralValue = ( | ||
248 | NumericLit | | ||
249 | "true" | | ||
250 | "false" | | ||
251 | "null" | ||
252 | ); | ||
253 | ``` | ||
254 | |||
255 | * Numeric literals represent values of type _number_. | ||
256 | * The `true` and `false` keywords represent values of type _bool_. | ||
257 | * The `null` keyword represents a null value of the dynamic pseudo-type. | ||
258 | |||
259 | String literals are not directly available in the expression sub-language, but | ||
260 | are available via the template sub-language, which can in turn be incorporated | ||
261 | via _template expressions_. | ||
262 | |||
263 | ### Collection Values | ||
264 | |||
265 | A _collection value_ combines zero or more other expressions to produce a | ||
266 | collection value. | ||
267 | |||
268 | ```ebnf | ||
269 | CollectionValue = tuple | object; | ||
270 | tuple = "[" ( | ||
271 | (Expression ("," Expression)* ","?)? | ||
272 | ) "]"; | ||
273 | object = "{" ( | ||
274 | (objectelem ("," objectelem)* ","?)? | ||
275 | ) "}"; | ||
276 | objectelem = (Identifier | Expression) "=" Expression; | ||
277 | ``` | ||
278 | |||
279 | Only tuple and object values can be directly constructed via native syntax. | ||
280 | Tuple and object values can in turn be converted to list, set and map values | ||
281 | with other operations, which behaves as defined by the syntax-agnostic HCL | ||
282 | information model. | ||
283 | |||
284 | When specifying an object element, an identifier is interpreted as a literal | ||
285 | attribute name as opposed to a variable reference. To populate an item key | ||
286 | from a variable, use parentheses to disambiguate: | ||
287 | |||
288 | * `{foo = "baz"}` is interpreted as an attribute literally named `foo`. | ||
289 | * `{(foo) = "baz"}` is interpreted as an attribute whose name is taken | ||
290 | from the variable named `foo`. | ||
291 | |||
292 | Between the open and closing delimiters of these sequences, newline sequences | ||
293 | are ignored as whitespace. | ||
294 | |||
295 | There is a syntax ambiguity between _for expressions_ and collection values | ||
296 | whose first element is a reference to a variable named `for`. The | ||
297 | _for expression_ interpretation has priority, so to produce a tuple whose | ||
298 | first element is the value of a variable named `for`, or an object with a | ||
299 | key named `for`, use paretheses to disambiguate: | ||
300 | |||
301 | * `[for, foo, baz]` is a syntax error. | ||
302 | * `[(for), foo, baz]` is a tuple whose first element is the value of variable | ||
303 | `for`. | ||
304 | * `{for: 1, baz: 2}` is a syntax error. | ||
305 | * `{(for): 1, baz: 2}` is an object with an attribute literally named `for`. | ||
306 | * `{baz: 2, for: 1}` is equivalent to the previous example, and resolves the | ||
307 | ambiguity by reordering. | ||
308 | |||
309 | ### Template Expressions | ||
310 | |||
311 | A _template expression_ embeds a program written in the template sub-language | ||
312 | as an expression. Template expressions come in two forms: | ||
313 | |||
314 | * A _quoted_ template expression is delimited by quote characters (`"`) and | ||
315 | defines a template as a single-line expression with escape characters. | ||
316 | * A _heredoc_ template expression is introduced by a `<<` sequence and | ||
317 | defines a template via a multi-line sequence terminated by a user-chosen | ||
318 | delimiter. | ||
319 | |||
320 | In both cases the template interpolation and directive syntax is available for | ||
321 | use within the delimiters, and any text outside of these special sequences is | ||
322 | interpreted as a literal string. | ||
323 | |||
324 | In _quoted_ template expressions any literal string sequences within the | ||
325 | template behave in a special way: literal newline sequences are not permitted | ||
326 | and instead _escape sequences_ can be included, starting with the | ||
327 | backslash `\`: | ||
328 | |||
329 | ``` | ||
330 | \n Unicode newline control character | ||
331 | \r Unicode carriage return control character | ||
332 | \t Unicode tab control character | ||
333 | \" Literal quote mark, used to prevent interpretation as end of string | ||
334 | \\ Literal backslash, used to prevent interpretation as escape sequence | ||
335 | \uNNNN Unicode character from Basic Multilingual Plane (NNNN is four hexadecimal digits) | ||
336 | \UNNNNNNNN Unicode character from supplementary planes (NNNNNNNN is eight hexadecimal digits) | ||
337 | ``` | ||
338 | |||
339 | The _heredoc_ template expression type is introduced by either `<<` or `<<-`, | ||
340 | followed by an identifier. The template expression ends when the given | ||
341 | identifier subsequently appears again on a line of its own. | ||
342 | |||
343 | If a heredoc template is introduced with the `<<-` symbol, any literal string | ||
344 | at the start of each line is analyzed to find the minimum number of leading | ||
345 | spaces, and then that number of prefix spaces is removed from all line-leading | ||
346 | literal strings. The final closing marker may also have an arbitrary number | ||
347 | of spaces preceding it on its line. | ||
348 | |||
349 | ```ebnf | ||
350 | TemplateExpr = quotedTemplate | heredocTemplate; | ||
351 | quotedTemplate = (as defined in prose above); | ||
352 | heredocTemplate = ( | ||
353 | ("<<" | "<<-") Identifier Newline | ||
354 | (content as defined in prose above) | ||
355 | Identifier Newline | ||
356 | ); | ||
357 | ``` | ||
358 | |||
359 | A quoted template expression containing only a single literal string serves | ||
360 | as a syntax for defining literal string _expressions_. In certain contexts | ||
361 | the template syntax is restricted in this manner: | ||
362 | |||
363 | ```ebnf | ||
364 | StringLit = '"' (quoted literals as defined in prose above) '"'; | ||
365 | ``` | ||
366 | |||
367 | The `StringLit` production permits the escape sequences discussed for quoted | ||
368 | template expressions as above, but does _not_ permit template interpolation | ||
369 | or directive sequences. | ||
370 | |||
371 | ### Variables and Variable Expressions | ||
372 | |||
373 | A _variable_ is a value that has been assigned a symbolic name. Variables are | ||
374 | made available for use in expressions by the calling application, by populating | ||
375 | the _global scope_ used for expression evaluation. | ||
376 | |||
377 | Variables can also be created by expressions themselves, which always creates | ||
378 | a _child scope_ that incorporates the variables from its parent scope but | ||
379 | (re-)defines zero or more names with new values. | ||
380 | |||
381 | The value of a variable is accessed using a _variable expression_, which is | ||
382 | a standalone `Identifier` whose name corresponds to a defined variable: | ||
383 | |||
384 | ```ebnf | ||
385 | VariableExpr = Identifier; | ||
386 | ``` | ||
387 | |||
388 | Variables in a particular scope are immutable, but child scopes may _hide_ | ||
389 | a variable from an ancestor scope by defining a new variable of the same name. | ||
390 | When looking up variables, the most locally-defined variable of the given name | ||
391 | is used, and ancestor-scoped variables of the same name cannot be accessed. | ||
392 | |||
393 | No direct syntax is provided for declaring or assigning variables, but other | ||
394 | expression constructs implicitly create child scopes and define variables as | ||
395 | part of their evaluation. | ||
396 | |||
397 | ### Functions and Function Calls | ||
398 | |||
399 | A _function_ is an operation that has been assigned a symbolic name. Functions | ||
400 | are made available for use in expressions by the calling application, by | ||
401 | populating the _function table_ used for expression evaluation. | ||
402 | |||
403 | The namespace of functions is distinct from the namespace of variables. A | ||
404 | function and a variable may share the same name with no implication that they | ||
405 | are in any way related. | ||
406 | |||
407 | A function can be executed via a _function call_ expression: | ||
408 | |||
409 | ```ebnf | ||
410 | FunctionCall = Identifier "(" arguments ")"; | ||
411 | Arguments = ( | ||
412 | () || | ||
413 | (Expression ("," Expression)* ("," | "...")?) | ||
414 | ); | ||
415 | ``` | ||
416 | |||
417 | The definition of functions and the semantics of calling them are defined by | ||
418 | the language-agnostic HCL information model. The given arguments are mapped | ||
419 | onto the function's _parameters_ and the result of a function call expression | ||
420 | is the return value of the named function when given those arguments. | ||
421 | |||
422 | If the final argument expression is followed by the ellipsis symbol (`...`), | ||
423 | the final argument expression must evaluate to either a list or tuple value. | ||
424 | The elements of the value are each mapped to a single parameter of the | ||
425 | named function, beginning at the first parameter remaining after all other | ||
426 | argument expressions have been mapped. | ||
427 | |||
428 | Within the parentheses that delimit the function arguments, newline sequences | ||
429 | are ignored as whitespace. | ||
430 | |||
431 | ### For Expressions | ||
432 | |||
433 | A _for expression_ is a construct for constructing a collection by projecting | ||
434 | the items from another collection. | ||
435 | |||
436 | ```ebnf | ||
437 | ForExpr = forTupleExpr | forObjectExpr; | ||
438 | forTupleExpr = "[" forIntro Expression forCond? "]"; | ||
439 | forObjectExpr = "{" forIntro Expression "=>" Expression "..."? forCond? "}"; | ||
440 | forIntro = "for" Identifier ("," Identifier)? "in" Expression ":"; | ||
441 | forCond = "if" Expression; | ||
442 | ``` | ||
443 | |||
444 | The punctuation used to delimit a for expression decide whether it will produce | ||
445 | a tuple value (`[` and `]`) or an object value (`{` and `}`). | ||
446 | |||
447 | The "introduction" is equivalent in both cases: the keyword `for` followed by | ||
448 | either one or two identifiers separated by a comma which define the temporary | ||
449 | variable names used for iteration, followed by the keyword `in` and then | ||
450 | an expression that must evaluate to a value that can be iterated. The | ||
451 | introduction is then terminated by the colon (`:`) symbol. | ||
452 | |||
453 | If only one identifier is provided, it is the name of a variable that will | ||
454 | be temporarily assigned the value of each element during iteration. If both | ||
455 | are provided, the first is the key and the second is the value. | ||
456 | |||
457 | Tuple, object, list, map, and set types are iterable. The type of collection | ||
458 | used defines how the key and value variables are populated: | ||
459 | |||
460 | * For tuple and list types, the _key_ is the zero-based index into the | ||
461 | sequence for each element, and the _value_ is the element value. The | ||
462 | elements are visited in index order. | ||
463 | * For object and map types, the _key_ is the string attribute name or element | ||
464 | key, and the _value_ is the attribute or element value. The elements are | ||
465 | visited in the order defined by a lexicographic sort of the attribute names | ||
466 | or keys. | ||
467 | * For set types, the _key_ and _value_ are both the element value. The elements | ||
468 | are visited in an undefined but consistent order. | ||
469 | |||
470 | The expression after the colon and (in the case of object `for`) the expression | ||
471 | after the `=>` are both evaluated once for each element of the source | ||
472 | collection, in a local scope that defines the key and value variable names | ||
473 | specified. | ||
474 | |||
475 | The results of evaluating these expressions for each input element are used | ||
476 | to populate an element in the new collection. In the case of tuple `for`, the | ||
477 | single expression becomes an element, appending values to the tuple in visit | ||
478 | order. In the case of object `for`, the pair of expressions is used as an | ||
479 | attribute name and value respectively, creating an element in the resulting | ||
480 | object. | ||
481 | |||
482 | In the case of object `for`, it is an error if two input elements produce | ||
483 | the same result from the attribute name expression, since duplicate | ||
484 | attributes are not possible. If the ellipsis symbol (`...`) appears | ||
485 | immediately after the value experssion, this activates the grouping mode in | ||
486 | which each value in the resulting object is a _tuple_ of all of the values | ||
487 | that were produced against each distinct key. | ||
488 | |||
489 | * `[for v in ["a", "b"]: v]` returns `["a", "b"]`. | ||
490 | * `[for i, v in ["a", "b"]: i]` returns `[0, 1]`. | ||
491 | * `{for i, v in ["a", "b"]: v => i}` returns `{a = 0, b = 1}`. | ||
492 | * `{for i, v in ["a", "a", "b"]: k => v}` produces an error, because attribute | ||
493 | `a` is defined twice. | ||
494 | * `{for i, v in ["a", "a", "b"]: v => i...}` returns `{a = [0, 1], b = [2]}`. | ||
495 | |||
496 | If the `if` keyword is used after the element expression(s), it applies an | ||
497 | additional predicate that can be used to conditionally filter elements from | ||
498 | the source collection from consideration. The expression following `if` is | ||
499 | evaluated once for each source element, in the same scope used for the | ||
500 | element expression(s). It must evaluate to a boolean value; if `true`, the | ||
501 | element will be evaluated as normal, while if `false` the element will be | ||
502 | skipped. | ||
503 | |||
504 | * `[for i, v in ["a", "b", "c"]: v if i < 2]` returns `["a", "b"]`. | ||
505 | |||
506 | If the collection value, element expression(s) or condition expression return | ||
507 | unknown values that are otherwise type-valid, the result is a value of the | ||
508 | dynamic pseudo-type. | ||
509 | |||
510 | ### Index Operator | ||
511 | |||
512 | The _index_ operator returns the value of a single element of a collection | ||
513 | value. It is a postfix operator and can be applied to any value that has | ||
514 | a tuple, object, map, or list type. | ||
515 | |||
516 | ```ebnf | ||
517 | Index = "[" Expression "]"; | ||
518 | ``` | ||
519 | |||
520 | The expression delimited by the brackets is the _key_ by which an element | ||
521 | will be looked up. | ||
522 | |||
523 | If the index operator is applied to a value of tuple or list type, the | ||
524 | key expression must be an non-negative integer number representing the | ||
525 | zero-based element index to access. If applied to a value of object or map | ||
526 | type, the key expression must be a string representing the attribute name | ||
527 | or element key. If the given key value is not of the appropriate type, a | ||
528 | conversion is attempted using the conversion rules from the HCL | ||
529 | syntax-agnostic information model. | ||
530 | |||
531 | An error is produced if the given key expression does not correspond to | ||
532 | an element in the collection, either because it is of an unconvertable type, | ||
533 | because it is outside the range of elements for a tuple or list, or because | ||
534 | the given attribute or key does not exist. | ||
535 | |||
536 | If either the collection or the key are an unknown value of an | ||
537 | otherwise-suitable type, the return value is an unknown value whose type | ||
538 | matches what type would be returned given known values, or a value of the | ||
539 | dynamic pseudo-type if type information alone cannot determine a suitable | ||
540 | return type. | ||
541 | |||
542 | Within the brackets that delimit the index key, newline sequences are ignored | ||
543 | as whitespace. | ||
544 | |||
545 | ### Attribute Access Operator | ||
546 | |||
547 | The _attribute access_ operator returns the value of a single attribute in | ||
548 | an object value. It is a postfix operator and can be applied to any value | ||
549 | that has an object type. | ||
550 | |||
551 | ```ebnf | ||
552 | GetAttr = "." Identifier; | ||
553 | ``` | ||
554 | |||
555 | The given identifier is interpreted as the name of the attribute to access. | ||
556 | An error is produced if the object to which the operator is applied does not | ||
557 | have an attribute with the given name. | ||
558 | |||
559 | If the object is an unknown value of a type that has the attribute named, the | ||
560 | result is an unknown value of the attribute's type. | ||
561 | |||
562 | ### Splat Operators | ||
563 | |||
564 | The _splat operators_ allow convenient access to attributes or elements of | ||
565 | elements in a tuple, list, or set value. | ||
566 | |||
567 | There are two kinds of "splat" operator: | ||
568 | |||
569 | * The _attribute-only_ splat operator supports only attribute lookups into | ||
570 | the elements from a list, but supports an arbitrary number of them. | ||
571 | |||
572 | * The _full_ splat operator additionally supports indexing into the elements | ||
573 | from a list, and allows any combination of attribute access and index | ||
574 | operations. | ||
575 | |||
576 | ```ebnf | ||
577 | Splat = attrSplat | fullSplat; | ||
578 | attrSplat = "." "*" GetAttr*; | ||
579 | fullSplat = "[" "*" "]" (GetAttr | Index)*; | ||
580 | ``` | ||
581 | |||
582 | The splat operators can be thought of as shorthands for common operations that | ||
583 | could otherwise be performed using _for expressions_: | ||
584 | |||
585 | * `tuple.*.foo.bar[0]` is approximately equivalent to | ||
586 | `[for v in tuple: v.foo.bar][0]`. | ||
587 | * `tuple[*].foo.bar[0]` is approximately equivalent to | ||
588 | `[for v in tuple: v.foo.bar[0]]` | ||
589 | |||
590 | Note the difference in how the trailing index operator is interpreted in | ||
591 | each case. This different interpretation is the key difference between the | ||
592 | _attribute-only_ and _full_ splat operators. | ||
593 | |||
594 | Splat operators have one additional behavior compared to the equivalent | ||
595 | _for expressions_ shown above: if a splat operator is applied to a value that | ||
596 | is _not_ of tuple, list, or set type, the value is coerced automatically into | ||
597 | a single-value list of the value type: | ||
598 | |||
599 | * `any_object.*.id` is equivalent to `[any_object.id]`, assuming that `any_object` | ||
600 | is a single object. | ||
601 | * `any_number.*` is equivalent to `[any_number]`, assuming that `any_number` | ||
602 | is a single number. | ||
603 | |||
604 | If the left operand of a splat operator is an unknown value of any type, the | ||
605 | result is a value of the dynamic pseudo-type. | ||
606 | |||
607 | ### Operations | ||
608 | |||
609 | Operations apply a particular operator to either one or two expression terms. | ||
610 | |||
611 | ```ebnf | ||
612 | Operation = unaryOp | binaryOp; | ||
613 | unaryOp = ("-" | "!") ExprTerm; | ||
614 | binaryOp = ExprTerm binaryOperator ExprTerm; | ||
615 | binaryOperator = compareOperator | arithmeticOperator | logicOperator; | ||
616 | compareOperator = "==" | "!=" | "<" | ">" | "<=" | ">="; | ||
617 | arithmeticOperator = "+" | "-" | "*" | "/" | "%"; | ||
618 | logicOperator = "&&" | "||" | "!"; | ||
619 | ``` | ||
620 | |||
621 | The unary operators have the highest precedence. | ||
622 | |||
623 | The binary operators are grouped into the following precedence levels: | ||
624 | |||
625 | ``` | ||
626 | Level Operators | ||
627 | 6 * / % | ||
628 | 5 + - | ||
629 | 4 > >= < <= | ||
630 | 3 == != | ||
631 | 2 && | ||
632 | 1 || | ||
633 | ``` | ||
634 | |||
635 | Higher values of "level" bind tighter. Operators within the same precedence | ||
636 | level have left-to-right associativity. For example, `x / y * z` is equivalent | ||
637 | to `(x / y) * z`. | ||
638 | |||
639 | ### Comparison Operators | ||
640 | |||
641 | Comparison operators always produce boolean values, as a result of testing | ||
642 | the relationship between two values. | ||
643 | |||
644 | The two equality operators apply to values of any type: | ||
645 | |||
646 | ``` | ||
647 | a == b equal | ||
648 | a != b not equal | ||
649 | ``` | ||
650 | |||
651 | Two values are equal if the are of identical types and their values are | ||
652 | equal as defined in the HCL syntax-agnostic information model. The equality | ||
653 | operators are commutative and opposite, such that `(a == b) == !(a != b)` | ||
654 | and `(a == b) == (b == a)` for all values `a` and `b`. | ||
655 | |||
656 | The four numeric comparison operators apply only to numbers: | ||
657 | |||
658 | ``` | ||
659 | a < b less than | ||
660 | a <= b less than or equal to | ||
661 | a > b greater than | ||
662 | a >= b greater than or equal to | ||
663 | ``` | ||
664 | |||
665 | If either operand of a comparison operator is a correctly-typed unknown value | ||
666 | or a value of the dynamic pseudo-type, the result is an unknown boolean. | ||
667 | |||
668 | ### Arithmetic Operators | ||
669 | |||
670 | Arithmetic operators apply only to number values and always produce number | ||
671 | values as results. | ||
672 | |||
673 | ``` | ||
674 | a + b sum (addition) | ||
675 | a - b difference (subtraction) | ||
676 | a * b product (multiplication) | ||
677 | a / b quotient (division) | ||
678 | a % b remainder (modulo) | ||
679 | -a negation | ||
680 | ``` | ||
681 | |||
682 | Arithmetic operations are considered to be performed in an arbitrary-precision | ||
683 | number space. | ||
684 | |||
685 | If either operand of an arithmetic operator is an unknown number or a value | ||
686 | of the dynamic pseudo-type, the result is an unknown number. | ||
687 | |||
688 | ### Logic Operators | ||
689 | |||
690 | Logic operators apply only to boolean values and always produce boolean values | ||
691 | as results. | ||
692 | |||
693 | ``` | ||
694 | a && b logical AND | ||
695 | a || b logical OR | ||
696 | !a logical NOT | ||
697 | ``` | ||
698 | |||
699 | If either operand of a logic operator is an unknown bool value or a value | ||
700 | of the dynamic pseudo-type, the result is an unknown bool value. | ||
701 | |||
702 | ### Conditional Operator | ||
703 | |||
704 | The conditional operator allows selecting from one of two expressions based on | ||
705 | the outcome of a boolean expression. | ||
706 | |||
707 | ```ebnf | ||
708 | Conditional = Expression "?" Expression ":" Expression; | ||
709 | ``` | ||
710 | |||
711 | The first expression is the _predicate_, which is evaluated and must produce | ||
712 | a boolean result. If the predicate value is `true`, the result of the second | ||
713 | expression is the result of the conditional. If the predicate value is | ||
714 | `false`, the result of the third expression is the result of the conditional. | ||
715 | |||
716 | The second and third expressions must be of the same type or must be able to | ||
717 | unify into a common type using the type unification rules defined in the | ||
718 | HCL syntax-agnostic information model. This unified type is the result type | ||
719 | of the conditional, with both expressions converted as necessary to the | ||
720 | unified type. | ||
721 | |||
722 | If the predicate is an unknown boolean value or a value of the dynamic | ||
723 | pseudo-type then the result is an unknown value of the unified type of the | ||
724 | other two expressions. | ||
725 | |||
726 | If either the second or third expressions produce errors when evaluated, | ||
727 | these errors are passed through only if the erroneous expression is selected. | ||
728 | This allows for expressions such as | ||
729 | `length(some_list) > 0 ? some_list[0] : default` (given some suitable `length` | ||
730 | function) without producing an error when the predicate is `false`. | ||
731 | |||
732 | ## Templates | ||
733 | |||
734 | The template sub-language is used within template expressions to concisely | ||
735 | combine strings and other values to produce other strings. It can also be | ||
736 | used in isolation as a standalone template language. | ||
737 | |||
738 | ```ebnf | ||
739 | Template = ( | ||
740 | TemplateLiteral | | ||
741 | TemplateInterpolation | | ||
742 | TemplateDirective | ||
743 | )* | ||
744 | TemplateDirective = TemplateIf | TemplateFor; | ||
745 | ``` | ||
746 | |||
747 | A template behaves like an expression that always returns a string value. | ||
748 | The different elements of the template are evaluated and combined into a | ||
749 | single string to return. If any of the elements produce an unknown string | ||
750 | or a value of the dynamic pseudo-type, the result is an unknown string. | ||
751 | |||
752 | An important use-case for standalone templates is to enable the use of | ||
753 | expressions in alternative HCL syntaxes where a native expression grammar is | ||
754 | not available. For example, the HCL JSON profile treats the values of JSON | ||
755 | strings as standalone templates when attributes are evaluated in expression | ||
756 | mode. | ||
757 | |||
758 | ### Template Literals | ||
759 | |||
760 | A template literal is a literal sequence of characters to include in the | ||
761 | resulting string. When the template sub-language is used standalone, a | ||
762 | template literal can contain any unicode character, with the exception | ||
763 | of the sequences that introduce interpolations and directives, and for the | ||
764 | sequences that escape those introductions. | ||
765 | |||
766 | The interpolation and directive introductions are escaped by doubling their | ||
767 | leading characters. The `${` sequence is escaped as `$${` and the `%{` | ||
768 | sequence is escaped as `%%{`. | ||
769 | |||
770 | When the template sub-language is embedded in the expression language via | ||
771 | _template expressions_, additional constraints and transforms are applied to | ||
772 | template literalsas described in the definition of template expressions. | ||
773 | |||
774 | The value of a template literal can be modified by _strip markers_ in any | ||
775 | interpolations or directives that are adjacent to it. A strip marker is | ||
776 | a tilde (`~`) placed immediately after the opening `{` or before the closing | ||
777 | `}` of a template sequence: | ||
778 | |||
779 | * `hello ${~ "world" }` produces `"helloworld"`. | ||
780 | * `%{ if true ~} hello %{~ endif }` produces `"hello"`. | ||
781 | |||
782 | When a strip marker is present, any spaces adjacent to it in the corresponding | ||
783 | string literal (if any) are removed before producing the final value. Space | ||
784 | characters are interpreted as per Unicode's definition. | ||
785 | |||
786 | Stripping is done at syntax level rather than value level. Values returned | ||
787 | by interpolations or directives are not subject to stripping: | ||
788 | |||
789 | * `${"hello" ~}${" world"}` produces `"hello world"`, and not `"helloworld"`, | ||
790 | because the space is not in a template literal directly adjacent to the | ||
791 | strip marker. | ||
792 | |||
793 | ### Template Interpolations | ||
794 | |||
795 | An _interpolation sequence_ evaluates an expression (written in the | ||
796 | expression sub-language), converts the result to a string value, and | ||
797 | replaces itself with the resulting string. | ||
798 | |||
799 | ```ebnf | ||
800 | TemplateInterpolation = ("${" | "${~") Expression ("}" | "~}"; | ||
801 | ``` | ||
802 | |||
803 | If the expression result cannot be converted to a string, an error is | ||
804 | produced. | ||
805 | |||
806 | ### Template If Directive | ||
807 | |||
808 | The template `if` directive is the template equivalent of the | ||
809 | _conditional expression_, allowing selection of one of two sub-templates based | ||
810 | on the value of a predicate expression. | ||
811 | |||
812 | ```ebnf | ||
813 | TemplateIf = ( | ||
814 | ("%{" | "%{~") "if" Expression ("}" | "~}") | ||
815 | Template | ||
816 | ( | ||
817 | ("%{" | "%{~") "else" ("}" | "~}") | ||
818 | Template | ||
819 | )? | ||
820 | ("%{" | "%{~") "endif" ("}" | "~}") | ||
821 | ); | ||
822 | ``` | ||
823 | |||
824 | The evaluation of the `if` directive is equivalent to the conditional | ||
825 | expression, with the following exceptions: | ||
826 | |||
827 | * The two sub-templates always produce strings, and thus the result value is | ||
828 | also always a string. | ||
829 | * The `else` clause may be omitted, in which case the conditional's third | ||
830 | expression result is implied to be the empty string. | ||
831 | |||
832 | ### Template For Directive | ||
833 | |||
834 | The template `for` directive is the template equivalent of the _for expression_, | ||
835 | producing zero or more copies of its sub-template based on the elements of | ||
836 | a collection. | ||
837 | |||
838 | ```ebnf | ||
839 | TemplateFor = ( | ||
840 | ("%{" | "%{~") "for" Identifier ("," Identifier) "in" Expression ("}" | "~}") | ||
841 | Template | ||
842 | ("%{" | "%{~") "endfor" ("}" | "~}") | ||
843 | ); | ||
844 | ``` | ||
845 | |||
846 | The evaluation of the `for` directive is equivalent to the _for expression_ | ||
847 | when producing a tuple, with the following exceptions: | ||
848 | |||
849 | * The sub-template always produces a string. | ||
850 | * There is no equivalent of the "if" clause on the for expression. | ||
851 | * The elements of the resulting tuple are all converted to strings and | ||
852 | concatenated to produce a flat string result. | ||
853 | |||
854 | ### Template Interpolation Unwrapping | ||
855 | |||
856 | As a special case, a template that consists only of a single interpolation, | ||
857 | with no surrounding literals, directives or other interpolations, is | ||
858 | "unwrapped". In this case, the result of the interpolation expression is | ||
859 | returned verbatim, without conversion to string. | ||
860 | |||
861 | This special case exists primarily to enable the native template language | ||
862 | to be used inside strings in alternative HCL syntaxes that lack a first-class | ||
863 | template or expression syntax. Unwrapping allows arbitrary expressions to be | ||
864 | used to populate attributes when strings in such languages are interpreted | ||
865 | as templates. | ||
866 | |||
867 | * `${true}` produces the boolean value `true` | ||
868 | * `${"${true}"}` produces the boolean value `true`, because both the inner | ||
869 | and outer interpolations are subject to unwrapping. | ||
870 | * `hello ${true}` produces the string `"hello true"` | ||
871 | * `${""}${true}` produces the string `"true"` because there are two | ||
872 | interpolation sequences, even though one produces an empty result. | ||
873 | * `%{ for v in [true] }${v}%{ endif }` produces the string `true` because | ||
874 | the presence of the `for` directive circumvents the unwrapping even though | ||
875 | the final result is a single value. | ||
876 | |||
877 | In some contexts this unwrapping behavior may be circumvented by the calling | ||
878 | application, by converting the final template result to string. This is | ||
879 | necessary, for example, if a standalone template is being used to produce | ||
880 | the direct contents of a file, since the result in that case must always be a | ||
881 | string. | ||
882 | |||
883 | ## Static Analysis | ||
884 | |||
885 | The HCL static analysis operations are implemented for some expression types | ||
886 | in the native syntax, as described in the following sections. | ||
887 | |||
888 | A goal for static analysis of the native syntax is for the interpretation to | ||
889 | be as consistent as possible with the dynamic evaluation interpretation of | ||
890 | the given expression, though some deviations are intentionally made in order | ||
891 | to maximize the potential for analysis. | ||
892 | |||
893 | ### Static List | ||
894 | |||
895 | The tuple construction syntax can be interpreted as a static list. All of | ||
896 | the expression elements given are returned as the static list elements, | ||
897 | with no further interpretation. | ||
898 | |||
899 | ### Static Map | ||
900 | |||
901 | The object construction syntax can be interpreted as a static map. All of the | ||
902 | key/value pairs given are returned as the static pairs, with no further | ||
903 | interpretation. | ||
904 | |||
905 | The usual requirement that an attribute name be interpretable as a string | ||
906 | does not apply to this static analyis, allowing callers to provide map-like | ||
907 | constructs with different key types by building on the map syntax. | ||
908 | |||
909 | ### Static Call | ||
910 | |||
911 | The function call syntax can be interpreted as a static call. The called | ||
912 | function name is returned verbatim and the given argument expressions are | ||
913 | returned as the static arguments, with no further interpretation. | ||
914 | |||
915 | ### Static Traversal | ||
916 | |||
917 | A variable expression and any attached attribute access operations and | ||
918 | constant index operations can be interpreted as a static traversal. | ||
919 | |||
920 | The keywords `true`, `false` and `null` can also be interpreted as | ||
921 | static traversals, behaving as if they were references to variables of those | ||
922 | names, to allow callers to redefine the meaning of those keywords in certain | ||
923 | contexts. | ||