From 5111762d31a50c4fccc8da02c7c327a2b51d0e77 Mon Sep 17 00:00:00 2001 From: aarne Date: Tue, 10 May 2005 11:49:13 +0000 Subject: modules for embedded GF --- src/GF/Embed/EmbedAPI.hs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/GF/Embed/EmbedAPI.hs (limited to 'src/GF/Embed/EmbedAPI.hs') 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 -- cgit v1.2.3