Guest User

Untitled

a guest
Jul 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import Data.Char
  2.  
  3. data Bracket = Round | Curly | Square deriving Show
  4.  
  5. data Token = StringLit String
  6. | Number String
  7. | Open Bracket
  8. | Close Bracket
  9. | DoubleQuote
  10. | Comma
  11. | Other String
  12. deriving Show
  13.  
  14. lexer :: String -> [Token]
  15. lexer (c:cs)
  16. | isAlpha c = let (other, rest) = lexOther (c:cs) in Other other : lexer rest
  17. | c == '"' = let (sLit, rest) = lexStringLit cs in StringLit sLit : lexer rest
  18. | isDigit c = let (num, rest) = lexNum (c:cs) in Number num : lexer rest
  19. | c == '(' = Open Round : lexer cs
  20. | c == '{' = Open Curly : lexer cs
  21. | c == '[' = Open Square : lexer cs
  22. | c == ')' = Close Round : lexer cs
  23. | c == '}' = Close Curly : lexer cs
  24. | c == ']' = Close Square : lexer cs
  25. | c == ',' = Comma : lexer cs
  26. | otherwise = lexer cs
  27.  
  28. lexStringLit :: String -> (String, String)
  29. lexStringLit [] = ("", "")
  30. lexStringLit ('"':rest) = ("", rest)
  31. lexStringLit (c:cs) = (c:cs', rest)
  32. where (cs', rest) = lexStringLit cs
  33.  
  34. lexNum :: String -> (String, String)
  35. lexNum [] = ("", "")
  36. lexNum (c:cs)
  37. | isDigit c = let (cs', rest) = lexNum cs in (c:cs', rest)
  38. | otherwise = ("", cs)
  39.  
  40. lexOther :: String -> (String, String)
  41. lexOther [] = ("", "")
  42. lexOther (c:cs)
  43. | isAlphaNum c = let (cs', rest) = lexOther cs in (c:cs', rest)
  44. | otherwise = ("", cs)
  45.  
  46. main = print . lexer $ '"': repeat 'a'
Add Comment
Please, Sign In to add comment