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/18 01:39:40 UTC

svn commit: r805213 - in /lucene/lucy/trunk/boilerplater: lib/Boilerplater/Parser.pm lib/Boilerplater/Type/Float.pm t/103-float_type.t

Author: marvin
Date: Mon Aug 17 23:39:40 2009
New Revision: 805213

URL: http://svn.apache.org/viewvc?rev=805213&view=rev
Log:
Commit LUCY-12, adding Boilerplater::Type::Float.

Added:
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Float.pm   (with props)
    lucene/lucy/trunk/boilerplater/t/103-float_type.t   (with props)
Modified:
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm

Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm?rev=805213&r1=805212&r2=805213&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Parser.pm Mon Aug 17 23:39:40 2009
@@ -8,6 +8,7 @@
 use Boilerplater::Type;
 use Boilerplater::Type::Primitive;
 use Boilerplater::Type::Integer;
+use Boilerplater::Type::Float;
 use Carp;
 
 our $grammar = <<'END_GRAMMAR';
@@ -32,6 +33,12 @@
     /([A-Z][A-Za-z0-9]+)(?!\w)/
     { $1 }
 
+primitive_type:
+      c_integer_type
+    | chy_integer_type
+    | float_type
+    { $item[1] }
+
 c_integer_type:
     type_qualifier(s?) c_integer_specifier
     { Boilerplater::Parser->new_integer_type(\%item) }
@@ -40,12 +47,17 @@
     type_qualifier(s?) chy_integer_specifier
     { Boilerplater::Parser->new_integer_type(\%item) }
 
+float_type:
+    type_qualifier(s?) c_float_specifier
+    { Boilerplater::Parser->new_float_type(\%item) }
+
 type_qualifier:
       'const' 
 
 primitive_type_specifier:
       chy_integer_specifier
     | c_integer_specifier 
+    | c_float_specifier 
     { $item[1] }
 
 chy_integer_specifier:
@@ -54,6 +66,9 @@
 c_integer_specifier:
     /(?:char|int|short|long|size_t)(?!\w)/
 
+c_float_specifier:
+    /(?:float|double)(?!\w)/
+
 END_GRAMMAR
 
 sub new { return shift->SUPER::new($grammar) }
@@ -70,6 +85,13 @@
     return Boilerplater::Type::Integer->new(%args);
 }
 
+sub new_float_type {
+    my ( undef, $item ) = @_;
+    my %args = ( specifier => $item->{c_float_specifier} );
+    $args{$_} = 1 for @{ $item->{'type_qualifier(s?)'} };
+    return Boilerplater::Type::Float->new(%args);
+}
+
 sub new_parcel {
     my ( undef, $item ) = @_;
     Boilerplater::Parcel->singleton(

Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Float.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Float.pm?rev=805213&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Float.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Float.pm Mon Aug 17 23:39:40 2009
@@ -0,0 +1,88 @@
+use strict;
+use warnings;
+
+package Boilerplater::Type::Float;
+use base qw( Boilerplater::Type::Primitive );
+use Boilerplater::Util qw( verify_args );
+use Carp;
+
+our %new_PARAMS = (
+    const     => undef,
+    specifier => undef,
+);
+
+our %specifiers = (
+    float  => undef,
+    double => undef,
+);
+
+sub new {
+    my ( $either, %args ) = @_;
+    verify_args( \%new_PARAMS, %args ) or confess $@;
+    confess("Unknown specifier: '$args{specifier}'")
+        unless exists $specifiers{ $args{specifier} };
+
+    # Cache the C representation of this type.
+    my $c_string = $args{const} ? "const $args{specifier}" : $args{specifier};
+
+    return $either->SUPER::new( %args, c_string => $c_string );
+}
+
+sub is_floating {1}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Type::Float - A primitive Type representing a floating point
+number.
+
+=head1 DESCRIPTION
+
+Boilerplater::Type::Float represents floating point types of various widths.
+Currently only two are supported:
+
+    float
+    double
+
+=head1 METHODS
+
+=head2 new
+
+    my $type = Boilerplater::Type::Float->new(
+        const     => 1,           # default: undef
+        specifier => 'double',    # required
+    );
+
+=over
+
+=item * B<const> - Should be true if the type is const.
+
+=item * B<specifier> - Must match one of the supported types.
+
+=back
+
+=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/Type/Float.pm
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/boilerplater/t/103-float_type.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/103-float_type.t?rev=805213&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/103-float_type.t (added)
+++ lucene/lucy/trunk/boilerplater/t/103-float_type.t Mon Aug 17 23:39:40 2009
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+use Test::More tests => 11;
+use Boilerplater::Type::Float;
+use Boilerplater::Parser;
+
+my $float_type = Boilerplater::Type::Float->new(
+    specifier => 'float',
+    const     => 1,
+);
+ok( $float_type->const, "const" );
+is( $float_type->get_specifier, "float" );
+like( $float_type->to_c, qr/const/, "'const' in C representation" );
+
+my $parser = Boilerplater::Parser->new;
+
+for my $specifier (qw( float double)) {
+    is( $parser->c_float_specifier($specifier),
+        $specifier, "c_float_specifier: $specifier" );
+    isa_ok( $parser->float_type($specifier), "Boilerplater::Type::Float" );
+    isa_ok( $parser->float_type("const $specifier"),
+        "Boilerplater::Type::Float" );
+    my $bogus = $specifier . "y";
+    ok( !$parser->c_float_specifier($bogus),
+        "c_float_specifier guards against partial word matches" );
+}
+

Propchange: lucene/lucy/trunk/boilerplater/t/103-float_type.t
------------------------------------------------------------------------------
    svn:eol-style = native