module DDC.Core.Parser.ImportSpec
( ImportSpec (..)
, pImportSpecs)
where
import DDC.Core.Module
import DDC.Core.Parser.Type
import DDC.Core.Parser.Context
import DDC.Core.Parser.Base
import DDC.Core.Parser.DataDef
import DDC.Core.Lexer.Tokens
import DDC.Base.Pretty
import Control.Monad
import qualified DDC.Base.Parser as P
data ImportSpec n
= ImportType n (ImportType n)
| ImportCap n (ImportCap n)
| ImportValue n (ImportValue n)
| ImportData (DataDef n)
deriving Show
pImportSpecs
:: (Ord n, Pretty n)
=> Context n -> Parser n [ImportSpec n]
pImportSpecs c
= do
pTok KImport
P.choice
[
do def <- pDataDef c
return [ ImportData def ]
, do P.choice [ pTok KValue, return () ]
pTok KBraceBra
specs <- P.sepEndBy1 (pImportValue c) (pTok KSemiColon)
pTok KBraceKet
return specs
, do pTok KForeign
src <- liftM (renderIndent . ppr) pName
P.choice
[
do pTok KType
pTok KBraceBra
sigs <- P.sepEndBy1 (pImportForeignType c src) (pTok KSemiColon)
pTok KBraceKet
return sigs
, do pTok KCapability
pTok KBraceBra
sigs <- P.sepEndBy1 (pImportForeignCap c src) (pTok KSemiColon)
pTok KBraceKet
return sigs
, do pTok KValue
pTok KBraceBra
sigs <- P.sepEndBy1 (pImportForeignValue c src) (pTok KSemiColon)
pTok KBraceKet
return sigs
]
]
P.<?> "something to import"
pImportForeignType
:: (Ord n, Pretty n)
=> Context n -> String -> Parser n (ImportSpec n)
pImportForeignType c src
| "abstract" <- src
= do n <- pName
pTokSP (KOp ":")
k <- pType c
return $ ImportType n (ImportTypeAbstract k)
| "boxed" <- src
= do n <- pName
pTokSP (KOp ":")
k <- pType c
return $ ImportType n (ImportTypeBoxed k)
| otherwise
= P.unexpected "import mode for foreign type."
pImportForeignCap
:: (Ord n, Pretty n)
=> Context n -> String -> Parser n (ImportSpec n)
pImportForeignCap c src
| "abstract" <- src
= do n <- pName
pTokSP (KOp ":")
t <- pType c
return $ ImportCap n (ImportCapAbstract t)
| otherwise
= P.unexpected "import mode for foreign capability."
pImportValue
:: (Ord n, Pretty n)
=> Context n -> Parser n (ImportSpec n)
pImportValue c
= do n <- pName
pTokSP (KOp ":")
t <- pType c
return (ImportValue n (ImportValueModule (ModuleName []) n t Nothing))
pImportForeignValue
:: (Ord n, Pretty n)
=> Context n -> String -> Parser n (ImportSpec n)
pImportForeignValue c src
| "c" <- src
= do n <- pName
pTokSP (KOp ":")
k <- pType c
let symbol = renderIndent (ppr n)
return $ ImportValue n (ImportValueSea symbol k)
| otherwise
= P.unexpected "import mode for foreign value."