lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r806972 - in /lucene/lucy/trunk/boilerplater: lib/Boilerplater/Function.pm lib/Boilerplater/Parser.pm lib/Boilerplater/Symbol.pm lib/Boilerplater/Variable.pm t/051-symbol.t t/200-function.t t/402-parcel.t
Date Sun, 23 Aug 2009 14:12:45 GMT
Author: marvin
Date: Sun Aug 23 14:12:44 2009
New Revision: 806972

URL: http://svn.apache.org/viewvc?rev=806972&view=rev
Log:
Commit LUCY-21, adding Boilerplater:Function and migrating more common code
into Boilerplater::Symbol.

Added:
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm   (with props)
    lucene/lucy/trunk/boilerplater/t/200-function.t   (with props)
Modified:
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Variable.pm
    lucene/lucy/trunk/boilerplater/t/051-symbol.t
    lucene/lucy/trunk/boilerplater/t/402-parcel.t

Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm?rev=806972&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm Sun Aug 23 14:12:44 2009
@@ -0,0 +1,140 @@
+use strict;
+use warnings;
+
+package Boilerplater::Function;
+use base qw( Boilerplater::Symbol );
+use Carp;
+use Boilerplater::Util qw( verify_args a_isa_b );
+use Boilerplater::Type;
+use Boilerplater::ParamList;
+
+my %new_PARAMS = (
+    return_type => undef,
+    class_name  => undef,
+    class_cnick => undef,
+    param_list  => undef,
+    micro_sym   => undef,
+    docucomment => undef,
+    parcel      => undef,
+    inline      => 0,
+    exposure    => 'parcel',
+);
+
+sub new {
+    my $either = shift;
+    verify_args( \%new_PARAMS, @_ ) or confess $@;
+    my $self = $either->SUPER::new( %new_PARAMS, @_ );
+
+    # Validate.
+    for (qw( return_type class_name param_list )) {
+        confess("$_ is mandatory") unless defined $self->{$_};
+    }
+    confess("Invalid micro_sym: '$self->{micro_sym}'")
+        unless $self->{micro_sym} =~ /^[a-z0-9_]+$/;
+    confess 'param_list must be a ParamList object'
+        unless a_isa_b( $self->{param_list}, "Boilerplater::ParamList" );
+    confess 'return_type must be a Type object'
+        unless a_isa_b( $self->{return_type}, "Boilerplater::Type" );
+
+    return $self;
+}
+
+sub get_return_type { shift->{return_type} }
+sub get_param_list  { shift->{param_list} }
+sub get_docucomment { shift->{docucomment} }
+sub inline          { shift->{inline} }
+
+sub void { shift->{return_type}->is_void }
+
+sub full_func_sym  { shift->SUPER::full_sym }
+sub short_func_sym { shift->SUPER::short_sym }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Function - Metadata describing a function.
+
+=head1 METHODS
+
+=head2 new
+
+    my $type = Boilerplater::Function->new(
+        class_name  => 'MyProject::FooFactory',    # required
+        class_cnick => 'FooFact',                  # required
+        return_type => $void_type                  # required
+        param_list  => $param_list,                # required
+        micro_sym   => 'count',                    # required
+        docucomment => $docucomment,               # default: undef
+        parcel      => 'Boil'                      # default: special
+        exposure    => 'public'                    # default: parcel
+        inline      => 1,                          # default: false
+    );
+
+=over
+
+=item * B<class_name> - The full name of the class in whose namespace the
+function resides.
+
+=item * B<class_cnick> - The C nickname for the class. 
+
+=item * B<return_type> - A L<Boilerplater::Type> representing the function's
+return type.
+
+=item * B<param_list> - A L<Boilerplater::ParamList> representing the
+function's argument list.
+
+=item * B<micro_sym> - The lower case name of the function, without any
+namespacing prefixes.
+
+=item * B<docucomment> - A L<Boilerplater::DocuComment> describing the
+function.
+
+=item * B<parcel> - A L<Boilerplater::Parcel> or a parcel name.
+
+=item * B<exposure> - The function's exposure (see L<Boilerplater::Symbol>).
+
+=item * B<inline> - Should be true if the function should be inlined by the
+compiler.
+
+=back
+
+=head2 get_return_type get_param_list get_docucomment inline 
+
+Accessors.
+
+=head2 void
+
+Returns true if the function has a void return type, false otherwise.
+
+=head2 full_func_sym
+
+A synonym for full_sym().
+
+=head2 short_func_sym
+
+A synonym for short_sym().
+
+=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/Function.pm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Function.pm
------------------------------------------------------------------------------
    svn:executable = *

Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm?rev=806972&r1=806971&r2=806972&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm Sun Aug 23 14:12:44 2009
@@ -16,6 +16,7 @@
 use Boilerplater::Type::Composite;
 use Boilerplater::Variable;
 use Boilerplater::DocuComment;
+use Boilerplater::Function;
 use Carp;
 
 our $grammar = <<'END_GRAMMAR';
@@ -50,6 +51,22 @@
         };
     }
 
+subroutine_declaration_statement:
+    docucomment(?)
+    exposure_specifier(?) 
+    subroutine_modifier(s?) 
+    type 
+    declarator 
+    param_list 
+    ';'
+    {
+        $return = {
+            exposure  => $item[2],
+            modifiers => $item[3],
+            declared  => Boilerplater::Parser->new_sub( \%item, \%arg ),
+        };
+    }
+
 param_list:
     '(' 
     param_list_elem(s? /,/)
@@ -127,6 +144,13 @@
     | 'incremented'
     | 'decremented'
 
+subroutine_modifier:
+      'inert'
+    | 'inline'
+    | 'abstract'
+    | 'final'
+    { $item[1] }
+
 exposure_specifier:
       'public'
     | 'private'
@@ -311,6 +335,38 @@
     );
 }
 
+sub new_sub {
+    my ( undef, $item, $arg ) = @_;
+    my $class;
+    my $modifiers  = $item->{'subroutine_modifier(s?)'};
+    my $docucom    = $item->{'docucomment(?)'}[0];
+    my $exposure   = $item->{'exposure_specifier(?)'}[0];
+    my $inert      = scalar grep { $_ eq 'inert' } @$modifiers;
+    my %extra_args = $exposure ? ( exposure => $exposure ) : ();
+
+    if ($inert) {
+        $class = 'Boilerplater::Function';
+        $extra_args{micro_sym} = $item->{declarator};
+        $extra_args{inline} = scalar grep { $_ eq 'inline' } @$modifiers;
+    }
+    else {
+        $class = 'Boilerplater::Method';
+        $extra_args{macro_sym} = $item->{declarator};
+        $extra_args{abstract} = scalar grep { $_ eq 'abstract' } @$modifiers;
+        $extra_args{final}    = scalar grep { $_ eq 'final' } @$modifiers;
+    }
+
+    return $class->new(
+        parcel      => $parcel,
+        docucomment => $docucom,
+        class_name  => $arg->{class},
+        class_cnick => $arg->{cnick},
+        return_type => $item->{type},
+        param_list  => $item->{param_list},
+        %extra_args,
+    );
+}
+
 sub new_parcel {
     my ( undef, $item ) = @_;
     Boilerplater::Parcel->singleton(

Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm?rev=806972&r1=806971&r2=806972&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Symbol.pm Sun Aug 23 14:12:44 2009
@@ -3,6 +3,7 @@
 
 package Boilerplater::Symbol;
 use Boilerplater::Parcel;
+use Boilerplater::Util qw( a_isa_b );
 use Scalar::Util qw( blessed );
 use Carp;
 
@@ -11,6 +12,7 @@
     exposure    => undef,
     class_name  => undef,
     class_cnick => undef,
+    micro_sym   => undef,
 );
 
 my $struct_regex     = qr/[A-Z]+[A-Z0-9]*[a-z]+[A-Za-z0-9]*/;
@@ -36,6 +38,11 @@
     my $self = bless { %new_PARAMS, %args, parcel => $parcel },
         ref($either) || $either;
 
+    # Validate micro_sym.
+    confess "micro_sym is required" unless $self->{micro_sym};
+    confess("Invalid micro_sym: '$self->{micro_sym}'")
+        unless $self->{micro_sym} =~ /^[A-Za-z_][A-Za-z0-9_]*$/;
+
     # Validate exposure.
     confess("Invalid value for 'exposure': $self->{exposure}")
         unless $self->{exposure} =~ /^(?:public|parcel|private|local)$/;
@@ -62,6 +69,7 @@
 sub get_parcel      { shift->{parcel} }
 sub get_class_name  { shift->{class_name} }
 sub get_class_cnick { shift->{class_cnick} }
+sub micro_sym       { shift->{micro_sym} }
 
 sub get_prefix { shift->{parcel}->get_prefix; }
 sub get_Prefix { shift->{parcel}->get_Prefix; }
@@ -72,10 +80,21 @@
 sub parcel  { shift->{exposure} eq 'parcel' }
 sub local   { shift->{exposure} eq 'local' }
 
+sub full_sym {
+    my $self   = shift;
+    my $prefix = $self->get_prefix;
+    return "$prefix$self->{class_cnick}_$self->{micro_sym}";
+}
+
+sub short_sym {
+    my $self = shift;
+    return "$self->{class_cnick}_$self->{micro_sym}";
+}
+
 sub equals {
     my ( $self, $other ) = @_;
-    return 0 unless blessed($other);
-    return 0 unless $other->isa(__PACKAGE__);
+    return 0 unless a_isa_b( $other, __PACKAGE__ );
+    return 0 unless $self->{micro_sym} eq $other->{micro_sym};
     return 0 unless $self->{parcel}->equals( $other->{parcel} );
     if ( defined $self->{exposure} ) {
         return 0 unless defined $other->{exposure};
@@ -117,6 +136,7 @@
         exposure    => $exposure,                           # required
         class_name  => "Crustacean::Lobster::LobsterClaw",  # default: undef
         class_cnick => "LobClaw",                           # default: special
+        micro_sym   => "rubber_band"                        # required
     );
 
 =over
@@ -136,11 +156,13 @@
 name.  If not supplied, will be derived if possible from C<class_name> by
 extracting the last class name component.
 
+=item * B<micro_sym> - The local identifier for the symbol.
+
 =back
 
 =head1 OBJECT METHODS
 
-=head2 get_parcel get_class_name get_class_cnick
+=head2 get_parcel get_class_name get_class_cnick micro_sym
 
 Getters.
 
@@ -161,6 +183,20 @@
 
 Returns true if the symbols are "equal", false otherwise.
 
+=head2 short_sym
+
+    # e.g. "LobClaw_rubber_band"
+    print $symbol->short_sym;
+
+Returns the C representation for the symbol minus the parcel's prefix.
+
+=head2 full_sym
+
+    # e.g. "crust_LobClaw_rubber_band"
+    print $symbol->full_sym;
+
+Returns the fully qualified C representation for the symbol.
+
 =head1 COPYRIGHT AND LICENSE
 
     /**

Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Variable.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Variable.pm?rev=806972&r1=806971&r2=806972&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Variable.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Variable.pm Sun Aug 23 14:12:44 2009
@@ -4,7 +4,7 @@
 package Boilerplater::Variable;
 use base qw( Boilerplater::Symbol );
 use Boilerplater::Type;
-use Boilerplater::Util qw( verify_args );
+use Boilerplater::Util qw( verify_args a_isa_b );
 use Carp;
 
 our %new_PARAMS = (
@@ -20,19 +20,15 @@
     my $either = shift;
     verify_args( \%new_PARAMS, @_ ) or confess $@;
     my $self = $either->SUPER::new( %new_PARAMS, @_ );
-    confess "micro_sym is required" unless $self->{micro_sym};
     confess "invalid type"
-        unless ref( $self->{type} )
-            && $self->{type}->isa("Boilerplater::Type");
+        unless a_isa_b( $self->{type}, "Boilerplater::Type" );
     return $self;
 }
 
-sub get_type  { shift->{type} }
-sub micro_sym { shift->{micro_sym} }
+sub get_type { shift->{type} }
 
 sub equals {
     my ( $self, $other ) = @_;
-    return 0 unless $self->{micro_sym} eq $other->{micro_sym};
     return 0 unless $self->{type}->equals( $other->{type} );
     return $self->SUPER::equals($other);
 }
@@ -51,7 +47,7 @@
 sub global_c {
     my $self = shift;
     my $type = $self->{type};
-    my $name = $self->get_prefix . "$self->{class_cnick}_$self->{micro_sym}";
+    my $name = $self->full_sym;
     my $postfix = '';
     if ( $type->is_composite ) {
         $postfix = $type->get_array || '';

Modified: lucene/lucy/trunk/boilerplater/t/051-symbol.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/051-symbol.t?rev=806972&r1=806971&r2=806972&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/051-symbol.t (original)
+++ lucene/lucy/trunk/boilerplater/t/051-symbol.t Sun Aug 23 14:12:44 2009
@@ -1,14 +1,15 @@
 use strict;
 use warnings;
 
-use Test::More tests => 37;
-
 package BoilingThing;
 use base qw( Boilerplater::Symbol );
 
-sub new { return shift->SUPER::new( exposure => 'parcel', @_ ) }
+sub new {
+    return shift->SUPER::new( micro_sym => 'sym', exposure => 'parcel', @_ );
+}
 
 package main;
+use Test::More tests => 44;
 
 for (qw( foo FOO 1Foo Foo_Bar FOOBAR 1FOOBAR )) {
     eval { my $thing = BoilingThing->new( class_name => $_ ) };
@@ -47,3 +48,20 @@
 my $luser = BoilingThing->new( parcel => 'Luser' );
 ok( !$lucifer->equals($luser), "different parcel spoils equals" );
 
+for ( qw( 1foo * 0 ), "\x{263a}" ) {
+    eval { my $thing = BoilingThing->new( micro_sym => $_ ); };
+    like( $@, qr/micro_sym/, "reject bad micro_sym" );
+}
+
+my $ooga  = BoilingThing->new( micro_sym => 'ooga' );
+my $booga = BoilingThing->new( micro_sym => 'booga' );
+ok( !$ooga->equals($booga), "Different micro_sym spoils equals()" );
+
+my $eep = BoilingThing->new(
+    parcel     => 'Eep',
+    class_name => "Op::Ork",
+    micro_sym  => 'ah_ah',
+);
+is( $eep->short_sym, "Ork_ah_ah",     "short_sym" );
+is( $eep->full_sym,  "eep_Ork_ah_ah", "full_sym" );
+

Added: lucene/lucy/trunk/boilerplater/t/200-function.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/200-function.t?rev=806972&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/200-function.t (added)
+++ lucene/lucy/trunk/boilerplater/t/200-function.t Sun Aug 23 14:12:44 2009
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+
+use Test::More tests => 6;
+
+BEGIN { use_ok('Boilerplater::Function') }
+use Boilerplater::Parser;
+use Boilerplater::Parcel;
+
+my $parser = Boilerplater::Parser->new;
+$parser->parcel_definition('parcel Boil;')
+    or die "failed to process parcel_definition";
+
+my %args = (
+    parcel      => 'Boil',
+    return_type => $parser->type('Obj*'),
+    class_name  => 'Boil::Foo',
+    class_cnick => 'Foo',
+    param_list  => $parser->param_list('(i32_t some_num)'),
+    micro_sym   => 'return_an_obj',
+);
+
+my $func = Boilerplater::Function->new(%args);
+isa_ok( $func, "Boilerplater::Function" );
+
+eval { my $death = Boilerplater::Function->new( %args, extra_arg => undef ) };
+like( $@, qr/extra_arg/, "Extra arg kills constructor" );
+
+eval { Boilerplater::Function->new( %args, micro_sym => 'Uh_Oh' ); };
+like( $@, qr/Uh_Oh/, "invalid micro_sym kills constructor" );
+
+my %sub_args = ( class => 'Boil::Obj', cnick => 'Obj' );
+
+isa_ok(
+    $parser->subroutine_declaration_statement( $_, 0, %sub_args, inert => 1 )
+        ->{declared},
+    "Boilerplater::Function",
+    "function declaration: $_"
+    )
+    for (
+    'inert int running_count(int biscuit);',
+    'public inert Hash* init_fave_hash(i32_t num_buckets, bool_t o_rly);',
+    );

Propchange: lucene/lucy/trunk/boilerplater/t/200-function.t
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/boilerplater/t/402-parcel.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/402-parcel.t?rev=806972&r1=806971&r2=806972&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/402-parcel.t (original)
+++ lucene/lucy/trunk/boilerplater/t/402-parcel.t Sun Aug 23 14:12:44 2009
@@ -8,7 +8,9 @@
 package BoilingThing;
 use base qw( Boilerplater::Symbol );
 
-sub new { return shift->SUPER::new( exposure => 'parcel', @_ ) }
+sub new {
+    return shift->SUPER::new( micro_sym => 'sym', exposure => 'parcel', @_ );
+}
 
 package main;
 



Mime
View raw message