-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanza_lexer.moon
150 lines (122 loc) · 5.15 KB
/
stanza_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- Copyright 2016 Jake Russo
-- License: MIT
howl.aux.lpeg_lexer ->
word = (...) ->
word_char = alpha + digit + S'_-+?!'
(-B(1) + B(-word_char)) * any(...) * -word_char
c = capture
ident_filler = (alpha + digit + S'_-+?!')^0
ident = (alpha + S'$#')^1 * ident_filler
ws = c 'whitespace', blank
identifier = c 'identifier', ident
fdecl = c('keyword', word { 'defn', 'defmulti', 'defmethod' }) * (c 'operator', '*')^0 * ws^1 *
c('type_def', ident) * (ws^1 + #P'<')
decl = c('keyword', word {'defstruct', 'defpackage', 'deftype'}) * ws^1 * c('label', ident)
keyword = c 'keyword', word {
'if', 'else', 'when', 'switch', 'match', 'let', 'let-var',
'where', 'for', 'while', 'label', 'yield', 'try', 'catch',
'finally', 'throw', 'attempt', 'fn', 'fn*', 'multifn', 'multifn*',
'qquote', 'return', 'call-c', 'val', 'var', 'import', 'with'
}
declarator = c 'member', word {
'val', 'var', 'label', 'let', 'let-var', 'new'
}
functions = c 'function', word({
'not-equal', 'not-equal?', 'equal', 'equal?', 'compare',
'less?', 'less-eq?', 'greater?', 'greater-eq?', 'to-seq',
'maximum', 'max', 'minimum', 'min', 'hash', 'length', 'push',
'empty?', 'next', 'peek', 'get?', 'get', 'set', 'map!', 'map',
'reverse!', 'reverse', 'in-reverse', 'println-all', 'println', 'print-all', 'print',
'with-output-stream', 'current-output-stream', 'get-char', 'get-byte',
'do-indented', 'indented', 'put', 'close', 'with-output-file', 'spit',
'write-all', 'write', 'close', 'slurp', 'peek?', 'info',
'bits-as-float', 'bits-as-double', 'bits', 'rand', 'fill-template', 'fill',
'ceil-log2', 'floor-log2', 'next-pow2', 'prev-pow2', 'sum', 'product',
'complement', 'digit?', 'letter?', 'upper-case?', 'upper-case',
'lower-case?', 'lower-case', 'start', 'end', 'step', 'inclusive?', 'to-string',
'matches?', 'prefix?', 'suffix?', 'append-all', 'append',
'string-join', 'last-index-of-chars', 'last-index-of-char', 'replace', 'trim',
'add-all', 'add', 'clear', 'to-array', 'get-chars', 'set-chars',
'to-tuple', 'cons', 'to-list', 'head', 'headn', 'tail', 'tailn',
'but-last', 'last', 'transpose', 'seq-append', 'filename', 'line', 'column',
'item', 'unwrap-token', 'unwrap-all', 'key?', 'keys?', 'key', 'value?', 'value!', 'values', 'value',
'to-symbol', 'symbol-join', 'gensym', 'name', 'id', 'qualified?', 'qualifier',
'throw', 'with-exception-handler', 'with-finally', 'try-catch-finally',
'fatal', 'fail', 'with-attempt', 'attempt-else', 'generate',
'resume', 'suspend', 'break', 'close', 'active?', 'open?',
'dynamic-wind', 'find!', 'find', 'first!', 'first', 'seq?', 'seq', 'filter',
'index-when!', 'index-when', 'split', 'take-while', 'take-until', 'seq-cat',
'all?', 'none?', 'any?', 'count', 'repeat_while', 'repeat', 'repeatedly',
'take-n', 'take-up-to-n', 'cat-all', 'cat', 'join', 'zip-all', 'zip',
'contains?', 'index-of!', 'index-of', 'reduce-right', 'reduce', 'unique',
'lookup??', 'parallel-seq', 'qsort!', 'lazy-qsort', 'marker!', 'marker',
'add-gc-notifier', 'command-line-arguments', 'file-exists?',
'delete-file', 'resolve-path', 'current-time-ms', 'current-time-us',
'get-env', 'set-env', 'call-system', 'stop', 'time',
'exp', 'log10', 'log', 'pow', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2',
'sinh', 'cosh', 'tanh', 'ceil', 'floor', 'round', 'to-radians', 'to-degrees',
'to-vector', 'pop', 'peek', 'remove-item', 'remove-when', 'remove', 'update', 'shorten',
'lengthen', 'default?', 'read-file', 'read-all', 'read', 'tagged-list?'
}) * #S'({'
constant = c 'constant', word { 'true', 'false', 'this' }
lotypes = c 'type', upper * ident_filler
uniqtypes = c 'special', (word { 'ref', 'ptr' }) + (-B'%w' * '?')
lostanza = c 'type', word { 'byte', 'int', 'long', 'float', 'double' }
modifier = c 'special', word { 'public', 'protected', 'extern', 'lostanza' }
wordop = c 'operator', word {
'to', 'through', 'by', 'in', 'and', 'or', 'not',
'as', 'as?', 'is', 'do', 'seq'
}
operator = c 'operator', S'~!@#$%^*+-=/.:&|<>'^1
comment = c 'comment', P';' * scan_until eol
number = c 'number', digit * scan_until(ws + S'()[]{}' + operator + eol)
char = c 'char', P"'" * (P'\\' * P(1) + P(1)) * P"'"
P {
'all'
all: any {
comment,
V'string',
V'deref',
V'fndel',
char,
number,
declarator,
wordop,
lostanza,
modifier,
fdecl,
decl,
keyword,
functions,
constant,
lotypes,
uniqtypes,
operator,
identifier
}
string: sequence {
c('string', '"'),
V'string_chunk',
c('string', '"')
}
string_chunk: c('string', scan_until(any('"', '%'), '\\')) * any {
#P('"'),
V'interpolation' * V'string_chunk',
c('string', P(1)) * V'string_chunk'
}
interpolation: c 'operator', P'%' * S'_*,~@%'
deref: sequence {
c('blue', B(eol + blank) * '['),
any({ ws, V'all', S'()' })^1,
c('blue', ']')
}
fndel: sequence {
c('fdecl', '{'),
any({
ws,
c('constant', P'_'),
V'all',
})^1,
c('fdecl', '}')
}
}