summaryrefslogtreecommitdiff
path: root/src/GF/Compile/GetGrammar.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/GF/Compile/GetGrammar.hs')
-rw-r--r--src/GF/Compile/GetGrammar.hs71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/GF/Compile/GetGrammar.hs b/src/GF/Compile/GetGrammar.hs
new file mode 100644
index 000000000..fb3fbf5ad
--- /dev/null
+++ b/src/GF/Compile/GetGrammar.hs
@@ -0,0 +1,71 @@
+module GetGrammar where
+
+import Operations
+import qualified ErrM as E ----
+
+import UseIO
+import Grammar
+import Modules
+import PrGrammar
+import qualified AbsGF as A
+import SourceToGrammar
+---- import Macros
+---- import Rename
+import Option
+--- import Custom
+import ParGF
+
+import ReadFiles ----
+
+import List (nub)
+import Monad (foldM)
+
+-- this module builds the internal GF grammar that is sent to the type checker
+
+getSourceModule :: FilePath -> IOE SourceModule
+getSourceModule file = do
+ string <- readFileIOE file
+ let tokens = myLexer string
+ mo1 <- ioeErr $ err2err $ pModDef tokens
+ ioeErr $ transModDef mo1
+
+
+-- for old GF format with includes
+
+getOldGrammar :: FilePath -> IOE SourceGrammar
+getOldGrammar file = do
+ defs <- parseOldGrammarFiles file
+ let g = A.OldGr A.NoIncl defs
+ ioeErr $ transOldGrammar g file
+
+parseOldGrammarFiles :: FilePath -> IOE [A.TopDef]
+parseOldGrammarFiles file = do
+ putStrE $ "reading grammar of old format" +++ file
+ (_, g) <- getImports "" ([],[]) file
+ return g -- now we can throw away includes
+ where
+ getImports oldInitPath (oldImps, oldG) f = do
+ (path,s) <- readFileLibraryIOE oldInitPath f
+ if not (elem path oldImps)
+ then do
+ (imps,g) <- parseOldGrammar path
+ foldM (getImports (initFilePath path)) (path : oldImps, g ++ oldG) imps
+ else
+ return (oldImps, oldG)
+
+parseOldGrammar :: FilePath -> IOE ([FilePath],[A.TopDef])
+parseOldGrammar file = do
+ putStrE $ "reading old file" +++ file
+ s <- ioeIO $ readFileIf file
+ A.OldGr incl topdefs <- ioeErr $ err2err $ pOldGrammar $ myLexer $ fixNewlines s
+ includes <- ioeErr $ transInclude incl
+ return (includes, topdefs)
+
+----
+
+err2err :: E.Err a -> Err a
+err2err (E.Ok v) = Ok v
+err2err (E.Bad s) = Bad s
+
+ioeEErr = ioeErr . err2err
+