-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokens.hs
28 lines (26 loc) · 992 Bytes
/
Tokens.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
module Tokens where
import Data.List (unlines)
-- Data type to "wrap" all tokens in one type
data Token
= KW !String
| SYM !Char
| IC !Int
| SC !String
| ID !String
| IF !Int
| WHILE !Int
instance Show Token where
show (KW k) = "<keyword> " ++ k ++ " </keyword>"
show (SYM s) = case s of
'<' -> "<symbol> < </symbol>"
'>' -> "<symbol> > </symbol>"
'&' -> "<symbol> & </symbol>"
_ -> "<symbol> " ++ [s] ++ " </symbol>"
show (IC ic) = "<integerConstant> " ++ shows ic " </integerConstant>"
show (SC sc) = "<stringConstant> " ++ sc ++ " </stringConstant>"
show (ID i) = "<identifier> " ++ i ++ " </identifier>"
show (IF i) = "<keyword ID = \"" ++ shows i "\"> if </keyword>"
show (WHILE i) = "<keyword ID = \"" ++ shows i "\"> while </keyword>"
newtype Tokens = Tokens [Token]
instance Show Tokens where
show (Tokens ts) = "<tokens>\n" ++ (unlines $ map show ts) ++ "</tokens>\n"