-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.x
102 lines (95 loc) · 4.42 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
{
module Lexer where
import Debug.Trace
}
%wrapper "basic"
$digit = 0-9
$alpha = [a-zA-Z]
tokens :-
:quit { const QuitTok}
= { const (OpTok EqOp) }
\+ { const (OpTok AddOp) }
\- { const (OpTok SubOp) }
\* { const (OpTok MultOp) }
\^ { const (OpTok ExpOp)}
\/ { const (OpTok DivOp)}
\% { const (OpTok ModOp) }
\\ { const SndTok}
\/\\ { const (OpTok AndOp)}
\\\/ { const (OpTok OrOp)}
">" { const (OpTok GOp)}
"<" { const (OpTok LOp)}
leq { const (OpTok LeqOp)}
geq { const (OpTok GeqOp)}
ifz { const IfzTok}
then { const ThenTok }
else { const ElseTok }
\( { const LeftPTok }
\) { const RightPTok }
"[" { const LeftBTok}
"]" { const RightBTok}
sqrt { const SqrtTok }
"\\n" { const EOLTok }
supposing { const SupposingTok}
hence { const HenceTok}
otherwise { const OtherwiseTok}
hearye { const HearyeTok}
oi { const OiTok}
is { const IsTok}
for { const ForTok}
innit { const InnitTok}
colonize { const ColonizeTok}
cheers { const CheersTok }
mate { const MateTok}
bloke { const BlokeTok}
"," { const ComTok}
";" { const SemiTok}
and { const AndTok }
"#" { const UnitTok }
=> { const RocketTok}
pie { const (ConstTok Pi)}
fee { const (ConstTok Fee)}
phi { const (ConstTok Phi)}
mole { const (ConstTok Mole)}
\-\-[.]* ;
$white ;
``[^``]*`` { StringTok}
[A-Z_]+ { VarTok }
$digit+ { IntTok . read }
$digit+ \. $digit+ { Realtok . read }
ace { const (BoolTok True)}
rank { const (BoolTok False)}
display { const DisplayTok}
== { const MutateTok }
whilst { const WhileTok }
doeth { const DoTok }
"|" { const DeRefTok }
"~" { const SeqTok }
noble { const NobleTok }
serfs { const SerfTok }
obeys { const ObeyTok }
oneself { const (VarTok "oneself")}
decree { const DecreeTok }
a { const ATok }
"." { const DotTok }
"{" { const LCurlyTok }
"}" { const RCurlyTok }
[A-Z] [a-zA-Z_]* { VarTok }
[a-z]([a-z_])* { FNameTok }
{
data Token = OpTok Op| ConstTok Const | VarTok String | IntTok Integer | Realtok Double | BoolTok Bool | StringTok String| IfzTok
| ThenTok | ElseTok | EOLTok| LeftPTok | RightPTok | LeftBTok | RightBTok | SqrtTok | SupposingTok | HenceTok | OtherwiseTok
| HearyeTok | OiTok | IsTok | ForTok | InnitTok | ColonizeTok | CheersTok | MateTok |BlokeTok | ComTok | SemiTok | AndTok
| UnitTok |SndTok | RocketTok | DisplayTok | MutateTok | WhileTok | DoTok | DeRefTok | SeqTok | NobleTok | SerfTok | ObeyTok
| SelfTok | DecreeTok | ATok | DotTok | LCurlyTok | RCurlyTok | QuitTok | FNameTok String deriving (Show, Eq)
data Op = EqOp | AddOp | SubOp | MultOp | DivOp | ExpOp | ModOp| GOp |LOp| GeqOp | LeqOp |AndOp | OrOp deriving (Show, Eq)
data Const = Pi | Fee | Phi | Mole deriving (Show, Eq)
scanTokens :: String -> Maybe [Token]
scanTokens str = go ('\n',[],str)
where go inp@(_,_bs,str) =
case alexScan inp 0 of
AlexEOF -> Just []
AlexError err -> Nothing
AlexSkip inp' len -> go inp'
AlexToken inp' len act -> fmap ((act (take len str)):) (go inp')
}