forked from cse130-wi19/04-nano
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLexer.x
93 lines (76 loc) · 2.19 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
{
{-# LANGUAGE FlexibleContexts #-}
module Language.Nano.Lexer (
Token(..),
scanTokens
) where
import Control.Monad.Except
}
%wrapper "posn"
$digit = 0-9
$alpha = [a-zA-Z]
$eol = [\n]
tokens :-
-- Whitespace insensitive
$eol ;
$white+ ;
-- Comments
"#".* ;
------------------------------------------------------------------------------
-- Syntax [ THIS IS THE ONLY SEGMENT YOU NEED TO CHANGE ]
in { \p _ -> IN p }
"&&" { \p _ -> AND p }
\( { \p _ -> LPAREN p }
\) { \p _ -> RPAREN p }
\: { \p _ -> COLON p }
\, { \p _ -> COMMA p }
-- DO NOT CHANGE ANYTHING AFTER THIS LINE ------------------------------------
------------------------------------------------------------------------------
{
data Token
= LET AlexPosn
| TRUE AlexPosn
| FALSE AlexPosn
| IN AlexPosn
| IF AlexPosn
| THEN AlexPosn
| ELSE AlexPosn
| AND AlexPosn
| OR AlexPosn
| LESS AlexPosn
| LEQ AlexPosn
| NEQ AlexPosn
| LAM AlexPosn
| NUM AlexPosn Int
| ID AlexPosn String
| ARROW AlexPosn
| EQB AlexPosn
| EQL AlexPosn
| PLUS AlexPosn
| MINUS AlexPosn
| MUL AlexPosn
| LPAREN AlexPosn
| RPAREN AlexPosn
| LBRAC AlexPosn
| RBRAC AlexPosn
| COLON AlexPosn
| COMMA AlexPosn
| EOF AlexPosn
deriving (Eq,Show)
getLineNum :: AlexPosn -> Int
getLineNum (AlexPn _ lineNum _) = lineNum
getColumnNum :: AlexPosn -> Int
getColumnNum (AlexPn _ _ colNum) = colNum
scanTokens :: String -> Except String [Token]
scanTokens str = go (alexStartPos,'\n',[],str)
where
go inp@(pos,_,_,str) =
case alexScan inp 0 of
AlexEOF -> return []
AlexError ((AlexPn _ line column),_,_,_) -> throwError $ "lexical error at " ++ (show line) ++ " line, " ++ (show column) ++ " column"
AlexSkip inp' _ -> go inp'
AlexToken inp' len act -> do
res <- go inp'
let rest = act pos (take len str)
return (rest : res)
}