summaryrefslogtreecommitdiff
path: root/src/GF/Embed/EmbedAPI.hs
diff options
context:
space:
mode:
authoraarne <unknown>2005-05-10 11:49:13 +0000
committeraarne <unknown>2005-05-10 11:49:13 +0000
commit5111762d31a50c4fccc8da02c7c327a2b51d0e77 (patch)
treea3165802392173f30968061df88ebe6b76e09308 /src/GF/Embed/EmbedAPI.hs
parent75f12f69e3c00eb6e6beec3f027cb2264cd3ea60 (diff)
modules for embedded GF
Diffstat (limited to 'src/GF/Embed/EmbedAPI.hs')
-rw-r--r--src/GF/Embed/EmbedAPI.hs78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/GF/Embed/EmbedAPI.hs b/src/GF/Embed/EmbedAPI.hs
new file mode 100644
index 000000000..197c24de2
--- /dev/null
+++ b/src/GF/Embed/EmbedAPI.hs
@@ -0,0 +1,78 @@
+----------------------------------------------------------------------
+-- |
+-- Module : EmbedAPI
+-- Maintainer : Aarne Ranta
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date:
+-- > CVS $Author:
+-- > CVS $Revision:
+--
+-- Reduced Application Programmer's Interface to GF, meant for
+-- embedded GF systems. AR 10/5/2005
+-----------------------------------------------------------------------------
+
+module GF.Embed.EmbedAPI where
+
+import GF.Compile.ShellState (ShellState,grammar2shellState,canModules,stateGrammarOfLang,abstract,grammar)
+import GF.UseGrammar.Linear (linTree2string)
+import GF.Embed.EmbedParsing (parseString)
+import GF.Canon.CMacros (noMark)
+import GF.Grammar.Grammar (Trm)
+import GF.Grammar.MMacros (exp2tree)
+import GF.Grammar.Macros (zIdent)
+import GF.Grammar.Values (tree2exp)
+import GF.Grammar.TypeCheck (annotate)
+import GF.Canon.GetGFC (getCanonGrammar)
+import GF.Infra.Modules (emptyMGrammar)
+import GF.CF.CFIdent (string2CFCat)
+import GF.Infra.UseIO
+import GF.Data.Operations
+import GF.Infra.Option (noOptions,useUntokenizer)
+import GF.Infra.Ident (prIdent)
+import GF.Embed.EmbedCustom
+
+-- This API is meant to be used when embedding GF grammars in Haskell
+-- programs. The embedded system is supposed to use the
+-- .gfcm grammar format, which is first produced by the gf program.
+
+---------------------------------------------------
+-- Interface
+---------------------------------------------------
+
+type MultiGrammar = ShellState
+type Language = String
+type Category = String
+type Tree = Trm
+
+file2grammar :: FilePath -> IO MultiGrammar
+linearize :: MultiGrammar -> Language -> Tree -> String
+parse :: MultiGrammar -> Language -> Category -> String -> [Tree]
+
+---------------------------------------------------
+-- Implementation
+---------------------------------------------------
+
+file2grammar file = do
+ can <- useIOE (error "cannot parse grammar file") $ getCanonGrammar file
+ return $ errVal (error "cannot build multigrammar") $ grammar2shellState noOptions (can,emptyMGrammar)
+
+linearize mgr lang =
+ untok .
+ linTree2string noMark (canModules mgr) (zIdent lang) .
+ errVal (error "illegal tree") .
+ annotate gr
+ where
+ gr = grammar sgr
+ sgr = stateGrammarOfLang mgr (zIdent lang)
+ untok = customOrDefault noOptions useUntokenizer customUntokenizer sgr
+
+parse mgr lang cat =
+ map tree2exp .
+ errVal [] .
+ parseString noOptions sgr cfcat
+ where
+ sgr = stateGrammarOfLang mgr (zIdent lang)
+ cfcat = string2CFCat abs cat
+ abs = maybe (error "no abstract syntax") prIdent $ abstract mgr