1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- JavaScript LPeg lexer.
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
= 'javascript'}
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("'")
20 local dq_str
= l
.delimited_range('"')
21 local regex_str
= #P('/') * l
.last_char_includes('+-*%^!=&|?:;,([{<>') *
22 l
.delimited_range('/', true) * S('igm')^
0
23 local string = token(l
.STRING
, sq_str
+ dq_str
) + token(l
.REGEX
, regex_str
)
26 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
29 local keyword
= token(l
.KEYWORD
, word_match
{
30 'abstract', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class',
31 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else',
32 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for',
33 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int',
34 'interface', 'let', 'long', 'native', 'new', 'null', 'package', 'private',
35 'protected', 'public', 'return', 'short', 'static', 'super', 'switch',
36 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try',
37 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield'
41 local identifier
= token(l
.IDENTIFIER
, l
.word
)
44 local operator
= token(l
.OPERATOR
, S('+-/*%^!=&|?:;,.()[]{}<>'))
49 {'identifier', identifier
},
53 {'operator', operator
},
57 _patterns
= {'[{}]', '/%*', '%*/', '//'},
58 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1},
59 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')}