logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

php.lua (4647B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- PHP LPeg lexer.
  3. local lexer = lexer
  4. local P, S = lpeg.P, lpeg.S
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Types.
  9. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  10. -- Functions.
  11. local word = (lexer.alpha + '_' + lpeg.R('\127\255')) * (lexer.alnum + '_' + lpeg.R('\127\255'))^0
  12. local func = lex:tag(lexer.FUNCTION, word)
  13. local method = lpeg.B('->') * lex:tag(lexer.FUNCTION_METHOD, word)
  14. lex:add_rule('function', (method + func) * #(lexer.space^0 * '('))
  15. -- Constants.
  16. lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
  17. -- Identifiers.
  18. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, word))
  19. -- Variables.
  20. lex:add_rule('variable', lex:tag(lexer.VARIABLE, '$' * word))
  21. -- Strings.
  22. local sq_str = lexer.range("'")
  23. local dq_str = lexer.range('"')
  24. local bq_str = lexer.range('`')
  25. local heredoc = '<<<' * P(function(input, index)
  26. local _, e, delimiter = input:find('([%a_][%w_]*)[\n\r\f]+', index)
  27. if delimiter then
  28. _, e = input:find('[\n\r\f]+' .. delimiter, e)
  29. return e and e + 1
  30. end
  31. end)
  32. lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + bq_str + heredoc))
  33. -- TODO: interpolated code.
  34. -- Comments.
  35. local line_comment = lexer.to_eol(P('//') + '#')
  36. local block_comment = lexer.range('/*', '*/')
  37. lex:add_rule('comment', lex:tag(lexer.COMMENT, block_comment + line_comment))
  38. -- Numbers.
  39. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
  40. -- Operators.
  41. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('!@%^*&()-+=|/?.,;:<>[]{}')))
  42. -- Embedded in HTML.
  43. local html = lexer.load('html')
  44. -- Embedded PHP.
  45. local php_start_rule = lex:tag(lexer.PREPROCESSOR, '<?' * ('php' * lexer.space)^-1)
  46. local php_end_rule = lex:tag(lexer.PREPROCESSOR, '?>')
  47. html:embed(lex, php_start_rule, php_end_rule)
  48. -- Fold points.
  49. lex:add_fold_point(lexer.PREPROCESSOR, '<?', '?>')
  50. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  51. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  52. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  53. -- Word lists.
  54. lex:set_word_list(lexer.KEYWORD, {
  55. -- Reserved words (http://php.net/manual/en/reserved.keywords.php)
  56. '__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch',
  57. 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else',
  58. 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval',
  59. 'exit', 'extends', 'final', 'finally', 'fn', 'for', 'foreach', 'function', 'global', 'goto', 'if',
  60. 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list',
  61. 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once',
  62. 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor',
  63. 'yield', 'from',
  64. -- Reserved classes (http://php.net/manual/en/reserved.classes.php)
  65. 'Directory', 'stdClass', '__PHP_Incomplete_Class', 'Exception', 'ErrorException',
  66. 'php_user_filter', 'Closure', 'Generator', 'ArithmeticError', 'AssertionError',
  67. 'DivisionByZeroError', 'Error', 'Throwable', 'ParseError', 'TypeError', 'self', 'static', 'parent'
  68. })
  69. lex:set_word_list(lexer.TYPE, 'int float bool string true false null void iterable object')
  70. lex:set_word_list(lexer.CONSTANT_BUILTIN, {
  71. -- Compile-time (https://www.php.net/manual/en/reserved.keywords.php)
  72. '__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__',
  73. '__TRAIT__',
  74. -- Reserved (https://www.php.net/manual/en/reserved.constants.php)
  75. 'PHP_VERSION', 'PHP_MAJOR_VERSION', 'PHP_MINOR_VERSION', 'PHP_RELEASE_VERSION', 'PHP_VERSION_ID',
  76. 'PHP_EXTRA_VERSION', 'PHP_ZTS', 'PHP_DEBUG', 'PHP_MAXPATHLEN', 'PHP_OS', 'PHP_OS_FAMILY',
  77. 'PHP_SAPI', 'PHP_EOL', 'PHP_INT_MAX', 'PHP_INT_MIN', 'PHP_INT_SIZE', 'PHP_FLOAT_DIG',
  78. 'PHP_FLOAT_EPSILON', 'PHP_FLOAT_MIN', 'PHP_FLOAT_MAX', 'DEFAULT_INCLUDE_PATH', 'PEAR_INSTALL_DIR',
  79. 'PEAR_EXTENSION_DIR', 'PHP_EXTENSION_DIR', 'PHP_PREFIX', 'PHP_BINDIR', 'PHP_BINARY', 'PHP_MANDIR',
  80. 'PHP_LIBDIR', 'PHP_DATADIR', 'PHP_SYSCONFDIR', 'PHP_LOCALSTATEDIR', 'PHP_CONFIG_FILE_PATH',
  81. 'PHP_CONFIG_FILE_SCAN_DIR', 'PHP_SHLIB_SUFFIX', 'PHP_FD_SETSIZE', 'E_ERROR', 'E_WARNING',
  82. 'E_PARSE', 'E_NOTICE', 'E_CORE_ERROR', 'E_CORE_WARNING', 'E_COMPILE_ERROR', 'E_USER_ERROR',
  83. 'E_USER_WARNING', 'E_USER_NOTICE', 'E_DEPRECATED', 'E_DEPRECATED', 'E_USER_DEPRECATED', 'E_ALL',
  84. 'E_STRICT', '__COMPILER_HALT_OFFSET__'
  85. })
  86. lexer.property['scintillua.comment'] = '//'
  87. return lex