php.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. -- Copyright 2006-2018 Mitchell mitchell.att.foicica.com. See License.txt.
  2. -- PHP LPeg lexer.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
  6. local lex = lexer.new('php')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Keywords.
  10. -- Taken from /usr/local/share/textadept/lexers/php.lua
  11. -- TA-Config Note: updated from https://secure.php.net/manual/en/reserved.keywords.php
  12. lex:add_rule('keyword', token(lexer.KEYWORD, word_match[[
  13. abstract and array as bool boolean break callable case catch cfunction class
  14. clone const continue declare default define die directory do double echo else elseif
  15. empty enddeclare endfor endforeach endif endswitch endwhile eval exit extends
  16. false final finally float for foreach function global goto if implements
  17. include include_once instanceof insteadof int integer interface isset list
  18. namespace new null object old_function or parent print private protected
  19. public real require require_once resource return static stdclass string switch
  20. throw trait true try unset use var while xor yield
  21. __class__ __file__ __function__ __line__ __sleep __wakeup
  22. ]]))
  23. local word = (lexer.alpha + '_' + R('\127\255')) *
  24. (lexer.alnum + '_' + R('\127\255'))^0
  25. -- Function related.
  26. lex:add_rule( 'func_tags', token('func_tags', word * #(lexer.space^0 * '(')) )
  27. lex:add_style('func_tags', lexer.STYLE_TYPE)
  28. --lex:add_rule( 'special_tags', token('special_tags', word_match([[
  29. -- function class
  30. --]], true) ))
  31. --lex:add_style('special_tags', lexer.STYLE_TYPE..',italics')
  32. -- Identifiers.
  33. lex:add_rule('identifier', token(lexer.IDENTIFIER, word))
  34. -- Variables.
  35. local vars = ('$' * word) + ('$$' * word)
  36. lex:add_rule('variable', token(lexer.VARIABLE, vars))
  37. -- Strings.
  38. local sq_str = lexer.delimited_range("'")
  39. local dq_str = lexer.delimited_range('"')
  40. local bt_str = lexer.delimited_range('`')
  41. local heredoc = '<<<' * P(function(input, index)
  42. local _, e, delimiter = input:find('([%a_][%w_]*)[\n\r\f]+', index)
  43. if delimiter then
  44. local _, e = input:find('[\n\r\f]+'..delimiter, e)
  45. return e and e + 1
  46. end
  47. end)
  48. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + bt_str + heredoc))
  49. -- TODO: interpolated code.
  50. -- Comments.
  51. local line_comment = (P('//') + '#') * lexer.nonnewline^0
  52. local block_comment = '/*' * (lexer.any - '*/')^0 * P('*/')^-1
  53. lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
  54. -- Numbers.
  55. lex:add_rule('number', token(lexer.NUMBER, lexer.float + lexer.integer))
  56. -- Operators.
  57. --lex:add_rule('operator', token(lexer.OPERATOR, S('!@%^*&()-+=|/?.,;:<>[]{}')))
  58. lex:add_rule('operator', token(lexer.OPERATOR, S('%^*-+=/<>')))
  59. -- Embedded in HTML.
  60. local html = lexer.load('html')
  61. -- Embedded PHP.
  62. local php_start_rule = token('php_tag', '<?' * ('php' * lexer.space)^-1)
  63. local php_end_rule = token('php_tag', '?>')
  64. html:embed(lex, php_start_rule, php_end_rule)
  65. lex:add_style('php_tag', lexer.STYLE_EMBEDDED)
  66. -- Fold points.
  67. lex:add_fold_point('php_tag', '<?', '?>')
  68. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  69. lex:add_fold_point(lexer.COMMENT, '//', lexer.fold_line_comments('//'))
  70. lex:add_fold_point(lexer.COMMENT, '#', lexer.fold_line_comments('#'))
  71. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  72. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  73. return lex