1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
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'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
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
)
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
)
25 local _
, e
= input
:find('[\n\r\f]+'..delimiter
, e
)
29 local string = token(l
.STRING
, sq_str
+ dq_str
+ bt_str
+ heredoc
)
30 -- TODO: interpolated code.
33 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
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'
52 local word
= (l
.alpha
+ '_' + R('\127\255')) * (l
.alnum
+ '_' + R('\127\255'))^
0
53 local variable
= token(l
.VARIABLE
, '$' * word
)
56 local identifier
= token(l
.IDENTIFIER
, word
)
59 local operator
= token(l
.OPERATOR
, S('!@%^*&()-+=|/.,;:<>[]{}') + '?' * -P('>'))
64 {'identifier', identifier
},
66 {'variable', variable
},
69 {'operator', operator
},
73 local html
= l
.load('html')
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
)
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