Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Char
- data Bracket = Round | Curly | Square deriving Show
- data Token = StringLit String
- | Number String
- | Open Bracket
- | Close Bracket
- | DoubleQuote
- | Comma
- | Other String
- deriving Show
- lexer :: String -> [Token]
- lexer (c:cs)
- | isAlpha c = let (other, rest) = lexOther (c:cs) in Other other : lexer rest
- | c == '"' = let (sLit, rest) = lexStringLit cs in StringLit sLit : lexer rest
- | isDigit c = let (num, rest) = lexNum (c:cs) in Number num : lexer rest
- | c == '(' = Open Round : lexer cs
- | c == '{' = Open Curly : lexer cs
- | c == '[' = Open Square : lexer cs
- | c == ')' = Close Round : lexer cs
- | c == '}' = Close Curly : lexer cs
- | c == ']' = Close Square : lexer cs
- | c == ',' = Comma : lexer cs
- | otherwise = lexer cs
- lexStringLit :: String -> (String, String)
- lexStringLit [] = ("", "")
- lexStringLit ('"':rest) = ("", rest)
- lexStringLit (c:cs) = (c:cs', rest)
- where (cs', rest) = lexStringLit cs
- lexNum :: String -> (String, String)
- lexNum [] = ("", "")
- lexNum (c:cs)
- | isDigit c = let (cs', rest) = lexNum cs in (c:cs', rest)
- | otherwise = ("", cs)
- lexOther :: String -> (String, String)
- lexOther [] = ("", "")
- lexOther (c:cs)
- | isAlphaNum c = let (cs', rest) = lexOther cs in (c:cs', rest)
- | otherwise = ("", cs)
- main = print . lexer $ '"': repeat 'a'
Add Comment
Please, Sign In to add comment