11package circular
22
33import (
4- "strings"
5-
64 . "github.com/alecthomas/chroma" // nolint
7- "github.com/alecthomas/chroma/lexers/h"
85 "github.com/alecthomas/chroma/lexers/internal"
96)
107
11- // PHP lexer.
12- var PHP = internal .Register (DelegatingLexer ( h . HTML , MustNewLexer (
8+ // PHP lexer for pure PHP code (not embedded in HTML) .
9+ var PHP = internal .Register (MustNewLexer (
1310 & Config {
1411 Name : "PHP" ,
1512 Aliases : []string {"php" , "php3" , "php4" , "php5" },
@@ -19,73 +16,65 @@ var PHP = internal.Register(DelegatingLexer(h.HTML, MustNewLexer(
1916 CaseInsensitive : true ,
2017 EnsureNL : true ,
2118 },
22- Rules {
23- "root" : {
24- {`<\?(php)?` , CommentPreproc , Push ("php" )},
25- {`[^<]+` , Other , nil },
26- {`<` , Other , nil },
27- },
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- },
19+ phpCommonRules .Rename ("php" , "root" ),
20+ ))
21+
22+ var phpCommonRules = Rules {
23+ "php" : {
24+ {`\?>` , CommentPreproc , Pop (1 )},
25+ {`(<<<)([\'"]?)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)(\2\n.*?\n\s*)(\3)(;?)(\n)` , ByGroups (LiteralString , LiteralString , LiteralStringDelimiter , LiteralString , LiteralStringDelimiter , Punctuation , Text ), nil },
26+ {`\s+` , Text , nil },
27+ {`#.*?\n` , CommentSingle , nil },
28+ {`//.*?\n` , CommentSingle , nil },
29+ {`/\*\*/` , CommentMultiline , nil },
30+ {`/\*\*.*?\*/` , LiteralStringDoc , nil },
31+ {`/\*.*?\*/` , CommentMultiline , nil },
32+ {`(->|::)(\s*)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)` , ByGroups (Operator , Text , NameAttribute ), nil },
33+ {`[~!%^&*+=|:.<>/@-]+` , Operator , nil },
34+ {`\?` , Operator , nil },
35+ {`[\[\]{}();,]+` , Punctuation , nil },
36+ {`(class)(\s+)` , ByGroups (Keyword , Text ), Push ("classname" )},
37+ {`(function)(\s*)(?=\()` , ByGroups (Keyword , Text ), nil },
38+ {`(function)(\s+)(&?)(\s*)` , ByGroups (Keyword , Text , Operator , Text ), Push ("functionname" )},
39+ {`(const)(\s+)((?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)` , ByGroups (Keyword , Text , NameConstant ), nil },
40+ {`(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 },
41+ {`(true|false|null)\b` , KeywordConstant , nil },
42+ Include ("magicconstants" ),
43+ {`\$\{\$+(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*\}` , NameVariable , nil },
44+ {`\$+(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*` , NameVariable , nil },
45+ {`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*` , NameOther , nil },
46+ {`(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?` , LiteralNumberFloat , nil },
47+ {`\d+e[+-]?[0-9]+` , LiteralNumberFloat , nil },
48+ {`0[0-7]+` , LiteralNumberOct , nil },
49+ {`0x[a-f0-9]+` , LiteralNumberHex , nil },
50+ {`\d+` , LiteralNumberInteger , nil },
51+ {`0b[01]+` , LiteralNumberBin , nil },
52+ {`'([^'\\]*(?:\\.[^'\\]*)*)'` , LiteralStringSingle , nil },
53+ {"`([^`\\ \\ ]*(?:\\ \\ .[^`\\ \\ ]*)*)`" , LiteralStringBacktick , nil },
54+ {`"` , LiteralStringDouble , Push ("string" )},
55+ },
56+ "magicfuncs" : {
57+ {Words (`` , `\b` , `__construct` , `__destruct` , `__call` , `__callStatic` , `__get` , `__set` , `__isset` , `__unset` , `__sleep` , `__wakeup` , `__toString` , `__invoke` , `__set_state` , `__clone` , `__debugInfo` ), NameFunctionMagic , nil },
58+ },
59+ "magicconstants" : {
60+ {Words (`` , `\b` , `__LINE__` , `__FILE__` , `__DIR__` , `__FUNCTION__` , `__CLASS__` , `__TRAIT__` , `__METHOD__` , `__NAMESPACE__` ), NameConstant , nil },
61+ },
62+ "classname" : {
63+ {`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*` , NameClass , Pop (1 )},
64+ },
65+ "functionname" : {
66+ Include ("magicfuncs" ),
67+ {`(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*` , NameFunction , Pop (1 )},
68+ Default (Pop (1 )),
69+ },
70+ "string" : {
71+ {`"` , LiteralStringDouble , Pop (1 )},
72+ {`[^{$"\\]+` , LiteralStringDouble , nil },
73+ {`\\([nrt"$\\]|[0-7]{1,3}|x[0-9a-f]{1,2})` , LiteralStringEscape , nil },
74+ {`\$(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*(\[\S+?\]|->(?:[\\_a-z]|[^\x00-\x7f])(?:[\\\w]|[^\x00-\x7f])*)?` , LiteralStringInterpol , nil },
75+ {`(\{\$\{)(.*?)(\}\})` , ByGroups (LiteralStringInterpol , UsingSelf ("root" ), LiteralStringInterpol ), nil },
76+ {`(\{)(\$.*?)(\})` , ByGroups (LiteralStringInterpol , UsingSelf ("root" ), LiteralStringInterpol ), nil },
77+ {`(\$\{)(\S+)(\})` , ByGroups (LiteralStringInterpol , NameVariable , LiteralStringInterpol ), nil },
78+ {`[${\\]` , LiteralStringDouble , nil },
8579 },
86- ).SetAnalyser (func (text string ) float32 {
87- if strings .Contains (text , "<?php" ) {
88- return 0.5
89- }
90- return 0.0
91- })))
80+ }
0 commit comments