mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-27 11:48:55 -06:00
bugfix and tiny optimization for callbacks from Haskell
This commit is contained in:
@@ -318,11 +318,8 @@ mkCallbacksMap concr callbacks pool = do
|
|||||||
where
|
where
|
||||||
match_callback match _ clin_idx csentence poffset out_pool = do
|
match_callback match _ clin_idx csentence poffset out_pool = do
|
||||||
sentence <- peekCString csentence
|
sentence <- peekCString csentence
|
||||||
coffset <- peek poffset
|
coffset <- peek poffset
|
||||||
offset <- alloca $ \pcsentence -> do
|
case match (fromIntegral clin_idx) sentence (fromIntegral coffset) of
|
||||||
poke pcsentence csentence
|
|
||||||
gu2hs_string_offset pcsentence (plusPtr csentence (fromIntegral coffset)) 0
|
|
||||||
case match (fromIntegral clin_idx) sentence offset of
|
|
||||||
Nothing -> return nullPtr
|
Nothing -> return nullPtr
|
||||||
Just (e,prob,offset') -> do poke poffset (fromIntegral offset')
|
Just (e,prob,offset') -> do poke poffset (fromIntegral offset')
|
||||||
|
|
||||||
@@ -345,13 +342,6 @@ mkCallbacksMap concr callbacks pool = do
|
|||||||
|
|
||||||
predict_callback _ _ _ _ = return nullPtr
|
predict_callback _ _ _ _ = return nullPtr
|
||||||
|
|
||||||
gu2hs_string_offset pcstart cend offset = do
|
|
||||||
cstart <- peek pcstart
|
|
||||||
if cstart < cend
|
|
||||||
then do gu_utf8_decode pcstart
|
|
||||||
gu2hs_string_offset pcstart cend (offset+1)
|
|
||||||
else return offset
|
|
||||||
|
|
||||||
linearize :: Concr -> Expr -> String
|
linearize :: Concr -> Expr -> String
|
||||||
linearize lang e = unsafePerformIO $
|
linearize lang e = unsafePerformIO $
|
||||||
withGuPool $ \pl ->
|
withGuPool $ \pl ->
|
||||||
|
|||||||
@@ -72,9 +72,6 @@ foreign import ccall "gu/enum.h gu_enum_next"
|
|||||||
foreign import ccall "gu/string.h gu_string_buf_freeze"
|
foreign import ccall "gu/string.h gu_string_buf_freeze"
|
||||||
gu_string_buf_freeze :: Ptr GuStringBuf -> Ptr GuPool -> IO CString
|
gu_string_buf_freeze :: Ptr GuStringBuf -> Ptr GuPool -> IO CString
|
||||||
|
|
||||||
foreign import ccall "gu/utf8.h gu_utf8_decode"
|
|
||||||
gu_utf8_decode :: Ptr (Ptr CChar) -> IO ()
|
|
||||||
|
|
||||||
withGuPool :: (Ptr GuPool -> IO a) -> IO a
|
withGuPool :: (Ptr GuPool -> IO a) -> IO a
|
||||||
withGuPool f = bracket gu_new_pool gu_pool_free f
|
withGuPool f = bracket gu_new_pool gu_pool_free f
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,55 @@
|
|||||||
#include <HsFFI.h>
|
#include <HsFFI.h>
|
||||||
#include <pgf/pgf.h>
|
#include <pgf/pgf.h>
|
||||||
|
#include <gu/utf8.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PgfLiteralCallback callback;
|
PgfLiteralCallback callback;
|
||||||
|
PgfExprProb* (*match)(PgfLiteralCallback* self,
|
||||||
|
size_t lin_idx,
|
||||||
|
GuString sentence, size_t* poffset,
|
||||||
|
GuPool *out_pool);
|
||||||
GuFinalizer fin;
|
GuFinalizer fin;
|
||||||
} HSPgfLiteralCallback;
|
} HSPgfLiteralCallback;
|
||||||
|
|
||||||
static void
|
static PgfExprProb*
|
||||||
|
hspgf_match_callback(PgfLiteralCallback* self,
|
||||||
|
size_t lin_idx,
|
||||||
|
GuString sentence, size_t* poffset,
|
||||||
|
GuPool *out_pool)
|
||||||
|
{
|
||||||
|
HSPgfLiteralCallback* callback = (HSPgfLiteralCallback*) self;
|
||||||
|
size_t offset = *poffset;
|
||||||
|
|
||||||
|
const uint8_t *start = sentence;
|
||||||
|
const uint8_t *end = sentence + offset;
|
||||||
|
size_t hs_offset = 0;
|
||||||
|
while (start < end) {
|
||||||
|
gu_utf8_decode(&start);
|
||||||
|
hs_offset++;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExprProb* ep =
|
||||||
|
callback->match(self, lin_idx, sentence, &hs_offset, out_pool);
|
||||||
|
|
||||||
|
start = sentence;
|
||||||
|
end = start;
|
||||||
|
while (hs_offset > 0) {
|
||||||
|
gu_utf8_decode(&end);
|
||||||
|
hs_offset--;
|
||||||
|
}
|
||||||
|
|
||||||
|
*poffset = (end - start);
|
||||||
|
|
||||||
|
return ep;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
hspgf_literal_callback_fin(GuFinalizer* self)
|
hspgf_literal_callback_fin(GuFinalizer* self)
|
||||||
{
|
{
|
||||||
HSPgfLiteralCallback* callback = gu_container(self, HSPgfLiteralCallback, fin);
|
HSPgfLiteralCallback* callback = gu_container(self, HSPgfLiteralCallback, fin);
|
||||||
|
|
||||||
if (callback->callback.match != NULL)
|
if (callback->callback.match != NULL)
|
||||||
hs_free_fun_ptr((HsFunPtr) callback->callback.match);
|
hs_free_fun_ptr((HsFunPtr) callback->match);
|
||||||
if (callback->callback.predict != NULL)
|
if (callback->callback.predict != NULL)
|
||||||
hs_free_fun_ptr((HsFunPtr) callback->callback.predict);
|
hs_free_fun_ptr((HsFunPtr) callback->callback.predict);
|
||||||
}
|
}
|
||||||
@@ -23,8 +60,9 @@ hspgf_callbacks_map_add_literal(PgfConcr* concr, PgfCallbacksMap* callbacks,
|
|||||||
GuPool* pool)
|
GuPool* pool)
|
||||||
{
|
{
|
||||||
HSPgfLiteralCallback* callback = gu_new(HSPgfLiteralCallback, pool);
|
HSPgfLiteralCallback* callback = gu_new(HSPgfLiteralCallback, pool);
|
||||||
callback->callback.match = (void*) match;
|
callback->callback.match = hspgf_match_callback;
|
||||||
callback->callback.predict = (void*) predict;
|
callback->callback.predict = (void*) predict;
|
||||||
|
callback->match = (void*) match;
|
||||||
callback->fin.fn = hspgf_literal_callback_fin;
|
callback->fin.fn = hspgf_literal_callback_fin;
|
||||||
gu_pool_finally(pool, &callback->fin);
|
gu_pool_finally(pool, &callback->fin);
|
||||||
pgf_callbacks_map_add_literal(concr, callbacks, cat, &callback->callback);
|
pgf_callbacks_map_add_literal(concr, callbacks, cat, &callback->callback);
|
||||||
|
|||||||
Reference in New Issue
Block a user