forked from GitHub/gf-core
better MkConcrete and example in lib/resource/doc/example
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
concrete QuestionsEng of Questions = QuestionsI with
|
concrete QuestionsEng of Questions = QuestionsExI with
|
||||||
(Resource = ResourceEng) ;
|
(Resource = ResourceEng) ;
|
||||||
|
|||||||
15
lib/resource/doc/example/QuestionsExI.gfe
Normal file
15
lib/resource/doc/example/QuestionsExI.gfe
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
--# -resource=../../langeng.gfcm
|
||||||
|
|
||||||
|
-- to compile: gf -makeconcrete QuestionsExI.gfe
|
||||||
|
|
||||||
|
incomplete concrete QuestionsExI of Questions = open Resource in {
|
||||||
|
lincat
|
||||||
|
Phrase = Phr ;
|
||||||
|
Entity = N ;
|
||||||
|
Action = V2 ;
|
||||||
|
|
||||||
|
lin Who love_V2 man_N = in Phr "who loves the men?" ;
|
||||||
|
lin Whom man_N love_V2 = in Phr "whom does the man love?" ;
|
||||||
|
lin Answer woman_N love_V2 man_N = in Phr "the woman loves the man." ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stability)
|
-- Stability : (stability)
|
||||||
-- Portability : (portability)
|
-- Portability : (portability)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/06/02 10:23:52 $
|
-- > CVS $Date: 2005/06/02 17:31:56 $
|
||||||
-- > CVS $Author: aarne $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.25 $
|
-- > CVS $Revision: 1.26 $
|
||||||
--
|
--
|
||||||
-- The Main module of GF program.
|
-- The Main module of GF program.
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@@ -85,7 +85,7 @@ helpMsg = unlines [
|
|||||||
" -noemit do not emit code when compiling",
|
" -noemit do not emit code when compiling",
|
||||||
" -v be verbose when compiling",
|
" -v be verbose when compiling",
|
||||||
" -batch structure session by XML tags (use > to send into a file)",
|
" -batch structure session by XML tags (use > to send into a file)",
|
||||||
" -makeconcrete batch-compile .gfp file to concrete syntax using parser",
|
" -makeconcrete batch-compile .gfe file to concrete syntax using parser",
|
||||||
" -help show this message",
|
" -help show this message",
|
||||||
"To use the GUI: jgf <option>* <file>*"
|
"To use the GUI: jgf <option>* <file>*"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/05/17 11:20:25 $
|
-- > CVS $Date: 2005/06/02 17:31:57 $
|
||||||
-- > CVS $Author: peb $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.36 $
|
-- > CVS $Revision: 1.37 $
|
||||||
--
|
--
|
||||||
-- Application Programmer's Interface to GF; also used by Shell. AR 10/11/2001
|
-- Application Programmer's Interface to GF; also used by Shell. AR 10/11/2001
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@@ -55,6 +55,7 @@ import qualified GF.Infra.Ident as I
|
|||||||
import qualified GF.Compile.GrammarToCanon as GC
|
import qualified GF.Compile.GrammarToCanon as GC
|
||||||
import qualified GF.Canon.CanonToGrammar as CG
|
import qualified GF.Canon.CanonToGrammar as CG
|
||||||
import qualified GF.Canon.MkGFC as MC
|
import qualified GF.Canon.MkGFC as MC
|
||||||
|
import qualified GF.Embed.EmbedAPI as EA
|
||||||
|
|
||||||
import GF.UseGrammar.Editing
|
import GF.UseGrammar.Editing
|
||||||
|
|
||||||
@@ -145,9 +146,11 @@ string2GFCat = string2CFCat
|
|||||||
-- then stg for customizable and internal use
|
-- then stg for customizable and internal use
|
||||||
|
|
||||||
optFile2grammar :: Options -> FilePath -> IOE GFGrammar
|
optFile2grammar :: Options -> FilePath -> IOE GFGrammar
|
||||||
optFile2grammar os f = do
|
optFile2grammar os f
|
||||||
((_,_,gr),_) <- compileModule os emptyShellState f
|
| fileSuffix f == "gfcm" = ioeIO $ liftM firstStateGrammar $ EA.file2grammar f
|
||||||
ioeErr $ grammar2stateGrammar os gr
|
| otherwise = do
|
||||||
|
((_,_,gr),_) <- compileModule os emptyShellState f
|
||||||
|
ioeErr $ grammar2stateGrammar os gr
|
||||||
|
|
||||||
optFile2grammarE :: Options -> FilePath -> IOE GFGrammar
|
optFile2grammarE :: Options -> FilePath -> IOE GFGrammar
|
||||||
optFile2grammarE = optFile2grammar
|
optFile2grammarE = optFile2grammar
|
||||||
@@ -292,6 +295,11 @@ morphoAnalyse opts gr
|
|||||||
where
|
where
|
||||||
mo = morpho gr
|
mo = morpho gr
|
||||||
|
|
||||||
|
isKnownWord :: GFGrammar -> String -> Bool
|
||||||
|
isKnownWord gr s = case morphoAnalyse (options [beShort]) gr s of
|
||||||
|
a@(_:_:_) -> last (init a) /= '*' -- [word *]
|
||||||
|
_ -> False
|
||||||
|
|
||||||
{-
|
{-
|
||||||
prExpXML :: StateGrammar -> Term -> [String]
|
prExpXML :: StateGrammar -> Term -> [String]
|
||||||
prExpXML gr = prElementX . term2elemx (stateAbstract gr)
|
prExpXML gr = prElementX . term2elemx (stateAbstract gr)
|
||||||
|
|||||||
@@ -16,11 +16,13 @@ module GF.Compile.MkConcrete (mkConcrete) where
|
|||||||
|
|
||||||
import GF.Grammar.Values (Tree,tree2exp)
|
import GF.Grammar.Values (Tree,tree2exp)
|
||||||
import GF.Grammar.PrGrammar (prt_)
|
import GF.Grammar.PrGrammar (prt_)
|
||||||
import GF.Compile.ShellState (absId,stateGrammarWords)
|
import GF.Compile.ShellState (absId,firstStateGrammar)
|
||||||
import GF.API
|
import GF.API
|
||||||
|
import qualified GF.Embed.EmbedAPI as EA
|
||||||
|
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
import GF.Infra.UseIO
|
import GF.Infra.UseIO
|
||||||
|
import GF.Infra.Option
|
||||||
|
|
||||||
import Data.Char
|
import Data.Char
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
@@ -33,26 +35,31 @@ import Control.Monad
|
|||||||
-- Format of resource path (on first line):
|
-- Format of resource path (on first line):
|
||||||
-- --# -resource=PATH
|
-- --# -resource=PATH
|
||||||
-- Other lines are copied verbatim.
|
-- 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 :: FilePath -> IO ()
|
||||||
mkConcrete file = do
|
mkConcrete file = do
|
||||||
cont <- liftM lines $ readFileIf file
|
cont <- liftM lines $ readFileIf file
|
||||||
let res = getResPath cont
|
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 abs = prt_ $ absId gr
|
||||||
let parser cat = parse gr (string2GFCat abs cat)
|
let parser cat = errVal ([],"No parse") .
|
||||||
let mor = \w -> isInBinTree w $ sorted2tree [(w,()) | w <- stateGrammarWords gr]
|
optParseArgErrMsg (options [newMParser, firstCat cat, beVerbose]) gr
|
||||||
|
let morpho = isKnownWord gr
|
||||||
writeFile (suffixFile "gf" (justModuleName file)) $ unlines $
|
writeFile (suffixFile "gf" (justModuleName file)) $ unlines $
|
||||||
map (mkCnc parser mor) cont
|
map (mkCnc parser morpho) cont
|
||||||
|
|
||||||
getResPath :: [String] -> String
|
getResPath :: [String] -> String
|
||||||
getResPath s = case head (dropWhile (all isSpace) s) of
|
getResPath s = case head (dropWhile (all isSpace) s) of
|
||||||
'-':'-':'#':path -> reverse (takeWhile (not . (=='=')) (reverse path))
|
'-':'-':'#':path -> reverse (takeWhile (not . (=='=')) (reverse path))
|
||||||
_ -> error "first line must be --# -resource=<PATH>"
|
_ -> error "first line must be --# -resource=<PATH>"
|
||||||
|
|
||||||
mkCnc :: (String -> String -> [Tree]) -> (String -> Bool) -> String -> String
|
mkCnc :: (String -> String -> ([Tree],String)) -> (String -> Bool) -> String -> String
|
||||||
mkCnc parser morph line = case words line of
|
mkCnc parser morpho line = case words line of
|
||||||
"lin" : rest -> mkLinRule rest
|
"lin" : rest -> mkLinRule rest
|
||||||
_ -> line
|
_ -> line
|
||||||
where
|
where
|
||||||
@@ -61,11 +68,14 @@ mkCnc parser morph line = case words line of
|
|||||||
(pre,str) = span (/= "in") s
|
(pre,str) = span (/= "in") s
|
||||||
([cat],rest) = splitAt 1 $ tail str
|
([cat],rest) = splitAt 1 $ tail str
|
||||||
lin = init (tail (unwords (init rest))) -- unquote
|
lin = init (tail (unwords (init rest))) -- unquote
|
||||||
def = case parser cat lin of
|
def
|
||||||
[t] -> prt_ $ tree2exp t
|
| last pre /= "=" = line -- ordinary lin rule
|
||||||
t:_ -> prt_ (tree2exp t) +++ "{- AMBIGUOUS -}"
|
| 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
|
in
|
||||||
if null def
|
def
|
||||||
then "-- NO PARSE " ++ line
|
morph s = case [w | w <- words s, not (morpho w)] of
|
||||||
else "lin " ++ unwords pre +++ def +++ ";"
|
[] -> ""
|
||||||
|
ws -> "unknown words: " ++ unwords ws
|
||||||
|
|||||||
Reference in New Issue
Block a user