summaryrefslogtreecommitdiff
path: root/compiler/Language/Haskell/Syntax
diff options
context:
space:
mode:
authorromes <rodrigo.m.mesquita@gmail.com>2022-06-29 15:36:43 +0200
committerromes <rodrigo.m.mesquita@gmail.com>2022-07-03 15:06:26 +0200
commitbae4d04456dc13f42eee2feae9d541836728ebbd (patch)
tree4a158e53deab52d58334230802990b30246e12c2 /compiler/Language/Haskell/Syntax
parentc33787c7cada83c1284f1b3a4194ab8d03479f00 (diff)
downloadhaskell-wip/romes/ttg-module.tar.gz
Refactor ModuleName to L.H.S.Module.Namewip/romes/ttg-module
ModuleName used to live in GHC.Unit.Module.Name. In this commit, the definition of ModuleName and its associated functions are moved to Language.Haskell.Syntax.Module.Name according to the current plan towards making the AST GHC-independent. The instances for ModuleName for Outputable, Uniquable and Binary were moved to the module in which the class is defined because these instances depend on GHC. The instance of Eq for ModuleName is slightly changed to no longer depend on unique explicitly and instead uses FastString's instance of Eq.
Diffstat (limited to 'compiler/Language/Haskell/Syntax')
-rw-r--r--compiler/Language/Haskell/Syntax/ImpExp.hs5
-rw-r--r--compiler/Language/Haskell/Syntax/ImpExp.hs-boot6
-rw-r--r--compiler/Language/Haskell/Syntax/Module/Name.hs60
3 files changed, 61 insertions, 10 deletions
diff --git a/compiler/Language/Haskell/Syntax/ImpExp.hs b/compiler/Language/Haskell/Syntax/ImpExp.hs
index 7e529701c4..b31c417936 100644
--- a/compiler/Language/Haskell/Syntax/ImpExp.hs
+++ b/compiler/Language/Haskell/Syntax/ImpExp.hs
@@ -3,6 +3,7 @@
module Language.Haskell.Syntax.ImpExp where
import Language.Haskell.Syntax.Extension
+import Language.Haskell.Syntax.Module.Name
import Data.Eq (Eq)
import Data.Ord (Ord)
@@ -14,7 +15,6 @@ import Data.String (String)
import Data.Int (Int)
import GHC.Hs.Doc -- ROMES:TODO Discuss in #21592 whether this is parsed AST or base AST
-import GHC.Data.FastString
{-
************************************************************************
@@ -26,9 +26,6 @@ Import and export declaration lists
One per import declaration in a module.
-}
--- | A ModuleName is essentially a simple string, e.g. @Data.List@.
-newtype ModuleName = ModuleName FastString deriving Show
-
-- | Located Import Declaration
type LImportDecl pass = XRec pass (ImportDecl pass)
-- ^ When in a list this may have
diff --git a/compiler/Language/Haskell/Syntax/ImpExp.hs-boot b/compiler/Language/Haskell/Syntax/ImpExp.hs-boot
index 9cc78600b8..68bd7fdc53 100644
--- a/compiler/Language/Haskell/Syntax/ImpExp.hs-boot
+++ b/compiler/Language/Haskell/Syntax/ImpExp.hs-boot
@@ -1,7 +1,5 @@
module Language.Haskell.Syntax.ImpExp where
-import GHC.Data.FastString
-
import Data.Eq
import Data.Ord
import Text.Show
@@ -16,7 +14,3 @@ instance Eq IsBootInterface
instance Ord IsBootInterface
instance Show IsBootInterface
instance Data IsBootInterface
-
-newtype ModuleName = ModuleName FastString
-
-instance Show ModuleName
diff --git a/compiler/Language/Haskell/Syntax/Module/Name.hs b/compiler/Language/Haskell/Syntax/Module/Name.hs
new file mode 100644
index 0000000000..65e64d8700
--- /dev/null
+++ b/compiler/Language/Haskell/Syntax/Module/Name.hs
@@ -0,0 +1,60 @@
+module Language.Haskell.Syntax.Module.Name where
+
+import Prelude
+
+import Data.Data
+import Data.Char (isAlphaNum)
+import Control.DeepSeq
+import qualified Text.ParserCombinators.ReadP as Parse
+import System.FilePath
+
+import GHC.Utils.Misc (abstractConstr)
+import GHC.Data.FastString
+
+-- | A ModuleName is essentially a simple string, e.g. @Data.List@.
+newtype ModuleName = ModuleName FastString deriving (Show, Eq)
+
+instance Ord ModuleName where
+ nm1 `compare` nm2 = stableModuleNameCmp nm1 nm2
+
+instance Data ModuleName where
+ -- don't traverse?
+ toConstr _ = abstractConstr "ModuleName"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNoRepType "ModuleName"
+
+instance NFData ModuleName where
+ rnf x = x `seq` ()
+
+stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
+-- ^ Compares module names lexically, rather than by their 'Unique's
+stableModuleNameCmp n1 n2 = moduleNameFS n1 `lexicalCompareFS` moduleNameFS n2
+
+moduleNameFS :: ModuleName -> FastString
+moduleNameFS (ModuleName mod) = mod
+
+moduleNameString :: ModuleName -> String
+moduleNameString (ModuleName mod) = unpackFS mod
+
+mkModuleName :: String -> ModuleName
+mkModuleName s = ModuleName (mkFastString s)
+
+mkModuleNameFS :: FastString -> ModuleName
+mkModuleNameFS s = ModuleName s
+
+-- |Returns the string version of the module name, with dots replaced by slashes.
+--
+moduleNameSlashes :: ModuleName -> String
+moduleNameSlashes = dots_to_slashes . moduleNameString
+ where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
+
+-- |Returns the string version of the module name, with dots replaced by colons.
+--
+moduleNameColons :: ModuleName -> String
+moduleNameColons = dots_to_colons . moduleNameString
+ where dots_to_colons = map (\c -> if c == '.' then ':' else c)
+
+parseModuleName :: Parse.ReadP ModuleName
+parseModuleName = fmap mkModuleName
+ $ Parse.munch1 (\c -> isAlphaNum c || c `elem` "_.")
+