You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/08/20 14:35:51 UTC
svn commit: r806144 - in /lucene/lucy/trunk/boilerplater:
lib/Boilerplater/ParamList.pm lib/Boilerplater/Parser.pm t/301-param_list.t
Author: marvin
Date: Thu Aug 20 12:35:50 2009
New Revision: 806144
URL: http://svn.apache.org/viewvc?rev=806144&view=rev
Log:
Commit LUCY-19, adding Boilerplater::ParamList.
Added:
lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm (with props)
lucene/lucy/trunk/boilerplater/t/301-param_list.t (with props)
Modified:
lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm
Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm?rev=806144&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm Thu Aug 20 12:35:50 2009
@@ -0,0 +1,138 @@
+use strict;
+use warnings;
+
+package Boilerplater::ParamList;
+use Boilerplater::Variable;
+use Boilerplater::Util qw( verify_args );
+use Carp;
+
+our %new_PARAMS = (
+ variables => undef,
+ initial_values => undef,
+ variadic => undef,
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = bless { %new_PARAMS, @_, }, ref($either) || $either;
+
+ # Validate variables.
+ confess "variables must be an arrayref"
+ unless ref( $self->{variables} ) eq 'ARRAY';
+ for my $var ( @{ $self->{variables} } ) {
+ confess "invalid variable: '$var'"
+ unless ref($var) && $var->isa("Boilerplater::Variable");
+ }
+
+ # Validate or init initial_values.
+ if ( defined $self->{initial_values} ) {
+ confess "variables must be an arrayref"
+ unless ref( $self->{variables} ) eq 'ARRAY';
+ my $num_init = scalar @{ $self->{initial_values} };
+ my $num_vars = $self->num_vars;
+ confess("mismatch of num vars and init values: $num_vars $num_init")
+ unless $num_init == $num_vars;
+ }
+ else {
+ my @initial_values;
+ $#initial_values = $#{ $self->{variables} };
+ $self->{initial_values} = \@initial_values;
+ }
+
+ return $self;
+}
+
+sub get_variables { shift->{variables} }
+sub get_initial_values { shift->{initial_values} }
+sub variadic { shift->{variadic} }
+sub num_vars { scalar @{ shift->{variables} } }
+
+sub to_c {
+ my $self = shift;
+ my $string = join( ', ', map { $_->local_c } @{ $self->{variables} } );
+ $string .= ", ..." if $self->{variadic};
+ return $string;
+}
+
+sub name_list {
+ my $self = shift;
+ return join( ', ', map { $_->micro_sym } @{ $self->{variables} } );
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::ParamList - parameter list.
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Boilerplater::ParamList->new(
+ variables => \@vars, # required
+ initial_values => \@vals, # default: undef
+ variadic => 1, # default: false
+ );
+
+=over
+
+=item * B<variables> - An array where each element is a
+L<Boilerplater::Variable>.
+
+=item * B<initial_values> - If supplied, an array of default values, one for
+each variable.
+
+=item * B<variadic> - Should be true if the function is variadic.
+
+=back
+
+=head2 get_variables get_initial_values variadic
+
+Accessors.
+
+=head2 num_vars
+
+Return the number of variables in the ParamList, including "self" for methods.
+
+=head2 to_c
+
+ # Prints "Obj* self, Foo* foo, Bar* bar".
+ print $param_list->to_c;
+
+Return a list of the variable's types and names, joined by commas.
+
+=head2 name_list
+
+ # Prints "self, foo, bar".
+ print $param_list->name_list;
+
+Return the variable's names, joined by commas.
+
+=head1 COPYRIGHT AND LICENSE
+
+ /**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+=cut
+
Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/ParamList.pm
------------------------------------------------------------------------------
svn:eol-style = native
Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm?rev=806144&r1=806143&r2=806144&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm Thu Aug 20 12:35:50 2009
@@ -49,6 +49,27 @@
};
}
+param_list:
+ '('
+ param_list_elem(s? /,/)
+ (/,\s*.../)(?)
+ ')'
+ {
+ Boilerplater::Parser->new_param_list( $item[2], $item[3][0] ? 1 : 0 );
+ }
+
+param_list_elem:
+ param_variable assignment(?)
+ { [ $item[1], $item[2][0] ] }
+
+param_variable:
+ type declarator
+ { Boilerplater::Parser->new_var(\%item); }
+
+assignment:
+ '=' scalar_constant
+ { $item[2] }
+
type:
composite_type
| simple_type
@@ -162,6 +183,31 @@
/\d+/
| /[A-Z_]+/
+scalar_constant:
+ hex_constant
+ | float_constant
+ | integer_constant
+ | string_literal
+ | 'NULL'
+ | 'true'
+ | 'false'
+
+integer_constant:
+ /(?:-\s*)?\d+/
+ { $item[1] }
+
+hex_constant:
+ /0x[a-fA-F0-9]+/
+ { $item[1] }
+
+float_constant:
+ /(?:-\s*)?\d+\.\d+/
+ { $item[1] }
+
+string_literal:
+ /"(?:[^"\\]|\\.)*"/
+ { $item[1] }
+
reserved_word:
/(char|const|double|enum|extern|float|int|long|register|signed|sizeof
|short|inert|struct|typedef|union|unsigned|void)(?!\w)/x
@@ -249,6 +295,17 @@
);
}
+sub new_param_list {
+ my ( undef, $param_list_elems, $variadic ) = @_;
+ my @vars = map { $_->[0] } @$param_list_elems;
+ my @vals = map { $_->[1] } @$param_list_elems;
+ return Boilerplater::ParamList->new(
+ variables => \@vars,
+ initial_values => \@vals,
+ variadic => $variadic,
+ );
+}
+
sub new_parcel {
my ( undef, $item ) = @_;
Boilerplater::Parcel->singleton(
Added: lucene/lucy/trunk/boilerplater/t/301-param_list.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/301-param_list.t?rev=806144&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/301-param_list.t (added)
+++ lucene/lucy/trunk/boilerplater/t/301-param_list.t Thu Aug 20 12:35:50 2009
@@ -0,0 +1,35 @@
+use strict;
+use warnings;
+
+use Test::More tests => 13;
+
+BEGIN { use_ok('Boilerplater::ParamList') }
+use Boilerplater::Type;
+use Boilerplater::Parser;
+
+my $parser = Boilerplater::Parser->new;
+$parser->parcel_definition('parcel Boil;')
+ or die "failed to process parcel_definition";
+
+isa_ok( $parser->param_variable($_),
+ "Boilerplater::Variable", "param_variable: $_" )
+ for ( 'u32_t baz', 'CharBuf *stuff', 'float **ptr', );
+
+my $param_list = $parser->param_list("(Obj *self, int num)");
+isa_ok( $param_list, "Boilerplater::ParamList" );
+ok( !$param_list->variadic, "not variadic" );
+is( $param_list->to_c, 'boil_Obj* self, int num', "to_c" );
+is( $param_list->name_list, 'self, num', "name_list" );
+
+$param_list = $parser->param_list("(Obj *self=NULL, int num, ...)");
+ok( $param_list->variadic, "variadic" );
+is_deeply(
+ $param_list->get_initial_values,
+ [ "NULL", undef ],
+ "initial_values"
+);
+is( $param_list->to_c, 'boil_Obj* self, int num, ...', "to_c" );
+is( $param_list->num_vars, 2, "num_vars" );
+isa_ok( $param_list->get_variables->[0],
+ "Boilerplater::Variable", "get_variables..." );
+
Propchange: lucene/lucy/trunk/boilerplater/t/301-param_list.t
------------------------------------------------------------------------------
svn:eol-style = native