mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-27 11:48:55 -06:00
added commands create cat & drop cat
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
module GF.Command.Abstract(module GF.Command.Abstract,Expr,showExpr,Literal(..),Term) where
|
module GF.Command.Abstract(module GF.Command.Abstract,Expr,showExpr,Literal(..),Term) where
|
||||||
|
|
||||||
import PGF2(Expr,showExpr,Literal(..),Type)
|
import PGF2
|
||||||
import GF.Grammar.Grammar(Term)
|
import GF.Grammar.Grammar(Term)
|
||||||
|
|
||||||
type Ident = String
|
type Ident = String
|
||||||
@@ -14,8 +14,10 @@ data Command
|
|||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
data TransactionCommand
|
data TransactionCommand
|
||||||
= CreateFun [Option] Ident Type
|
= CreateFun [Option] Fun Type
|
||||||
| DropFun [Option] Ident
|
| CreateCat [Option] Cat [Hypo]
|
||||||
|
| DropFun [Option] Fun
|
||||||
|
| DropCat [Option] Cat
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
data Option
|
data Option
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module GF.Command.Parse(readCommandLine, readTransactionCommand, pCommand) where
|
module GF.Command.Parse(readCommandLine, readTransactionCommand, pCommand) where
|
||||||
|
|
||||||
import PGF(pExpr,pIdent)
|
import PGF(pExpr,pIdent)
|
||||||
import PGF2(readType)
|
import PGF2(readType,readContext)
|
||||||
import GF.Grammar.Parser(runPartial,pTerm)
|
import GF.Grammar.Parser(runPartial,pTerm)
|
||||||
import GF.Command.Abstract
|
import GF.Command.Abstract
|
||||||
|
|
||||||
@@ -63,6 +63,16 @@ pTransactionCommand = do
|
|||||||
| take 1 cmd == "d" -> do
|
| take 1 cmd == "d" -> do
|
||||||
f <- pIdent
|
f <- pIdent
|
||||||
return (DropFun opts f)
|
return (DropFun opts f)
|
||||||
|
"cat" | take 1 cmd == "c" -> do
|
||||||
|
c <- pIdent
|
||||||
|
skipSpaces
|
||||||
|
ctxt <- readS_to_P (\s -> case readContext s of
|
||||||
|
Just ty -> [(ty,"")]
|
||||||
|
Nothing -> [])
|
||||||
|
return (CreateCat opts c ctxt)
|
||||||
|
| take 1 cmd == "d" -> do
|
||||||
|
c <- pIdent
|
||||||
|
return (DropCat opts c)
|
||||||
_ -> pfail
|
_ -> pfail
|
||||||
|
|
||||||
pOption = do
|
pOption = do
|
||||||
|
|||||||
@@ -236,9 +236,18 @@ transactionCommand (CreateFun opts f ty) pgf = do
|
|||||||
Left msg -> putStrLnE msg
|
Left msg -> putStrLnE msg
|
||||||
Right ty -> do lift $ modifyPGF pgf (createFunction f ty 0 [] prob)
|
Right ty -> do lift $ modifyPGF pgf (createFunction f ty 0 [] prob)
|
||||||
return ()
|
return ()
|
||||||
|
transactionCommand (CreateCat opts c ctxt) pgf = do
|
||||||
|
let prob = realToFrac (valFltOpts "prob" (1/0) opts)
|
||||||
|
case checkContext pgf ctxt of
|
||||||
|
Left msg -> putStrLnE msg
|
||||||
|
Right ty -> do lift $ modifyPGF pgf (createCategory c ctxt prob)
|
||||||
|
return ()
|
||||||
transactionCommand (DropFun opts f) pgf = do
|
transactionCommand (DropFun opts f) pgf = do
|
||||||
lift $ modifyPGF pgf (dropFunction f)
|
lift $ modifyPGF pgf (dropFunction f)
|
||||||
return ()
|
return ()
|
||||||
|
transactionCommand (DropCat opts c) pgf = do
|
||||||
|
lift $ modifyPGF pgf (dropCategory c)
|
||||||
|
return ()
|
||||||
|
|
||||||
-- | Commands that work on 'GFEnv'
|
-- | Commands that work on 'GFEnv'
|
||||||
moreCommands = [
|
moreCommands = [
|
||||||
|
|||||||
@@ -1025,6 +1025,72 @@ exit:
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PgfTypeHypo *PgfExprParser::parse_context(size_t *p_n_hypos)
|
||||||
|
{
|
||||||
|
size_t n_hypos = 0;
|
||||||
|
PgfTypeHypo *hypos = NULL;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
if (token_tag == PGF_TOKEN_LPAR) {
|
||||||
|
token();
|
||||||
|
|
||||||
|
size_t n_start = n_hypos;
|
||||||
|
|
||||||
|
if ((token_tag == PGF_TOKEN_IDENT &&
|
||||||
|
(lookahead(',') ||
|
||||||
|
lookahead(':'))) ||
|
||||||
|
(token_tag == PGF_TOKEN_LCURLY) ||
|
||||||
|
(token_tag == PGF_TOKEN_WILD)) {
|
||||||
|
|
||||||
|
if (!parse_hypos(&n_hypos, &hypos))
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
if (token_tag != PGF_TOKEN_COLON)
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
token();
|
||||||
|
} else {
|
||||||
|
hypos = (PgfTypeHypo*) realloc(hypos, sizeof(PgfTypeHypo)*(n_hypos+1));
|
||||||
|
PgfTypeHypo *bt = &hypos[n_hypos];
|
||||||
|
bt->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||||
|
bt->cid = mk_wildcard();
|
||||||
|
bt->type = 0;
|
||||||
|
n_hypos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t n_end = n_hypos;
|
||||||
|
|
||||||
|
PgfType type = parse_type();
|
||||||
|
if (type == 0)
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
if (token_tag != PGF_TOKEN_RPAR)
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
token();
|
||||||
|
|
||||||
|
for (size_t i = n_start; i < n_end; i++) {
|
||||||
|
hypos[i].type = type;
|
||||||
|
}
|
||||||
|
} else if (token_tag == PGF_TOKEN_IDENT) {
|
||||||
|
hypos = (PgfTypeHypo*) realloc(hypos, sizeof(PgfTypeHypo)*(n_hypos+1));
|
||||||
|
PgfTypeHypo *bt = &hypos[n_hypos];
|
||||||
|
bt->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||||
|
bt->cid = mk_wildcard();
|
||||||
|
bt->type = u->dtyp(0,NULL,token_value,0,NULL);
|
||||||
|
n_hypos++;
|
||||||
|
|
||||||
|
token();
|
||||||
|
} else {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exit:
|
||||||
|
*p_n_hypos = n_hypos;
|
||||||
|
return hypos;
|
||||||
|
}
|
||||||
|
|
||||||
PgfExpr PgfExprProbEstimator::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body)
|
PgfExpr PgfExprProbEstimator::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body)
|
||||||
{
|
{
|
||||||
m->match_expr(this, body);
|
m->match_expr(this, body);
|
||||||
|
|||||||
@@ -185,6 +185,7 @@ public:
|
|||||||
|
|
||||||
bool parse_hypos(size_t *n_hypos, PgfTypeHypo **hypos);
|
bool parse_hypos(size_t *n_hypos, PgfTypeHypo **hypos);
|
||||||
PgfType parse_type();
|
PgfType parse_type();
|
||||||
|
PgfTypeHypo *parse_context(size_t *p_n_hypos);
|
||||||
|
|
||||||
bool eof();
|
bool eof();
|
||||||
|
|
||||||
|
|||||||
@@ -718,6 +718,22 @@ PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PGF_API
|
||||||
|
PgfTypeHypo *pgf_read_context(PgfText *input, PgfUnmarshaller *u, size_t *n_hypos)
|
||||||
|
{
|
||||||
|
PgfExprParser parser(input, u);
|
||||||
|
PgfTypeHypo *res = parser.parse_context(n_hypos);
|
||||||
|
if (!parser.eof()) {
|
||||||
|
for (size_t i = 0; i < *n_hypos; i++) {
|
||||||
|
free(res[i].cid);
|
||||||
|
u->free_ref(res[i].type);
|
||||||
|
}
|
||||||
|
*n_hypos = (size_t) -1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
PGF_API
|
PGF_API
|
||||||
PgfText *pgf_print_category_internal(object o)
|
PgfText *pgf_print_category_internal(object o)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -376,7 +376,10 @@ PgfText *pgf_print_context(size_t n_hypos, PgfTypeHypo *hypos,
|
|||||||
PGF_API_DECL
|
PGF_API_DECL
|
||||||
PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u);
|
PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u);
|
||||||
|
|
||||||
PGF_API
|
PGF_API_DECL
|
||||||
|
PgfTypeHypo *pgf_read_context(PgfText *input, PgfUnmarshaller *u, size_t *n_hypos);
|
||||||
|
|
||||||
|
PGF_API_DECL
|
||||||
PgfText *pgf_print_start_cat_internal(PgfDB *db, PgfRevision revision, PgfExn *err);
|
PgfText *pgf_print_start_cat_internal(PgfDB *db, PgfRevision revision, PgfExn *err);
|
||||||
|
|
||||||
PGF_API_DECL
|
PGF_API_DECL
|
||||||
|
|||||||
@@ -41,14 +41,14 @@ module PGF2 (-- * PGF
|
|||||||
|
|
||||||
-- ** Types
|
-- ** Types
|
||||||
Type(..), Hypo, BindType(..), startCat,
|
Type(..), Hypo, BindType(..), startCat,
|
||||||
readType, showType, showContext,
|
readType, showType, readContext, showContext,
|
||||||
mkType, unType,
|
mkType, unType,
|
||||||
mkHypo, mkDepHypo, mkImplHypo,
|
mkHypo, mkDepHypo, mkImplHypo,
|
||||||
|
|
||||||
-- ** Type checking
|
-- ** Type checking
|
||||||
-- | Dynamically-built expressions should always be type-checked before using in other functions,
|
-- | Dynamically-built expressions should always be type-checked before using in other functions,
|
||||||
-- as the exceptions thrown by using invalid expressions may not catchable.
|
-- as the exceptions thrown by using invalid expressions may not catchable.
|
||||||
checkExpr, inferExpr, checkType,
|
checkExpr, inferExpr, checkType, checkContext,
|
||||||
|
|
||||||
-- ** Computing
|
-- ** Computing
|
||||||
compute,
|
compute,
|
||||||
@@ -407,6 +407,11 @@ inferExpr p e =
|
|||||||
checkType :: PGF -> Type -> Either String Type
|
checkType :: PGF -> Type -> Either String Type
|
||||||
checkType pgf ty = Right ty
|
checkType pgf ty = Right ty
|
||||||
|
|
||||||
|
-- | Check whether a context is consistent with the abstract
|
||||||
|
-- syntax of the grammar.
|
||||||
|
checkContext :: PGF -> [Hypo] -> Either String [Hypo]
|
||||||
|
checkContext pgf ctxt = Right ctxt
|
||||||
|
|
||||||
compute :: PGF -> Expr -> Expr
|
compute :: PGF -> Expr -> Expr
|
||||||
compute = error "TODO: compute"
|
compute = error "TODO: compute"
|
||||||
|
|
||||||
@@ -1334,6 +1339,38 @@ readType str =
|
|||||||
freeStablePtr c_ty
|
freeStablePtr c_ty
|
||||||
return (Just ty)
|
return (Just ty)
|
||||||
|
|
||||||
|
readContext :: String -> Maybe [Hypo]
|
||||||
|
readContext str =
|
||||||
|
unsafePerformIO $
|
||||||
|
withText str $ \c_str ->
|
||||||
|
withForeignPtr unmarshaller $ \u ->
|
||||||
|
alloca $ \p_n_hypos -> do
|
||||||
|
c_hypos <- pgf_read_context c_str u p_n_hypos
|
||||||
|
n_hypos <- peek p_n_hypos
|
||||||
|
if c_hypos == nullPtr && n_hypos /= 0
|
||||||
|
then return Nothing
|
||||||
|
else do hypos <- peekHypos (castPtrToStablePtr nullPtr) n_hypos c_hypos
|
||||||
|
free c_hypos
|
||||||
|
return (Just hypos)
|
||||||
|
where
|
||||||
|
peekHypos last 0 p_hypo = do
|
||||||
|
if last /= castPtrToStablePtr nullPtr
|
||||||
|
then freeStablePtr last
|
||||||
|
else return ()
|
||||||
|
return []
|
||||||
|
peekHypos last n_hypos p_hypo = do
|
||||||
|
bt <- fmap unmarshalBindType ((#peek PgfTypeHypo, bind_type) p_hypo)
|
||||||
|
c_cid <- (#peek PgfTypeHypo, cid) p_hypo
|
||||||
|
cid <- peekText c_cid
|
||||||
|
free c_cid
|
||||||
|
c_ty <- (#peek PgfTypeHypo, type) p_hypo
|
||||||
|
ty <- deRefStablePtr c_ty
|
||||||
|
if last /= c_ty
|
||||||
|
then freeStablePtr last
|
||||||
|
else return ()
|
||||||
|
hs <- peekHypos c_ty (n_hypos-1) (p_hypo `plusPtr` (#size PgfTypeHypo))
|
||||||
|
return ((bt,cid,ty):hs)
|
||||||
|
|
||||||
readProbabilitiesFromFile :: FilePath -> IO (Map.Map String Double)
|
readProbabilitiesFromFile :: FilePath -> IO (Map.Map String Double)
|
||||||
readProbabilitiesFromFile fpath = do
|
readProbabilitiesFromFile fpath = do
|
||||||
s <- readFile fpath
|
s <- readFile fpath
|
||||||
|
|||||||
@@ -96,8 +96,9 @@ foreign import ccall "pgf_print_type"
|
|||||||
|
|
||||||
foreign import ccall pgf_print_context :: CSize -> Ptr PgfTypeHypo -> Ptr PgfPrintContext -> CInt -> Ptr PgfMarshaller -> IO (Ptr PgfText)
|
foreign import ccall pgf_print_context :: CSize -> Ptr PgfTypeHypo -> Ptr PgfPrintContext -> CInt -> Ptr PgfMarshaller -> IO (Ptr PgfText)
|
||||||
|
|
||||||
foreign import ccall "pgf_read_type"
|
foreign import ccall pgf_read_type :: Ptr PgfText -> Ptr PgfUnmarshaller -> IO (StablePtr Type)
|
||||||
pgf_read_type :: Ptr PgfText -> Ptr PgfUnmarshaller -> IO (StablePtr Type)
|
|
||||||
|
foreign import ccall pgf_read_context :: Ptr PgfText -> Ptr PgfUnmarshaller -> Ptr CSize -> IO (Ptr PgfTypeHypo)
|
||||||
|
|
||||||
foreign import ccall pgf_print_start_cat_internal :: Ptr PgfDB -> Ptr PGF -> Ptr PgfExn -> IO (Ptr PgfText)
|
foreign import ccall pgf_print_start_cat_internal :: Ptr PgfDB -> Ptr PGF -> Ptr PgfExn -> IO (Ptr PgfText)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user