-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.x
233 lines (199 loc) · 10.4 KB
/
Lexer.x
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
{
module Lexer (scanner, showTokenPos) where
import Tokens
}
%wrapper "monadUserState"
$digit = 0-9
$Alpha = [a-zA-Z]
$alpha = [a-z]
$ALPHA = [A-Z]
tokens :-
<0> $white+ ;
<0> "//".* ;
-- Reserved words
<0> "int" {pushTk TkInt}
<0> "bool" {pushTk TkBool}
<0> "array" {pushTk TkArray}
<0> "declare" {pushTk TkDeclare}
<0> "read" {pushTk TkRead}
<0> "print" {pushTk TkPrint}
<0> "println" {pushTk TkPrintln}
<0> "if" {pushTk TkIf}
<0> "fi" {pushTk TkFi}
<0> "for" {pushTk TkFor}
<0> "in" {pushTk TkIn}
<0> "to" {pushTk TkTo}
<0> "rof" {pushTk TkRof}
<0> "do" {pushTk TkDo}
<0> "od" {pushTk TkOd}
<0> "atoi" {pushTk TkAtoi}
<0> "size" {pushTk TkSize}
<0> "max" {pushTk TkMax}
<0> "min" {pushTk TkMin}
-- Operators
<0> \, {pushTk TkComma}
<0> : {pushTk TkColon}
<0> \+ {pushTk TkPlus}
<0> \- {pushTk TkMinus}
<0> \* {pushTk TkMult}
<0> \/ {pushTk TkDiv}
<0> \% {pushTk TkMod}
<0> \\\/ {pushTk TkOr}
<0> \/\\ {pushTk TkAnd}
<0> \! {pushTk TkNot}
<0> \=\= {pushTk TkEqual}
<0> \!\= {pushTk TkNEqual}
<0> \< {pushTk TkLess}
<0> \<\= {pushTk TkLeq}
<0> \> {pushTk TkGreater}
<0> \>\= {pushTk TkGeq}
<0> \[\] {pushTk TkGuard}
<0> :\= {pushTk TkAssign}
<0> \|\| {pushTk TkConcat}
<0> \; {pushTk TkSemicolon}
<0> \.\. {pushTk TkSoForth}
<0> \-\-\> {pushTk TkArrow}
-- Delimiters
<0> \( {pushTk TkOpenPar}
<0> \) {pushTk TkClosePar}
<0> \[ {pushTk TkOpenBracket}
<0> \] {pushTk TkCloseBracket}
<0> \|\[ {pushTk TkOpenBlock}
<0> \]\| {pushTk TkCloseBlock}
-- Constants
<0> $digit+ {pushNum}
<0> "true" {pushTk TkTrue}
<0> "false" {pushTk TkFalse}
<0> \" {enterString `andBegin` stringSt} -- Start string
<stringSt> \\n {addChar '\n'} -- Insert \n to string
<stringSt> \\\\ {addChar '\\'} -- Insert '\' to string
<stringSt> \\\" {addChar '\"'}
<stringSt> \\ {pushInvalid} -- Invalid escape
<stringSt> \" {leaveString `andBegin` stateInitial} -- Leave string
<stringSt> \n+\r?\r+\n? {pushInvalidBreak} -- Invalid Break Lines
<stringSt> $printable {addCurrentChar} -- Insert any printable char to string
<stringSt> . {pushInvalidNPrint} -- Invalid Not Printable chars
-- Identifiers
<0> $Alpha[$Alpha $digit \_]* {pushId}
-- Invalid characters
<0> . {pushInvalid}
{
type LexerError = String
-- Initial State
stateInitial :: Int
stateInitial = 0
-- Definition needed by Alex
alexEOF :: Alex TokenPos
alexEOF = return (TkEOF, undefined, undefined)
-- User state
data AlexUserState = AlexUserState
{
lexerStringState :: Bool,
lexerStringValue :: String,
lexerErrors :: [LexerError]
}
alexInitUserState :: AlexUserState
alexInitUserState = AlexUserState
{
lexerStringState = False ,
lexerStringValue = "" ,
lexerErrors = []
}
-- Push Tokens
pushTk :: Token -> AlexInput -> Int -> Alex TokenPos
pushTk tok (AlexPn _ l c, _, _, _) _ = return (tok, l, c)
pushNum :: AlexInput -> Int -> Alex TokenPos
pushNum (AlexPn _ l c, _, _, inp) len = return $ (TkNum $ read $ take len inp, l, c)
pushId :: AlexInput -> Int -> Alex TokenPos
pushId (AlexPn _ l c, _, _, inp) len = return $ (TkId $ take len inp, l, c)
pushInvalid :: AlexInput -> Int -> Alex TokenPos
pushInvalid (AlexPn _ l c, _, _, inp) len =
do
ust@AlexUserState{lexerErrors = lexErr} <- alexGetUserState
alexSetUserState $ ust{lexerErrors = errorMsg : lexErr}
alexMonadScan
where errorMsg = "Error: Unexpected character \"" ++ (take len inp) ++ "\" in row "
++ show l ++ " column " ++ show c
pushInvalidBreak :: AlexInput -> Int -> Alex TokenPos
pushInvalidBreak (AlexPn _ l c, _, _, inp) _ =
do
ust@AlexUserState{lexerErrors = lexErr} <- alexGetUserState
alexSetUserState $ ust{lexerErrors = errorMsg : lexErr}
alexMonadScan
where errorMsg = "Error: Unexpected breakline in row "
++ show l ++ " column " ++ show c
pushInvalidNPrint :: AlexInput -> Int -> Alex TokenPos
pushInvalidNPrint (AlexPn _ l c, _, _, inp) _ =
do
ust@AlexUserState{lexerErrors = lexErr} <- alexGetUserState
alexSetUserState $ ust{lexerErrors = errorMsg : lexErr}
alexMonadScan
where errorMsg = "Error: Unexpected control char in row "
++ show l ++ " column " ++ show c
showTokenPos :: TokenPos -> String
showTokenPos (tk, l, c) = show tk ++ " " ++ show l ++ " " ++ show c
-- String mode
enterString :: AlexAction TokenPos
enterString _ _ = do
setStringState True
setStringValue []
alexMonadScan
addChar :: Char -> AlexAction TokenPos
addChar c _ _ = do
addCharStringValue c
alexMonadScan
addCurrentChar :: AlexAction TokenPos
addCurrentChar (_, _, _, inp) _ = do
addCharStringValue $ head inp
alexMonadScan
leaveString :: AlexAction TokenPos
leaveString (AlexPn _ l c, _, _, _) _ = do
str <- getStringValue
setStringState False
return $ (TkString $ reverse str, l, c - (length str) - 2)
setStringState :: Bool -> Alex ()
setStringState state = Alex setState
where
setState s = Right (s{alex_ust = (alex_ust s){lexerStringState = state} }, ())
setStringValue :: String -> Alex ()
setStringValue string = Alex setValue
where
setValue s = Right (s{alex_ust = (alex_ust s){lexerStringValue = string} }, ())
addCharStringValue :: Char -> Alex ()
addCharStringValue char = Alex addChar
where
addChar s = Right (s{alex_ust = (alex_ust s){lexerStringValue = char:lexerStringValue (alex_ust s)} }, ())
getStringValue :: Alex String
getStringValue = Alex getString
where
getString s@AlexState{alex_ust = usSt} = Right (s, lexerStringValue usSt)
getStringState :: Alex Bool
getStringState = Alex getState
where
getState s@AlexState{alex_ust = usSt} = Right (s, lexerStringState usSt)
checkOpenString :: Alex ()
checkOpenString = do
stringOpen <- getStringState
if stringOpen
then do
ust@AlexUserState{lexerErrors = lexErr} <- alexGetUserState
alexSetUserState $ ust{lexerErrors = errorMsg : lexErr}
else
return ()
where errorMsg = "Error: EOF reached while scanning string"
-- Scanner
scanner :: String -> Either String [TokenPos]
scanner str =
let loop = do
tkPos@(tok,l,c) <- alexMonadScan
if tok == TkEOF
then do
checkOpenString
AlexUserState{lexerErrors = lexErr} <- alexGetUserState
if null lexErr
then return []
else alexError $ unlines $ reverse lexErr
else do toks <- loop
return (tkPos : toks)
in runAlex str loop
}