summaryrefslogtreecommitdiff
path: root/src/GF/Compile
diff options
context:
space:
mode:
authoraarne <unknown>2005-06-02 16:31:56 +0000
committeraarne <unknown>2005-06-02 16:31:56 +0000
commitf0e13dd29f495a109e0fa693624c9455bb36b2b1 (patch)
treef6eca09a85415e5ef75dcabc74d99f75b088edc2 /src/GF/Compile
parenta38a894481aff1b658b1d86409a1eaa59c737f2e (diff)
better MkConcrete and example in lib/resource/doc/example
Diffstat (limited to 'src/GF/Compile')
-rw-r--r--src/GF/Compile/MkConcrete.hs38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/GF/Compile/MkConcrete.hs b/src/GF/Compile/MkConcrete.hs
index 6295e9851..be3d6f5b4 100644
--- a/src/GF/Compile/MkConcrete.hs
+++ b/src/GF/Compile/MkConcrete.hs
@@ -16,11 +16,13 @@ module GF.Compile.MkConcrete (mkConcrete) where
import GF.Grammar.Values (Tree,tree2exp)
import GF.Grammar.PrGrammar (prt_)
-import GF.Compile.ShellState (absId,stateGrammarWords)
+import GF.Compile.ShellState (absId,firstStateGrammar)
import GF.API
+import qualified GF.Embed.EmbedAPI as EA
import GF.Data.Operations
import GF.Infra.UseIO
+import GF.Infra.Option
import Data.Char
import Control.Monad
@@ -33,26 +35,31 @@ import Control.Monad
-- Format of resource path (on first line):
-- --# -resource=PATH
-- Other lines are copied verbatim.
+-- Assumes: resource has been built with
+-- i -src -optimize=share SOURCE
+-- because mcfg parsing is used.
mkConcrete :: FilePath -> IO ()
mkConcrete file = do
cont <- liftM lines $ readFileIf file
let res = getResPath cont
- gr <- file2grammar res
+ egr <- appIOE $ optFile2grammar (options [useOptimizer "share"]) res --- for -mcfg
+ gr <- err (\s -> putStrLn s >> error "resource file rejected") return egr
let abs = prt_ $ absId gr
- let parser cat = parse gr (string2GFCat abs cat)
- let mor = \w -> isInBinTree w $ sorted2tree [(w,()) | w <- stateGrammarWords gr]
+ let parser cat = errVal ([],"No parse") .
+ optParseArgErrMsg (options [newMParser, firstCat cat, beVerbose]) gr
+ let morpho = isKnownWord gr
writeFile (suffixFile "gf" (justModuleName file)) $ unlines $
- map (mkCnc parser mor) cont
+ map (mkCnc parser morpho) cont
getResPath :: [String] -> String
getResPath s = case head (dropWhile (all isSpace) s) of
'-':'-':'#':path -> reverse (takeWhile (not . (=='=')) (reverse path))
_ -> error "first line must be --# -resource=<PATH>"
-mkCnc :: (String -> String -> [Tree]) -> (String -> Bool) -> String -> String
-mkCnc parser morph line = case words line of
+mkCnc :: (String -> String -> ([Tree],String)) -> (String -> Bool) -> String -> String
+mkCnc parser morpho line = case words line of
"lin" : rest -> mkLinRule rest
_ -> line
where
@@ -61,11 +68,14 @@ mkCnc parser morph line = case words line of
(pre,str) = span (/= "in") s
([cat],rest) = splitAt 1 $ tail str
lin = init (tail (unwords (init rest))) -- unquote
- def = case parser cat lin of
- [t] -> prt_ $ tree2exp t
- t:_ -> prt_ (tree2exp t) +++ "{- AMBIGUOUS -}"
- [] -> ""
+ def
+ | last pre /= "=" = line -- ordinary lin rule
+ | otherwise = case parser cat lin of
+ ([t],_) -> "lin " ++ unwords pre +++ prt_ (tree2exp t) +++ ";"
+ (t:_,_) -> "lin " ++ unwords pre +++ prt_ (tree2exp t) +++ "{- AMBIGUOUS -} ;"
+ ([],msg) -> "{-" ++ line ++++ morph lin ++++ "-}"
in
- if null def
- then "-- NO PARSE " ++ line
- else "lin " ++ unwords pre +++ def +++ ";"
+ def
+ morph s = case [w | w <- words s, not (morpho w)] of
+ [] -> ""
+ ws -> "unknown words: " ++ unwords ws