aboutsummaryrefslogtreecommitdiff
path: root/src/Language/SimpleShell/Parser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Language/SimpleShell/Parser.hs')
-rw-r--r--src/Language/SimpleShell/Parser.hs66
1 files changed, 8 insertions, 58 deletions
diff --git a/src/Language/SimpleShell/Parser.hs b/src/Language/SimpleShell/Parser.hs
index be16c9f..32015f7 100644
--- a/src/Language/SimpleShell/Parser.hs
+++ b/src/Language/SimpleShell/Parser.hs
@@ -1,32 +1,19 @@
{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE FlexibleContexts #-}
module Language.SimpleShell.Parser
- ( Parser
+ ( MainParser
, lexeme
, symbol
, commentFirstChars
- , lookupVar
- , lookupFun
- , declareVars
- , initContext
- , parseTest
)
where
-import Language.SimpleShell.AST.Name (FunName, VarName)
-import Language.SimpleShell.AST.SimpleType (SimpleType, FunSig, Typed)
-
import Control.Monad (void)
-import Control.Monad.Reader (ask, local, ReaderT, runReaderT)
-import Data.List (nub)
-import Data.Map (Map)
-import qualified Data.Map as Map (empty, lookup, fromList)
-import Data.Text (Text, unpack)
-import Data.Tuple (swap)
+import Data.Text (Text)
import Data.Void (Void)
-import Text.Megaparsec (Parsec)
-import qualified Text.Megaparsec as MP (parseTest)
+import Text.Megaparsec (MonadParsec, Parsec)
import Text.Megaparsec.Char (space1)
import qualified Text.Megaparsec.Char.Lexer as L
( lexeme
@@ -37,22 +24,11 @@ import qualified Text.Megaparsec.Char.Lexer as L
)
-type Parser = ReaderT Context (Parsec Void Text)
-
-data Context = Context
- { ctxVars :: Map VarName SimpleType
- , ctxFuns :: Map FunName FunSig
- }
-
-initContext :: Context
-initContext = Context
- { ctxVars = Map.empty
- , ctxFuns = Map.empty
- }
+type MainParser = Parsec Void Text
-- Must be kept in sync with 'commentFirstChars'.
-sc :: Parser ()
+sc :: (MonadParsec e Text m) => m ()
sc = L.space
space1
(L.skipLineComment "//")
@@ -62,34 +38,8 @@ sc = L.space
commentFirstChars :: [Char]
commentFirstChars = "/"
-lexeme :: Parser a -> Parser a
+lexeme :: (MonadParsec e Text m) => m a -> m a
lexeme = L.lexeme sc
-symbol :: Text -> Parser ()
+symbol :: (MonadParsec e Text m) => Text -> m ()
symbol = void . L.symbol sc
-
-
-lookupVar :: VarName -> Parser SimpleType
-lookupVar varname = do
- mt <- Map.lookup varname . ctxVars <$> ask
- case mt of
- Just t -> return t
- Nothing -> fail $ "Undeclared variable $" ++ unpack varname
-
-lookupFun :: FunName -> Parser FunSig
-lookupFun fname = do
- msig <- Map.lookup fname . ctxFuns <$> ask
- case msig of
- Just sig -> return sig
- Nothing -> fail $ "Undefined function " ++ unpack fname
-
-declareVars :: [Typed VarName] -> Parser a -> Parser a
-declareVars decls p = do
- let names = map snd decls
- if length (nub names) == length names -- TODO: inefficient
- then local (\ctx -> ctx { ctxVars = Map.fromList $ map swap decls }) p
- else fail "Duplicate variable name."
-
-
-parseTest :: Show a => Parser a -> Text -> IO ()
-parseTest p = MP.parseTest (runReaderT p initContext)