-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathTerraform.sublime-syntax
751 lines (685 loc) · 25.1 KB
/
Terraform.sublime-syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
%YAML 1.2
#
# This syntax definition is based on the Terraform guide:
# https://www.terraform.io/docs/language/index.html
#
# As well as the HCL Native Syntax Spec:
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md
#
# For documentation on the .subline-syntax format:
# https://www.sublimetext.com/docs/syntax.html
#
# Regex's in this file support the Oniguruma regex engine:
# https://raw.githubusercontent.com/kkos/oniguruma/5.9.6/doc/RE
#
---
name: Terraform
scope: source.terraform
version: 2
# File Extensions:
#
# - ".tf": the standard file extension
# https://www.terraform.io/docs/language/index.html
file_extensions:
- tf
- nomad
variables:
# Identifiers: ID_Start (ID_Continue | '-')*;
#
# There is an undocumented exception
# that an underscore character is also accepted
# as an id start character.
#
# https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#identifiers
# http://unicode.org/reports/tr31/#Table_Lexical_Classes_for_Identifiers
ID_Start: '[\p{ID_Start}_]'
ID_Continue: '[\p{ID_Continue}-]'
identifier: (?:{{ID_Start}}{{ID_Continue}}*)
# Exponent: "e" or "E" followed by an optional sign
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#numeric-literals
exponent: ([Ee][+-]?)
# Character Escapes:
#
# - \n: newline
# - \r: carriage return
# - \t: tab
# - \": quote
# - \\: backslash
# - \uNNNN: unicode char (NNNN is 4 hex digits)
# - \uNNNNNNNN: unicode char (NNNNNNNN us 8 digits)
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
char_escapes: \\[nrt"\\]|\\u(\h{8}|\h{4})
# Terraform Named Values
#
# https://www.terraform.io/docs/language/expressions/references.html
named_values: var|local|module|data|path|terraform|each|count|self
# Block types that are known to Terraform.
#
# data: https://developer.hashicorp.com/terraform/language/data-sources
# ephemeral: https://developer.hashicorp.com/terraform/language/resources/ephemeral
# locals: https://developer.hashicorp.com/terraform/language/values/locals
# module: https://developer.hashicorp.com/terraform/language/modules/syntax
# output: https://developer.hashicorp.com/terraform/language/values/outputs
# provider: https://developer.hashicorp.com/terraform/language/providers/configuration
# resource: https://developer.hashicorp.com/terraform/language/resources/syntax
# terraform: https://developer.hashicorp.com/terraform/language/terraform#terraform-block-syntax
# variable: https://developer.hashicorp.com/terraform/language/values/variables
terraform_known_blocks: data|ephemeral|locals|module|output|provider|resource|terraform|variable
# Terraform built-in type keywords
#
# https://www.terraform.io/docs/language/expressions/type-constraints.html#primitive-types
# https://www.terraform.io/docs/language/expressions/type-constraints.html#dynamic-types-the-quot-any-quot-constraint
terraform_type_keywords: any|string|number|bool
# Built-In Functions
#
# https://developer.hashicorp.com/terraform/language/functions
builtin_functions: |-
(?x:
# numbers
abs | ceil | floor | log | max | min | parseint | pow | signum
# string
| chomp | endswith | format | formatlist | indent | join | lower | regex
| regexall | replace | split | startswith | strcontains | strrev | substr
| templatestring | title | trim | trimprefix | trimsuffix | trimspace | upper
# collection
| alltrue | anytrue | chunklist | coalesce | coalescelist | compact | concat
| contains | distinct | element | flatten | index | keys | length | list
| lookup | map | matchkeys | merge | one | range | reverse | setintersection
| setproduct | setsubtract | setunion | slice | sort | sum | transpose
| values | zipmap
# encoding
| base64decode | base64encode | base64gzip | csvdecode | jsondecode
| jsonencode | textdecodebase64 | textencodebase64 | urlencode | yamldecode
| yamlencode
# filesystem
| abspath | dirname | pathexpand | basename | file | fileexists | fileset
| filebase64 | templatefile
# date and time
| formatdate | plantimestamp | timeadd | timecmp | timestamp
# hash and crypto
| base64sha256 | base64sha512 | bcrypt | filebase64sha256
| filebase64sha512 | filemd5 | filesha1 | filesha256 | filesha512 | md5
| rsadecrypt | sha1 | sha256 | sha512 | uuid | uuidv5
# ip network
| cidrhost | cidrnetmask | cidrsubnet | cidrsubnets
# type conversion
| can | issensitive | nonsensitive | sensitive | tobool | tolist | tomap
| tonumber | toset | tostring | try | type
# terraform-specific
| provider::terraform::(?:encode_tfvars | decode_tfvars | encode_expr)
# deprecated/old
| filemd1
)
contexts:
main:
- include: comments
- include: attribute-definition
- include: imports
- include: block
- include: expressions
comments:
- include: inline-comments
- include: block-comments
# Expressions:
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#expression-terms
expressions:
- include: literals
- include: operators
- include: brackets
- include: objects
- include: attribute-access
- include: functions
- include: parens
- include: identifiers
comma:
- match: \,
comment: Commas - used in certain expressions
scope: punctuation.separator.terraform
parens:
- match: \(
scope: punctuation.section.parens.begin.terraform
comment: Parens - matched *after* function syntax
push: paren-body
paren-body:
- meta_scope: meta.parens.terraform
- match: \)
scope: punctuation.section.parens.end.terraform
pop: 1
- include: expressions
# Literal Values: Numbers, Language Constants, and Strings
#
# Strings are _technically_ part of the "expression sub-language",
# but make the most sense to be part of this stack.
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#literal-values
literals:
- include: numeric-literals
- include: language-constants
- include: string-literals
- include: heredoc
- include: type-keywords
- include: named-value-references
named-value-references:
- match: \b(?:{{named_values}})\b
comment: Special variables available only to Terraform.
scope: variable.language.terraform
type-keywords:
- match: \b(?:{{terraform_type_keywords}})\b
comment: Type keywords known to Terraform.
scope: storage.type.terraform
# Inline Comments: begin at the operator, end at the end of the line.
#
# https://www.terraform.io/docs/language/syntax/configuration.html#comments
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#comments-and-whitespace
inline-comments:
- match: '#|//'
comment: Inline Comments
scope: punctuation.definition.comment.terraform
push: inline-comment-body
inline-comment-body:
- meta_scope: comment.line.terraform
- match: $\n?
scope: punctuation.definition.comment.terraform
pop: 1
# Block comments: start and end delimiters for multi-line comments.
#
# https://www.terraform.io/docs/language/syntax/configuration.html#comments
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#comments-and-whitespace
block-comments:
- match: /\*
comment: Block comments
scope: punctuation.definition.comment.terraform
push: block-comments-body
block-comments-body:
- meta_scope: comment.block.terraform
- match: \*/
scope: punctuation.definition.comment.terraform
pop: 1
# Language Constants: booleans and `null`.
#
# https://www.terraform.io/docs/language/expressions/types.html#literal-expressions
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#literal-values
language-constants:
- match: \btrue\b
scope: constant.language.boolean.true.terraform
- match: \bfalse\b
scope: constant.language.boolean.false.terraform
- match: \bnull\b
scope: constant.language.null.terraform
# Numbers: Integers, fractions and exponents
#
# https://www.terraform.io/docs/language/expressions/types.html
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#numeric-literals
numeric-literals:
- match: \b\d+{{exponent}}\d+\b
comment: Integer, no fraction, optional exponent
scope: meta.number.float.decimal.terraform constant.numeric.value.terraform
- match: \b\d+(\.)\d+(?:{{exponent}}\d+)?\b
comment: Integer, fraction, optional exponent
scope: meta.number.float.decimal.terraform constant.numeric.value.terraform
captures:
1: punctuation.separator.decimal.terraform
- match: \b\d+\b
comment: Integers
scope: meta.number.integer.decimal.terraform constant.numeric.value.terraform
# Strings:
#
# https://www.terraform.io/docs/language/expressions/types.html
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
string-literals:
- match: (")(true)(")
scope: meta.string.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: constant.language.boolean.true.terraform
3: punctuation.definition.string.end.terraform
- match: (")(false)(")
scope: meta.string.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: constant.language.boolean.false.terraform
3: punctuation.definition.string.end.terraform
- match: (")(none|null)(")
scope: meta.string.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: constant.language.null.terraform
3: punctuation.definition.string.end.terraform
- match: (")(-?)(\d+)(")
scope: meta.string.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: keyword.operator.arithmetic.terraform
3: meta.number.integer.terraform constant.numeric.value.terraform
4: punctuation.definition.string.end.terraform
- match: \"
comment: Strings
scope: punctuation.definition.string.begin.terraform
push: string-body
string-body:
- meta_scope: meta.string.terraform string.quoted.double.terraform
- match: \"
scope: punctuation.definition.string.end.terraform
pop: 1
- match: '{{char_escapes}}'
comment: Character Escapes
scope: constant.character.escape.terraform
- include: string-interpolation
- include: aws-acl
aws-acl:
- match: (?=\barn:aws:)
push: aws-acl-body
aws-acl-body:
- clear_scopes: 1 # Clear the string.* scope.
- meta_scope: variable.language.acl.terraform
- match: ([$%]\{)(~)?
captures:
1: punctuation.section.interpolation.begin.terraform
2: keyword.operator.template.trim.left.terraform
push: acl-interpolation-body
- match: :|/
scope: punctuation.separator.sequence.terraform
- match: \*
scope: constant.other.wildcard.asterisk.terraform
- match: (?![\w-])
pop: 1
acl-interpolation-body:
- meta_scope: meta.interpolation.terraform
- meta_content_scope: source.terraform
- include: string-interpolation-body
# String Interpolation: ("${" | "${~") Expression ("}" | "~}"
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#templates
string-interpolation:
- match: ([$%]\{)(~)?
captures:
1: punctuation.section.interpolation.begin.terraform
2: keyword.operator.template.trim.left.terraform
push: string-interpolation-body
string-interpolation-body:
- clear_scopes: 1 # Clear the string.* scope.
- meta_scope: meta.interpolation.terraform
- meta_content_scope: source.terraform
- match: (~)?(\})
captures:
1: keyword.operator.template.trim.right.terraform
2: punctuation.section.interpolation.end.terraform
pop: 1
- match: \bif\b
scope: keyword.control.conditional.if.terraform
- match: \belse\b
scope: keyword.control.conditional.else.terraform
- match: \bendif\b
scope: keyword.control.conditional.end.terraform
- match: \bfor\b
scope: keyword.control.loop.for.terraform
- match: \bendfor\b
scope: keyword.control.loop.end.terraform
- match: \bin\b
scope: keyword.operator.iteration.in.terraform
- include: expressions
# String Heredocs
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
heredoc:
- match: (\<\<\-?)\s*({{identifier}})\s*$
comment: String Heredoc's
captures:
1: keyword.operator.heredoc.terraform
2: keyword.control.heredoc.terraform
push: heredoc-body
heredoc-body:
- meta_content_scope: meta.string.terraform string.unquoted.heredoc.terraform
- match: ^\s*\2\s*$
comment: The heredoc token identifier (second capture above).
scope: keyword.control.heredoc.terraform
pop: 1
- include: string-interpolation
# Operators:
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#operators-and-delimiters
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#operations
operators:
- match: \>\= # >=
scope: keyword.operator.comparison.terraform
- match: \<\= # <=
scope: keyword.operator.comparison.terraform
- match: \=\= # ==
scope: keyword.operator.comparison.terraform
- match: \!\= # !=
scope: keyword.operator.comparison.terraform
- match: \+ # +
scope: keyword.operator.arithmetic.terraform
- match: \- # -
scope: keyword.operator.arithmetic.terraform
- match: \* # *
scope: keyword.operator.arithmetic.terraform
- match: \/ # /
scope: keyword.operator.arithmetic.terraform
- match: \% # %
scope: keyword.operator.arithmetic.terraform
- match: \&\& # &&
scope: keyword.operator.logical.terraform
- match: \|\| # ||
scope: keyword.operator.logical.terraform
- match: \! # !
scope: keyword.operator.logical.terraform
- match: \> # >
scope: keyword.operator.comparison.terraform
- match: \< # <
scope: keyword.operator.comparison.terraform
- match: \? # ?
scope: keyword.operator.ternary.terraform
- match: \.\.\. # ...
scope: keyword.operator.terraform
- match: ':' # :
scope: keyword.operator.ternary.terraform
# Terraform "import" statements
#
# https://www.terraform.io/docs/cli/import/usage.html
imports:
- match: \b(terraform)\s+(import)\b
comment: Importing resources
captures:
1: support.constant.terraform
2: keyword.control.import.terraform
push: import-body
import-body:
- match: \"
comment: String literal label
scope: punctuation.definition.string.begin.terraform
push: literal-label-body
- match: '{{identifier}}'
comment: Identifier label
scope: entity.name.label.terraform
- include: numeric-literals
- include: attribute-access
- match: $\n?
comment: Pop at newline
pop: 1
literal-label-body:
- meta_scope: meta.string.terraform string.quoted.double.terraform
- match: \"
scope: punctuation.definition.string.end.terraform
pop: 1
# Brackets: matches tuples and subscript notation
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#index-operator
brackets:
- match: \[
scope: punctuation.section.brackets.begin.terraform
push: bracket-body
bracket-body:
- meta_scope: meta.brackets.terraform
- match: (\*)?\]
comment: Full-splat operator
scope: punctuation.section.brackets.end.terraform
captures:
1: keyword.operator.splat.terraform
pop: 1
- include: comma
- include: comments
- include: tuple-for-expression
- include: expressions
# Objects: collection values
#
# Allows keys as identifiers, strings, and computed values wrapped in parens.
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#collection-values
objects:
- match: \{
scope: punctuation.section.braces.begin.terraform
push: object-body
object-body:
- meta_scope: meta.braces.terraform
- match: \}
scope: punctuation.section.braces.end.terraform
pop: 1
- include: object-for-expression
- include: comments
- match: (?=({{identifier}}|\".*?\")\s*=)
push:
- object-value
- assignment-operator
- object-literal-key
- match: \(
comment: Computed object key (any expression between parens)
scope: punctuation.section.parens.begin.terraform
push:
- object-value
- assignment-operator
- object-computed-key
- include: Packages/Terraform/JSON (Terraform).sublime-syntax#object-body
object-computed-key:
- meta_scope: meta.mapping.key.terraform meta.parens.terraform
- include: paren-body
object-literal-key:
- match: '{{identifier}}'
scope: meta.mapping.key.terraform meta.string.terraform string.unquoted.terraform
pop: 1
- match: (\").*?(\")
scope: meta.mapping.key.terraform meta.string.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: punctuation.definition.string.end.terraform
pop: 1
- include: else-pop
assignment-operator:
- match: =
scope: keyword.operator.assignment.terraform
pop: 1
- include: else-pop
# Object key values: pop at comma, newline, and closing-bracket
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#collection-values
object-value:
- include: comments
- include: expressions
- match: \,
comment: Pop scope on comma.
scope: punctuation.separator.terraform
pop: 1
- match: $\n?
comment: Pop scope on EOL.
pop: 1
- match: (?=\})
comment: Lookahead (don't consume) and pop scope on a bracket.
scope: punctuation.section.braces.end.terraform
pop: 1
# Attribute Access: "." Identifier
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#attribute-access-operator
attribute-access:
- match: \.
scope: punctuation.accessor.dot.terraform
push: member
member:
- include: comments
- match: '{{identifier}}'
comment: Attribute access
scope: variable.other.member.terraform
pop: 1
- match: \d+
comment: Subscript
scope: meta.number.integer.decimal.terraform constant.numeric.value.terraform
pop: 1
- match: \*
comment: Attribute-only splat
scope: keyword.operator.splat.terraform
pop: 1
- include: else-pop
# Attribute Definition: Identifier "=" Expression Newline
#
# The "=" operator cannot be immediately followed by "="
# ">", as those are other operators, not attr definitions.
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#attribute-definitions
attribute-definition:
- match: (?=(\()?({{identifier}})(\))?\s*(\=(?![\=\>])))
push:
- assignment-operator
- attribute-key
attribute-key:
- match: \((?={{identifier}}\))
scope: punctuation.section.parens.begin.terraform
set:
- attribute-key-end
- attribute-key
# https://developer.hashicorp.com/terraform/language/meta-arguments/for_each
- match: for_each\b
scope: variable.declaration.terraform keyword.control.loop.for.terraform
pop: 1
# https://developer.hashicorp.com/terraform/language/meta-arguments/count
- match: count\b
scope: variable.declaration.terraform keyword.control.conditional.terraform
pop: 1
- match: '{{identifier}}'
scope: variable.declaration.terraform variable.other.readwrite.terraform
pop: 1
attribute-key-end:
- meta_scope: meta.parens.terraform
- match: \)
scope: punctuation.section.parens.end.terraform
pop: 1
# Functions: Terraform builtins and unknown
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#functions-and-function-calls
# https://www.terraform.io/docs/language/expressions/function-calls.html
functions:
- match: (?={{builtin_functions}}\()
push: builtin-function-name
- match: (?=(?:{{identifier}}::)*{{identifier}}\()
push: other-function-name
builtin-function-name:
- meta_content_scope: meta.function-call.identifier.terraform
- include: function-argument-list
- include: namespaces
- match: '{{identifier}}'
scope: support.function.builtin.terraform
other-function-name:
- meta_content_scope: meta.function-call.identifier.terraform
- include: function-argument-list
- include: namespaces
- match: '{{identifier}}'
scope: variable.function.terraform
function-argument-list:
- match: \(
scope: punctuation.section.parens.begin.terraform
set: function-argument-list-body
function-argument-list-body:
- meta_scope: meta.function-call.arguments.terraform meta.parens.terraform
- match: \)
scope: punctuation.section.parens.end.terraform
pop: 1
- include: comments
- include: expressions
- include: comma
# Tuple for-Expression:
#
# "[" "for" Identifier ("," Identifier)? "in" Expression ":" Expression ("if" Expression)? "]";
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#for-expressions
# https://www.terraform.io/docs/language/expressions/for.html
tuple-for-expression:
- match: \bfor\b
comment: for expression (arrays)
scope: keyword.control.loop.for.terraform
set: tuple-for-expression-body
tuple-for-expression-body:
- meta_content_scope: meta.brackets.terraform
- match: \]
scope: meta.brackets.terraform punctuation.section.brackets.end.terraform
pop: 1
- include: for-expression-body
# Object for-Expression:
#
# "{" "for" Identifier ("," Identifier)? "in" Expression ":" Expression "=>" Expression "..."? ("if" Expression)? "}";
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#for-expressions
# https://www.terraform.io/docs/language/expressions/for.html
object-for-expression:
- match: \bfor\b
comment: for expression (arrays)
scope: keyword.control.loop.for.terraform
set: object-for-expression-body
object-for-expression-body:
- meta_content_scope: meta.braces.terraform
- match: \}
scope: meta.braces.terraform punctuation.section.braces.end.terraform
pop: 1
- match: \=\>
scope: punctuation.separator.key-value.terraform
- include: for-expression-body
# Shared body syntax for tuple and object for-expressions.
# They require different `set` blocks because they are
# pop'd with different characters and objects allow `=>`.
for-expression-body:
- match: \bin\b
scope: keyword.operator.iteration.in.terraform
- match: \bif\b
scope: keyword.control.conditional.terraform
- match: '\:'
scope: punctuation.section.block.loop.for.terraform
- include: expressions
- include: comments
- include: comma
namespaces:
- match: ({{identifier}})(::)
captures:
1: variable.namespace.terraform
2: punctuation.accessor.double-colon.terraform
identifiers:
- match: '{{identifier}}'
scope: variable.other.readwrite.terraform
# Blocks: Identifier (StringLit|Identifier)* "{" Newline Body "}" Newline;
#
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#structural-elements
block:
# Special case heuristic for the AWS and friends two-term resources
- match: (\b(resource)\s+(")({{identifier}})(")\s+(")({{identifier}})("))\s*(\{)
captures:
1: meta.type.terraform
2: keyword.declaration.terraform
3: punctuation.definition.begin.terraform
4: support.type.terraform
5: punctuation.definition.end.terraform
6: punctuation.definition.begin.terraform
7: entity.name.type.terraform
8: punctuation.definition.end.terraform
9: meta.block.terraform punctuation.section.block.begin.terraform
push: block-body
# Generic
- match: (?:\b({{terraform_known_blocks}})\b|({{identifier}}))(?=[-\s\w"]*\{)
scope: meta.type.terraform
captures:
1: keyword.declaration.terraform
2: entity.name.type.terraform
push: block-name
block-name:
- meta_content_scope: meta.type.terraform
- match: \"
scope: punctuation.definition.string.begin.terraform
push: block-name-body
- match: '{{identifier}}'
scope: entity.name.label.terraform
- match: \s*(\{)
captures:
1: meta.block.terraform punctuation.section.block.begin.terraform
set: block-body
block-name-body:
- meta_scope: meta.string.terraform string.quoted.double.terraform
- match: \"
scope: punctuation.definition.string.end.terraform
pop: 1
block-body:
- meta_content_scope: meta.block.terraform
- match: \}
scope: meta.block.terraform punctuation.section.block.end.terraform
pop: 1
- include: main
else-pop:
- match: (?=\S)
pop: 1