Happy parse lex #1

Merged
msydneyslaga merged 7 commits from happy-parse-lex into main 2023-11-20 14:09:33 -07:00
5 changed files with 59 additions and 8 deletions
Showing only changes of commit c39a843660 - Show all commits

View File

@@ -26,12 +26,14 @@ library
, Core.Syntax , Core.Syntax
, Core.Parse , Core.Parse
, Core.Lex , Core.Lex
, Core.TH
-- other-extensions: -- other-extensions:
build-depends: base ^>=4.18.0.0 build-depends: base ^>=4.18.0.0
, containers , containers
, microlens , microlens
, microlens-th , microlens-th
, template-haskell
hs-source-dirs: src hs-source-dirs: src
default-language: GHC2021 default-language: GHC2021

View File

@@ -62,6 +62,11 @@ instance (MonadFail m) => MonadFail (ParserT i m) where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
eof :: (MonadPlus m) => ParserT [a] m ()
eof = ParserT $ \case
[] -> pure ([], ())
_ -> empty
-- TODO: generalise to non-lists -- TODO: generalise to non-lists
satisfy :: (MonadPlus m) => (a -> Bool) -> ParserT [a] m a satisfy :: (MonadPlus m) => (a -> Bool) -> ParserT [a] m a
satisfy p = ParserT $ \case satisfy p = ParserT $ \case

View File

@@ -1,6 +1,7 @@
{-# LANGUAGE LambdaCase, BlockArguments #-} {-# LANGUAGE LambdaCase, BlockArguments #-}
module Core.Parse module Core.Parse
( parseCore ( parseCore
, parseCoreExpr
) )
where where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -15,6 +16,9 @@ type CoreParser = ParserT [CoreToken] Result
parseCore :: [CoreToken] -> Result Program parseCore :: [CoreToken] -> Result Program
parseCore = fmap snd . runParserT program parseCore = fmap snd . runParserT program
parseCoreExpr :: [CoreToken] -> Result Expr
parseCoreExpr = fmap snd . runParserT expr
program :: CoreParser Program program :: CoreParser Program
program = Program <$> termMany (char TokSemicolon) scdef program = Program <$> termMany (char TokSemicolon) scdef

View File

@@ -21,6 +21,7 @@ import Data.Pretty
import Data.List (intersperse) import Data.List (intersperse)
import Data.Function ((&)) import Data.Function ((&))
import Data.String import Data.String
import Language.Haskell.TH.Syntax (Lift)
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
data Expr = Var Name data Expr = Var Name
@@ -30,14 +31,14 @@ data Expr = Var Name
| Lam [Name] Expr | Lam [Name] Expr
| App Expr Expr | App Expr Expr
| IntE Int | IntE Int
deriving Show deriving (Show, Lift)
infixl 2 :$ infixl 2 :$
pattern (:$) :: Expr -> Expr -> Expr pattern (:$) :: Expr -> Expr -> Expr
pattern f :$ x = App f x pattern f :$ x = App f x
data Binding = Binding Name Expr data Binding = Binding Name Expr
deriving Show deriving (Show, Lift)
infixl 1 := infixl 1 :=
pattern (:=) :: Name -> Expr -> Binding pattern (:=) :: Name -> Expr -> Binding
@@ -45,18 +46,18 @@ pattern k := v = Binding k v
data Rec = Rec data Rec = Rec
| NonRec | NonRec
deriving (Show, Eq) deriving (Show, Eq, Lift)
data Alter = Alter Int [Name] Expr data Alter = Alter Int [Name] Expr
deriving Show deriving (Show, Lift)
type Name = String type Name = String
data ScDef = ScDef Name [Name] Expr data ScDef = ScDef Name [Name] Expr
deriving (Show) deriving (Show, Lift)
newtype Program = Program [ScDef] newtype Program = Program [ScDef]
deriving (Show) deriving (Show, Lift)
instance IsString Expr where instance IsString Expr where
fromString = Var fromString = Var

39
src/Core/TH.hs Normal file
View File

@@ -0,0 +1,39 @@
module Core.TH
( coreExpr
, core
)
where
----------------------------------------------------------------------------------
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Quote
import Core.Parse
import Core.Lex
----------------------------------------------------------------------------------
core :: QuasiQuoter
core = QuasiQuoter
{ quoteExp = qCore
, quotePat = error "core quasiquotes may only be used in expressions"
, quoteType = error "core quasiquotes may only be used in expressions"
, quoteDec = error "core quasiquotes may only be used in expressions"
}
coreExpr :: QuasiQuoter
coreExpr = QuasiQuoter
{ quoteExp = qCoreExpr
, quotePat = error "core quasiquotes may only be used in expressions"
, quoteType = error "core quasiquotes may only be used in expressions"
, quoteDec = error "core quasiquotes may only be used in expressions"
}
qCore :: String -> Q Exp
qCore s = case lexCore s >>= parseCore of
Success a -> lift a
Error e _ _ -> error e
qCoreExpr :: String -> Q Exp
qCoreExpr s = case lexCore s >>= parseCoreExpr of
Success a -> lift a
Error e _ _ -> error e