-
Notifications
You must be signed in to change notification settings - Fork 53
/
Terraform.sublime-syntax
632 lines (580 loc) · 20.8 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
%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
version: 2
# File Extensions:
#
# - ".tf": the standard file extension
# https://www.terraform.io/docs/language/index.html
file_extensions:
- tf
- nomad
scope: source.terraform
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.
#
# resource: https://www.terraform.io/docs/language/resources/syntax.html
# provider: https://www.terraform.io/docs/language/providers/configuration.html
# variable: https://www.terraform.io/docs/language/values/variables.html
# output: https://www.terraform.io/docs/language/values/outputs.html
# locals: https://www.terraform.io/docs/language/values/locals.html
# module: https://www.terraform.io/docs/language/modules/syntax.html
# data: https://www.terraform.io/docs/language/data-sources/index.html
# terraform: https://www.terraform.io/docs/language/settings/index.html#terraform-block-syntax
terraform_known_blocks: resource|provider|variable|output|locals|module|data|terraform
# 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
predeclared_funcs: |-
\b(?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
)\b
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:
- match: \)
scope: punctuation.section.parens.end.terraform
pop: true
- 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: true
# 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: true
# 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: \b(true|false|null)\b
comment: Language Constants
scope: constant.language.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: constant.numeric.float.terraform
captures:
1: punctuation.separator.exponent.terraform
- match: \b\d+(\.)\d+(?:({{exponent}})\d+)?\b
comment: Integer, fraction, optional exponent
scope: constant.numeric.float.terraform
captures:
1: punctuation.separator.decimal.terraform
2: punctuation.separator.exponent.terraform
- match: \b\d+\b
comment: Integers
scope: constant.numeric.integer.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: '"'
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: true
- include: string_interpolation
- match: '{{char_escapes}}'
comment: Character Escapes
scope: constant.character.escape.terraform
# 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:
- meta_scope: meta.interpolation.terraform
- meta_content_scope: source.terraform
- clear_scopes: 1 # Clear the string.* scope.
- match: (~)?(\})
captures:
1: keyword.operator.template.trim.right.terraform
2: punctuation.section.interpolation.end.terraform
pop: true
- match: \b(if|else|endif|for|in|endfor)\b
comment: if/else/endif and for/in/endfor directives
scope: keyword.control.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:
- 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: true
- 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.terraform
- match: \<\= # <=
scope: keyword.operator.terraform
- match: \=\= # ==
scope: keyword.operator.terraform
- match: \!\= # !=
scope: keyword.operator.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.terraform
- match: \< # <
scope: keyword.operator.terraform
- match: \? # ?
scope: keyword.operator.terraform
- match: \.\.\. # ...
scope: keyword.operator.terraform
- match: ':' # :
scope: keyword.operator.terraform
# Terraform "import" statements
#
# https://www.terraform.io/docs/cli/import/usage.html
imports:
- match: \s*(terraform)\s*(import)\s*
comment: Importing resources
captures:
1: support.constant.terraform
2: keyword.control.import.terraform
push:
- match: \"
comment: String literal label
scope: punctuation.definition.string.begin.terraform
push:
- meta_scope: string.quoted.double.terraform
- match: \"
scope: punctuation.definition.string.end.terraform
pop: true
- match: "{{identifier}}"
comment: Identifier label
scope: entity.name.label.terraform
- include: numeric_literals
- include: attribute_access
- match: $\n?
comment: Pop at newline
pop: true
# 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:
- match: \]
scope: punctuation.section.brackets.end.terraform
pop: true
- match: (\*)\]
comment: Full-splat operator
scope: punctuation.section.brackets.end.terraform
captures:
1: keyword.operator.splat.terraform
pop: true
- 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: true
- include: object_for_expression
- include: comments
- match: (?=({{identifier}}|\".*?\")\s*=)
push:
- object_value
- assignment_operator
- object_key
- match: \(
comment: Computed object key (any expression between parens)
scope: punctuation.section.parens.begin.terraform
push:
- meta_scope: meta.mapping.key.terraform
- match: '(\))\s*(\=)\s*'
captures:
1: punctuation.section.parens.end.terraform
2: keyword.operator.terraform
set: object_value
- include: expressions
object_key:
- match: '{{identifier}}'
scope: meta.mapping.key.terraform string.unquoted.terraform
pop: true
- match: (\").*?(\")
scope: meta.mapping.key.terraform string.quoted.double.terraform
captures:
1: punctuation.definition.string.begin.terraform
2: punctuation.definition.string.end.terraform
pop: true
- include: else_pop
assignment_operator:
- match: =
scope: keyword.operator.assignment.terraform
pop: true
- 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: true
- match: $\n?
comment: Pop scope on EOL.
pop: true
- match: (?=\})
comment: Lookahead (don't consume) and pop scope on a bracket.
scope: punctuation.section.braces.end.terraform
pop: true
# 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: true
- match: \d+
comment: Subscript
scope: constant.numeric.integer.terraform
pop: true
- match: \*
comment: Attribute-only splat
scope: keyword.operator.splat.terraform
pop: true
- 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: true
# https://developer.hashicorp.com/terraform/language/meta-arguments/count
- match: count\b
scope: variable.declaration.terraform keyword.control.conditional.terraform
pop: true
- match: '{{identifier}}'
scope: variable.declaration.terraform variable.other.readwrite.terraform
pop: true
attribute_key_end:
- match: \)
scope: punctuation.section.parens.end.terraform
pop: true
# 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: (?:({{predeclared_funcs}})|\b({{identifier}})\b)(\()
comment: Built-in function calls
captures:
1: support.function.builtin.terraform
2: variable.function.terraform
3: punctuation.section.parens.begin.terraform
push:
- meta_scope: meta.function-call.terraform
- match: \)
scope: punctuation.section.parens.end.terraform
pop: true
- 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:
- include: for_expression_body
- match: \]
scope: punctuation.section.brackets.end.terraform
pop: true
# 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:
- match: \=\>
scope: punctuation.separator.key-value.terraform
- include: for_expression_body
- match: \}
scope: punctuation.section.braces.end.terraform
pop: true
# 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
comment: in keyword
scope: keyword.control.loop.in.terraform
- match: \bif\b
comment: if keyword
scope: keyword.control.conditional.terraform
- match: '\:'
scope: punctuation.section.block.loop.for.terraform
- include: expressions
- include: comments
- include: comma
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:
- match: (?:\b({{terraform_known_blocks}})\b|({{identifier}}))(?=[\s\"\-[:word:]]*(\{))
captures:
1: keyword.declaration.terraform
2: entity.name.type.terraform
push: block_name
block_name:
- meta_scope: meta.type.terraform
- match: \"
scope: punctuation.definition.string.begin.terraform
push: block_name_body
- match: "{{identifier}}"
scope: entity.name.label.terraform
- match: \{
scope: punctuation.section.block.begin.terraform
set: block_body
block_name_body:
- meta_scope: string.quoted.double.terraform
- match: \"
scope: punctuation.definition.string.end.terraform
pop: true
block_body:
- meta_scope: meta.block.terraform
- include: main
- match: \}
scope: punctuation.section.block.end.terraform
pop: true
else_pop:
- match: (?=\S)
pop: true