mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 19:22:50 -06:00
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
|
-- 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
|
-- This library is free software; you can redistribute it and/or
|
||||||
-- modify it under the terms of the GNU Lesser General Public
|
-- modify it under the terms of the GNU Lesser General Public
|
||||||
@@ -24,7 +24,7 @@ import Foreign.C.Types
|
|||||||
import Control.Exception
|
import Control.Exception
|
||||||
import IO
|
import IO
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import GF.Text.Lexing
|
-- import GF.Text.Lexing
|
||||||
|
|
||||||
|
|
||||||
-- Utility functions used in the implementation (not exported):
|
-- Utility functions used in the implementation (not exported):
|
||||||
@@ -217,13 +217,13 @@ gf_parse pgf lang cat input = do
|
|||||||
|
|
||||||
-- GF.Text.Lexing:
|
-- GF.Text.Lexing:
|
||||||
|
|
||||||
foreign export ccall gf_stringOp :: CString -> CString -> IO CString
|
-- foreign export ccall gf_stringOp :: CString -> CString -> IO CString
|
||||||
gf_stringOp op str = do
|
-- gf_stringOp op str = do
|
||||||
o <- (peekCString op)
|
-- o <- (peekCString op)
|
||||||
s <- (peekCString str)
|
-- s <- (peekCString str)
|
||||||
case (stringOp o) of
|
-- case (stringOp o) of
|
||||||
Just fn -> (newCString (fn s))
|
-- Just fn -> (newCString (fn s))
|
||||||
Nothing -> (return nullPtr)
|
-- Nothing -> (return nullPtr)
|
||||||
|
|
||||||
|
|
||||||
-- Unused (exception handling):
|
-- Unused (exception handling):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# GF C Bindings
|
# 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
|
# This library is free software; you can redistribute it and/or
|
||||||
# modify it under the terms of the GNU Lesser General Public
|
# 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/>.
|
# 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 $* -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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "pgf.h"
|
#include "pgf.h"
|
||||||
|
#include "gf_lexing.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
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_PGF pgf = gf_readPGF("../examples/tutorial/embedded/Query.pgf");
|
||||||
GF_Language lang = gf_readLanguage("QueryEng");
|
GF_Language lang = gf_readLanguage("QueryEng");
|
||||||
GF_Type cat = gf_startCat(pgf);
|
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);
|
GF_Tree *result = gf_parse(pgf, lang, cat, lexed);
|
||||||
free(lexed);
|
free(lexed);
|
||||||
GF_Tree *p = result;
|
GF_Tree *p = result;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
GF C Bindings
|
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
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
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)
|
stringOp :: String -> Maybe (String -> String)
|
||||||
is wrapped as:
|
is provided as:
|
||||||
char *gf_stringOp(char *op, char *str)
|
typedef char *(*GF_StringOp)(const char *str);
|
||||||
which returns NULL if op is not a valid operation name, otherwise applies the
|
GF_StringOp gf_stringOp(const char *op); /* may return NULL */
|
||||||
function corresponding to op to the string str. The resulting string must be
|
which returns NULL if op is not a valid operation name, otherwise a pointer to a
|
||||||
freed with free if non-NULL.
|
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