-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokens.hs
333 lines (333 loc) · 10.7 KB
/
Tokens.hs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
--------------------------------------------------------------------------------------------------------------------------------
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE StandaloneDeriving #-}
module Tokens (Token (..), Tokens, current_line_and_char, nom_token, Tokens.null, tokenise) where
import Control.Monad.Except (MonadError (..))
import Control.Monad.State.Strict (MonadState (..), StateT, runStateT)
import Data.Bifunctor (first)
import Data.Char (isDigit, isLetter)
import Errors (Err', Error' (..), Line_and_char, init_line_and_char, next_char, next_line)
data Char' =
Delimiter_char Token |
Invalid_char |
Minus_char |
Nat_char Char |
Newline_char |
Operator_char Char |
Slash_char |
Space_char |
Tick_char |
Tilde_char |
Word_char Char
deriving Show
data Token =
Algebraic_token |
Blank_token |
Branch_token |
Check_token |
Class_token |
Comma_token |
Data_token |
Def_token |
Eq_token |
Eval_token |
Instance_token |
Import_token |
Left_curly_token |
Left_round_token |
Left_square_token |
Match_token |
Name_token String |
Nat_token Integer |
Negate_token |
Operator_token String |
Right_curly_token |
Right_round_token |
Right_square_token |
Run_token |
Struct_token |
Term_token |
Test_token |
Type_token
deriving Show
data Token' = Token' Line_and_char Token
deriving Show
type Tokeniser = StateT (Line_and_char, [Char']) Err'
data Tokens = Tokens [Token'] Line_and_char
deriving Show
deriving instance Eq Token
add_token :: Tokeniser (Token, [Token']) -> Tokeniser [Token']
add_token tokenise'' =
do
line_and_char <- get_line_and_char
(token, tokens) <- tokenise''
return (Token' line_and_char token : tokens)
classify_char :: Char -> Char'
classify_char c =
case c of
'\n' -> Newline_char
' ' -> Space_char
'(' -> Delimiter_char Left_round_token
')' -> Delimiter_char Right_round_token
',' -> Delimiter_char Comma_token
'-' -> Minus_char
'/' -> Slash_char
'[' -> Delimiter_char Left_square_token
']' -> Delimiter_char Right_square_token
'`' -> Tick_char
'{' -> Delimiter_char Left_curly_token
'}' -> Delimiter_char Right_curly_token
'~' -> Tilde_char
_ ->
case (is_operator c, is_letter c, isDigit c) of
(True, False, False) -> Operator_char c
(False, True, False) -> Word_char c
(False, False, True) -> Nat_char c
_ -> Invalid_char
current_line_and_char :: Tokens -> Line_and_char
current_line_and_char (Tokens tokens end_line_and_char) =
case tokens of
[] -> end_line_and_char
Token' line_and_char _ : _ -> line_and_char
end_tokens :: Tokeniser [Token']
end_tokens = return []
gather_token :: (Char' -> Maybe Char) -> (String -> Token) -> Tokeniser [Token']
gather_token token_char string_to_token = add_token (first string_to_token <$> gather_token' token_char)
gather_token' :: (Char' -> Maybe Char) -> Tokeniser (String, [Token'])
gather_token' token_char =
do
maybe_char <- get_char 0
case maybe_char >>= token_char of
Nothing -> (,) "" <$> tokenise'
Just c ->
do
Tokens.next_char
(token, tokens) <- gather_token' token_char
return (c : token, tokens)
get_char :: Integer -> Tokeniser (Maybe Char')
get_char i = index i <$> get_text
get_line_and_char :: Tokeniser Line_and_char
get_line_and_char =
do
(line_and_char, _) <- get
return line_and_char
get_text :: Tokeniser [Char']
get_text =
do
(_, text) <- get
return text
index :: Integer -> [t] -> Maybe t
index i x =
case x of
[] -> Nothing
y : x' ->
case i of
0 -> Just y
_ -> index (i - 1) x'
is_letter :: Char -> Bool
is_letter c = elem c ['\'', '_'] || isLetter c
is_operator :: Char -> Bool
is_operator c = elem c ['!', '#', '$', '%', '&', '*', '+', '.', ':', ';', '<', '=', '>', '?', '@', '\\', '^', '|']
nat_char :: Char' -> Maybe Char
nat_char char =
case char of
Nat_char c -> Just c
_ -> Nothing
nat_token :: String -> Token
nat_token i = Nat_token (read i)
next_char :: Tokeniser ()
next_char =
do
(line_and_char, text) <- get
case text of
[] -> undefined
char : text' ->
put
(
case char of
Newline_char -> next_line line_and_char
_ -> Errors.next_char line_and_char,
text')
nom_token :: (Token -> Maybe t) -> Tokens -> Maybe (t, Tokens)
nom_token f (Tokens tokens end_line_and_char) =
case tokens of
[] -> Nothing
Token' _ token : tokens' ->
do
x <- f token
Just (x, Tokens tokens' end_line_and_char)
null :: Tokens -> Bool
null (Tokens tokens _) = Prelude.null tokens
operator_char :: Char' -> Maybe Char
operator_char char' =
case char' of
Operator_char char -> Just char
Minus_char -> Just '-'
Slash_char -> Just '/'
Tilde_char -> Just '~'
_ -> Nothing
operator_token :: String -> Token
operator_token operator =
case operator of
"=" -> Eq_token
_ -> Operator_token operator
throw_error :: (Line_and_char -> Error') -> Tokeniser t
throw_error err =
do
line_and_char <- get_line_and_char
throwError (err line_and_char)
tokenise :: String -> Err' Tokens
tokenise text =
do
(tokens, (line_and_char, _)) <- runStateT tokenise' (init_line_and_char, classify_char <$> text)
Right (Tokens tokens line_and_char)
tokenise' :: Tokeniser [Token']
tokenise' =
do
maybe_char <- get_char 0
case maybe_char of
Nothing -> end_tokens
Just char ->
case char of
Delimiter_char token ->
add_token
(do
Tokens.next_char
tokens <- tokenise'
return (token, tokens))
Invalid_char -> throw_error Invalid_character
Minus_char ->
do
maybe_char' <- get_char 1
case maybe_char' >>= nat_char of
Nothing -> tokenise_operator
Just c ->
case c of
'0' -> throw_error Negation_of_int_starting_with_zero
_ ->
add_token
(do
Tokens.next_char
tokens <- tokenise_nat
return (Negate_token, tokens))
Nat_char c ->
do
maybe_char' <- get_char 1
case (c, maybe_char' >>= nat_char) of
('0', Just _) -> throw_error Int_starting_with_zero
_ -> tokenise_nat
Newline_char ->
do
Tokens.next_char
tokenise'
Operator_char _ -> tokenise_operator
Slash_char -> tokenise_operator
Space_char ->
do
Tokens.next_char
tokenise'
Tick_char ->
do
Tokens.next_char
tokenise_single_line_comment
Tilde_char ->
do
maybe_char' <- get_char 1
maybe_char'' <- get_char 2
case (maybe_char', maybe_char'' >>= operator_char) of
(Just Slash_char, Nothing) ->
do
Tokens.next_char
Tokens.next_char
tokenise_multiline_comment
_ -> tokenise_operator
Word_char _ -> tokenise_word
tokenise_multiline_comment :: Tokeniser [Token']
tokenise_multiline_comment =
do
maybe_char <- get_char 0
case maybe_char of
Nothing -> throwError Missing_end_comment
Just char ->
do
Tokens.next_char
case char of
Slash_char -> tokenise_multiline_comment_slash
_ ->
case operator_char char of
Nothing -> tokenise_multiline_comment
Just _ -> tokenise_multiline_comment_operator
tokenise_multiline_comment_operator :: Tokeniser [Token']
tokenise_multiline_comment_operator =
do
maybe_char <- get_char 0
case maybe_char >>= operator_char of
Nothing -> tokenise_multiline_comment
Just _ ->
do
Tokens.next_char
tokenise_multiline_comment_operator
tokenise_multiline_comment_slash :: Tokeniser [Token']
tokenise_multiline_comment_slash =
do
maybe_char <- get_char 0
case maybe_char >>= operator_char of
Nothing -> tokenise_multiline_comment
Just c ->
do
Tokens.next_char
case c of
'~' -> tokenise_multiline_comment_slash_tilde
_ -> tokenise_multiline_comment_operator
tokenise_multiline_comment_slash_tilde :: Tokeniser [Token']
tokenise_multiline_comment_slash_tilde =
do
maybe_char <- get_char 0
case maybe_char >>= operator_char of
Nothing -> tokenise'
Just _ -> tokenise_multiline_comment_operator
tokenise_nat :: Tokeniser [Token']
tokenise_nat = gather_token nat_char nat_token
tokenise_operator :: Tokeniser [Token']
tokenise_operator = gather_token operator_char operator_token
tokenise_single_line_comment :: Tokeniser [Token']
tokenise_single_line_comment =
do
maybe_char <- get_char 0
case maybe_char of
Nothing -> end_tokens
Just char ->
do
Tokens.next_char
case char of
Newline_char -> tokenise'
_ -> tokenise_single_line_comment
tokenise_word :: Tokeniser [Token']
tokenise_word = gather_token word_char word_token
word_char :: Char' -> Maybe Char
word_char char' =
case char' of
Nat_char char -> Just char
Word_char char -> Just char
_ -> Nothing
word_token :: String -> Token
word_token word =
case word of
"_" -> Blank_token
"Algebraic" -> Algebraic_token
"Branch" -> Branch_token
"Check" -> Check_token
"Class" -> Class_token
"Data" -> Data_token
"Def" -> Def_token
"Eval" -> Eval_token
"Import" -> Import_token
"Instance" -> Instance_token
"Match" -> Match_token
"Run" -> Run_token
"Struct" -> Struct_token
"Term" -> Term_token
"Test" -> Test_token
"Type" -> Type_token
_ -> Name_token word
--------------------------------------------------------------------------------------------------------------------------------