diff --git a/src/Ubc/Parse/Syntax/File.hs b/src/Ubc/Parse/Syntax/File.hs index e83f507..3552203 100644 --- a/src/Ubc/Parse/Syntax/File.hs +++ b/src/Ubc/Parse/Syntax/File.hs @@ -13,11 +13,15 @@ import Data.Functor ((<&>)) import Text.Parsec (choice, ParsecT, many) -import Ubc.Parse.Syntax.Struct ( Struct ) +import Ubc.Parse.Syntax (Transformer) + +import {-# SOURCE #-} Ubc.Parse.Syntax.Import (Import) +import Ubc.Parse.Syntax.Struct (Struct) import Ubc.Parse.Syntax.Function (Function) import Ubc.Parse.Syntax.Statement (Statement) import Ubc.Parse.Syntax.Enumeration (Enumeration) +import {-# SOURCE #-} qualified Ubc.Parse.Syntax.Import as Import import qualified Ubc.Parse.Syntax.Struct as Struct import qualified Ubc.Parse.Syntax.Function as Function import qualified Ubc.Parse.Syntax.Statement as Statement @@ -30,7 +34,8 @@ data File = File deriving (Show) data FileBody = FileBody - { structs :: [Struct] + { imports :: [Import] + , structs :: [Struct] , functions :: [Function] , statements :: [Statement] , enumerations :: [Enumeration] @@ -39,12 +44,13 @@ data FileBody = FileBody deriving (Semigroup, Monoid) via Generically FileBody -- dont use `deriving ... via FileBody` because that leads to a loop, somehow -parse :: Monad m => String -> ParsecT String u m File +parse :: String -> ParsecT String u Transformer File parse source = File source <$!> mconcat <$!> many fileMember -fileMember :: Monad m => ParsecT String u m FileBody +fileMember :: ParsecT String u Transformer FileBody fileMember = choice [ Struct.parse <&> \s -> mempty { structs = [s] } + , Import.parse <&> \i -> mempty { imports = [i] } , Function.parse <&> \f -> mempty { functions = [f] } , Statement.parse <&> \s -> mempty { statements = [s] } , Enumeration.parse <&> \e -> mempty { enumerations = [e] } diff --git a/src/Ubc/Parse/Syntax/Import.hs b/src/Ubc/Parse/Syntax/Import.hs index b16ba6e..3f4ba86 100644 --- a/src/Ubc/Parse/Syntax/Import.hs +++ b/src/Ubc/Parse/Syntax/Import.hs @@ -1,4 +1,5 @@ {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE DerivingStrategies #-} module Ubc.Parse.Syntax.Import ( parse , Import(..) @@ -37,6 +38,7 @@ data Import = Import { file :: File.File , alias :: String } + deriving stock (Show) importPath :: Monad m => ParsecT String u m (NonEmpty String) importPath = UbcLanguage.angles (many importChar `sepBy1` char '/') @@ -84,7 +86,7 @@ notFoundMessage relFile searchedLocations = "Could not locate import file path" <> "Searched locations were:" <> (unlines . List.map (('\t':) . Path.fromAbsFile) $ searchedLocations) -parseFile :: MonadIO m => Path Abs File -> String -> ParsecT String u m Import +parseFile :: Path Abs File -> String -> ParsecT String u Transformer Import parseFile path importAs = do let stringPath = Path.fromAbsFile path contents <- liftIO . readFile $ stringPath