-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.x
90 lines (73 loc) · 2.63 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
{
module Lexer (lexThis, WithPosition(..), getLineColPair) where
import Data.Char
import Token
}
%wrapper "posn"
$digit = 0-9 -- digits
$alpha = [a-zA-Z] -- alphabetic characters
$notv = [^$alpha $digit \_]
$aw = [$white \n]
tokens :-
$white*\n$white* { helper' NewLine }
$white+ ;
"--".*\n* ;
"/*"(.|\n)*"*/" ;
let { helper' Let }
mut { helper' Mut }
const { helper' Const }
if { helper' If }
$aw*else$aw* { helper' Else }
while { helper' While }
def { helper' Def }
$digit+ { helper (Integer . read) }
true { helper' TrueTok }
false { helper' FalseTok }
return { helper' Return }
\"[^\"]*\" { helper strHelper }
\( { helper' LParens }
\) { helper' RParens }
\[ { helper' LSqB }
\] { helper' RSqB }
\{$aw* { helper' LCurly }
\} { helper' RCurly }
\= { helper' Equals }
\, { helper' Comma }
\. { helper' Dot }
\: { helper' Colon }
\-\> { helper' RightArrow }
\- { helper' Minus }
[\+\*\/\<\>\%] { helper (Sym . fromCharToOp) }
\>\=|\<\=|\=\=|\!\= { helper (Sym . fromCharToOp) }
$alpha [$alpha $digit \_]* { helper Var }
{
data WithPosition = WithPosition {
pos :: AlexPosn,
token :: Token
} deriving Eq
instance Show WithPosition where
show (WithPosition (AlexPn _ line col) tok) = "(line: " ++ show line ++ ", col: " ++ show col ++ ") " ++ show tok
getLineColPair :: AlexPosn -> (Int, Int)
getLineColPair (AlexPn _ line col) = (line,col)
helper :: (String -> Token) -> AlexPosn -> String -> WithPosition
helper convert pos = WithPosition pos . convert
helper' :: Token -> AlexPosn -> String -> WithPosition
helper' tok pos _ = WithPosition pos tok
-- Each action has type :: String -> Token
strHelper :: String -> Token
strHelper ('\"':sTmp) =
let s = init sTmp
-- helper "" = ""
-- helper ('\\':'n':ls) = '\n':(helper ls)
-- helper ('\\':'t':ls) = '\t':(helper ls)
-- helper ('\\':'r':ls) = '\r':(helper ls)
-- helper ('\\':'\\':ls) = '\\':(helper ls)
-- helper ('\\':c:ls) = error $ "Found escape seq: \\" ++ [c] ++ " but can't understand that"
-- helper (s:ls) = s:(helper ls)
in Str s --(helper s)
-- main = do
-- s <- getContents
-- print (alexScanTokens s)
--
lexThis s = alexScanTokens s
}