1package p
2
3import (
4	. "github.com/alecthomas/chroma" // nolint
5	"github.com/alecthomas/chroma/lexers/internal"
6)
7
8// Perl lexer.
9var Perl = internal.Register(MustNewLazyLexer(
10	&Config{
11		Name:      "Perl",
12		Aliases:   []string{"perl", "pl"},
13		Filenames: []string{"*.pl", "*.pm", "*.t"},
14		MimeTypes: []string{"text/x-perl", "application/x-perl"},
15		DotAll:    true,
16	},
17	perlRules,
18))
19
20func perlRules() Rules {
21	return Rules{
22		"balanced-regex": {
23			{`/(\\\\|\\[^\\]|[^\\/])*/[egimosx]*`, LiteralStringRegex, Pop(1)},
24			{`!(\\\\|\\[^\\]|[^\\!])*![egimosx]*`, LiteralStringRegex, Pop(1)},
25			{`\\(\\\\|[^\\])*\\[egimosx]*`, LiteralStringRegex, Pop(1)},
26			{`\{(\\\\|\\[^\\]|[^\\}])*\}[egimosx]*`, LiteralStringRegex, Pop(1)},
27			{`<(\\\\|\\[^\\]|[^\\>])*>[egimosx]*`, LiteralStringRegex, Pop(1)},
28			{`\[(\\\\|\\[^\\]|[^\\\]])*\][egimosx]*`, LiteralStringRegex, Pop(1)},
29			{`\((\\\\|\\[^\\]|[^\\)])*\)[egimosx]*`, LiteralStringRegex, Pop(1)},
30			{`@(\\\\|\\[^\\]|[^\\@])*@[egimosx]*`, LiteralStringRegex, Pop(1)},
31			{`%(\\\\|\\[^\\]|[^\\%])*%[egimosx]*`, LiteralStringRegex, Pop(1)},
32			{`\$(\\\\|\\[^\\]|[^\\$])*\$[egimosx]*`, LiteralStringRegex, Pop(1)},
33		},
34		"root": {
35			{`\A\#!.+?$`, CommentHashbang, nil},
36			{`\#.*?$`, CommentSingle, nil},
37			{`^=[a-zA-Z0-9]+\s+.*?\n=cut`, CommentMultiline, nil},
38			{Words(``, `\b`, `case`, `continue`, `do`, `else`, `elsif`, `for`, `foreach`, `if`, `last`, `my`, `next`, `our`, `redo`, `reset`, `then`, `unless`, `until`, `while`, `print`, `new`, `BEGIN`, `CHECK`, `INIT`, `END`, `return`), Keyword, nil},
39			{`(format)(\s+)(\w+)(\s*)(=)(\s*\n)`, ByGroups(Keyword, Text, Name, Text, Punctuation, Text), Push("format")},
40			{`(eq|lt|gt|le|ge|ne|not|and|or|cmp)\b`, OperatorWord, nil},
41			{`s/(\\\\|\\[^\\]|[^\\/])*/(\\\\|\\[^\\]|[^\\/])*/[egimosx]*`, LiteralStringRegex, nil},
42			{`s!(\\\\|\\!|[^!])*!(\\\\|\\!|[^!])*![egimosx]*`, LiteralStringRegex, nil},
43			{`s\\(\\\\|[^\\])*\\(\\\\|[^\\])*\\[egimosx]*`, LiteralStringRegex, nil},
44			{`s@(\\\\|\\[^\\]|[^\\@])*@(\\\\|\\[^\\]|[^\\@])*@[egimosx]*`, LiteralStringRegex, nil},
45			{`s%(\\\\|\\[^\\]|[^\\%])*%(\\\\|\\[^\\]|[^\\%])*%[egimosx]*`, LiteralStringRegex, nil},
46			{`s\{(\\\\|\\[^\\]|[^\\}])*\}\s*`, LiteralStringRegex, Push("balanced-regex")},
47			{`s<(\\\\|\\[^\\]|[^\\>])*>\s*`, LiteralStringRegex, Push("balanced-regex")},
48			{`s\[(\\\\|\\[^\\]|[^\\\]])*\]\s*`, LiteralStringRegex, Push("balanced-regex")},
49			{`s\((\\\\|\\[^\\]|[^\\)])*\)\s*`, LiteralStringRegex, Push("balanced-regex")},
50			{`m?/(\\\\|\\[^\\]|[^\\/\n])*/[gcimosx]*`, LiteralStringRegex, nil},
51			{`m(?=[/!\\{<\[(@%$])`, LiteralStringRegex, Push("balanced-regex")},
52			{`((?<==~)|(?<=\())\s*/(\\\\|\\[^\\]|[^\\/])*/[gcimosx]*`, LiteralStringRegex, nil},
53			{`\s+`, Text, nil},
54			{Words(``, `\b`, `abs`, `accept`, `alarm`, `atan2`, `bind`, `binmode`, `bless`, `caller`, `chdir`, `chmod`, `chomp`, `chop`, `chown`, `chr`, `chroot`, `close`, `closedir`, `connect`, `continue`, `cos`, `crypt`, `dbmclose`, `dbmopen`, `defined`, `delete`, `die`, `dump`, `each`, `endgrent`, `endhostent`, `endnetent`, `endprotoent`, `endpwent`, `endservent`, `eof`, `eval`, `exec`, `exists`, `exit`, `exp`, `fcntl`, `fileno`, `flock`, `fork`, `format`, `formline`, `getc`, `getgrent`, `getgrgid`, `getgrnam`, `gethostbyaddr`, `gethostbyname`, `gethostent`, `getlogin`, `getnetbyaddr`, `getnetbyname`, `getnetent`, `getpeername`, `getpgrp`, `getppid`, `getpriority`, `getprotobyname`, `getprotobynumber`, `getprotoent`, `getpwent`, `getpwnam`, `getpwuid`, `getservbyname`, `getservbyport`, `getservent`, `getsockname`, `getsockopt`, `glob`, `gmtime`, `goto`, `grep`, `hex`, `import`, `index`, `int`, `ioctl`, `join`, `keys`, `kill`, `last`, `lc`, `lcfirst`, `length`, `link`, `listen`, `local`, `localtime`, `log`, `lstat`, `map`, `mkdir`, `msgctl`, `msgget`, `msgrcv`, `msgsnd`, `my`, `next`, `oct`, `open`, `opendir`, `ord`, `our`, `pack`, `pipe`, `pop`, `pos`, `printf`, `prototype`, `push`, `quotemeta`, `rand`, `read`, `readdir`, `readline`, `readlink`, `readpipe`, `recv`, `redo`, `ref`, `rename`, `reverse`, `rewinddir`, `rindex`, `rmdir`, `scalar`, `seek`, `seekdir`, `select`, `semctl`, `semget`, `semop`, `send`, `setgrent`, `sethostent`, `setnetent`, `setpgrp`, `setpriority`, `setprotoent`, `setpwent`, `setservent`, `setsockopt`, `shift`, `shmctl`, `shmget`, `shmread`, `shmwrite`, `shutdown`, `sin`, `sleep`, `socket`, `socketpair`, `sort`, `splice`, `split`, `sprintf`, `sqrt`, `srand`, `stat`, `study`, `substr`, `symlink`, `syscall`, `sysopen`, `sysread`, `sysseek`, `system`, `syswrite`, `tell`, `telldir`, `tie`, `tied`, `time`, `times`, `tr`, `truncate`, `uc`, `ucfirst`, `umask`, `undef`, `unlink`, `unpack`, `unshift`, `untie`, `utime`, `values`, `vec`, `wait`, `waitpid`, `wantarray`, `warn`, `write`), NameBuiltin, nil},
55			{`((__(DATA|DIE|WARN)__)|(STD(IN|OUT|ERR)))\b`, NameBuiltinPseudo, nil},
56			{`(<<)([\'"]?)([a-zA-Z_]\w*)(\2;?\n.*?\n)(\3)(\n)`, ByGroups(LiteralString, LiteralString, LiteralStringDelimiter, LiteralString, LiteralStringDelimiter, Text), nil},
57			{`__END__`, CommentPreproc, Push("end-part")},
58			{`\$\^[ADEFHILMOPSTWX]`, NameVariableGlobal, nil},
59			{"\\$[\\\\\\\"\\[\\]'&`+*.,;=%~?@$!<>(^|/-](?!\\w)", NameVariableGlobal, nil},
60			{`[$@%#]+`, NameVariable, Push("varname")},
61			{`0_?[0-7]+(_[0-7]+)*`, LiteralNumberOct, nil},
62			{`0x[0-9A-Fa-f]+(_[0-9A-Fa-f]+)*`, LiteralNumberHex, nil},
63			{`0b[01]+(_[01]+)*`, LiteralNumberBin, nil},
64			{`(?i)(\d*(_\d*)*\.\d+(_\d*)*|\d+(_\d*)*\.\d+(_\d*)*)(e[+-]?\d+)?`, LiteralNumberFloat, nil},
65			{`(?i)\d+(_\d*)*e[+-]?\d+(_\d*)*`, LiteralNumberFloat, nil},
66			{`\d+(_\d+)*`, LiteralNumberInteger, nil},
67			{`'(\\\\|\\[^\\]|[^'\\])*'`, LiteralString, nil},
68			{`"(\\\\|\\[^\\]|[^"\\])*"`, LiteralString, nil},
69			{"`(\\\\\\\\|\\\\[^\\\\]|[^`\\\\])*`", LiteralStringBacktick, nil},
70			{`<([^\s>]+)>`, LiteralStringRegex, nil},
71			{`(q|qq|qw|qr|qx)\{`, LiteralStringOther, Push("cb-string")},
72			{`(q|qq|qw|qr|qx)\(`, LiteralStringOther, Push("rb-string")},
73			{`(q|qq|qw|qr|qx)\[`, LiteralStringOther, Push("sb-string")},
74			{`(q|qq|qw|qr|qx)\<`, LiteralStringOther, Push("lt-string")},
75			{`(q|qq|qw|qr|qx)([\W_])(.|\n)*?\2`, LiteralStringOther, nil},
76			{`(package)(\s+)([a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*)`, ByGroups(Keyword, Text, NameNamespace), nil},
77			{`(use|require|no)(\s+)([a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*)`, ByGroups(Keyword, Text, NameNamespace), nil},
78			{`(sub)(\s+)`, ByGroups(Keyword, Text), Push("funcname")},
79			{Words(``, `\b`, `no`, `package`, `require`, `use`), Keyword, nil},
80			{`(\[\]|\*\*|::|<<|>>|>=|<=>|<=|={3}|!=|=~|!~|&&?|\|\||\.{1,3})`, Operator, nil},
81			{`[-+/*%=<>&^|!\\~]=?`, Operator, nil},
82			{`[()\[\]:;,<>/?{}]`, Punctuation, nil},
83			{`(?=\w)`, Name, Push("name")},
84		},
85		"format": {
86			{`\.\n`, LiteralStringInterpol, Pop(1)},
87			{`[^\n]*\n`, LiteralStringInterpol, nil},
88		},
89		"varname": {
90			{`\s+`, Text, nil},
91			{`\{`, Punctuation, Pop(1)},
92			{`\)|,`, Punctuation, Pop(1)},
93			{`\w+::`, NameNamespace, nil},
94			{`[\w:]+`, NameVariable, Pop(1)},
95		},
96		"name": {
97			{`[a-zA-Z_]\w*(::[a-zA-Z_]\w*)*(::)?(?=\s*->)`, NameNamespace, Pop(1)},
98			{`[a-zA-Z_]\w*(::[a-zA-Z_]\w*)*::`, NameNamespace, Pop(1)},
99			{`[\w:]+`, Name, Pop(1)},
100			{`[A-Z_]+(?=\W)`, NameConstant, Pop(1)},
101			{`(?=\W)`, Text, Pop(1)},
102		},
103		"funcname": {
104			{`[a-zA-Z_]\w*[!?]?`, NameFunction, nil},
105			{`\s+`, Text, nil},
106			{`(\([$@%]*\))(\s*)`, ByGroups(Punctuation, Text), nil},
107			{`;`, Punctuation, Pop(1)},
108			{`.*?\{`, Punctuation, Pop(1)},
109		},
110		"cb-string": {
111			{`\\[{}\\]`, LiteralStringOther, nil},
112			{`\\`, LiteralStringOther, nil},
113			{`\{`, LiteralStringOther, Push("cb-string")},
114			{`\}`, LiteralStringOther, Pop(1)},
115			{`[^{}\\]+`, LiteralStringOther, nil},
116		},
117		"rb-string": {
118			{`\\[()\\]`, LiteralStringOther, nil},
119			{`\\`, LiteralStringOther, nil},
120			{`\(`, LiteralStringOther, Push("rb-string")},
121			{`\)`, LiteralStringOther, Pop(1)},
122			{`[^()]+`, LiteralStringOther, nil},
123		},
124		"sb-string": {
125			{`\\[\[\]\\]`, LiteralStringOther, nil},
126			{`\\`, LiteralStringOther, nil},
127			{`\[`, LiteralStringOther, Push("sb-string")},
128			{`\]`, LiteralStringOther, Pop(1)},
129			{`[^\[\]]+`, LiteralStringOther, nil},
130		},
131		"lt-string": {
132			{`\\[<>\\]`, LiteralStringOther, nil},
133			{`\\`, LiteralStringOther, nil},
134			{`\<`, LiteralStringOther, Push("lt-string")},
135			{`\>`, LiteralStringOther, Pop(1)},
136			{`[^<>]+`, LiteralStringOther, nil},
137		},
138		"end-part": {
139			{`.+`, CommentPreproc, Pop(1)},
140		},
141	}
142}
143