[C2hs] hierarchical modules

Duncan Coutts duncan.coutts at worcester.oxford.ac.uk
Fri Jul 16 14:57:34 EDT 2004


All,

So far as I know c2hs doesn't support hierarchical modules well. I've
not tried the latest version, so apologies if this has already been
addressed.

Under the Hierarchical Module Namespace Extension
http://www.haskell.org/hierarchical-modules/
module names can have dots '.' in them and when mapping these to
filenames they are converted to directory separators.
Foo.Bar.Baz -> Foo/Bar/Bas(.hs|.chi|etc)

I've knocked up a patch that I think solves the problem (works for me),
but an expert eye would be appreciated.

I've made a change in two places:
      * CHSLexer.hs: identOrKW function, added '.' as an allowable
        character in an identifier within binding hooks.
      * CHS.hs: parseImport function, inserted a call to a new function
        moduleNameToFileName before calling loadCHI. The new function
        does the module name to file path conversion.

Here's a patch (it's against the gtk2hs fork of c2hs but it's quite
small so the idea should be clear)

diff -U2 -r1.2 CHSLexer.hs
--- CHSLexer.hs 1 Oct 2002 15:17:07 -0000       1.2
+++ CHSLexer.hs 16 Jul 2004 18:52:26 -0000
@@ -557,5 +557,5 @@
 --
 identOrKW  =
-  letter +> (letter >|< digit >|< char '\'')`star` epsilon
+  letter +> (letter >|< digit >|< char '\'' >|< char '.')`star` epsilon
   `lexactionName` \cs pos name -> (idkwtok $!pos) cs name
   where


diff -U2 -r1.4 CHS.hs
--- CHS.hs      20 May 2004 16:42:17 -0000      1.4
+++ CHS.hs      16 Jul 2004 18:52:26 -0000
@@ -747,9 +747,14 @@
       CHSTokQualif _: CHSTokIdent _ ide:toks -> return (True , ide, toks)
       _                                             -> syntaxError toks
-  chi <- loadCHI . identToLexeme $ modid
+  chi <- loadCHI . moduleNameToFileName . identToLexeme $ modid
   toks'' <- parseEndHook toks'
   frags <- parseFrags toks''
   return $ CHSHook (CHSImport qual modid chi pos) : frags
  
+moduleNameToFileName :: String -> FilePath
+moduleNameToFileName = map dotToSlash
+  where dotToSlash '.' = '/'
+        dotToSlash c   = c
+
 parseContext          :: Position -> [CHSToken] -> CST s [CHSFrag]
 parseContext pos toks  = do


Does this seem ok? It makes me a bit nervous changing the lexical
definition of an identifier (we really only want to change the module
identifier definition, not all identifiers).

Duncan



More information about the C2hs mailing list