IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 23, 2005, 2:15:15 PM (21 years ago)
Author:
jhoblitt
Message:

re-add _merge_duplicates()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-Metadata-Config/lib/PS/IPP/Metadata/Config.pm

    r3485 r3488  
    11# Copyright (c) 2005  Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.11 2005-03-23 21:30:37 jhoblitt Exp $
     3# $Id: Config.pm,v 1.12 2005-03-24 00:15:15 jhoblitt Exp $
    44
    55package PS::IPP::Metadata::Config;
     
    4545    return undef unless defined $tree;
    4646
     47    # look for duplicate names, there should be none after processing the
     48    # multi-symbols
     49    $self->_merge_duplicates( $tree ) or return undef;
     50
    4751    #print Dumper($tree);
    4852
    4953    return $tree;
     54}
     55
     56sub _merge_duplicates {
     57    my ( $self, $tree ) = @_;
     58
     59    # encountered names
     60    my %names;
     61
     62    # Iteratate through the parse tree looking for duplicate declarations and
     63    # resolving them by discarding elements according to the value of
     64    # 'overwrite'.
     65    for (my $i = 0; $i < @{$tree}; $i++) {
     66        my $elem = $tree->[$i];
     67
     68        # stop if the prevous pass removed the last element and called redo
     69        last unless defined $elem;
     70               
     71        # recurse through nested metadata
     72        if ( $elem->{class} eq "metadata" ) {
     73            $self->_merge_duplicates( $elem->{value} );
     74        }
     75
     76        # ignore elements with the "multi" flag
     77        if ( defined $elem->{multi} ) {
     78            delete $elem->{multi};
     79            next;
     80        }
     81
     82        if ( defined $names{ $elem->{name} } ) {
     83            if ( $self->{overwrite} ) {
     84                # remove the previous occurance
     85                carp "duplicate variable name: ", $elem->{name}
     86                    , ", removed previous occurance\n";
     87                splice @{$tree}, $names{ $elem->{name} }, 1;
     88            } else {
     89                # remove the current occurance
     90                carp "duplicate variable name: ", $elem->{name}
     91                    , ", removed\n";
     92                splice @{$tree}, $i, 1;
     93            }
     94
     95            # the list just got shorter by one element so we don't want to
     96            # increment the cursor
     97            redo;
     98        }
     99
     100        # add element name and location to record of previously seen names
     101        $names{ $elem->{name} } = $i;
     102    }
     103
     104    return 1;
    50105}
    51106
     
    86141    <skip:'[ \t\r]*'> name type <matchrule:$item{type}> comment(?) "\n"
    87142        {
     143            $thisparser->{local}{name}{ $item{name} }++
     144                unless defined $thisparser->{local}{name}{ $item{name} };
     145
    88146            $return = {
    89147                class   => 'scalar',
     
    95153            $return->{comment} = $item{'comment(?)'}[0]
    96154                if $item{'comment(?)'}[0];
     155
     156            if ( defined $thisparser->{local}{name}{ $item{name} } ) {
     157                $return->{multi}++
     158                    if $thisparser->{local}{name}{ $item{name} } =~ /MULTI/;
     159            } else {
     160                $thisparser->{local}{name}{ $item{name} }++;
     161            }
    97162        }
    98163
     
    100165     <skip:'[ \t\r]*'> vname vtype vvalue[ $item{vtype} ] comment(?) "\n"
    101166        {
     167            $thisparser->{local}{name}{ $item{vname} }++
     168                unless defined $thisparser->{local}{name}{ $item{vname} };
     169
    102170            $return = {
    103171                class   => 'vector',
     
    113181
    114182multi_declare:
    115     <skip:'[ \t\r]*'> name /MULTI/i comment(?) "\n"
    116         {{
    117             class   => 'comment_line',
    118         }}
     183    <skip:'[ \t\r]*'> name /MULTI/i <commit> comment(?) "\n"
     184        {
     185            if ( ! defined $thisparser->{local}{name}{ $item{name} } ) {
     186                $thisparser->{local}{name}{ $item{name} } = "MULTI";
     187                $return = { class   => 'comment_line' };
     188            } else {
     189                $return = undef;
     190            }
     191        }
     192    | <error?:redefinition of MULTI> <reject>
    119193
    120194metadata:
Note: See TracChangeset for help on using the changeset viewer.