added startCat

This commit is contained in:
krangelov
2021-08-12 12:39:05 +02:00
parent 01db0224be
commit 39f38ed0e2
5 changed files with 59 additions and 5 deletions

View File

@@ -161,13 +161,47 @@ PgfText *pgf_abstract_name(PgfPGF* pgf)
}
PGF_API
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor)
void pgf_iter_categories(PgfPGF *pgf, PgfItor *itor)
{
DB_scope scope(pgf, READER_SCOPE);
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.cats, itor);
}
PGF_API
uintptr_t pgf_start_cat(PgfPGF *pgf)
{
DB_scope scope(pgf, READER_SCOPE);
PgfText *startcat = (PgfText *)
alloca(sizeof(PgfText)+9);
startcat->size = 8;
strcpy(startcat->text, "startcat");
ref<PgfFlag> flag =
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.aflags, startcat);
if (flag != 0) {
switch (ref<PgfLiteral>::get_tag(flag->value)) {
case PgfLiteralStr::tag: {
auto lstr = ref<PgfLiteralStr>::untagged(flag->value);
uintptr_t type = pgf_read_type(&lstr->val, pgf->u);
if (type == 0)
break;
return type;
}
}
}
PgfText *s = (PgfText *)
alloca(sizeof(PgfText)+2);
s->size = 1;
s->text[0] = 'S';
s->text[1] = 0;
return pgf->u->dtyp(0,NULL,s,0,NULL);
}
PGF_API
PgfTypeHypo *pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos)
{

View File

@@ -157,10 +157,13 @@ PGF_API_DECL
void pgf_free(PgfPGF *pgf);
PGF_API_DECL
PgfText *pgf_abstract_name(PgfPGF* pgf);
PgfText *pgf_abstract_name(PgfPGF *pgf);
PGF_API_DECL
void pgf_iter_categories(PgfPGF* pgf, PgfItor* itor);
void pgf_iter_categories(PgfPGF *pgf, PgfItor *itor);
PGF_API_DECL
uintptr_t pgf_start_cat(PgfPGF *pgf);
PGF_API_DECL PgfTypeHypo*
pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos);

View File

@@ -3,7 +3,7 @@ module PGF ( PGF2.PGF, readPGF
, CId, mkCId, wildCId, showCId, readCId
, categories
, PGF2.categories, PGF2.categoryContext, PGF2.startCat
, functions, functionsByCat
, PGF2.Expr(..), PGF2.Literal(..), Tree

View File

@@ -33,7 +33,7 @@ module PGF2 (-- * PGF
mkFloat, unFloat,
mkMeta, unMeta,
-- ** Types
Type(..), Hypo, BindType(..),
Type(..), Hypo, BindType(..), startCat,
readType,
mkType, unType,
mkHypo, mkDepHypo, mkImplHypo,
@@ -139,6 +139,20 @@ abstractName p =
bracket (pgf_abstract_name p_pgf) free $ \c_text ->
peekText c_text
-- | The start category is defined in the grammar with
-- the \'startcat\' flag. This is usually the sentence category
-- but it is not necessary. Despite that there is a start category
-- defined you can parse with any category. The start category
-- definition is just for convenience.
startCat :: PGF -> Type
startCat p =
unsafePerformIO $
withForeignPtr (a_pgf p) $ \c_pgf -> do
c_typ <- pgf_start_cat c_pgf
typ <- deRefStablePtr c_typ
freeStablePtr c_typ
return typ
-- | The type of a function
functionType :: PGF -> Fun -> Maybe Type
functionType p fn =

View File

@@ -63,6 +63,9 @@ foreign import ccall "wrapper"
foreign import ccall "pgf_iter_categories"
pgf_iter_categories :: Ptr PgfPGF -> Ptr PgfItor -> IO ()
foreign import ccall "pgf_start_cat"
pgf_start_cat :: Ptr PgfPGF -> IO (StablePtr Type)
foreign import ccall "pgf/pgf.h pgf_category_context"
pgf_category_context :: Ptr PgfPGF -> Ptr PgfText -> Ptr CSize -> IO (Ptr PgfTypeHypo)