T12790.hs 1.17 KB
Newer Older
Ryan Scott's avatar
Ryan Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
module T12790 (list) where

import Data.Foldable (asum)
import Text.ParserCombinators.Parsec (Parser, sepBy, try)

data Expr
  = Var Fixity String
  | App Expr Expr

data Fixity = Pref | Inf

cons, nil :: Expr
cons = Var Inf  ":"
nil  = Var Pref "[]"

brackets :: Parser a -> Parser a
brackets = undefined

symbol :: String -> Parser String
symbol = undefined

list :: Parser Expr
list = asum (map (try . brackets) plist) where
  plist = [
    foldr (\e1 e2 -> cons `App` e1 `App` e2) nil `fmap`
      (myParser False `sepBy` symbol ","),
    do e <- myParser False
       _ <- symbol ".."
       return $ Var Pref "enumFrom" `App` e,
    do e  <- myParser False
       _  <- symbol ","
       e' <- myParser False
       _  <- symbol ".."
       return $ Var Pref "enumFromThen" `App` e `App` e',
    do e  <- myParser False
       _  <- symbol ".."
       e' <- myParser False
       return $ Var Pref "enumFromTo" `App` e `App` e',
    do e   <- myParser False
       _   <- symbol ","
       e'  <- myParser False
       _   <- symbol ".."
       e'' <- myParser False
       return $ Var Pref "enumFromThenTo" `App` e `App` e' `App` e''
    ]

myParser :: Bool -> Parser Expr
myParser = undefined