mindfork 0.10.2

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
%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/hcl/blob/main/hclsyntax/spec.md
# (previously 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})

  # String literals (basically static and single-line strings)
  # for usage in look-aheads.
  #
  # https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#template-expressions
  string_literal: '"(?:[^"]|{{char_escapes}})*"'

  # Template literals
  #
  # https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#template-literals
  template_literal: '(?:\$\$|%%)\{'

  # A block label
  #
  # https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#structural-elements
  label: (?:{{identifier}}|{{string_literal}})

  # 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
  # resource: https://developer.hashicorp.com/terraform/language/resources/syntax
  terraform_typed_and_named_blocks: (?:data|ephemeral|resource)
  # 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
  # variable: https://developer.hashicorp.com/terraform/language/values/variables
  terraform_named_blocks: (?:module|output|provider|variable)
  # locals: https://developer.hashicorp.com/terraform/language/values/locals
  # terraform: https://developer.hashicorp.com/terraform/language/terraform#terraform-block-syntax
  # (Currently unused because they do not have special behavior implemented.)
  terraform_other_blocks: (?:locals|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
  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
    )

  # IPs and CIDR
  ## IPv6 pattern stolen lovingly from https://github.com/tijn/hosts.tmLanguage,
  ## where Michael Lyons adapted them from David M. Szydek https://stackoverflow.com/a/17871737.
  zero_to_32: (?:3[0-2]|[12][0-9]|[0-9])
  zero_to_128: (?:12[0-8]|1[01][0-9]|[1-9][0-9]|[0-9])
  zero_to_255: (?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9][0-9])|(?:[1-9][0-9])|[0-9])
  zero_to_65535: (?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9])
  ipv4: (?:(?:{{zero_to_255}}\.){3}{{zero_to_255}})
  ipv6: |-
    (?xi:
      (?:::(?:ffff(?::0{1,4}){0,1}:){0,1}{{ipv4}})          # ::255.255.255.255  ::ffff:255.255.255.255  ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
      |(?:(?:[0-9a-f]{1,4}:){1,4}:{{ipv4}})                 # 2001:db8:3:4::192.0.2.33  64:ff9b::192.0.2.33                       (IPv4-Embedded IPv6 Address)
      |(?:fe80:(?::[0-9a-f]{1,4}){0,4}%[0-9a-z]{1,})        # fe80::7:8%eth0     fe80::7:8%1                                      (link-local IPv6 addresses with zone index)
      |(?:(?:[0-9a-f]{1,4}:){7,7}    [0-9a-f]{1,4})         # 1:2:3:4:5:6:7:8
      |   (?:[0-9a-f]{1,4}:      (?::[0-9a-f]{1,4}){1,6})   # 1::3:4:5:6:7:8     1::3:4:5:6:7:8   1::8
      |(?:(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5})   # 1::4:5:6:7:8       1:2::4:5:6:7:8   1:2::8
      |(?:(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4})   # 1::5:6:7:8         1:2:3::5:6:7:8   1:2:3::8
      |(?:(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3})   # 1::6:7:8           1:2:3:4::6:7:8   1:2:3:4::8
      |(?:(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2})   # 1::7:8             1:2:3:4:5::7:8   1:2:3:4:5::8
      |(?:(?:[0-9a-f]{1,4}:){1,6}   :[0-9a-f]{1,4})         # 1::8               1:2:3:4:5:6::8   1:2:3:4:5:6::8
      |(?:(?:[0-9a-f]{1,4}:){1,7}   :)                      # 1::                                 1:2:3:4:5:6:7::
      |(?::(?:(?::[0-9a-f]{1,4}){1,7}|:))                   # ::2:3:4:5:6:7:8    ::2:3:4:5:6:7:8  ::8       ::
    )

contexts:
  prototype:
    - include: comments

  main:
    - include: attribute-definition
    - include: imports
    - include: blocks
    - 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
    - include: illegal-strays

  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:
    - include: ip-address-strings
    - match: \"
      comment: Strings
      scope: punctuation.definition.string.begin.terraform
      push: string-body

  string-body:
    - meta_include_prototype: false
    - meta_scope: meta.string.terraform string.quoted.double.terraform
    - match: \"
      scope: punctuation.definition.string.end.terraform
      pop: 1
    - match: \n
      scope: invalid.illegal.unclosed-string.terraform
      pop: 1
    - include: character-escapes
    - include: string-interpolation
    - include: aws-acl

  character-escapes:
    - match: '{{char_escapes}}'
      comment: Character Escapes
      scope: constant.character.escape.terraform

  template-literals:
    - match: '{{template_literal}}'
      comment: Template Literal
      scope: constant.character.escape.terraform

  aws-acl:
    - match: (?=\barn:aws:)
      push: aws-acl-body

  aws-acl-body:
    - meta_include_prototype: false
    - 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

  ip-address-strings:
    - match: (")({{ipv6}})(?:(/)({{zero_to_128}}))?(")
      scope: meta.string.terraform string.quoted.double.terraform
      captures:
        1: punctuation.definition.string.begin.terraform
        2: meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
        3: punctuation.separator.terraform
        4: constant.other.range.terraform
        5: punctuation.definition.string.end.terraform
    - match: (")((\[){{ipv6}}(\]))(?:(/)({{zero_to_128}})|(:)({{zero_to_65535}}))?(")
      scope: meta.string.terraform string.quoted.double.terraform
      captures:
        1: punctuation.definition.string.begin.terraform
        2: meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
        3: punctuation.definition.constant.begin.terraform
        4: punctuation.definition.constant.end.terraform
        5: punctuation.separator.terraform
        6: constant.other.range.terraform
        7: punctuation.separator.terraform
        8: constant.other.port.terraform
        9: punctuation.definition.string.end.terraform
    - match: (")({{ipv4}})(?:(/)({{zero_to_32}})|(:)({{zero_to_65535}}))?(")
      scope: meta.string.terraform string.quoted.double.terraform
      captures:
        1: punctuation.definition.string.begin.terraform
        2: meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
        3: punctuation.separator.terraform
        4: constant.other.range.terraform
        5: punctuation.separator.terraform
        6: constant.other.port.terraform
        7: punctuation.definition.string.end.terraform

  # String Interpolation: ("${" | "${~") Expression ("}" | "~}"
  #
  # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#templates
  string-interpolation:
    - include: template-literals
    - 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.control.loop.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_include_prototype: false
    - 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: 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
    - 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:
    - 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
    - include: expressions

  # 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:
    - 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: 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: comma

  namespaces:
    - match: ({{identifier}})(::)
      captures:
        1: variable.namespace.terraform
        2: punctuation.accessor.double-colon.terraform

  identifiers:
    - match: '{{identifier}}'
      scope: variable.other.readwrite.terraform

  illegal-strays:
    - match: '[\])}]'
      scope: invalid.illegal.stray.terraform

  # Blocks: Identifier (StringLit|Identifier)* "{" Newline Body "}" Newline;
  #
  # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#structural-elements
  blocks:
    - include: typed-and-named-blocks
    - include: named-blocks
    - include: generic-blocks

  typed-and-named-blocks:
    # Special case for two-label resources,
    # where the first denotes the type and the second the name.
    - match: (?=\b{{terraform_typed_and_named_blocks}}(\s+{{label}}){2}\s*\{)
      push:
        - body
        - block-name-label
        - block-type-label
        - block-declaration

  named-blocks:
    # Special case for two-label resources,
    # where the first denotes the type and the second the name.
    - match: (?=\b{{terraform_named_blocks}}\s+{{label}}\s*\{)
      push:
        - body
        - block-name-label
        - block-declaration

  generic-blocks:
    - match: (?=\b({{identifier}})(\s+{{label}})*\s*\{)
      push:
        - body
        - generic-block-labels
        - block-declaration

  block-declaration:
    - match: '{{identifier}}'
      scope: keyword.declaration.terraform
      pop: 1
    - include: else-pop

  block-type-label:
    - match: \"
      scope: punctuation.definition.string.begin.terraform
      set: block-type-label-body
    - match: '{{identifier}}'
      scope: support.type.terraform
      pop: 1
    - include: else-pop

  block-type-label-body:
    - meta_include_prototype: false
    - meta_scope: meta.string.terraform
    - meta_content_scope: support.type.terraform
    - match: \"
      scope: punctuation.definition.string.end.terraform
      pop: 1
    - include: character-escapes

  block-name-label:
    - match: \"
      scope: punctuation.definition.string.begin.terraform
      set: block-name-label-body
    - match: '{{identifier}}'
      scope: entity.name.label.terraform
      pop: 1
    - include: else-pop

  block-name-label-body:
    - meta_include_prototype: false
    - meta_scope: meta.string.terraform
    - meta_content_scope: entity.name.label.terraform
    - match: \"
      scope: punctuation.definition.string.end.terraform
      pop: 1
    - include: character-escapes

  generic-block-labels:
    # Labels probably have a meaning, but we don't know which,
    # so we just scope them as (un-)quoted strings.
    - match: \"
      scope: punctuation.definition.string.begin.terraform
      push: generic-block-label-body
    - match: '{{identifier}}'
      scope: string.unquoted.double.terraform
    - include: else-pop

  generic-block-label-body:
    - meta_scope: meta.string.terraform string.quoted.double.terraform
    - match: \"
      scope: punctuation.definition.string.end.terraform
      pop: 1
    - include: character-escapes

  body:
    - meta_content_scope: meta.block.head.terraform
    - match: \{
      scope: punctuation.section.block.begin.terraform
      set: body-body
    - include: else-pop

  body-body:
    - meta_scope: meta.block.body.terraform
    - match: \}
      scope: punctuation.section.block.end.terraform
      pop: 1
    - include: main

  else-pop:
    - match: (?=\S)
      pop: 1