Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Combinators where
  2.  
  3. import Tokenizer
  4. -- Make sure that the names don't clash
  5. import Prelude hiding (lookup, (>>=), map, pred, return, elem)
  6.  
  7. -- Input abstraction
  8. type Input = String
  9.  
  10. -- Result is polymorphic in the ... result
  11. data Result r = Success r
  12.               | Error String
  13.               deriving (Show)
  14.  
  15. -- The result of parsing is some payload r and the suffix which wasn't parsed
  16. type Parser r = Input -> Result (r, Input)
  17.  
  18. -- Choice combinator: checks if the input can be parsed with either the first, or the second parser
  19. -- Left biased: make sure, that the first parser consumes more input
  20. infixl 6 <|>
  21. (<|>) :: Parser a -> Parser a -> Parser a
  22. p <|> q = \inp ->
  23.   case p $ skipSpaces inp of
  24.     Error _ -> q $ skipSpaces inp
  25.     result  -> result
  26.  
  27. -- Sequential combinator: if the first parser successfully parses some prefix, the second is run on the suffix
  28. -- The second parser is supposed to use the result of the first parser
  29. infixl 7 >>=
  30. (>>=) :: Parser a -> (a -> Parser b ) -> Parser b
  31. p >>= q = \inp ->
  32.   case p $ skipSpaces inp of
  33.     Success (r, inp') -> q r $ skipSpaces inp'
  34.     Error err -> Error err
  35.  
  36. -- Sequential combinator which ignores the result of the first parser
  37. infixl 7 |>
  38. (|>) :: Parser a -> Parser b -> Parser b
  39. p |> q = p >>= const q
  40.  
  41. -- Succeedes without consuming any input, returning a value
  42. return :: a -> Parser a
  43. return r inp = Success (r, inp)
  44.  
  45. -- Always fails
  46. zero :: String -> Parser a
  47. zero err = const $ Error err
  48.  
  49. -- Chops of the first element of the string
  50. elem :: Parser Char
  51. elem (c : cs) = Success (c, cs)
  52. elem [] = Error "Empty string"
  53.  
  54. get :: (Char -> Bool) -> Parser String
  55. get pred s = let (a, b) = span pred s in
  56.                  case a of
  57.                    [] -> Error "No number found"
  58.                    a -> Success(a, b)
  59. get pred [] = Error "Empty string"
  60.  
  61. -- Checks if the first character of the string is the given one
  62. char :: Char -> Parser Char
  63. char c = sat (== c) elem
  64.        
  65. -- Checks if the parser result satisfies the predicate
  66. sat :: (a -> Bool) -> Parser a -> Parser a
  67. sat pred parser inp =
  68.   case parser inp of
  69.     Success (r, inp') | pred r ->  Success (r, inp')
  70.     Success _ -> Error "Predicate is not satisfied"
  71.     Error err -> Error err
  72.  
  73. -- Applies the function to the result of the parser
  74. map :: (a -> b) -> Parser a -> Parser b
  75. map f parser inp =
  76.   case parser inp of
  77.     Success (r, inp') -> Success (f r, inp')
  78.     Error err -> Error err
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement