Work in progress
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@525 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a24ba76d41
commit
76c442982f
7 changed files with 177 additions and 137 deletions
|
|
@ -394,6 +394,7 @@ yylex1(void) {
|
|||
if (strcmp(yytext,"%type") == 0) return (TYPE);
|
||||
if (strcmp(yytext,"%init") == 0) return(INIT);
|
||||
if (strcmp(yytext,"%wrapper") == 0) return(WRAPPER);
|
||||
if (strcmp(yytext,"%runtime") == 0) return(RUNTIME);
|
||||
if (strcmp(yytext,"%readonly") == 0) return(READONLY);
|
||||
if (strcmp(yytext,"%readwrite") == 0) return(READWRITE);
|
||||
if (strcmp(yytext,"%name") == 0) return(NAME);
|
||||
|
|
|
|||
|
|
@ -234,7 +234,7 @@ static int promote(int t1, int t2) {
|
|||
/* SWIG directives */
|
||||
%token <tok> ADDMETHODS ALPHA_MODE APPLY CHECKOUT CLEAR CONSTANT DOCONLY DOC_DISABLE DOC_ENABLE ECHO EXCEPT
|
||||
%token <tok> ILLEGAL IMPORT INCLUDE INIT INLINE LOCALSTYLE MACRO MODULE NAME NATIVE NEW PRAGMA
|
||||
%token <tok> RAW_MODE READONLY READWRITE RENAME SECTION STYLE SUBSECTION SUBSUBSECTION TEXT TITLE
|
||||
%token <tok> RAW_MODE READONLY READWRITE RENAME RUNTIME SECTION STYLE SUBSECTION SUBSUBSECTION TEXT TITLE
|
||||
%token <tok> TYPE TYPEMAP USERDIRECTIVE WEXTERN WRAPPER MAP
|
||||
|
||||
/* Operators */
|
||||
|
|
@ -455,6 +455,10 @@ code_block : HBLOCK {
|
|||
Seek(pp,0,SEEK_SET);
|
||||
LParse_push(pp);
|
||||
}
|
||||
| RUNTIME HBLOCK {
|
||||
$$ = new_node("runtimeblock",$2.filename,$2.line);
|
||||
Setattr($$,"code",$2.text);
|
||||
}
|
||||
;
|
||||
|
||||
/* -- Documentation directives -- */
|
||||
|
|
@ -872,6 +876,9 @@ map_element : variable_decl map_element {
|
|||
}
|
||||
| STRING COLON LBRACE {
|
||||
DOH *text = LParse_skip_balanced('{','}');
|
||||
Delitem(text,0);
|
||||
Delitem(text,DOH_END);
|
||||
|
||||
$$ = new_node("maprule",$1.filename, $1.line);
|
||||
Setattr($$,ATTR_NAME,$1.text);
|
||||
Setattr($$,"code",text);
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ CC = @CC@
|
|||
AR = @AR@
|
||||
RANLIB = @RANLIB@
|
||||
CFLAGS = @CFLAGS@
|
||||
INCLUDE = -I$(srcdir)/. -I$(srcdir)/../DOH/Include -I$(srcdir)/../Swig
|
||||
INCLUDE = -I$(srcdir)/. -I$(srcdir)/../DOH/Include -I$(srcdir)/../Swig -I$(srcdir)/../Preprocessor -I$(srcdir)/../LParse
|
||||
TARGET = libmodules.a
|
||||
|
||||
.c.o:
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
#include "swig.h"
|
||||
#include "preprocessor.h"
|
||||
#include "lparse.h"
|
||||
|
||||
/* -------- Module variables ------- */
|
||||
|
||||
|
|
@ -18,6 +20,8 @@ static DOHFile *headers = 0;
|
|||
static DOHFile *wrappers = 0;
|
||||
static DOHFile *init = 0;
|
||||
|
||||
static DOH *config_top = 0;
|
||||
|
||||
static int ExternMode = 0;
|
||||
static int ImportMode = 0;
|
||||
static int ReadOnly = 0;
|
||||
|
|
@ -108,6 +112,13 @@ emit_initblock(DOH *obj, void *clientdata) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* %runtime %{ ... %} directive */
|
||||
static int
|
||||
emit_runtimeblock(DOH *obj, void *clientdata) {
|
||||
Dump(Getattr(obj,"code"),runtime);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------ Basic C declarations ----- */
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
|
@ -141,64 +152,103 @@ emit_function(DOH *obj, void *clientdata) {
|
|||
}
|
||||
|
||||
wf = NewSwigWrapper();
|
||||
/* Printf(wf->def,"void %s(args) {\n", Swig_name_wrapper(scriptname));*/
|
||||
|
||||
Printv(wf->def,
|
||||
"static PyObject *", Swig_name_wrapper(scriptname), "(PyObject *self, PyObject *args) {\n",
|
||||
0);
|
||||
|
||||
/* Mapping test */
|
||||
|
||||
{
|
||||
DOH *rules;
|
||||
int mlen = 0;
|
||||
rules = Swig_map_match(Rules,"input",parms, &mlen);
|
||||
Printf(stdout,"match(%d)\n", mlen);
|
||||
}
|
||||
|
||||
/* Loop over all of the function arguments and build pieces of the wrapper function */
|
||||
{
|
||||
DOHString *formatstr; /* Format string for parsing arguments */
|
||||
DOHString *parsestr; /* Format string for parsing arguments */
|
||||
DOHString *argstr; /* List of arguments */
|
||||
formatstr = NewString("");
|
||||
argstr = NewString("");
|
||||
DOHString *inputstr;
|
||||
DOHString *checkstr;
|
||||
DOHString *initstr;
|
||||
|
||||
for (p = parms, i=0; p; p = Swig_next(p), i++) {
|
||||
DOHString *pdecl, *pname, *ptype, *value;
|
||||
pname = NewStringf("_arg%d",i);
|
||||
parsestr = NewString("");
|
||||
argstr = NewString("");
|
||||
inputstr = NewString("");
|
||||
checkstr = NewString("");
|
||||
initstr = NewString("");
|
||||
|
||||
ptype = Getattr(p,"type");
|
||||
p = parms;
|
||||
i = 0;
|
||||
|
||||
while (p) {
|
||||
int nmatch = 0;
|
||||
DOHHash *map;
|
||||
DOHHash *rules;
|
||||
|
||||
if (SwigType_isarray(ptype)) {
|
||||
int i;
|
||||
int nd;
|
||||
nd = SwigType_array_ndim(ptype);
|
||||
for (i = 0; i < nd; i++) {
|
||||
Printf(stdout,"array[%d] = %S\n", i, SwigType_array_getdim(ptype,i));
|
||||
map = Swig_map_match(Rules, "argument", p, &nmatch);
|
||||
|
||||
if (!map) {
|
||||
Printf(stderr,"%s:%d. No argument rule for %S\n", Getfile(p),Getline(p),SwigType_cstr(Getattr(p,"type"),Getattr(p,"name")));
|
||||
return;
|
||||
}
|
||||
Printf(stdout,"Got match: %s\n", Getattr(map,"parms"));
|
||||
|
||||
/* Pull out the rules */
|
||||
|
||||
rules = Getattr(map,"rules");
|
||||
if (rules) {
|
||||
DOH *linit;
|
||||
DOH *lparse;
|
||||
DOH *linput;
|
||||
DOH *lcheck;
|
||||
|
||||
linit = Getattr(rules,"init");
|
||||
lparse = Getattr(rules,"parse");
|
||||
linput = Getattr(rules,"input");
|
||||
lcheck = Getattr(rules,"check");
|
||||
|
||||
/* Construct the parse and argument strings */
|
||||
|
||||
if (lparse) {
|
||||
DOHList *sp;
|
||||
DOHString *pstr;
|
||||
int i;
|
||||
Seek(lparse,0,SEEK_SET);
|
||||
sp = DohSplit(lparse,",",-1);
|
||||
|
||||
Printf(stdout,"*** %s\n", sp);
|
||||
|
||||
pstr = Getitem(sp,0);
|
||||
if (pstr) {
|
||||
char *c = Char(pstr);
|
||||
while (*c && (*c != '\"')) c++;
|
||||
c++;
|
||||
while (*c && (*c != '\"')) {
|
||||
Putc(*c, parsestr);
|
||||
c++;
|
||||
}
|
||||
}
|
||||
for (i = 1; i < Len(sp); i++) {
|
||||
Printf(argstr,",%s",Getitem(sp,i));
|
||||
}
|
||||
}
|
||||
if (linput) Append(inputstr,linput);
|
||||
if (lcheck) Append(checkstr,lcheck);
|
||||
if (linit) Append(initstr,linit);
|
||||
}
|
||||
|
||||
pdecl = SwigType_cstr(ptype,pname);
|
||||
|
||||
if ((value = Getattr(p,"value"))) {
|
||||
Printf(pdecl," = %s", value);
|
||||
numopt++;
|
||||
} else {
|
||||
if (numopt > 0) {
|
||||
printf("*** Error: Non-optional argument follows optional argument!\n");
|
||||
}
|
||||
while (nmatch > 0) {
|
||||
p = Swig_next(p);
|
||||
nmatch--;
|
||||
}
|
||||
SwigWrapper_add_local(wf,pdecl,pname);
|
||||
Printf(argstr,",&%s",pname);
|
||||
|
||||
Delete(pname);
|
||||
Delete(pdecl);
|
||||
}
|
||||
|
||||
Printv(wf->code,
|
||||
"if (!PyParseArgs(\"", formatstr, "\"", argstr, ")) {\n",
|
||||
"return NULL;\n",
|
||||
"}\n",
|
||||
"init:\n",
|
||||
initstr,
|
||||
"\nparse:\n",
|
||||
parsestr,
|
||||
"\nargstr:\n",
|
||||
argstr,
|
||||
"\ninput:\n",
|
||||
inputstr,
|
||||
"\ncheck:\n",
|
||||
checkstr,
|
||||
0);
|
||||
}
|
||||
|
||||
|
|
@ -454,13 +504,13 @@ void test_emit(DOH *top, void *clientdata) {
|
|||
Rules = NewHash();
|
||||
RenameHash = NewHash();
|
||||
|
||||
if (config_top)
|
||||
Swig_emit(config_top, clientdata);
|
||||
|
||||
Swig_emit(top, clientdata);
|
||||
|
||||
Swig_banner(stdout);
|
||||
|
||||
/* Get the runtime library */
|
||||
Swig_insert_file("python.swg",runtime);
|
||||
|
||||
Dump(runtime,stdout);
|
||||
Dump(headers,stdout);
|
||||
Dump(wrappers,stdout);
|
||||
|
|
@ -479,6 +529,7 @@ static SwigRule rules[] = {
|
|||
"headerblock", emit_headerblock,
|
||||
"wrapperblock", emit_wrapperblock,
|
||||
"initblock", emit_initblock,
|
||||
"runtimeblock", emit_runtimeblock,
|
||||
"scope", emit_scope,
|
||||
"function", emit_function,
|
||||
"variable", emit_variable,
|
||||
|
|
@ -512,7 +563,28 @@ static SwigRule rules[] = {
|
|||
|
||||
/* Initialize the module */
|
||||
void test_init() {
|
||||
DOHString *config;
|
||||
DOHString *pconfig;
|
||||
|
||||
Swig_add_rules(rules);
|
||||
|
||||
|
||||
/* Try to get the language specific configuration */
|
||||
|
||||
config = Swig_include("pyconf.swg");
|
||||
|
||||
if (!config) {
|
||||
Printf(stderr,"*** Fatal error. Unable to find pyconf.swg\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* Run the preprocessor on the configuration file and parse it */
|
||||
Seek(config,0,SEEK_SET);
|
||||
Setline(config,1);
|
||||
pconfig = Preprocessor_parse(config);
|
||||
|
||||
Seek(pconfig,0,SEEK_SET);
|
||||
config_top = LParse_parse(pconfig);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -136,6 +136,8 @@ Swig_map_match(DOHHash *ruleset, DOHString_or_char *rulename, DOHHash *parms, in
|
|||
DOH *bestobj = 0;
|
||||
int bestdepth = -1;
|
||||
|
||||
*nmatch = 0;
|
||||
|
||||
/* Get the nameset */
|
||||
nameset = Getattr(ruleset,rulename);
|
||||
if (!nameset) return 0;
|
||||
|
|
@ -301,100 +303,20 @@ Swig_map_match(DOHHash *ruleset, DOHString_or_char *rulename, DOHHash *parms, in
|
|||
}
|
||||
if (bestobj) {
|
||||
*nmatch = bestdepth;
|
||||
}
|
||||
return bestobj;
|
||||
}
|
||||
|
||||
#ifdef OLD
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* Swig_map_match()
|
||||
*
|
||||
* Perform a longest map for a list of parameters and a set of mapping rules.
|
||||
* Returns the corresponding rule object and the number of parameters that
|
||||
* were matched.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
/* Internal function used for recursively searching rulesets */
|
||||
static DOH *
|
||||
Swig_map_match_internal(DOHHash *nameset, DOHHash *parms, int *nmatch) {
|
||||
DOHHash *nn1, *nn2;
|
||||
DOHString *ty;
|
||||
DOHString *name;
|
||||
DOHString *key;
|
||||
int mlen1 = 0, mlen2 = 0, bestlen = 0;
|
||||
DOH *obj1 = 0, *obj2 = 0;
|
||||
DOH *bestobj, *bestn;
|
||||
DOHHash *nextp;
|
||||
|
||||
if (!parms) {
|
||||
bestobj = Getattr(nameset,"*obj*");
|
||||
if (bestobj) *nmatch++;
|
||||
return bestobj;
|
||||
}
|
||||
|
||||
ty = Getattr(parms,"type");
|
||||
name = Getattr(parms,"name");
|
||||
key = NewStringf("%s-%s",name,ty);
|
||||
|
||||
/* See if there is an exact name match */
|
||||
nn1 = Getattr(nameset,key);
|
||||
if (nn1) {
|
||||
mlen1++;
|
||||
obj1 = Swig_map_match_internal(nn1,Swig_next(parms), &mlen1);
|
||||
}
|
||||
|
||||
/* See if there is a generic name match */
|
||||
Clear(key);
|
||||
Printf(key,"-%s",ty);
|
||||
nn2 = Getattr(nameset,key);
|
||||
if (nn2) {
|
||||
mlen2++;
|
||||
obj2 = Swig_map_match_internal(nn2, Swig_next(parms), &mlen2);
|
||||
}
|
||||
/* Pick the best match. Note: an exact name match is preferred */
|
||||
if (obj1 && obj2) {
|
||||
if (mlen2 > mlen1) {
|
||||
bestlen = mlen2;
|
||||
bestobj = obj2;
|
||||
} else {
|
||||
bestlen = mlen1;
|
||||
bestobj = obj1;
|
||||
} else {
|
||||
/* If there is no match at all. I guess we can check for a default type */
|
||||
DOH *rs;
|
||||
DOHString *key, *name;
|
||||
DOHString *dty = SwigType_default(Getattr(parms,"type"));
|
||||
key = NewStringf("-%s",dty);
|
||||
|
||||
rs = Getattr(nameset,key);
|
||||
if (rs) {
|
||||
bestobj = Getattr(rs,"*obj*");
|
||||
if (bestobj) *nmatch = 1;
|
||||
}
|
||||
} else if (obj1) {
|
||||
bestobj = obj1;
|
||||
bestlen = mlen1;
|
||||
} else if (obj2) {
|
||||
bestobj = obj2;
|
||||
bestlen = mlen2;
|
||||
Delete(key);
|
||||
Delete(dty);
|
||||
}
|
||||
if (!bestobj) {
|
||||
bestobj = Getattr(nameset,"*obj*");
|
||||
if (bestobj) bestlen = 1;
|
||||
}
|
||||
Delete(key);
|
||||
*nmatch = *nmatch + bestlen;
|
||||
return bestobj;
|
||||
}
|
||||
|
||||
DOH *
|
||||
Swig_map_match(DOHHash *ruleset, DOHString_or_char *rulename, DOHHash *parms, int *nmatch)
|
||||
{
|
||||
DOHHash *nameset;
|
||||
int mlen = 0;
|
||||
DOH *best;
|
||||
|
||||
/* Get the nameset */
|
||||
nameset = Getattr(ruleset,rulename);
|
||||
if (!nameset) return 0;
|
||||
|
||||
best = Swig_map_match_internal(nameset,parms,&mlen);
|
||||
*nmatch = mlen;
|
||||
return best;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -488,6 +488,42 @@ void SwigType_array_setdim(DOHString_or_char *t, int n, DOHString_or_char *rep)
|
|||
Delete(result);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* SwigType_default()
|
||||
*
|
||||
* Create the default string for this datatype. This takes a type and strips it
|
||||
* down to its most primitive form--resolving all typedefs and removing operators.
|
||||
*
|
||||
* Rules:
|
||||
* Pointers: p.SWIGPOINTER
|
||||
* References: r.SWIGREFERENCE
|
||||
* Arrays: a().SWIGARRAY
|
||||
* Types: SWIGTYPE
|
||||
*
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
DOHString *SwigType_default(DOHString_or_char *t) {
|
||||
DOHString *r1, *def;
|
||||
DOHString *r = NewString(t);
|
||||
|
||||
while ((r1 = SwigType_typedef_resolve(r))) {
|
||||
Delete(r);
|
||||
r = r1;
|
||||
}
|
||||
|
||||
if (SwigType_ispointer(r)) {
|
||||
def = NewString("p.SWIGPOINTER");
|
||||
} else if (SwigType_isreference(r)) {
|
||||
def = NewString("r.SWIGREFERENCE");
|
||||
} else if (SwigType_isarray(r)) {
|
||||
def = NewString("a().SWIGARRAY");
|
||||
} else {
|
||||
def = NewString("SWIGTYPE");
|
||||
}
|
||||
Delete(r);
|
||||
return def;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* SwigType_cstr(DOH *s, DOH *id)
|
||||
*
|
||||
|
|
@ -702,6 +738,7 @@ DOHString *SwigType_typedef_resolve(DOHString_or_char *t) {
|
|||
DOHString *r;
|
||||
int level;
|
||||
|
||||
init_scopes();
|
||||
base = SwigType_base(t);
|
||||
|
||||
level = scope_level;
|
||||
|
|
|
|||
|
|
@ -149,6 +149,7 @@ extern int SwigType_cmp(DOHString_or_char *pat, DOHString_or_char *t);
|
|||
extern int SwigType_array_ndim(DOHString_or_char *t);
|
||||
extern DOHString *SwigType_array_getdim(DOHString_or_char *t, int n);
|
||||
extern void SwigType_array_setdim(DOHString_or_char *t, int n, DOHString_or_char *rep);
|
||||
extern DOHString *SwigType_default(DOHString_or_char *t);
|
||||
|
||||
/* --- Parse tree support --- */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue