perga/lib/Parser.hs

118 lines
3.2 KiB
Haskell
Raw Normal View History

2024-10-05 13:31:09 -07:00
module Parser where
2024-10-05 16:04:13 -07:00
import Control.Monad
import Control.Monad.State.Strict
2024-11-11 16:38:46 -08:00
import Data.Bifunctor (first)
2024-10-05 16:04:13 -07:00
import Data.Functor.Identity
import Data.List (elemIndex)
2024-11-11 16:38:46 -08:00
import Data.List.NonEmpty (NonEmpty ((:|)))
2024-10-05 16:04:13 -07:00
import qualified Data.List.NonEmpty as NE
import Expr (Expr (..), incIndices, (.->))
2024-10-05 16:04:13 -07:00
import Text.Megaparsec hiding (State)
2024-10-05 13:31:09 -07:00
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
2024-10-05 16:04:13 -07:00
type InnerState = [String]
data CustomErrors = UnboundVariable String [String] deriving (Eq, Ord, Show)
2024-10-05 13:31:09 -07:00
2024-10-05 16:04:13 -07:00
instance ShowErrorComponent CustomErrors where
2024-11-11 16:38:46 -08:00
showErrorComponent (UnboundVariable var bound) =
"Unbound variable: " ++ var ++ ". Did you mean one of: " ++ unwords bound ++ "?"
2024-10-05 16:04:13 -07:00
type Parser = ParsecT CustomErrors String (State InnerState)
2024-10-05 13:31:09 -07:00
skipSpace :: Parser ()
skipSpace =
2024-11-11 16:38:46 -08:00
L.space
space1
(L.skipLineComment "--")
(L.skipBlockCommentNested "(*" "*)")
2024-10-05 13:31:09 -07:00
lexeme :: Parser a -> Parser a
lexeme = L.lexeme skipSpace
2024-10-05 16:04:13 -07:00
pIdentifier :: Parser String
pIdentifier = label "identifier" $ lexeme $ do
2024-11-11 16:38:46 -08:00
firstChar <- letterChar <|> char '_'
rest <- many $ alphaNumChar <|> char '_'
return $ firstChar : rest
2024-10-05 13:31:09 -07:00
2024-10-05 16:04:13 -07:00
pVar :: Parser Expr
pVar = label "variable" $ lexeme $ do
2024-11-11 16:38:46 -08:00
var <- pIdentifier
binders <- get
case elemIndex var binders of
Just i -> return $ Var (fromIntegral i) var
Nothing -> customFailure $ UnboundVariable var binders
2024-10-05 13:31:09 -07:00
2024-10-05 16:04:13 -07:00
defChoice :: NE.NonEmpty String -> Parser ()
defChoice options = lexeme $ label labelText $ void $ choice $ NE.map string options
2024-11-11 16:38:46 -08:00
where
labelText = NE.head options
pParamGroup :: Parser [(String, Expr)]
pParamGroup = lexeme $ label "parameter group" $ between (char '(') (char ')') $ do
idents <- some pIdentifier
_ <- defChoice $ ":" :| []
ty <- pExpr
modify (flip (foldl $ flip (:)) idents)
2024-11-11 20:08:21 -08:00
pure $ zip idents (iterate incIndices ty)
2024-11-11 16:38:46 -08:00
pParams :: Parser [(String, Expr)]
pParams = concat <$> some pParamGroup
2024-10-05 13:31:09 -07:00
pLAbs :: Parser Expr
pLAbs = lexeme $ label "λ-abstraction" $ do
2024-11-11 16:38:46 -08:00
_ <- defChoice $ "λ" :| ["lambda", "fun"]
params <- pParams
_ <- defChoice $ "." :| ["=>", ""]
body <- pExpr
modify (drop $ length params)
pure $ foldr (uncurry Abs) body params
2024-10-05 13:31:09 -07:00
pPAbs :: Parser Expr
pPAbs = lexeme $ label "Π-abstraction" $ do
2024-11-11 16:38:46 -08:00
_ <- defChoice $ "" :| ["Pi", "forall", ""]
params <- pParams
_ <- defChoice $ "." :| [","]
body <- pExpr
modify (drop $ length params)
pure $ foldr (uncurry Pi) body params
pArrow :: Parser Expr
pArrow = lexeme $ label "->" $ do
a <- pAppTerm
_ <- defChoice $ "->" :| [""]
b <- pExpr
pure $ a .-> b
2024-10-05 13:31:09 -07:00
pApp :: Parser Expr
pApp = foldl1 App <$> some pTerm
pStar :: Parser Expr
2024-10-05 16:04:13 -07:00
pStar = Star <$ defChoice ("*" :| [])
2024-10-05 13:31:09 -07:00
pSquare :: Parser Expr
2024-10-05 16:04:13 -07:00
pSquare = Square <$ defChoice ("" :| ["[]"])
2024-10-05 13:31:09 -07:00
pTerm :: Parser Expr
pTerm =
2024-11-11 16:38:46 -08:00
lexeme $
label "term" $
choice
[ between (char '(') (char ')') pExpr
, pVar
, pStar
, pSquare
]
pAppTerm :: Parser Expr
pAppTerm = lexeme $ pLAbs <|> pPAbs <|> pApp
2024-10-05 13:31:09 -07:00
pExpr :: Parser Expr
2024-11-11 16:38:46 -08:00
pExpr = lexeme $ try pArrow <|> pAppTerm
2024-10-05 13:31:09 -07:00
pAll :: String -> Either String Expr
2024-10-05 16:04:13 -07:00
pAll input = first errorBundlePretty $ fst $ runIdentity $ runStateT (runParserT pExpr "" input) []