-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnix_lexer.moon
66 lines (54 loc) · 1.46 KB
/
nix_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
61
62
63
64
65
66
howl.util.lpeg_lexer ->
c = capture
keyword = c 'keyword', word {
'rec', 'with', 'let', 'in', 'inherit',
'assert', 'if', 'else', 'then', '...'
}
builtins = c 'function', word {
'import', 'abort', 'baseNameOf', 'dirOf', 'isNull', 'builtins',
'map', 'removeAttrs', 'throw', 'toString', 'derivation'
}
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('"', '"', '\\')
-- operator = c 'operator', S'+-*!/%^#~=<>;,.(){}[]?'
operator = c 'operator', word {
'++', '+', '?', '.', '!', '//', '==',
'!=', '&&', '||', '->', '='
}
-- 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
identifier = c 'identifier', ident
path = any {
'<' * (S('.+-') + alpha)^1 * ('/' * (S('.+-') + alpha)^1)^0 * '>'
(S('.+-') + alpha)^0 * ('/' * (S('.+-') + alpha)^1)^1
}
special = c 'special', any {
'true',
'false',
'null',
ident * ":",
ident * "@",
path
}
any {
number,
string,
comment,
special,
operator,
keyword,
identifier,
builtins
}