Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

perlxs failed to parse my typemap

by llancet (Friar)
on Mar 11, 2014 at 06:29 UTC ( #1077798=perlquestion: print w/ replies, xml ) Need Help??
llancet has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks:

I'm making a perl module to wrap a library. Everything goes well, until I added typemap for this enum. This is the enum:

namespace geno_eye { typedef enum { LABEL_BIND_TOP = 1, LABEL_BIND_BOTTOM = 1 << 1, LABEL_BIND_LEFT = 1 << 2, LABEL_BIND_RIGHT = 1 << 3 } LabelBind; }
and this is the typemap. I attempt to convert it from/to string for perl SV:
geno_eye::LabelBind GENO_EYE_LABEL_BIND_T INPUT GENO_EYE_LABEL_BIND_T char* sv_text = SvPV_nolen($arg); $var = 0; if (strstr(sv_text, "top") || strstr(sv_text, "TOP")) $var |= geno_eye::LABEL_BIND_TOP; else if (strstr(sv_text, "bottom") || strstr(sv_text, "BOTTOM")) $var |= geno_eye::LABEL_BIND_BOTTOM; if (strstr(sv_text, "left") || strstr(sv_text, "LEFT")) $var |= geno_eye::LABEL_BIND_LEFT; else if (strstr(sv_text, "right") || strstr(sv_text, "RIGHT")) $var |= geno_eye::LABEL_BIND_RIGHT; OUTPUT GENO_EYE_LABEL_BIND_T if ($var|geno_eye::LABEL_BIND_TOP) sv_setpv($arg, "top"); else if ($var|geno_eye::LABEL_BIND_BOTTOM) sv_setpv($arg, "bottom"); if ($var|geno_eye::LABEL_BIND_LEFT) sv_catpv($arg, "left"); else if ($var|geno_eye::LABEL_BIND_RIGHT) sv_catpv($arg, "right");

I wrote a minimum test XS file:

#ifdef __cplusplus extern "C" { #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #undef do_open #undef do_close #ifdef __cplusplus } #endif #include "geno_eye_perl.h" #include <exception> #include <iostream> #undef xsp_constructor_class #define xsp_constructor_class(c) (c) #include <geno_eye/common.h> MODULE=Test PACKAGE=Test void test(geno_eye::LabelBind value) CODE: std::cout << "binary value: " << value << std::endl;
When I run perlxs, it cries at the first code line of the INPUT section:
$ xsubpp -typemap test_typemap test.xs >test.cpp Bareword found where operator expected at (eval 2) line 3, near ""\n + char* sv_text = SvPV_nolen($arg); $var = 0; if (strstr(sv_text, "top" (Might be a runaway multi-line "" string starting on line 1) (Missing operator before top?) String found where operator expected at (eval 2) line 3, near "top") | +| strstr(sv_text, "" Bareword found where operator expected at (eval 2) line 3, near "") || + strstr(sv_text, "TOP" (Missing operator before TOP?) String found where operator expected at (eval 2) line 5, near "TOP")) $var |= geno_eye::LABEL_BIND_TOP; else if (strstr(sv_text, "" (Might be a runaway multi-line "" string starting on line 3) String found where operator expected at (eval 2) line 5, near "bottom" +) || strstr(sv_text, "" Bareword found where operator expected at (eval 2) line 5, near "") || + strstr(sv_text, "BOTTOM" (Missing operator before BOTTOM?) String found where operator expected at (eval 2) line 7, near "BOTTOM" +)) $var |= geno_eye::LABEL_BIND_BOTTOM; if (strstr(sv_text, "" (Might be a runaway multi-line "" string starting on line 5) String found where operator expected at (eval 2) line 7, near "left") +|| strstr(sv_text, "" Bareword found where operator expected at (eval 2) line 7, near "") || + strstr(sv_text, "LEFT" (Missing operator before LEFT?) String found where operator expected at (eval 2) line 9, near "LEFT")) $var |= geno_eye::LABEL_BIND_LEFT; else if (strstr(sv_text, "" (Might be a runaway multi-line "" string starting on line 7) String found where operator expected at (eval 2) line 9, near "right") + || strstr(sv_text, "" Bareword found where operator expected at (eval 2) line 9, near "") || + strstr(sv_text, "RIGHT" (Missing operator before RIGHT?) String found where operator expected at (eval 2) line 11, near "RIGHT" +)) $var |= geno_eye::LABEL_BIND_RIGHT ;\n"" (Might be a runaway multi-line "" string starting on line 9) syntax error at (eval 2) line 3, near ""\n char* sv_text = SvPV_nol +en($arg); $var = 0; if (strstr(sv_text, "top"

Comment on perlxs failed to parse my typemap
Select or Download Code
Re: perlxs failed to parse my typemap (typemaps are double quoted perl strings)
by Anonymous Monk on Mar 11, 2014 at 07:42 UTC

    Why not use T_ENUM?

    Why not do the string-to-flag conversion in pure-perl?

    Have you read perlxstypemap#Writing typemap Entries?

    It says Each INPUT or OUTPUT typemap entry is a double-quoted Perl string that will be evaluated in the presence of certain variables to get the final C code for mapping a certain C type.

    typemap file is mostly perl code

    Try  perl -S -MCarp::Always xsubpp     -typemap test_typemap    test.xs     -output test.cpp 2>2

    With this typemap

    It spits out

      == Why not use T_ENUM?

      Because I need input and output from perl, and T_ENUM only allows to transfer from C

      == Why not do the string-to-flag conversion in pure-perl?

      Because I don't want my typemap code looks full of {{{}}}

      Now I use an ultimate solution: put all conversion code in functions in another .c and .h file, and call these functions in XS code.

Re: perlxs failed to parse my typemap
by bulk88 (Priest) on Mar 11, 2014 at 11:42 UTC
    No blank lines are allowed in a typemape entry, Blank line means new entry.
    OUTPUT GENO_EYE_LABEL_BIND_T if ($var|geno_eye::LABEL_BIND_TOP) sv_setpv($arg, "top"); else if ($var|geno_eye::LABEL_BIND_BOTTOM) sv_setpv($arg, "bottom"); if ($var|geno_eye::LABEL_BIND_LEFT) sv_catpv($arg, "left"); else if ($var|geno_eye::LABEL_BIND_RIGHT) sv_catpv($arg, "right");
    I see a blank line that doesn't start a new type.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1077798]
Approved by hdb
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-07-25 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (171 votes), past polls