-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7eedfa
commit 0860a9e
Showing
15 changed files
with
589 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,5 +67,9 @@ $( makeAccKeysTH | |
extImports | ||
moduleMapper | ||
tdComments | ||
modelType | ||
defaults | ||
defaultType | ||
viewConfigType | ||
|] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
{-# OPTIONS_GHC -Wno-unused-imports #-} | ||
{-# OPTIONS_GHC -Wno-unused-local-binds #-} | ||
{-# OPTIONS_GHC -Wno-unused-top-binds #-} | ||
|
||
module NammaDSL.DSL.Parser.Frontend.Purs where | ||
|
||
import Control.Lens.Combinators | ||
import Control.Lens.Operators | ||
import Control.Monad.Trans.RWS.Lazy | ||
import Data.Aeson | ||
import Data.Aeson.Lens (key, _Array, _Object, _String, _Value) | ||
import Data.Bifunctor | ||
import qualified Data.ByteString as BS | ||
import Data.Default | ||
import Data.List (find) | ||
import qualified Data.List.Extra as L | ||
import Data.Maybe | ||
import qualified Data.Text as T | ||
import qualified Data.Yaml as Yaml | ||
import NammaDSL.AccessorTH | ||
import NammaDSL.DSL.Parser.TechDesign (etImp, getRequiredDeclType) | ||
import NammaDSL.DSL.Syntax.Common | ||
import NammaDSL.DSL.Syntax.Frontend.Purs | ||
import NammaDSL.DSL.Syntax.TechDesign | ||
import NammaDSL.Utils (makeTypeQualified, mkList, toModelList) | ||
import Prelude | ||
|
||
frontendPursParser :: FrontendPursRead -> FilePath -> IO PursFrontend | ||
frontendPursParser frontendPursRead filepath = do | ||
contents <- BS.readFile filepath | ||
case Yaml.decodeEither' contents of | ||
Left _ -> error "Not a Valid Yaml" | ||
Right yml -> do | ||
evalParser parsePursFrontend (frontendPursRead {_fPursYamlObject = yml}) def | ||
|
||
parsePursFrontend :: FrontendPursM () | ||
parsePursFrontend = do | ||
parseModelList | ||
makeComponentsQualified | ||
|
||
parseModelList :: FrontendPursM () | ||
parseModelList = do | ||
yaml <- asks _fPursYamlObject | ||
let modelList = filter ((/= "imports") . fst) $ toModelList yaml | ||
mapM_ | ||
( \(modelName, model) -> do | ||
let modelType = fromMaybe (error $ "Model type required for purs model " <> modelName) (model ^? ix acc_modelType . _String) | ||
case modelType of | ||
"Component" -> parseComponent modelName model | ||
"Screen" -> parseScreen modelName model | ||
_ -> error $ "Invalid model type for purs model " <> modelName | ||
) | ||
modelList | ||
|
||
parseComponent :: String -> Object -> FrontendPursM () | ||
parseComponent _compName _obj = do | ||
let typeList = fromMaybe [] $ _obj ^? ix acc_types . _Value . to mkList | ||
mkTypeList = | ||
map | ||
( \(typeName, typeObj) -> | ||
let fields = mkList typeObj | ||
declType = maybe PTYPE (getRequiredDeclType . T.pack . snd) $ find (("declType" ==) . fst) fields | ||
actualFields = filter (("declType" /=) . fst) fields | ||
in PursType typeName declType (map (\(fieldName, fieldType) -> PursField fieldName fieldType) actualFields) | ||
) | ||
typeList | ||
defaultList = fromMaybe [] $ _obj ^? ix acc_defaults . _Value . to mkList | ||
mkDefaultList = | ||
map | ||
( \(defaultName, defaultObj) -> | ||
let fields = mkList defaultObj | ||
defaultType = maybe PTYPE (getRequiredDeclType . T.pack . snd) $ find (("defaultType" ==) . fst) fields | ||
actualFields = filter (("defaultType" /=) . fst) fields | ||
in PursDefaultObj defaultName defaultType (map (\(fieldName, fieldType) -> PursField fieldName fieldType) actualFields) | ||
) | ||
defaultList | ||
viewConfigType' = _obj ^? ix acc_viewConfigType . _String . to T.unpack | ||
pCom = Component _compName mkTypeList mkDefaultList [] viewConfigType' | ||
modify $ \s -> s {_components = _components s ++ [pCom]} | ||
|
||
makeComponentsQualified :: FrontendPursM () | ||
makeComponentsQualified = do | ||
defaultTypeImportMapper <- asks _fPursDefaultImportMapper | ||
yaml <- asks _fPursYamlObject | ||
pf <- get | ||
let dNames = (pf ^. components) & map _componentName | ||
qType = makeTypeQualified defaultTypeImportMapper Nothing Nothing (Just dNames) mempty yaml | ||
qualifiedComponents = | ||
map | ||
( \comp -> | ||
let types = comp ^. componentTypes | ||
qualifiedTypes = | ||
map | ||
( \tp -> | ||
let fields = tp ^. ptypeFields | ||
qualifiedFields = | ||
map | ||
( \field -> | ||
let fieldType = field ^. pfieldType | ||
in field & pfieldType .~ qType fieldType | ||
) | ||
fields | ||
in tp & ptypeFields .~ qualifiedFields | ||
) | ||
types | ||
in comp & componentTypes .~ qualifiedTypes | ||
) | ||
(pf ^. components) | ||
modify $ \s -> s {_components = figureComponentImports <$> qualifiedComponents} | ||
|
||
figureComponentImports :: Component -> Component | ||
figureComponentImports comp = do | ||
let potentialImportStrings = map _pfieldType $ (comp ^. componentTypes & concatMap _ptypeFields) <> (comp ^. componentDefauts & concatMap _pdefaultFieldsValue) | ||
figuredImports = etImp potentialImportStrings | ||
comp & componentImports .~ (L.nub figuredImports) | ||
|
||
parseScreen :: String -> Object -> FrontendPursM () | ||
parseScreen _screenName _obj = pure () -- TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module NammaDSL.DSL.Syntax.Frontend.Purs where | ||
|
||
import Control.Lens | ||
import Data.Aeson | ||
import Data.Default | ||
import NammaDSL.DSL.Syntax.Common | ||
import NammaDSL.DSL.Syntax.TechDesign | ||
import Prelude | ||
|
||
data Screen = Screen | ||
{ | ||
} -- TODO | ||
|
||
$(makeLenses ''Screen) | ||
|
||
data PursField = PursField | ||
{ _pfieldName :: String, | ||
_pfieldType :: String | ||
} | ||
|
||
$(makeLenses ''PursField) | ||
|
||
data PursDefaultObj = PursDefaultObj | ||
{ _pdefaultObjName :: String, | ||
_pdefaultObjType :: PRecordType, | ||
_pdefaultFieldsValue :: [PursField] | ||
} | ||
|
||
$(makeLenses ''PursDefaultObj) | ||
|
||
data PursType = PursType | ||
{ _ptypeName :: String, | ||
_ptypeOf :: PRecordType, | ||
_ptypeFields :: [PursField] | ||
} | ||
|
||
$(makeLenses ''PursType) | ||
|
||
data Component = Component | ||
{ _componentName :: String, | ||
_componentTypes :: [PursType], | ||
_componentDefauts :: [PursDefaultObj], | ||
_componentImports :: [String], | ||
_viewConfigType :: Maybe String | ||
} | ||
|
||
$(makeLenses ''Component) | ||
|
||
instance Default Component where | ||
def = Component mempty [] [] [] Nothing | ||
|
||
data PursFrontend = PursFrontend | ||
{ _extraFileChanges :: [Ann Change], | ||
_components :: [Component], | ||
_screens :: [Screen] | ||
} | ||
|
||
$(makeLenses ''PursFrontend) | ||
|
||
data FrontendPursRead = FrontendPursRead | ||
{ _fPursDefaultImportMapper :: [(String, String)], | ||
_fPursDefaultImports :: [String], | ||
_fPursYamlObject :: Object, | ||
_fPursComponentModulePrefix :: String, | ||
_fPursScreenModulePrefix :: String | ||
} | ||
|
||
$(makeLenses ''FrontendPursRead) | ||
|
||
instance Default FrontendPursRead where | ||
def = FrontendPursRead [] [] mempty mempty mempty | ||
|
||
type FrontendPursM = ParserM FrontendPursRead PursFrontend | ||
|
||
instance Default PursFrontend where | ||
def = PursFrontend [] [] [] |
Oops, something went wrong.