1
0
forked from GitHub/gf-core

Second attempt. Reading enum is closer to working but all strings are empty.

This commit is contained in:
John J. Camilleri
2021-05-03 14:25:35 +02:00
parent 450368f9bb
commit e56d1b2959
2 changed files with 25 additions and 11 deletions

View File

@@ -977,30 +977,44 @@ complete :: Concr -- ^ the language with which we parse
-> String -- ^ the input sentence -> String -- ^ the input sentence
-> String -- ^ prefix (?) -> String -- ^ prefix (?)
-> Maybe Int -- ^ maximum number of results -> Maybe Int -- ^ maximum number of results
-> ParseOutput [(Expr,Float)] -> ParseOutput [String]
complete lang (Type ctype _) sent pfx mn = complete lang (Type ctype _) sent pfx mn =
unsafePerformIO $ do unsafePerformIO $ do
parsePl <- gu_new_pool parsePl <- gu_new_pool
exprPl <- gu_new_pool
exn <- gu_new_exn parsePl exn <- gu_new_exn parsePl
sent <- newUtf8CString sent parsePl sent <- newUtf8CString sent parsePl
pfx <- newUtf8CString pfx parsePl pfx <- newUtf8CString pfx parsePl
enum <- pgf_complete (concr lang) ctype sent pfx exn parsePl enum <- pgf_complete (concr lang) ctype sent pfx exn parsePl
failed <- gu_exn_is_raised exn failed <- gu_exn_is_raised exn
if failed if failed
then do then do
-- TODO better error handling, cleanup
is_parse_error <- gu_exn_caught exn gu_exn_type_PgfParseError is_parse_error <- gu_exn_caught exn gu_exn_type_PgfParseError
if is_parse_error if is_parse_error
then return (ParseFailed 0 "") then return (ParseFailed 0 "")
else throwIO (PGFError "Some other error") else throwIO (PGFError "Some other error")
-- TODO cleanup!!!
else do else do
parseFPl <- newForeignPtr gu_pool_finalizer parsePl fpl <- newForeignPtr gu_pool_finalizer parsePl
exprFPl <- newForeignPtr gu_pool_finalizer exprPl ParseOk <$> fromCompletions enum fpl
exprs <- fromPgfExprEnum enum parseFPl (touchConcr lang >> touchForeignPtr exprFPl) where
return (ParseOk exprs) fromCompletions :: Ptr GuEnum -> ForeignPtr GuPool -> IO [String]
fromCompletions enum fpl =
withGuPool $ \tmpPl -> do
cmpEntry <- alloca $ \ptr ->
withForeignPtr fpl $ \pl ->
do gu_enum_next enum ptr pl
peek ptr
if cmpEntry == nullPtr
then do
finalizeForeignPtr fpl
touchConcr lang
return []
else do
(sb,out) <- newOut tmpPl
cstr <- gu_string_buf_freeze sb tmpPl
tok <- peekUtf8CString cstr
toks <- unsafeInterleaveIO (fromCompletions enum fpl)
return (tok : toks)
-- | Returns True if there is a linearization defined for that function in that language -- | Returns True if there is a linearization defined for that function in that language
hasLinearization :: Concr -> Fun -> Bool hasLinearization :: Concr -> Fun -> Bool

View File

@@ -5,8 +5,8 @@ main :: IO ()
main = do main = do
pgf <- readPGF "/Users/john/repositories/GF/contrib/foods/Foods.pgf" pgf <- readPGF "/Users/john/repositories/GF/contrib/foods/Foods.pgf"
let Just concr = M.lookup "FoodsEng" (languages pgf) let Just concr = M.lookup "FoodsEng" (languages pgf)
let pr = complete concr (startCat pgf) "this" "wi" Nothing let pr = complete concr (startCat pgf) "" "th" Nothing
case pr of case pr of
ParseOk x -> print (head x) ParseOk x -> print x
ParseFailed _ _ -> putStrLn "parse failed" ParseFailed _ _ -> putStrLn "parse failed"
ParseIncomplete -> putStrLn "input incomplete" ParseIncomplete -> putStrLn "input incomplete"