-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwren_lexer.moon
60 lines (48 loc) · 1.35 KB
/
wren_lexer.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
howl.util.lpeg_lexer ->
c = capture
keyword = c 'keyword', word {
'break','class','construct','else','for','foreign','if',
'import','in','is','return',
'static','super','this','var','while'
}
comment_span = (start_pat, end_pat) ->
start_pat * ((V'nested_comment' + P 1) - end_pat)^0 * (end_pat + P(-1))
comment = c 'comment', any {
'//' * scan_until(eol),
P { 'nested_comment', nested_comment: comment_span('/*','*/') }
}
string = c 'string', span('"', '"', P'\\')
operator = c 'operator', S'+-*!/%^#~=<>;:,.(){}[]'
-- numbers
hexadecimal_number = P'0x' * R('AF','af','09')^1
float = digit^0 * P'.' * digit^1
number = c 'number', any {
hexadecimal_number,
(float + digit^1) * (S'eE' * P('-')^0 * digit^1)^0
}
-- identifiers
ident = (alpha + '_')^1 * (alpha + digit + '_')^0
class_name = upper * (alpha + digit + '_')^0
field_name = '_' * (alpha + digit + '_')^0
identifier = c 'identifier', ident
class_c = c 'class', class_name
field = c 'field', field_name
-- constant = c 'constant', upper^1 * any(upper, '_', digit)^0 * any(eol, -#lower)
special = c 'special', any {
'true',
'false',
'null'
}
-- ws = c 'whitespace', blank^0
any {
number,
string,
comment,
operator,
special,
keyword,
-- constant,
identifier,
class_c,
field
}