1package t
2
3import (
4	. "github.com/alecthomas/chroma" // nolint
5	"github.com/alecthomas/chroma/lexers/internal"
6)
7
8// Tex lexer.
9var TeX = internal.Register(MustNewLazyLexer(
10	&Config{
11		Name:      "TeX",
12		Aliases:   []string{"tex", "latex"},
13		Filenames: []string{"*.tex", "*.aux", "*.toc"},
14		MimeTypes: []string{"text/x-tex", "text/x-latex"},
15	},
16	texRules,
17))
18
19func texRules() Rules {
20	return Rules{
21		"general": {
22			{`%.*?\n`, Comment, nil},
23			{`[{}]`, NameBuiltin, nil},
24			{`[&_^]`, NameBuiltin, nil},
25		},
26		"root": {
27			{`\\\[`, LiteralStringBacktick, Push("displaymath")},
28			{`\\\(`, LiteralString, Push("inlinemath")},
29			{`\$\$`, LiteralStringBacktick, Push("displaymath")},
30			{`\$`, LiteralString, Push("inlinemath")},
31			{`\\([a-zA-Z]+|.)`, Keyword, Push("command")},
32			{`\\$`, Keyword, nil},
33			Include("general"),
34			{`[^\\$%&_^{}]+`, Text, nil},
35		},
36		"math": {
37			{`\\([a-zA-Z]+|.)`, NameVariable, nil},
38			Include("general"),
39			{`[0-9]+`, LiteralNumber, nil},
40			{`[-=!+*/()\[\]]`, Operator, nil},
41			{`[^=!+*/()\[\]\\$%&_^{}0-9-]+`, NameBuiltin, nil},
42		},
43		"inlinemath": {
44			{`\\\)`, LiteralString, Pop(1)},
45			{`\$`, LiteralString, Pop(1)},
46			Include("math"),
47		},
48		"displaymath": {
49			{`\\\]`, LiteralString, Pop(1)},
50			{`\$\$`, LiteralString, Pop(1)},
51			{`\$`, NameBuiltin, nil},
52			Include("math"),
53		},
54		"command": {
55			{`\[.*?\]`, NameAttribute, nil},
56			{`\*`, Keyword, nil},
57			Default(Pop(1)),
58		},
59	}
60}
61