1
0
forked from GitHub/gf-core

Embed/TemplateApp

This commit is contained in:
aarne
2005-10-07 12:06:19 +00:00
parent 01466aa3fd
commit ea1dcfd70a
3 changed files with 76 additions and 1 deletions

View File

@@ -15,13 +15,15 @@
module GF.Embed.EmbedAPI where module GF.Embed.EmbedAPI where
import GF.Compile.ShellState (ShellState,grammar2shellState,canModules,stateGrammarOfLang,abstract,grammar) import GF.Compile.ShellState (ShellState,grammar2shellState,canModules,stateGrammarOfLang,abstract,grammar,firstStateGrammar,allLanguages,allCategories)
import GF.UseGrammar.Linear (linTree2string) import GF.UseGrammar.Linear (linTree2string)
import GF.UseGrammar.GetTree (string2tree)
import GF.Embed.EmbedParsing (parseString) import GF.Embed.EmbedParsing (parseString)
import GF.Canon.CMacros (noMark) import GF.Canon.CMacros (noMark)
import GF.Grammar.Grammar (Trm) import GF.Grammar.Grammar (Trm)
import GF.Grammar.MMacros (exp2tree) import GF.Grammar.MMacros (exp2tree)
import GF.Grammar.Macros (zIdent) import GF.Grammar.Macros (zIdent)
import GF.Grammar.PrGrammar (prt_)
import GF.Grammar.Values (tree2exp) import GF.Grammar.Values (tree2exp)
import GF.Grammar.TypeCheck (annotate) import GF.Grammar.TypeCheck (annotate)
import GF.Canon.GetGFC (getCanonGrammar) import GF.Canon.GetGFC (getCanonGrammar)
@@ -47,9 +49,19 @@ type Category = String
type Tree = Trm type Tree = Trm
file2grammar :: FilePath -> IO MultiGrammar file2grammar :: FilePath -> IO MultiGrammar
linearize :: MultiGrammar -> Language -> Tree -> String linearize :: MultiGrammar -> Language -> Tree -> String
parse :: MultiGrammar -> Language -> Category -> String -> [Tree] parse :: MultiGrammar -> Language -> Category -> String -> [Tree]
linearizeAll :: MultiGrammar -> Tree -> [String]
parseAll :: MultiGrammar -> Category -> String -> [[Tree]]
readTree :: MultiGrammar -> String -> Tree
showTree :: Tree -> String
languages :: MultiGrammar -> [Language]
categories :: MultiGrammar -> [Category]
--------------------------------------------------- ---------------------------------------------------
-- Implementation -- Implementation
--------------------------------------------------- ---------------------------------------------------
@@ -76,3 +88,15 @@ parse mgr lang cat =
sgr = stateGrammarOfLang mgr (zIdent lang) sgr = stateGrammarOfLang mgr (zIdent lang)
cfcat = string2CFCat abs cat cfcat = string2CFCat abs cat
abs = maybe (error "no abstract syntax") prIdent $ abstract mgr abs = maybe (error "no abstract syntax") prIdent $ abstract mgr
linearizeAll mgr t = [linearize mgr lang t | lang <- languages mgr]
parseAll mgr cat s = [parse mgr lang cat s | lang <- languages mgr]
readTree mgr s = tree2exp $ string2tree (firstStateGrammar mgr) s
showTree t = prt_ t
languages mgr = [prt_ l | l <- allLanguages mgr]
categories mgr = [prt_ c | (_,c) <- allCategories mgr]

View File

@@ -0,0 +1,44 @@
module Main where
import GF.Embed.EmbedAPI
import System
-- Simple translation application built on EmbedAPI. AR 7/10/2005
main :: IO ()
main = do
file:_ <- getArgs
grammar <- file2grammar file
translate grammar
translate :: MultiGrammar -> IO ()
translate grammar = do
s <- getLine
if s == "quit" then return () else do
treat grammar s
translate grammar
treat :: MultiGrammar -> String -> IO ()
treat grammar s = putStrLn $ case comm of
["lin"] -> unlines $ linearizeAll grammar $ readTree grammar rest
["lin",lang] -> linearize grammar lang $ readTree grammar rest
["parse",cat] -> unlines $ map showTree $ concat $ parseAll grammar cat rest
["parse",lang,cat] -> unlines $ map showTree $ parse grammar lang cat rest
["langs"] -> unwords $ languages grammar
["cats"] -> unwords $ categories grammar
["help"] -> helpMsg
_ -> "command not interpreted: " ++ s
where
(comm,rest) = (words c,drop 1 r) where
(c,r) = span (/=':') s
helpMsg = unlines [
"lin : <Tree>",
"lin <Lang> : <Tree>",
"parse <Cat> : <String>",
"parse <Lang> <Cat> : <String>",
"langs",
"cats",
"help",
"quit"
]

View File

@@ -26,6 +26,8 @@ MSI_FILE=gf-$(subst .,_,$(PACKAGE_VERSION)).msi
GF_DATA_DIR=$(datadir)/GF-$(PACKAGE_VERSION) GF_DATA_DIR=$(datadir)/GF-$(PACKAGE_VERSION)
EMBED = GF/Embed/TemplateApp
# use the temporary binary file name 'gf-bin' to not clash with directory 'GF' # use the temporary binary file name 'gf-bin' to not clash with directory 'GF'
# on case insensitive file systems (such as FAT) # on case insensitive file systems (such as FAT)
GF_EXE=gf$(EXEEXT) GF_EXE=gf$(EXEEXT)
@@ -73,6 +75,11 @@ build:
opt: GHCFLAGS += $(GHCOPTFLAGS) opt: GHCFLAGS += $(GHCOPTFLAGS)
opt: build opt: build
embed: GHCFLAGS += $(GHCOPTFLAGS)
embed:
$(GHMAKE) $(GHCFLAGS) $(EMBED) -o $(EMBED)
strip $(EMBED)
noopt: build noopt: build
ghci: ghci-nofud ghci: ghci-nofud