-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lexer.hs
194 lines (174 loc) · 5.49 KB
/
Lexer.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
module Lexer where
import qualified Source
import Data.List (sortBy)
import Data.Char (isDigit, isAlpha, isAlphaNum)
data TypeE = Void
| Int
| Bool
deriving (Show, Eq, Read)
data OperatorE = Plus
| Minus
| Multiplication
| Division
| Modulo
| Equals
| LesserThan
| GreaterThan
| LesserEqualThan
| GreaterEqualThan
| Nequals
| And
| Or
| Cons
| Not
deriving (Show, Eq, Read)
data TokenE = Comment String
| CurlyBracketOpen
| CurlyBracketClose
| AssignmentSign
| Semicolon
| ParenthesesOpen
| ParenthesesClose
| Type TypeE
| SquareBracketsOpen
| SquareBracketsClose
| Comma
| Identifier String
| If
| Else
| While
| Return
| Integer Int
| TrueT
| FalseT
| Operator OperatorE
| Extern -- For allowing linkage with other languages (like extern "C")
| QuotedString String -- String literals, and the language part in `extern "C"`
| Export -- For generating code for non-main functions
deriving (Show, Eq, Read)
data TokenMeta loc = Token TokenE loc
deriving (Show, Eq, Read)
type Token = TokenMeta Source.IndexSpan
data LexerResult = Match [Token] (String, Source.Index)
| NoMatch Source.Index
type LexerFunc = (String, Source.Index) -> LexerResult
literalMap :: [(String, TokenE)]
literalMap = [
("{", CurlyBracketOpen),
("}", CurlyBracketClose),
("=", AssignmentSign),
(";", Semicolon),
("(", ParenthesesOpen),
(")", ParenthesesClose),
("Void", Type Void),
("Int", Type Int),
("Bool", Type Bool),
("[", SquareBracketsOpen),
("]", SquareBracketsClose),
(",", Comma),
("if", If),
("else", Else),
("while", While),
("return", Return),
("True", TrueT),
("False", FalseT),
("+", Operator Plus),
("-", Operator Minus),
("*", Operator Multiplication),
("/", Operator Division),
("%", Operator Modulo),
("==", Operator Equals),
("<", Operator LesserThan),
(">", Operator GreaterThan),
("<=", Operator LesserEqualThan),
(">=", Operator GreaterEqualThan),
("!=", Operator Nequals),
("&&", Operator And),
("||", Operator Or),
(":", Operator Cons),
("!", Operator Not),
("extern", Extern),
("export", Export)
]
(>>>) :: LexerFunc -> LexerFunc -> LexerFunc
(>>>) f g = \x -> case f x of
Match tok loc -> Match tok loc
NoMatch _ -> g x
consumeWhitespace :: LexerFunc
consumeWhitespace (' ':xs, i) = Match [] (xs, i+1)
consumeWhitespace ('\t':xs, i) = Match [] (xs, i+1)
consumeWhitespace ('\n':xs, i) = Match [] (xs, i+1)
consumeWhitespace ('\r':xs, i) = Match [] (xs, i+1)
consumeWhitespace (_, i) = NoMatch i
consumeComment :: LexerFunc
consumeComment (str, start)
| (Source.isPrefixOf "/*" str) = case Source.findstr "*/" (Source.precut str 2) of
Nothing -> NoMatch (start+2)
Just n -> let
size = 2+n+2
end = start+size
in Match [Token (Comment (Source.substr str 2 n)) (Source.IndexSpan start end)] (Source.precut str size, end)
| (Source.isPrefixOf "//" str) = case Source.findLast (\c -> c /= '\n') (Source.precut str 2) of
Nothing -> Match [Token (Comment "") (Source.IndexSpan start (start+2))] (Source.precut str 2, start+2)
Just n -> let
size = 2+n+1
end = start+size
in Match [Token (Comment (Source.substr str 2 (n+1))) (Source.IndexSpan start end)] (Source.precut str size, end)
| otherwise = NoMatch start
-- NOTE: one cannot (yet) escape characters in spl...
consumeQuotedString :: LexerFunc
consumeQuotedString (str, start)
| (Source.isPrefixOf "\"" str) = case Source.findstr "\"" (Source.precut str 1) of
Nothing -> NoMatch (start+1)
Just n -> let
size = 1+n+1
end = start+size
in Match [Token (QuotedString (Source.substr str 1 n)) (Source.IndexSpan start end)] (Source.precut str size, end)
| otherwise = NoMatch start
consumeLiteral :: LexerFunc
consumeLiteral = foldr1 (>>>) literalFuncs
where literalFuncs = map f (reverse (sortBy (\(x, _) (y, _) -> compare (length x) (length y)) literalMap))
where f (lstr, ltok) (str, start)
| not (Source.isPrefixOf lstr str) = NoMatch start
| otherwise = let
size = length lstr
end = start + size
in Match [Token ltok (Source.IndexSpan start end)] (Source.precut str size, end)
consumeInteger :: LexerFunc
consumeInteger (str, start) = case Source.findLast isDigit str of
Nothing -> NoMatch start
Just n -> let
size = n+1
end = start+size
in Match [Token (Integer (read (Source.substr str 0 size))) (Source.IndexSpan start end)] (Source.precut str size, end)
consumeIdentifier :: LexerFunc
consumeIdentifier ([], start) = NoMatch start
consumeIdentifier (x:xs, start)
| isAlpha x = f (x:xs, start) -- Also include first character for token Location
| otherwise = NoMatch start
where f (str, start) = case Source.findLast (\c -> isAlphaNum c || c == '_') str of
Nothing -> NoMatch start
Just n -> let
size = n+1
end = start+size
in Match [Token (Identifier (Source.substr str 0 size)) (Source.IndexSpan start end)] (Source.precut str size, end)
lextok :: LexerFunc
lextok = consumeWhitespace
>>> consumeComment
>>> consumeQuotedString
>>> consumeInteger
>>> consumeLiteral
>>> consumeIdentifier
lexer :: LexerFunc
lexer ([], n) = Match [] ([], n)
lexer x = case lextok x of
NoMatch i -> NoMatch i
Match ts y -> case lexer y of
NoMatch i -> NoMatch i
Match us ([], n) -> Match (ts ++ us) ([], n)
Match _ (_:_, _) -> error "COMPILER BUG: Lexer didn't lex whole string!"
filterComment :: [TokenMeta a] -> [TokenMeta a]
filterComment = filter (\t -> case t of
Lexer.Token (Lexer.Comment _) _ -> False
_ -> True
)