You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

php.go 4.4KB

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