hcl.go

 1package h
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// HCL lexer.
 9var HCL = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "HCL",
12		Aliases:   []string{"hcl"},
13		Filenames: []string{"*.hcl"},
14		MimeTypes: []string{"application/x-hcl"},
15	},
16	Rules{
17		"root": {
18			Include("string"),
19			Include("punctuation"),
20			Include("curly"),
21			Include("basic"),
22			Include("whitespace"),
23			{`[0-9]+`, LiteralNumber, nil},
24		},
25		"basic": {
26			{Words(`\b`, `\b`, `true`, `false`), KeywordType, nil},
27			{`\s*/\*`, CommentMultiline, Push("comment")},
28			{`\s*#.*\n`, CommentSingle, nil},
29			{`(.*?)(\s*)(=)`, ByGroups(Name, Text, Operator), nil},
30			{`\d+`, Number, nil},
31			{`\b\w+\b`, Keyword, nil},
32			{`\$\{`, LiteralStringInterpol, Push("var_builtin")},
33		},
34		"function": {
35			{`(\s+)(".*")(\s+)`, ByGroups(Text, LiteralString, Text), nil},
36			Include("punctuation"),
37			Include("curly"),
38		},
39		"var_builtin": {
40			{`\$\{`, LiteralStringInterpol, Push()},
41			{Words(`\b`, `\b`, `concat`, `file`, `join`, `lookup`, `element`), NameBuiltin, nil},
42			Include("string"),
43			Include("punctuation"),
44			{`\s+`, Text, nil},
45			{`\}`, LiteralStringInterpol, Pop(1)},
46		},
47		"string": {
48			{`(".*")`, ByGroups(LiteralStringDouble), nil},
49		},
50		"punctuation": {
51			{`[\[\](),.]`, Punctuation, nil},
52		},
53		"curly": {
54			{`\{`, TextPunctuation, nil},
55			{`\}`, TextPunctuation, nil},
56		},
57		"comment": {
58			{`[^*/]`, CommentMultiline, nil},
59			{`/\*`, CommentMultiline, Push()},
60			{`\*/`, CommentMultiline, Pop(1)},
61			{`[*/]`, CommentMultiline, nil},
62		},
63		"whitespace": {
64			{`\n`, Text, nil},
65			{`\s+`, Text, nil},
66			{`\\\n`, Text, nil},
67		},
68	},
69))