|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +module Parser (module Parser, module Control.Applicative) where |
| 3 | + |
| 4 | +import Control.Monad |
| 5 | +import Control.Applicative |
| 6 | +import Data.Char |
| 7 | + |
| 8 | +-- | A parser for things is a function from strings to list of pairs of things and strings. |
| 9 | +-- The empty list of of results denotes failure of parsing. |
| 10 | +newtype Parser a = Parser (String -> [(a, String)]) |
| 11 | + |
| 12 | +parse (Parser p) = p |
| 13 | + |
| 14 | +item :: Parser Char |
| 15 | +item = Parser (\case |
| 16 | + "" -> [] |
| 17 | + (c:cs) -> [(c,cs)]) |
| 18 | + |
| 19 | +instance Functor Parser where |
| 20 | + fmap = liftM |
| 21 | + -- fmap g p = P (\inp -> case parse p inp of |
| 22 | + --[] -> [] |
| 23 | + --[(v,out)] -> [(g v, out)]) |
| 24 | + |
| 25 | +instance Applicative Parser where |
| 26 | + pure a = Parser (\cs -> [(a,cs)]) |
| 27 | + (<*>) = ap |
| 28 | + |
| 29 | +instance Monad Parser where |
| 30 | + p >>= f = Parser (\inp -> concat [parse (f a) out | (a,out) <- parse p inp]) |
| 31 | + |
| 32 | +-- ======================= Choice combinators ======================= |
| 33 | + |
| 34 | +instance Alternative Parser where |
| 35 | + empty = Parser (const []) |
| 36 | + p <|> q = Parser (\cs -> case parse p cs of |
| 37 | + [] -> parse q cs |
| 38 | + res -> res) |
| 39 | + |
| 40 | +-- | Keep only one first parsing successfully result, |
| 41 | +-- throw away other ways the parsing could succeed when making a choice |
| 42 | +(+++) :: Parser a -> Parser a -> Parser a |
| 43 | +p +++ q = Parser (\cs -> case parse (p <|> q) cs of |
| 44 | + [] -> [] |
| 45 | + (x:xs) -> [x]) |
| 46 | + |
| 47 | +sat :: (Char -> Bool) -> Parser Char |
| 48 | +sat p = do c <- item |
| 49 | + guard (p c) -- fail if predicate doesn't hold |
| 50 | + return c |
| 51 | + |
| 52 | +char :: Char -> Parser Char |
| 53 | +char c = sat (c ==) |
| 54 | + |
| 55 | +-- ======================= Recursion combinators ======================= |
| 56 | + |
| 57 | +-- | Parse a specific string |
| 58 | +string :: String -> Parser String |
| 59 | +string "" = return "" |
| 60 | +string (c:cs) = do { char c; string cs; return (c:cs); } |
| 61 | + |
| 62 | +-- | Parse repeated applications of a parser p; |
| 63 | +-- the many combinator permits zero or more applications of p |
| 64 | +-- `many` |
| 65 | + |
| 66 | +-- | Parse repeated applications of a parser p; |
| 67 | +-- the some combinator permits one or more applications of p |
| 68 | +-- `some` |
| 69 | + |
| 70 | +-- | Parse repeated applications of a parser p, separated by applications |
| 71 | +-- of a parser sep whose result values are thrown away. Permits zero applications |
| 72 | +sepBy :: Parser a -> Parser a -> Parser [a] |
| 73 | +p `sepBy` sep = (p `sepBy1` sep) +++ return [] |
| 74 | + |
| 75 | +-- | Parse repeated applications of a parser p, separated by applications |
| 76 | +-- of a parser sep whose result values are thrown away. Permits one or more applications |
| 77 | +sepBy1 :: Parser a -> Parser a -> Parser [a] |
| 78 | +p `sepBy1` sep = do a <- p |
| 79 | + as <- many (do {sep; p}) |
| 80 | + return (a:as) |
| 81 | + |
| 82 | +-- | Parse repeated applications of a parser p, separated by applications of a parser |
| 83 | +-- op whose result value is an operator that is assumed to associate to the left, |
| 84 | +-- and which is used to combine the results from the p parsers |
| 85 | +-- Can be applied zero or more times. |
| 86 | +chainl :: Parser a -> Parser (a->a->a) -> a -> Parser a |
| 87 | +chainl p op a = (p `chainl1` op) +++ return a |
| 88 | + |
| 89 | +-- | Parse repeated applications of a parser p, separated by applications of a parser |
| 90 | +-- op whose result value is an operator that is assumed to associate to the left, |
| 91 | +-- and which is used to combine the results from the p parsers. |
| 92 | +-- Can be applied one or more times. |
| 93 | +chainl1 :: Parser a -> Parser (a->a->a) -> Parser a |
| 94 | +p `chainl1` op = do { a <- p; rest a} |
| 95 | + where |
| 96 | + rest a = do f <- op |
| 97 | + b <- p |
| 98 | + rest (f a b) |
| 99 | + +++ return a |
| 100 | + |
| 101 | +-- chainr and chainr1 can be implemented simmiliarly |
| 102 | + |
| 103 | + |
| 104 | +-- ======================= Lexical combinators ======================= |
| 105 | + |
| 106 | +digit :: Parser Int |
| 107 | +digit = do x <- digitChar |
| 108 | + return (ord x - ord '0') |
| 109 | + |
| 110 | +letter :: Parser Char |
| 111 | +letter = sat isLetter |
| 112 | + |
| 113 | +digitChar :: Parser Char |
| 114 | +digitChar = sat isDigit |
| 115 | + |
| 116 | +lower :: Parser Char |
| 117 | +lower = sat isLower |
| 118 | + |
| 119 | +upper :: Parser Char |
| 120 | +upper = sat isUpper |
| 121 | + |
| 122 | +alphanum :: Parser Char |
| 123 | +alphanum = sat isAlphaNum |
| 124 | + |
| 125 | +nat :: Parser Int |
| 126 | +nat = do x <- some digitChar |
| 127 | + return (read x) |
| 128 | + |
| 129 | +-- | Parse a string of spaces, tabs, and newlines |
| 130 | +space :: Parser String |
| 131 | +space = many (sat (\c -> c == '\t' || isSeparator c)) |
| 132 | + |
| 133 | +-- | Parse an identifier, starting with a lowercase and zero or more alphanumerics. |
| 134 | +identifier :: Parser String |
| 135 | +identifier = do x <- lower |
| 136 | + xs <- many alphanum |
| 137 | + return (x:xs) |
| 138 | + |
| 139 | +-- | Parse a token using a parser p, throwing away any trailing space |
| 140 | +token :: Parser a -> Parser a |
| 141 | +token p = do a <- p |
| 142 | + space |
| 143 | + return a |
| 144 | + |
| 145 | +-- | Parse a symbolic token |
| 146 | +symbol :: String -> Parser String |
| 147 | +symbol cs = token (string cs) |
| 148 | + |
| 149 | +-- | Apply a parser p, throwing away any leading space |
| 150 | +apply :: Parser a -> String -> [(a,String)] |
| 151 | +apply p = parse $ do { space; p } |
| 152 | + |
| 153 | +-- ======================= Example ======================= |
| 154 | +{- |
| 155 | +expr :: Parser Int |
| 156 | +addop :: Parser (Int -> Int -> Int) |
| 157 | +mulop :: Parser (Int -> Int -> Int) |
| 158 | +
|
| 159 | +expr = term `chainl1` addop |
| 160 | +term = factor `chainl1` mulop |
| 161 | +factor = digit +++ do { symbol "("; n <- expr; symbol ")"; return n } |
| 162 | +
|
| 163 | +addop = do { symbol "+"; return (+) } +++ do { symbol "-"; return (-) } |
| 164 | +mulop = do { symbol "*"; return (*) } +++ do { symbol "/"; return div } |
| 165 | +
|
| 166 | +res :: Int |
| 167 | +res = fst . head $ apply expr " (1 - 2*3)+4 " -- -1 |
| 168 | +
|
| 169 | +-} |
0 commit comments