1package t
2
3import (
4	. "github.com/alecthomas/chroma" // nolint
5	"github.com/alecthomas/chroma/lexers/internal"
6)
7
8// Terraform lexer.
9var Terraform = internal.Register(MustNewLazyLexer(
10	&Config{
11		Name:      "Terraform",
12		Aliases:   []string{"terraform", "tf"},
13		Filenames: []string{"*.tf"},
14		MimeTypes: []string{"application/x-tf", "application/x-terraform"},
15	},
16	terraformRules,
17))
18
19func terraformRules() Rules {
20	return Rules{
21		"root": {
22			{`[\[\](),.{}]`, Punctuation, nil},
23			{`-?[0-9]+`, LiteralNumber, nil},
24			{`=>`, Punctuation, nil},
25			{Words(``, `\b`, `true`, `false`), KeywordConstant, nil},
26			{`/(?s)\*(((?!\*/).)*)\*/`, CommentMultiline, nil},
27			{`\s*(#|//).*\n`, CommentSingle, nil},
28			{`([a-zA-Z]\w*)(\s*)(=(?!>))`, ByGroups(NameAttribute, Text, Text), nil},
29			{Words(`^\s*`, `\b`, `variable`, `data`, `resource`, `provider`, `provisioner`, `module`, `output`), KeywordReserved, nil},
30			{Words(``, `\b`, `for`, `in`), Keyword, nil},
31			{Words(``, ``, `count`, `data`, `var`, `module`, `each`), NameBuiltin, nil},
32			{Words(``, `\b`, `abs`, `ceil`, `floor`, `log`, `max`, `min`, `parseint`, `pow`, `signum`), NameBuiltin, nil},
33			{Words(``, `\b`, `chomp`, `format`, `formatlist`, `indent`, `join`, `lower`, `regex`, `regexall`, `replace`, `split`, `strrev`, `substr`, `title`, `trim`, `trimprefix`, `trimsuffix`, `trimspace`, `upper`), NameBuiltin, nil},
34			{Words(`[^.]`, `\b`, `chunklist`, `coalesce`, `coalescelist`, `compact`, `concat`, `contains`, `distinct`, `element`, `flatten`, `index`, `keys`, `length`, `list`, `lookup`, `map`, `matchkeys`, `merge`, `range`, `reverse`, `setintersection`, `setproduct`, `setsubtract`, `setunion`, `slice`, `sort`, `transpose`, `values`, `zipmap`), NameBuiltin, nil},
35			{Words(`[^.]`, `\b`, `base64decode`, `base64encode`, `base64gzip`, `csvdecode`, `jsondecode`, `jsonencode`, `urlencode`, `yamldecode`, `yamlencode`), NameBuiltin, nil},
36			{Words(``, `\b`, `abspath`, `dirname`, `pathexpand`, `basename`, `file`, `fileexists`, `fileset`, `filebase64`, `templatefile`), NameBuiltin, nil},
37			{Words(``, `\b`, `formatdate`, `timeadd`, `timestamp`), NameBuiltin, nil},
38			{Words(``, `\b`, `base64sha256`, `base64sha512`, `bcrypt`, `filebase64sha256`, `filebase64sha512`, `filemd5`, `filesha1`, `filesha256`, `filesha512`, `md5`, `rsadecrypt`, `sha1`, `sha256`, `sha512`, `uuid`, `uuidv5`), NameBuiltin, nil},
39			{Words(``, `\b`, `cidrhost`, `cidrnetmask`, `cidrsubnet`), NameBuiltin, nil},
40			{Words(``, `\b`, `can`, `tobool`, `tolist`, `tomap`, `tonumber`, `toset`, `tostring`, `try`), NameBuiltin, nil},
41			{`=(?!>)|\+|-|\*|\/|:|!|%|>|<(?!<)|>=|<=|==|!=|&&|\||\?`, Operator, nil},
42			{`\n|\s+|\\\n`, Text, nil},
43			{`[a-zA-Z]\w*`, NameOther, nil},
44			{`"`, LiteralStringDouble, Push("string")},
45			{`(?s)(<<-?)(\w+)(\n\s*(?:(?!\2).)*\s*\n\s*)(\2)`, ByGroups(Operator, Operator, String, Operator), nil},
46		},
47		"declaration": {
48			{`(\s*)("(?:\\\\|\\"|[^"])*")(\s*)`, ByGroups(Text, NameVariable, Text), nil},
49			{`\{`, Punctuation, Pop(1)},
50		},
51		"string": {
52			{`"`, LiteralStringDouble, Pop(1)},
53			{`\\\\`, LiteralStringDouble, nil},
54			{`\\\\"`, LiteralStringDouble, nil},
55			{`\$\{`, LiteralStringInterpol, Push("interp-inside")},
56			{`\$`, LiteralStringDouble, nil},
57			{`[^"\\\\$]+`, LiteralStringDouble, nil},
58		},
59		"interp-inside": {
60			{`\}`, LiteralStringInterpol, Pop(1)},
61			Include("root"),
62		},
63	}
64}
65