From 055c0d0d5a5bb0dc75904fe53df7f2e4f5732a8f Mon Sep 17 00:00:00 2001 From: aarne Date: Wed, 21 May 2008 09:26:44 +0000 Subject: GF/src is now for 2.9, and the new sources are in src-3.0 - keep it this way until the release of GF 3 --- src-3.0/GF/Canon/GetGFC.hs | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src-3.0/GF/Canon/GetGFC.hs (limited to 'src-3.0/GF/Canon/GetGFC.hs') diff --git a/src-3.0/GF/Canon/GetGFC.hs b/src-3.0/GF/Canon/GetGFC.hs new file mode 100644 index 000000000..049f75efe --- /dev/null +++ b/src-3.0/GF/Canon/GetGFC.hs @@ -0,0 +1,78 @@ +---------------------------------------------------------------------- +-- | +-- Module : GetGFC +-- Maintainer : AR +-- Stability : (stable) +-- Portability : (portable) +-- +-- > CVS $Date: 2005/05/30 18:39:43 $ +-- > CVS $Author: aarne $ +-- > CVS $Revision: 1.9 $ +-- +-- (Description of the module) +----------------------------------------------------------------------------- + +module GF.Canon.GetGFC (getCanonModule, getCanonGrammar) where + +import GF.Data.Operations +import GF.Canon.ParGFC +import GF.Canon.GFC +import GF.Canon.MkGFC +import GF.Infra.Modules +import GF.Infra.UseIO + +import System.IO +import System.Directory +import Control.Monad + +getCanonModule :: FilePath -> IOE CanonModule +getCanonModule file = do + gr <- getCanonGrammar file + case modules gr of + [m] -> return m + _ -> ioeErr $ Bad "expected exactly one module in a file" + +getCanonGrammar :: FilePath -> IOE CanonGrammar +-- getCanonGrammar = getCanonGrammarByLine +getCanonGrammar file = do + s <- ioeIO $ readFileIf file + c <- ioeErr $ pCanon $ myLexer s + return $ canon2grammar c + +{- +-- the following surprisingly does not save memory so it is +-- not in use + +getCanonGrammarByLine :: FilePath -> IOE CanonGrammar +getCanonGrammarByLine file = do + b <- ioeIO $ doesFileExist file + if not b + then ioeErr $ Bad $ "file" +++ file +++ "does not exist" + else do + ioeIO $ putStrLn "" + hand <- ioeIO $ openFile file ReadMode ---- err + size <- ioeIO $ hFileSize hand + gr <- addNextLine (size,0) 1 hand emptyMGrammar + ioeIO $ hClose hand + return $ MGrammar $ reverse $ modules gr + + where + addNextLine (size,act) d hand gr = do + eof <- ioeIO $ hIsEOF hand + if eof + then return gr + else do + s <- ioeIO $ hGetLine hand + let act' = act + toInteger (length s) +-- if isHash act act' then (ioeIO $ putChar '#') else return () + updGrammar act' d gr $ pLine $ myLexer s + where + updGrammar a d gr (Ok t) = case buildCanonGrammar d gr t of + (gr',d') -> addNextLine (size,a) d' hand gr' + updGrammar _ _ gr (Bad s) = do + ioeIO $ putStrLn s + return emptyMGrammar + + isHash a b = a `div` step < b `div` step + step = size `div` 50 +-} -- cgit v1.2.3