1 -- Copyright (c) 2014-2015 Piotr Orzechowski [drzewo.org]. See LICENSE.
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
8 local M
= {_NAME
= 'xtend'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local line_comment
= '//' * l
.nonnewline_esc^
0
15 local block_comment
= '/*' * (l
.any
- '*/')^
0 * P('*/')^
-1
16 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
19 local sq_str
= l
.delimited_range("'", true)
20 local dq_str
= l
.delimited_range('"', true)
21 local string = token(l
.STRING
, sq_str
+ dq_str
)
24 local templ_str
= "'''" * (l
.any
- P("'''"))^
0 * P("'''")^
-1
25 local template
= token('template', templ_str
, true)
28 local small_suff
= S('lL')
29 local med_suff
= P(S('bB') * S('iI'))
30 local large_suff
= S('dD') + S('fF') + P(S('bB') * S('dD'))
31 local exp = S('eE') * l
.digit^
1
33 local dec_inf
= ('_' * l
.digit^
1)^
0
34 local hex_inf
= ('_' * l
.xdigit^
1)^
0
35 local float_pref
= l
.digit^
1 * '.' * l
.digit^
1
36 local float_suff
= exp^
-1 * med_suff^
-1 * large_suff^
-1
38 local dec
= l
.digit
* dec_inf
* (small_suff^
-1 + float_suff
)
39 local hex
= l
.hex_num
* hex_inf
* P('#' * (small_suff
+ med_suff
))^
-1
40 local float
= float_pref
* dec_inf
* float_suff
42 local number = token(l
.NUMBER
, float
+ hex
+ dec
)
45 local keyword
= token(l
.KEYWORD
, word_match
{
47 'abstract', 'annotation', 'as', 'case', 'catch', 'class', 'create', 'def',
48 'default', 'dispatch', 'do', 'else', 'enum', 'extends', 'extension', 'final',
49 'finally', 'for', 'if', 'implements', 'import', 'interface', 'instanceof',
50 'it', 'new', 'override', 'package', 'private', 'protected', 'public',
51 'return', 'self', 'static', 'super', 'switch', 'synchronized', 'this',
52 'throw', 'throws', 'try', 'typeof', 'val', 'var', 'while',
54 -- 'AFTER', 'BEFORE', 'ENDFOR', 'ENDIF', 'FOR', 'IF', 'SEPARATOR',
56 'true', 'false', 'null'
60 local type = token(l
.TYPE
, word_match
{
61 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void',
62 'Boolean', 'Byte', 'Character', 'Double', 'Float', 'Integer', 'Long', 'Short',
67 local identifier
= token(l
.IDENTIFIER
, l
.word
)
70 local operator
= token(l
.OPERATOR
, S('+-/*%<>!=^&|?~:;.()[]{}#'))
73 local annotation
= token('annotation', '@' * l
.word
)
76 local func
= token(l
.FUNCTION
, l
.word
) * #P('(')
79 local class
= token(l
.KEYWORD
, P('class')) * ws^
1 * token(l
.CLASS
, l
.word
)
88 {'identifier', identifier
},
89 {'template', template
},
93 {'annotation', annotation
},
94 {'operator', operator
},
95 {'error', token(l
.ERROR
, l
.any
)},
100 annotation
= l
.STYLE_PREPROCESSOR
,
101 template
= l
.STYLE_EMBEDDED
106 _patterns
= {'[{}]', '/%*', '%*/', '//', 'import'},
107 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1},
108 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')},
109 [l
.KEYWORD
] = {['import'] = l
.fold_line_comments('import')}