Alt-Acme-Math-XS-CPP
view release on metacpan or search on metacpan
inc/Inline/CPP/Parser/RecDescent.pm view on Meta::CPAN
Inline::CPP::Parser::RecDescent::strip_ellipsis($thisparser,
$item[1]->{args});
[$item[1]];
}
| member_def
{
# print "class_decl found one or more members:\n", Dumper(\@item);
$_->{thing} = 'member' for @{$item[1]};
$item[1];
}
function_def: operator <commit> ';'
{
$item[1]
}
| operator <commit> smod(?) code_block
{
$item[1]
}
| IDENTIFIER '(' <commit> <leftop: arg ',' arg>(s?) ')' smod(?) code_block
{
{name => $item{IDENTIFIER}, args => $item{__DIRECTIVE2__}, rtype => '' }
}
| rtype IDENTIFIER '(' <leftop: arg ',' arg>(s?) ')' ';'
{
{rtype => $item[1], name => $item[2], args => $item{__DIRECTIVE1__} }
}
| rtype IDENTIFIER '(' <leftop: arg ',' arg>(s?) ')' smod(?) code_block
{
{rtype => $item{rtype}, name => $item[2], args => $item{__DIRECTIVE1__} }
}
method_def: operator <commit> method_imp
{
# print "method operator:\n", Dumper $item[1];
$item[1];
}
| IDENTIFIER '(' <commit> <leftop: arg ',' arg>(s?) ')' method_imp
{
# print "con-/de-structor found: $item[1]\n";
{name => $item[1], args => $item{__DIRECTIVE2__}, abstract => ${$item{method_imp}} };
}
| rtype IDENTIFIER '(' <leftop: arg ',' arg>(s?) ')' method_imp
{
# print "method found: $item[2]\n";
$return =
{name => $item[2], rtype => $item[1], args => $item[4],
abstract => ${$item[6]},
rconst => $thisparser->{data}{smod}{const},
};
$thisparser->{data}{smod}{const} = 0;
}
operator: rtype(?) 'operator' /\(\)|[^()]+/ '(' <leftop: arg ',' arg>(s?) ')'
{
# print "Found operator: $item[1][0] operator $item[3]\n";
{name=> "operator $item[3]", args => $item[5], ret => $item[1][0]}
}
# By adding smod, we allow 'const' member functions. This would also bind to
# incorrect C++ with the word 'static' after the argument list, but we don't
# care at all because such code would never be compiled successfully.
# By adding init, we allow constructors to initialize references. Again, we'll
# allow them anywhere, but our goal is not to enforce c++ standards -- that's
# the compiler's job.
method_imp: smod(?) ';' { \0 }
| smod(?) '=' <commit> '0' ';' { \1 }
| smod(?) initlist(?) code_block { \0 }
| smod(?) '=' '0' code_block { \0 }
initlist: ':' <leftop: subexpr ',' subexpr>
member_def: anytype <leftop: var ',' var> ';'
{
my @retval;
for my $def (@{$item[2]}) {
my $type = join '', $item[1], @{$def->[0]};
my $name = $def->[1];
# print "member found: type=$type, name=$name\n";
push @retval, { name => $name, type => $type };
}
\@retval;
}
var: star(s?) IDENTIFIER '=' expr { [@item[1,2]] }
| star(s?) IDENTIFIER '[' expr ']' { [@item[1,2]] }
| star(s?) IDENTIFIER { [@item[1,2]] }
arg: type IDENTIFIER '=' expr
{
# print "argument $item{IDENTIFIER} found\n";
# print "expression: $item{expr}\n";
{type => $item[1], name => $item{IDENTIFIER}, optional => 1,
offset => $thisoffset}
}
| type IDENTIFIER
{
# print "argument $item{IDENTIFIER} found\n";
{type => $item[1], name => $item{IDENTIFIER}, offset => $thisoffset}
}
| type { {type => $item[1]} }
| '...'
{ {name => '...', type => '...', offset => $thisoffset} }
ident_part: /[~_a-z]\w*/i '<' <commit> <leftop: IDENTIFIER ',' IDENTIFIER>(s?) '>'
{
$item[1].'<'.join('', @{$item[4]}).'>'
}
| /[~_a-z]\w*/i
{
$item[1]
}
IDENTIFIER: <leftop: ident_part '::' ident_part>
{
my $x = join '::', @{$item[1]};
# print "IDENTIFIER: $x\n";
$x
inc/Inline/CPP/Parser/RecDescent.pm view on Meta::CPAN
}
anytype: anytype2 | anytype1
anytype1: TYPE star(s?)
{
$return = $item[1];
$return .= join '',' ',@{$item[2]} if @{$item[2]};
}
anytype2: modifier(s) TYPE star(s?)
{
$return = $item[2];
$return = join ' ',grep{$_}@{$item[1]},$return if @{$item[1]};
$return .= join '',' ',@{$item[3]} if @{$item[3]};
}
comment: m{\s* // [^\n]* \n }x
| m{\s* /\* (?:[^*]+|\*(?!/))* \*/ ([ \t]*)? }x
# long and short aren't recognized as modifiers because they break when used
# as regular types. Another Parse::RecDescent problem is greedy matching; I
# need tmodifier to "give back" long or short in cases where keeping them would
# cause the modifier rule to fail. One side-effect is 'long long' can never
# be parsed correctly here.
modifier: tmod
| smod { ++$thisparser->{data}{smod}{$item[1]}; ''}
| nmod { '' }
tmod: 'unsigned' # | 'long' | 'short'
smod: 'const' | 'static'
nmod: 'extern' | 'virtual' | 'mutable' | 'volatile' | 'inline'
scope: 'public' | 'private' | 'protected'
class: 'class' { $thisparser->{data}{defaultscope} = 'private'; $item[1] }
| 'struct' { $thisparser->{data}{defaultscope} = 'public'; $item[1] }
star: '*' | '&'
code_block: /$Inline::CPP::Parser::RecDescent::code_block/
# Consume expressions
expr: <leftop: subexpr OP subexpr> {
my $o = join '', @{$item[1]};
# print "expr: $o\n";
$o;
}
subexpr: /$Inline::CPP::Parser::RecDescent::funccall/ # Matches a macro, too
| /$Inline::CPP::Parser::RecDescent::string/
| /$Inline::CPP::Parser::RecDescent::number/
| UOP subexpr
OP: '+' | '-' | '*' | '/' | '^' | '&' | '|' | '%' | '||' | '&&'
UOP: '~' | '!' | '-' | '*' | '&'
TYPE: IDENTIFIER
all: /.*/
END
}
#============================================================================
# Generate typemap code for the classes and structs we bind to. This allows
# functions declared after a class to return or accept class objects as
# parameters.
#============================================================================
$TYPEMAP_KIND = 'O_Inline_CPP_Class';
sub typemap {
my ($parser, $typename) = @_;
# print "Inline::CPP::Parser::RecDescent::typemap(): typename=$typename\n";
my ($TYPEMAP, $INPUT, $OUTPUT);
$TYPEMAP = "$typename *\t\t$TYPEMAP_KIND\n";
$INPUT = <<"END";
if (sv_isobject(\$arg) && (SvTYPE(SvRV(\$arg)) == SVt_PVMG)) {
\$var = (\$type)SvIV((SV*)SvRV( \$arg ));
}
else {
warn ( \\"\${Package}::\$func_name() -- \$var is not a blessed reference\\" );
XSRETURN_UNDEF;
}
END
$OUTPUT = <<"END";
sv_setref_pv( \$arg, CLASS, (void*)\$var );
END
my $ctypename = $typename . ' *';
$parser->{data}{typeconv}{input_expr}{$TYPEMAP_KIND} ||= $INPUT;
$parser->{data}{typeconv}{output_expr}{$TYPEMAP_KIND} ||= $OUTPUT;
$parser->{data}{typeconv}{type_kind}{$ctypename} = $TYPEMAP_KIND;
$parser->{data}{typeconv}{valid_types}{$ctypename}++;
$parser->{data}{typeconv}{valid_rtypes}{$ctypename}++;
return;
}
#============================================================================
# Default action is to strip ellipses from the C++ code. This allows having
# _only_ a '...' in the code, just like XS. It is the default.
#============================================================================
sub strip_ellipsis {
my ($parser, $args) = @_;
return if $parser->{ILSM}{PRESERVE_ELLIPSIS};
for (my $i = 0; $i < @$args; $i++) {
next unless $args->[$i]{name} eq '...';
# if it's the first one, just strip it
if ($i == 0) {
substr($parser->{ILSM}{code}, $args->[$i]{offset} - 3, 3, ' ');
}
else {
my $prev = $i - 1;
my $prev_offset = $args->[$prev]{offset};
my $length = $args->[$i]{offset} - $prev_offset;
substr($parser->{ILSM}{code}, $prev_offset, $length) =~ s/\S/ /g;
}
}
return;
}
my $hack = sub { # Appease -w using Inline::Files
print Parse::RecDescent::IN '';
( run in 0.668 second using v1.01-cache-2.11-cpan-2398b32b56e )