twig.go

 1package t
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Twig lexer.
 9var Twig = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "Twig",
12		Aliases:   []string{"twig"},
13		Filenames: []string{},
14		MimeTypes: []string{"application/x-twig"},
15		DotAll:    true,
16	},
17	Rules{
18		"root": {
19			{`[^{]+`, Other, nil},
20			{`\{\{`, CommentPreproc, Push("var")},
21			{`\{\#.*?\#\}`, Comment, nil},
22			{`(\{%)(-?\s*)(raw)(\s*-?)(%\})(.*?)(\{%)(-?\s*)(endraw)(\s*-?)(%\})`, ByGroups(CommentPreproc, Text, Keyword, Text, CommentPreproc, Other, CommentPreproc, Text, Keyword, Text, CommentPreproc), nil},
23			{`(\{%)(-?\s*)(verbatim)(\s*-?)(%\})(.*?)(\{%)(-?\s*)(endverbatim)(\s*-?)(%\})`, ByGroups(CommentPreproc, Text, Keyword, Text, CommentPreproc, Other, CommentPreproc, Text, Keyword, Text, CommentPreproc), nil},
24			{`(\{%)(-?\s*)(filter)(\s+)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w-]|[^\x00-\x7f])*)`, ByGroups(CommentPreproc, Text, Keyword, Text, NameFunction), Push("tag")},
25			{`(\{%)(-?\s*)([a-zA-Z_]\w*)`, ByGroups(CommentPreproc, Text, Keyword), Push("tag")},
26			{`\{`, Other, nil},
27		},
28		"varnames": {
29			{`(\|)(\s*)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w-]|[^\x00-\x7f])*)`, ByGroups(Operator, Text, NameFunction), nil},
30			{`(is)(\s+)(not)?(\s*)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w-]|[^\x00-\x7f])*)`, ByGroups(Keyword, Text, Keyword, Text, NameFunction), nil},
31			{`(?i)(true|false|none|null)\b`, KeywordPseudo, nil},
32			{`(in|not|and|b-and|or|b-or|b-xor|isif|elseif|else|importconstant|defined|divisibleby|empty|even|iterable|odd|sameasmatches|starts\s+with|ends\s+with)\b`, Keyword, nil},
33			{`(loop|block|parent)\b`, NameBuiltin, nil},
34			{`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w-]|[^\x00-\x7f])*`, NameVariable, nil},
35			{`\.(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w-]|[^\x00-\x7f])*`, NameVariable, nil},
36			{`\.[0-9]+`, LiteralNumber, nil},
37			{`:?"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
38			{`:?'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
39			{`([{}()\[\]+\-*/,:~%]|\.\.|\?|:|\*\*|\/\/|!=|[><=]=?)`, Operator, nil},
40			{`[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?`, LiteralNumber, nil},
41		},
42		"var": {
43			{`\s+`, Text, nil},
44			{`(-?)(\}\})`, ByGroups(Text, CommentPreproc), Pop(1)},
45			Include("varnames"),
46		},
47		"tag": {
48			{`\s+`, Text, nil},
49			{`(-?)(%\})`, ByGroups(Text, CommentPreproc), Pop(1)},
50			Include("varnames"),
51			{`.`, Punctuation, nil},
52		},
53	},
54))