vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

php.lua

(4613B)


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