diff options
| author | aarne <aarne@cs.chalmers.se> | 2008-05-21 09:26:44 +0000 |
|---|---|---|
| committer | aarne <aarne@cs.chalmers.se> | 2008-05-21 09:26:44 +0000 |
| commit | 055c0d0d5a5bb0dc75904fe53df7f2e4f5732a8f (patch) | |
| tree | 0e63fb68c69c8f6ad0f78893c63420f0a3600e1c /src-3.0/GF/Canon/GetGFC.hs | |
| parent | 915a1de71783ab8446b1af9e72c7ba7dfbc12d3f (diff) | |
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
Diffstat (limited to 'src-3.0/GF/Canon/GetGFC.hs')
| -rw-r--r-- | src-3.0/GF/Canon/GetGFC.hs | 78 |
1 files changed, 78 insertions, 0 deletions
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 +-} |
