1package circular
2
3import (
4	. "github.com/alecthomas/chroma" // nolint
5	"github.com/alecthomas/chroma/lexers/internal"
6)
7
8// PHP lexer for pure PHP code (not embedded in HTML).
9var PHP = internal.Register(MustNewLazyLexer(
10	&Config{
11		Name:            "PHP",
12		Aliases:         []string{"php", "php3", "php4", "php5"},
13		Filenames:       []string{"*.php", "*.php[345]", "*.inc"},
14		MimeTypes:       []string{"text/x-php"},
15		DotAll:          true,
16		CaseInsensitive: true,
17		EnsureNL:        true,
18	},
19	phpRules,
20))
21
22func phpRules() Rules {
23	return phpCommonRules().Rename("php", "root")
24}
25
26func phpCommonRules() Rules {
27	return Rules{
28		"php": {
29			{`\?>`, CommentPreproc, Pop(1)},
30			{`(<<<)([\'"]?)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)(\2\n.*?\n\s*)(\3)(;?)(\n)`, ByGroups(LiteralString, LiteralString, LiteralStringDelimiter, LiteralString, LiteralStringDelimiter, Punctuation, Text), nil},
31			{`\s+`, Text, nil},
32			{`#.*?\n`, CommentSingle, nil},
33			{`//.*?\n`, CommentSingle, nil},
34			{`/\*\*/`, CommentMultiline, nil},
35			{`/\*\*.*?\*/`, LiteralStringDoc, nil},
36			{`/\*.*?\*/`, CommentMultiline, nil},
37			{`(->|::)(\s*)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)`, ByGroups(Operator, Text, NameAttribute), nil},
38			{`[~!%^&*+=|:.<>/@-]+`, Operator, nil},
39			{`\?`, Operator, nil},
40			{`[\[\]{}();,]+`, Punctuation, nil},
41			{`(class)(\s+)`, ByGroups(Keyword, Text), Push("classname")},
42			{`(function)(\s*)(?=\()`, ByGroups(Keyword, Text), nil},
43			{`(function)(\s+)(&?)(\s*)`, ByGroups(Keyword, Text, Operator, Text), Push("functionname")},
44			{`(const)(\s+)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)`, ByGroups(Keyword, Text, NameConstant), nil},
45			{`(and|E_PARSE|old_function|E_ERROR|or|as|E_WARNING|parent|eval|PHP_OS|break|exit|case|extends|PHP_VERSION|cfunction|FALSE|print|for|require|continue|foreach|require_once|declare|return|default|static|do|switch|die|stdClass|echo|else|TRUE|elseif|var|empty|if|xor|enddeclare|include|virtual|endfor|include_once|while|endforeach|global|endif|list|endswitch|new|endwhile|not|array|E_ALL|NULL|final|php_user_filter|interface|implements|public|private|protected|abstract|clone|try|catch|throw|this|use|namespace|trait|yield|finally)\b`, Keyword, nil},
46			{`(true|false|null)\b`, KeywordConstant, nil},
47			Include("magicconstants"),
48			{`\$\{\$+(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*\}`, NameVariable, nil},
49			{`\$+(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*`, NameVariable, nil},
50			{`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*`, NameOther, nil},
51			{`(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?`, LiteralNumberFloat, nil},
52			{`\d+e[+-]?[0-9]+`, LiteralNumberFloat, nil},
53			{`0[0-7]+`, LiteralNumberOct, nil},
54			{`0x[a-f0-9_]+`, LiteralNumberHex, nil},
55			{`[\d_]+`, LiteralNumberInteger, nil},
56			{`0b[01]+`, LiteralNumberBin, nil},
57			{`'([^'\\]*(?:\\.[^'\\]*)*)'`, LiteralStringSingle, nil},
58			{"`([^`\\\\]*(?:\\\\.[^`\\\\]*)*)`", LiteralStringBacktick, nil},
59			{`"`, LiteralStringDouble, Push("string")},
60		},
61		"magicfuncs": {
62			{Words(``, `\b`, `__construct`, `__destruct`, `__call`, `__callStatic`, `__get`, `__set`, `__isset`, `__unset`, `__sleep`, `__wakeup`, `__toString`, `__invoke`, `__set_state`, `__clone`, `__debugInfo`), NameFunctionMagic, nil},
63		},
64		"magicconstants": {
65			{Words(``, `\b`, `__LINE__`, `__FILE__`, `__DIR__`, `__FUNCTION__`, `__CLASS__`, `__TRAIT__`, `__METHOD__`, `__NAMESPACE__`), NameConstant, nil},
66		},
67		"classname": {
68			{`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*`, NameClass, Pop(1)},
69		},
70		"functionname": {
71			Include("magicfuncs"),
72			{`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*`, NameFunction, Pop(1)},
73			Default(Pop(1)),
74		},
75		"string": {
76			{`"`, LiteralStringDouble, Pop(1)},
77			{`[^{$"\\]+`, LiteralStringDouble, nil},
78			{`\\([nrt"$\\]|[0-7]{1,3}|x[0-9a-f]{1,2})`, LiteralStringEscape, nil},
79			{`\$(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*(\[\S+?\]|->(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)?`, LiteralStringInterpol, nil},
80			{`(\{\$\{)(.*?)(\}\})`, ByGroups(LiteralStringInterpol, UsingSelf("root"), LiteralStringInterpol), nil},
81			{`(\{)(\$.*?)(\})`, ByGroups(LiteralStringInterpol, UsingSelf("root"), LiteralStringInterpol), nil},
82			{`(\$\{)(\S+)(\})`, ByGroups(LiteralStringInterpol, NameVariable, LiteralStringInterpol), nil},
83			{`[${\\]`, LiteralStringDouble, nil},
84		},
85	}
86}
87