mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
example substitutions
This commit is contained in:
@@ -18,7 +18,7 @@ Changes in functionality since May 17, 2005, release of GF Version 2.2
|
|||||||
<b>grammar writing by examples</b>. Files of this format are first
|
<b>grammar writing by examples</b>. Files of this format are first
|
||||||
converted to <tt>.gf</tt> files by the command
|
converted to <tt>.gf</tt> files by the command
|
||||||
<pre>
|
<pre>
|
||||||
gf -makeconcrete File.gfe
|
gf -examples File.gfe
|
||||||
</pre>
|
</pre>
|
||||||
See <a href="../lib/resource/doc/examples/QuestionsI.gfe">
|
See <a href="../lib/resource/doc/examples/QuestionsI.gfe">
|
||||||
<tt>../lib/resource/doc/examples/QuestionsI.gfe</tt></a>
|
<tt>../lib/resource/doc/examples/QuestionsI.gfe</tt></a>
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stability)
|
-- Stability : (stability)
|
||||||
-- Portability : (portability)
|
-- Portability : (portability)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/06/02 17:31:56 $
|
-- > CVS $Date: 2005/06/03 21:51:58 $
|
||||||
-- > CVS $Author: aarne $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.26 $
|
-- > CVS $Revision: 1.27 $
|
||||||
--
|
--
|
||||||
-- The Main module of GF program.
|
-- The Main module of GF program.
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@@ -60,7 +60,7 @@ main = do
|
|||||||
_ -> putStrLnFlush "expecting exactly one gf file to compile"
|
_ -> putStrLnFlush "expecting exactly one gf file to compile"
|
||||||
|
|
||||||
_ | opt makeConcrete -> do
|
_ | opt makeConcrete -> do
|
||||||
mapM_ mkConcrete fs
|
mkConcretes fs
|
||||||
|
|
||||||
_ | opt doBatch -> do
|
_ | opt doBatch -> do
|
||||||
if opt beSilent then return () else putStrLnFlush "<gfbatch>"
|
if opt beSilent then return () else putStrLnFlush "<gfbatch>"
|
||||||
@@ -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 .gfe file to concrete syntax using parser",
|
" -examples batch-compile .gfe file by parsing examples",
|
||||||
" -help show this message",
|
" -help show this message",
|
||||||
"To use the GUI: jgf <option>* <file>*"
|
"To use the GUI: jgf <option>* <file>*"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -12,11 +12,14 @@
|
|||||||
-- Compile a gfl file into a concrete syntax by using the parser on a resource grammar.
|
-- Compile a gfl file into a concrete syntax by using the parser on a resource grammar.
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
module GF.Compile.MkConcrete (mkConcrete) where
|
module GF.Compile.MkConcrete (mkConcretes,mkCncLine) 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,firstStateGrammar)
|
import GF.Grammar.Grammar (Term(Q,QC)) ---
|
||||||
|
import GF.Grammar.Macros (composSafeOp, record2subst)
|
||||||
|
import GF.Compile.ShellState (firstStateGrammar)
|
||||||
|
import GF.Compile.PGrammar (pTerm)
|
||||||
import GF.API
|
import GF.API
|
||||||
import qualified GF.Embed.EmbedAPI as EA
|
import qualified GF.Embed.EmbedAPI as EA
|
||||||
|
|
||||||
@@ -35,22 +38,32 @@ 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
|
-- The resource has to be built with
|
||||||
-- i -src -optimize=share SOURCE
|
-- i -src -optimize=share SOURCE
|
||||||
-- because mcfg parsing is used.
|
-- because mcfg parsing is used.
|
||||||
|
-- A sequence of files can be processed with the same resource without
|
||||||
|
-- rebuilding the grammar and parser.
|
||||||
|
|
||||||
|
mkConcretes :: [FilePath] -> IO ()
|
||||||
mkConcrete :: FilePath -> IO ()
|
mkConcretes [] = putStrLn "no files to process"
|
||||||
mkConcrete file = do
|
mkConcretes files@(file:_) = do
|
||||||
cont <- liftM lines $ readFileIf file
|
cont <- liftM lines $ readFileIf file
|
||||||
let res = getResPath cont
|
let res = getResPath cont
|
||||||
egr <- appIOE $
|
egr <- appIOE $
|
||||||
optFile2grammar (options [useOptimizer "share",fromSource,beSilent,notEmitCode]) res --- for -mcfg
|
optFile2grammar (options
|
||||||
|
[useOptimizer "share",fromSource,beSilent,notEmitCode]) res --- for -mcfg
|
||||||
gr <- err (\s -> putStrLn s >> error "resource file rejected") return egr
|
gr <- err (\s -> putStrLn s >> error "resource file rejected") return egr
|
||||||
let abs = prt_ $ absId gr
|
|
||||||
let parser cat = errVal ([],"No parse") .
|
let parser cat = errVal ([],"No parse") .
|
||||||
optParseArgErrMsg (options [newMParser, firstCat cat, beVerbose]) gr
|
optParseArgErrMsg (options [newMParser, firstCat cat, beVerbose]) gr
|
||||||
let morpho = isKnownWord gr
|
let morpho = isKnownWord gr
|
||||||
|
mapM_ (mkConcrete parser morpho) files
|
||||||
|
|
||||||
|
type Parser = String -> String -> ([Tree],String)
|
||||||
|
type Morpho = String -> Bool
|
||||||
|
|
||||||
|
mkConcrete :: Parser -> Morpho -> FilePath -> IO ()
|
||||||
|
mkConcrete parser morpho file = do
|
||||||
|
cont <- liftM lines $ readFileIf file
|
||||||
let out = suffixFile "gf" $ justModuleName file
|
let out = suffixFile "gf" $ justModuleName file
|
||||||
writeFile out ""
|
writeFile out ""
|
||||||
mapM_ (mkCnc out parser morpho) cont
|
mapM_ (mkCnc out parser morpho) cont
|
||||||
@@ -60,8 +73,7 @@ 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 :: FilePath -> (String -> String -> ([Tree],String)) -> (String -> Bool) ->
|
mkCnc :: FilePath -> Parser -> Morpho -> String -> IO ()
|
||||||
String -> IO ()
|
|
||||||
mkCnc out parser morpho line = do
|
mkCnc out parser morpho line = do
|
||||||
let (res,msg) = mkCncLine parser morpho line
|
let (res,msg) = mkCncLine parser morpho line
|
||||||
appendFile out res
|
appendFile out res
|
||||||
@@ -77,15 +89,15 @@ mkCncLine parser morpho line = case words line of
|
|||||||
where
|
where
|
||||||
mkLinRule key s =
|
mkLinRule key s =
|
||||||
let
|
let
|
||||||
(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,subst) = span (/= '"') $ tail $ unwords rest
|
||||||
def
|
def
|
||||||
| last pre /= "=" = line -- ordinary lin rule
|
| last pre /= "=" = line -- ordinary lin rule
|
||||||
| otherwise = case parser cat lin of
|
| otherwise = case parser cat lin of
|
||||||
([t],_) -> ind ++ key +++ unwords pre +++ prt_ (tree2exp t) +++ ";"
|
(t:ts,_) -> ind ++ key +++ unwords pre +++
|
||||||
(t:_,_) -> ind ++ key +++ unwords pre +++ prt_ (tree2exp t) +++ ";"
|
doSubst (init (tail subst)) (tree2exp t) +++ ";" ++
|
||||||
+++ "-- AMBIGUOUS"
|
if null ts then [] else " -- AMBIGUOUS"
|
||||||
([],msg) -> "{-" ++ line ++++ morph lin ++++ "-}"
|
([],msg) -> "{-" ++ line ++++ morph lin ++++ "-}"
|
||||||
in
|
in
|
||||||
(def,def)
|
(def,def)
|
||||||
@@ -93,3 +105,13 @@ mkCncLine parser morpho line = case words line of
|
|||||||
[] -> ""
|
[] -> ""
|
||||||
ws -> "unknown words: " ++ unwords ws
|
ws -> "unknown words: " ++ unwords ws
|
||||||
ind = takeWhile isSpace line
|
ind = takeWhile isSpace line
|
||||||
|
|
||||||
|
doSubst :: String -> Term -> String
|
||||||
|
doSubst subst0 trm = prt_ $ subt subst trm where
|
||||||
|
subst
|
||||||
|
| all isSpace subst0 = []
|
||||||
|
| otherwise = err error id $ pTerm subst0 >>= record2subst
|
||||||
|
subt g t = case t of
|
||||||
|
Q _ c -> maybe t id $ lookup c g
|
||||||
|
QC _ c -> maybe t id $ lookup c g
|
||||||
|
_ -> composSafeOp (subt g) t
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/05/09 15:45:00 $
|
-- > CVS $Date: 2005/06/03 21:51:58 $
|
||||||
-- > CVS $Author: aarne $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.21 $
|
-- > CVS $Revision: 1.22 $
|
||||||
--
|
--
|
||||||
-- Macros for constructing and analysing source code terms.
|
-- Macros for constructing and analysing source code terms.
|
||||||
--
|
--
|
||||||
@@ -280,6 +280,11 @@ mkRecTypeN int lab typs = RecType [ (lab i, t) | (i,t) <- zip [int..] typs]
|
|||||||
mkRecType :: (Int -> Label) -> [Type] -> Type
|
mkRecType :: (Int -> Label) -> [Type] -> Type
|
||||||
mkRecType = mkRecTypeN 0
|
mkRecType = mkRecTypeN 0
|
||||||
|
|
||||||
|
record2subst :: Term -> Err Substitution
|
||||||
|
record2subst t = case t of
|
||||||
|
R fs -> return [(zIdent x, t) | (LIdent x,(_,t)) <- fs]
|
||||||
|
_ -> prtBad "record expected, found" t
|
||||||
|
|
||||||
typeType, typePType, typeStr, typeTok, typeStrs :: Term
|
typeType, typePType, typeStr, typeTok, typeStrs :: Term
|
||||||
|
|
||||||
typeType = srt "Type"
|
typeType = srt "Type"
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/06/02 10:23:52 $
|
-- > CVS $Date: 2005/06/03 21:51:59 $
|
||||||
-- > CVS $Author: aarne $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.28 $
|
-- > CVS $Revision: 1.29 $
|
||||||
--
|
--
|
||||||
-- Options and flags used in GF shell commands and files.
|
-- Options and flags used in GF shell commands and files.
|
||||||
--
|
--
|
||||||
@@ -244,7 +244,7 @@ nostripQualif = iOpt "nostrip"
|
|||||||
showAll = iOpt "all"
|
showAll = iOpt "all"
|
||||||
showMulti = iOpt "multi"
|
showMulti = iOpt "multi"
|
||||||
fromSource = iOpt "src"
|
fromSource = iOpt "src"
|
||||||
makeConcrete = iOpt "makeconcrete"
|
makeConcrete = iOpt "examples"
|
||||||
|
|
||||||
-- ** mainly for stand-alone
|
-- ** mainly for stand-alone
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/04/21 16:23:47 $
|
-- > CVS $Date: 2005/06/03 21:51:59 $
|
||||||
-- > CVS $Author: bringert $
|
-- > CVS $Author: aarne $
|
||||||
-- > CVS $Revision: 1.7 $
|
-- > CVS $Revision: 1.8 $
|
||||||
--
|
--
|
||||||
-- how to form linearizable trees from strings and from terms of different levels
|
-- how to form linearizable trees from strings and from terms of different levels
|
||||||
--
|
--
|
||||||
@@ -39,6 +39,7 @@ string2tree :: StateGrammar -> String -> Tree
|
|||||||
string2tree gr = errVal uTree . string2treeErr gr
|
string2tree gr = errVal uTree . string2treeErr gr
|
||||||
|
|
||||||
string2treeErr :: StateGrammar -> String -> Err Tree
|
string2treeErr :: StateGrammar -> String -> Err Tree
|
||||||
|
string2treeErr _ "" = Bad "empty string"
|
||||||
string2treeErr gr s = do
|
string2treeErr gr s = do
|
||||||
t <- pTerm s
|
t <- pTerm s
|
||||||
let t1 = refreshMetas [] t
|
let t1 = refreshMetas [] t
|
||||||
|
|||||||
Reference in New Issue
Block a user