perga/lib/Parser.hs

117 lines
3.4 KiB
Haskell
Raw Normal View History

2024-11-14 19:56:33 -08:00
module Parser (pAll) where
2024-10-05 13:31:09 -07:00
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
2024-11-14 22:02:04 -08:00
import Data.Text (Text)
import qualified Data.Text as T
2024-11-14 19:56:33 -08:00
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
2024-11-14 22:02:04 -08:00
type InnerState = [Text]
2024-10-05 16:04:13 -07:00
2024-11-14 22:02:04 -08:00
data CustomErrors = UnboundVariable Text [Text] 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) =
2024-11-14 22:02:04 -08:00
"Unbound variable: " ++ T.unpack var ++ ". Did you mean one of: " ++ T.unpack (T.unwords bound) ++ "?"
2024-10-05 16:04:13 -07:00
2024-11-14 22:02:04 -08:00
type Parser = ParsecT CustomErrors Text (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-11-14 22:02:04 -08:00
pIdentifier :: Parser Text
2024-10-05 16:04:13 -07:00
pIdentifier = label "identifier" $ lexeme $ do
2024-11-11 16:38:46 -08:00
firstChar <- letterChar <|> char '_'
rest <- many $ alphaNumChar <|> char '_'
2024-11-14 22:02:04 -08:00
return $ T.pack (firstChar : rest) -- Still need T.pack here as we're building from chars
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-11-14 22:02:04 -08:00
defChoice :: NE.NonEmpty Text -> Parser ()
defChoice options = lexeme $ label (T.unpack $ NE.head options) $ void $ choice $ NE.map chunk options
2024-11-11 16:38:46 -08:00
2024-11-14 22:02:04 -08:00
pParamGroup :: Parser [(Text, Expr)]
2024-11-11 16:38:46 -08:00
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
2024-11-14 22:02:04 -08:00
pParams :: Parser [(Text, Expr)]
2024-11-11 16:38:46 -08:00
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 $ "->" :| [""]
2024-11-14 19:56:33 -08:00
Pi "" a . incIndices <$> pExpr
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
2024-11-14 22:02:04 -08:00
pAll :: Text -> Either Text Expr
pAll input = first (T.pack . errorBundlePretty) $ fst $ runIdentity $ runStateT (runParserT pExpr "" input) []