ui: make primary cursor blink even if no lua theme has been loaded
[vis.git] / lexers / php.lua
blob22eb96028a6c4085cf755650e41507d8587b016e
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- PHP LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
8 local M = {_NAME = 'php'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = (P('//') + '#') * l.nonnewline^0
15 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
16 local comment = token(l.COMMENT, block_comment + line_comment)
18 -- Strings.
19 local sq_str = l.delimited_range("'")
20 local dq_str = l.delimited_range('"')
21 local bt_str = l.delimited_range('`')
22 local heredoc = '<<<' * P(function(input, index)
23 local _, e, delimiter = input:find('([%a_][%w_]*)[\n\r\f]+', index)
24 if delimiter then
25 local _, e = input:find('[\n\r\f]+'..delimiter, e)
26 return e and e + 1
27 end
28 end)
29 local string = token(l.STRING, sq_str + dq_str + bt_str + heredoc)
30 -- TODO: interpolated code.
32 -- Numbers.
33 local number = token(l.NUMBER, l.float + l.integer)
35 -- Keywords.
36 local keyword = token(l.KEYWORD, word_match{
37 'and', 'array', 'as', 'bool', 'boolean', 'break', 'case',
38 'cfunction', 'class', 'const', 'continue', 'declare', 'default',
39 'die', 'directory', 'do', 'double', 'echo', 'else', 'elseif',
40 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif',
41 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'false',
42 'float', 'for', 'foreach', 'function', 'global', 'if', 'include',
43 'include_once', 'int', 'integer', 'isset', 'list', 'new', 'null',
44 'object', 'old_function', 'or', 'parent', 'print', 'real',
45 'require', 'require_once', 'resource', 'return', 'static',
46 'stdclass', 'string', 'switch', 'true', 'unset', 'use', 'var',
47 'while', 'xor', '__class__', '__file__', '__function__',
48 '__line__', '__sleep', '__wakeup'
51 -- Variables.
52 local word = (l.alpha + '_' + R('\127\255')) * (l.alnum + '_' + R('\127\255'))^0
53 local variable = token(l.VARIABLE, '$' * word)
55 -- Identifiers.
56 local identifier = token(l.IDENTIFIER, word)
58 -- Operators.
59 local operator = token(l.OPERATOR, S('!@%^*&()-+=|/.,;:<>[]{}') + '?' * -P('>'))
61 M._rules = {
62 {'whitespace', ws},
63 {'keyword', keyword},
64 {'identifier', identifier},
65 {'string', string},
66 {'variable', variable},
67 {'comment', comment},
68 {'number', number},
69 {'operator', operator},
72 -- Embedded in HTML.
73 local html = l.load('html')
75 -- Embedded PHP.
76 local php_start_rule = token('php_tag', '<?' * ('php' * l.space)^-1)
77 local php_end_rule = token('php_tag', '?>')
78 l.embed_lexer(html, M, php_start_rule, php_end_rule)
80 M._tokenstyles = {
81 php_tag = l.STYLE_EMBEDDED
84 local _foldsymbols = html._foldsymbols
85 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '<%?'
86 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '%?>'
87 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '/%*'
88 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '%*/'
89 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '//'
90 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '#'
91 _foldsymbols._patterns[#_foldsymbols._patterns + 1] = '[{}()]'
92 _foldsymbols.php_tag = {['<?'] = 1, ['?>'] = -1}
93 _foldsymbols[l.COMMENT]['/*'], _foldsymbols[l.COMMENT]['*/'] = 1, -1
94 _foldsymbols[l.COMMENT]['//'] = l.fold_line_comments('//')
95 _foldsymbols[l.COMMENT]['#'] = l.fold_line_comments('#')
96 _foldsymbols[l.OPERATOR] = {['{'] = 1, ['}'] = -1, ['('] = 1, [')'] = -1}
97 M._foldsymbols = _foldsymbols
99 return M