1 -- Copyright 2006-2013 JMS. 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
= 'scala'}
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 symbol
= "'" * l
.word
20 local dq_str
= l
.delimited_range('"', true)
21 local tq_str
= '"""' * (l
.any
- '"""')^
0 * P('"""')^
-1
22 local string = token(l
.STRING
, tq_str
+ symbol
+ dq_str
)
25 local number = token(l
.NUMBER
, (l
.float
+ l
.integer
) * S('LlFfDd')^
-1)
28 local keyword
= token(l
.KEYWORD
, word_match
{
29 'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false',
30 'final', 'finally', 'for', 'forSome', 'if', 'implicit', 'import', 'lazy',
31 'match', 'new', 'null', 'object', 'override', 'package', 'private',
32 'protected', 'return', 'sealed', 'super', 'this', 'throw', 'trait', 'try',
33 'true', 'type', 'val', 'var', 'while', 'with', 'yield'
37 local type = token(l
.TYPE
, word_match
{
38 'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float',
39 'Int', 'Iterator', 'LinkedList', 'List', 'Long', 'Map', 'None', 'Option',
40 'Set', 'Short', 'SortedMap', 'SortedSet', 'String', 'TreeMap', 'TreeSet'
44 local identifier
= token(l
.IDENTIFIER
, l
.word
)
47 local operator
= token(l
.OPERATOR
, S('+-/*%<>!=^&|?~:;.()[]{}'))
50 local func
= token(l
.FUNCTION
, l
.word
) * #P('(')
53 local class_sequence
= token(l
.KEYWORD
, P('class')) * ws^
1 *
54 token(l
.CLASS
, l
.word
)
58 {'class', class_sequence
},
62 {'identifier', identifier
},
66 {'operator', operator
},
70 _patterns
= {'[{}]', '/%*', '%*/', '//'},
71 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1},
72 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')}