From a2626e24dd9e468ee0c5ccd455e4a5f782d56522 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Tue, 15 Nov 2011 19:12:22 +0000 Subject: now we store version number in every .gfo file. If the file is compiled with different compiler then we simply recompile it. --- src/compiler/GF/Grammar/Binary.hs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'src/compiler/GF/Grammar') diff --git a/src/compiler/GF/Grammar/Binary.hs b/src/compiler/GF/Grammar/Binary.hs index d1a3ac413..20adf3c48 100644 --- a/src/compiler/GF/Grammar/Binary.hs +++ b/src/compiler/GF/Grammar/Binary.hs @@ -9,7 +9,9 @@ module GF.Grammar.Binary where +import Data.Char import Data.Binary +import Control.Monad import qualified Data.Map as Map import qualified Data.ByteString.Char8 as BS @@ -18,7 +20,11 @@ import GF.Infra.Ident import GF.Infra.Option import GF.Grammar.Grammar -import PGF.Binary hiding (decodingError) +import PGF.Binary + +-- Please change this every time when the GFO format is changed +gfoVersion = "GF01" + instance Binary Ident where put id = put (ident2bs id) @@ -274,9 +280,24 @@ instance Binary Label where 1 -> fmap LVar get _ -> decodingError -decodeModHeader :: FilePath -> IO SourceModule -decodeModHeader fpath = do - (m,mtype,mstatus,mflags,mextend,mwith,mopens,med,msrc) <- decodeFile fpath + +putGFOVersion = mapM_ (putWord8 . fromIntegral . ord) gfoVersion +getGFOVersion = replicateM (length gfoVersion) (fmap (chr . fromIntegral) getWord8) + +decodeModule :: FilePath -> IO SourceModule +decodeModule fpath = do + (m,mtype,mstatus,mflags,mextend,mwith,mopens,med,msrc) <- decodeFile_ fpath (getGFOVersion >> get) return (m,ModInfo mtype mstatus mflags mextend mwith mopens med msrc Nothing Map.empty) -decodingError = fail "This GFO file was compiled with different version of GF" +decodeModuleHeader fpath = decodeFile_ fpath getVersionedMod + where + getVersionedMod = do + ver <- getGFOVersion + if ver == gfoVersion + then do (m,mtype,mstatus,mflags,mextend,mwith,mopens,med,msrc) <- get + return (Just (m,ModInfo mtype mstatus mflags mextend mwith mopens med msrc Nothing Map.empty)) + else return Nothing + +encodeModule :: FilePath -> SourceModule -> IO () +encodeModule fpath mo = + encodeFile_ fpath (putGFOVersion >> put mo) -- cgit v1.2.3