From 39dbada85b32a8775e3cba7a3676f99cfc482829 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 3 Mar 2020 21:40:43 +0100 Subject: Basic parser combinator --- parser.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 parser.hs diff --git a/parser.hs b/parser.hs new file mode 100644 index 0000000..61565c3 --- /dev/null +++ b/parser.hs @@ -0,0 +1,48 @@ +module Parser where + +import Control.Applicative + + +newtype Parser a = Parser { runParser :: String -> Maybe (a, String) } + +instance Functor Parser where + fmap f (Parser p) = Parser new_p + where new_p s = do + (x, s') <- p s + return (f x, s') + +instance Applicative Parser where + pure x = Parser (\s -> Just (x, s)) + (Parser p1) <*> (Parser p2) = Parser new_p + where new_p s = do + (f, s') <- p1 s + (x, s'') <- p2 s' + return (f x, s'') + -- (Parser p1) *> (Parser p2) = Parser new_p + -- (Parser p1) <* (Parser p2) = Parser new_p + +instance Alternative Parser where + empty = Parser (\_ -> Nothing) + (Parser p1) <|> (Parser p2) = Parser new_p + where new_p s = p1 s <|> p2 s + + +data Equation + = EquationAdd + | EquationSub + | EquationMul + -- | EquationDiv ? + | EquationExp + | EquationEqu + | EquationNum Float + +charP :: Char -> Parser Char +charP c = Parser f + where f "" = Nothing + f (c:cs) = Just (c, cs) + +-- equationAddP :: Parser Equation +-- equationAddP = charP '+' *> EquationAdd + +-- equationP :: Parser Equation +-- equationP = -- cgit