forked from GitHub/gf-core
Reimplement a lexer in C for the C bindings not to depend on GF.Text.Lexing for now.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
-- GF C Bindings
|
||||
-- Copyright (C) 2008-2009 Kevin Kofler
|
||||
-- Copyright (C) 2008-2010 Kevin Kofler
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or
|
||||
-- modify it under the terms of the GNU Lesser General Public
|
||||
@@ -24,7 +24,7 @@ import Foreign.C.Types
|
||||
import Control.Exception
|
||||
import IO
|
||||
import Data.Maybe
|
||||
import GF.Text.Lexing
|
||||
-- import GF.Text.Lexing
|
||||
|
||||
|
||||
-- Utility functions used in the implementation (not exported):
|
||||
@@ -217,13 +217,13 @@ gf_parse pgf lang cat input = do
|
||||
|
||||
-- GF.Text.Lexing:
|
||||
|
||||
foreign export ccall gf_stringOp :: CString -> CString -> IO CString
|
||||
gf_stringOp op str = do
|
||||
o <- (peekCString op)
|
||||
s <- (peekCString str)
|
||||
case (stringOp o) of
|
||||
Just fn -> (newCString (fn s))
|
||||
Nothing -> (return nullPtr)
|
||||
-- foreign export ccall gf_stringOp :: CString -> CString -> IO CString
|
||||
-- gf_stringOp op str = do
|
||||
-- o <- (peekCString op)
|
||||
-- s <- (peekCString str)
|
||||
-- case (stringOp o) of
|
||||
-- Just fn -> (newCString (fn s))
|
||||
-- Nothing -> (return nullPtr)
|
||||
|
||||
|
||||
-- Unused (exception handling):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
# GF C Bindings
|
||||
# Copyright (C) 2008-2009 Kevin Kofler
|
||||
# Copyright (C) 2008-2010 Kevin Kofler
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
@@ -17,4 +17,4 @@
|
||||
# License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
ghc --make -fglasgow-exts -O2 -no-hs-main $* -c PGFFFI.hs &&
|
||||
ghc --make -fglasgow-exts -O2 -no-hs-main $* gfctest.c PGFFFI.hs -o gfctest
|
||||
ghc --make -fglasgow-exts -O2 -no-hs-main $* gfctest.c gf_lexing.c PGFFFI.hs -o gfctest
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "pgf.h"
|
||||
#include "gf_lexing.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@@ -26,7 +27,7 @@ int main(int argc, char *argv[])
|
||||
GF_PGF pgf = gf_readPGF("../examples/tutorial/embedded/Query.pgf");
|
||||
GF_Language lang = gf_readLanguage("QueryEng");
|
||||
GF_Type cat = gf_startCat(pgf);
|
||||
char *lexed = gf_stringOp("lextext", "Is 2 prime");
|
||||
char *lexed = gf_stringOp("lextext")("Is 2 prime");
|
||||
GF_Tree *result = gf_parse(pgf, lang, cat, lexed);
|
||||
free(lexed);
|
||||
GF_Tree *p = result;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
GF C Bindings
|
||||
Copyright (C) 2008-2009 Kevin Kofler
|
||||
Copyright (C) 2008-2010 Kevin Kofler
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -109,10 +109,12 @@ void gf_freeTrees(GF_Tree *p);
|
||||
|
||||
|
||||
|
||||
In addition, the following function from GF.Text.Lexing:
|
||||
In addition, a C equivalent to the following function from GF.Text.Lexing:
|
||||
stringOp :: String -> Maybe (String -> String)
|
||||
is wrapped as:
|
||||
char *gf_stringOp(char *op, char *str)
|
||||
which returns NULL if op is not a valid operation name, otherwise applies the
|
||||
function corresponding to op to the string str. The resulting string must be
|
||||
freed with free if non-NULL.
|
||||
is provided as:
|
||||
typedef char *(*GF_StringOp)(const char *str);
|
||||
GF_StringOp gf_stringOp(const char *op); /* may return NULL */
|
||||
which returns NULL if op is not a valid operation name, otherwise a pointer to a
|
||||
function which applies the function corresponding to op to the string str. The
|
||||
resulting string must be freed with free. The function pointer MUST NOT be
|
||||
freed.
|
||||
|
||||
Reference in New Issue
Block a user