~narazaka/kawari-perl/trunk

« back to all changes in this revision

Viewing changes to lib/Kawari/Dictionary.pm

  • Committer: Narazaka
  • Date: 2012-05-25 15:46:18 UTC
  • mfrom: (5.2.16 dictionary)
  • Revision ID: info@narazaka.net-20120525154618-cdj4wqhe1abt1i28
merged dictionary

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
        return bless $data,$package;
13
13
}
14
14
 
15
 
# TODO: access readonly (writeprotect)
 
15
# TODO: entry name validation
16
16
# (temporary dictionary can be satisfied by using temporary instance of this)
17
17
 
18
18
sub load{
47
47
                                $self->push(undef, $script);
48
48
                        }else{ # =dict
49
49
                                while($string !~ /^[ \t\n]*$/){
50
 
                                        $string =~ s/^[ \t\n]*([^ \t]+?)[ \t]*://s;
 
50
                                        $string =~ s/^[ \t\n]*(.+?)[ \t]*://s;
51
51
                                        my ($entry_names_str) = $1;
52
 
                                        die 'dict error' unless defined $entry_names_str;
53
 
                                        die 'invalid entry name' unless $entry_names_str =~ /^[\w\d_.?@]+$/; # TODO: validation
 
52
                                        die 'dict error',"[$1]",$string unless defined $entry_names_str;
54
53
                                        my @entry_names = split /[ \t]*,[ \t]*/,$entry_names_str;
55
54
                                        while($string !~ /^[ \t]*(?:\n|$)/){
56
55
                                                $string =~ s/^[ \t]*,//;
63
62
                }
64
63
        }
65
64
}
66
 
=begin comments
67
 
                my $keylist;
68
 
                my $noblacketend;
69
 
                my $entryvalue;
70
 
                $entryvalue = Kawari::Dict::Value::Analize->new();
71
 
                my $kisvalue = Kawari::Dict::Value::Analize->new('cmd');
72
 
                my @mode;
73
 
                my %flag;
74
 
                while(<$fh>){
75
 
                        chomp;
76
 
#print STDERR "\n_=$_\n";###
77
 
                        if(/^[ \t]*(?:#.*)?$/){ # コメント・空白行をスキップ
78
 
                                next;
79
 
                        }elsif(/^:(.*)$/){
80
 
                                if($1 eq 'rem'){
81
 
                                        $flag{comment} = 1;
82
 
                                }elsif($1 eq 'endrem'){
83
 
                                        $flag{comment} = undef;
84
 
                                }
85
 
                        }elsif($flag{comment}){
86
 
                                next;
87
 
                        }elsif(/^=kis$/){
88
 
                                die 'Unmatch blacket.'if !$noblacketend;
89
 
                                push @mode,'kis';
90
 
                        }elsif(/^=dict$/){
91
 
                                die 'Unmatch blacket.'if !$noblacketend;
92
 
                                push @mode,'dict';
93
 
                        }elsif(/^=end$/){
94
 
                                die 'Unmatch blacket.'if !$noblacketend;
95
 
                                pop @mode;
96
 
                        }else{
97
 
                                if($mode->[-1] eq 'kis'){
98
 
                                        $noblacketend = $kisvalue->analize($_);
99
 
                                }else{
100
 
                                        my ($key,$value);
101
 
                                        if(!$entryvalue->stackdepth()){ # 括弧の外
102
 
                                                ($key,$value) = /^[ \t]*(.+?)[ \t]*:[ \t]*(.*)[ \t]*$/; # エントリのキーと値を分割
103
 
                                                die "Wrong format." unless defined $key;
104
 
                                                $keylist = [split /[ \t]*,[ \t]*/,$key]; # キーの分割
105
 
                                                $entryvalue = Kawari::Dict::Value::Analize->new();
106
 
                                        }else{
107
 
                                                $value = $_;
108
 
                                        }
109
 
#print STDERR "  keylist=@{[join '/',@$keylist]}\n";###
110
 
#print STDERR "  value  =$value\n";###
111
 
                                        $noblacketend = $entryvalue->analize($value);
112
 
                                        if($noblacketend){
113
 
                                                $self->pushentry($keylist,$entryvalue->value()->valuetree());
114
 
                                                #push @{$self->{$_}},$entryvalue->getvalue() for @$keylist;
115
 
#use Data::Dumper;
116
 
#print STDERR Data::Dumper->Dump($entryvalue->{value}->{valuetree});###
117
 
                                        }
118
 
                                }
119
 
                        }
120
 
                }
121
 
                close $fh;
122
 
                if($kisvalue->stackdepth() || $entryvalue->stackdepth()){
123
 
                        die "Unmatch bracket.";
124
 
                }
125
 
                push @{$self->{'/'}},Kawari::Dict::Value->new('readonly',$kisvalue->value());
126
 
#print STDERR "Fin.\n";###
127
 
#print "$_:",(join " | ",@{$self->{dic}->{$_}}),"\n" for sort keys %{$self->{dic}};
128
 
#print STDERR "$_:",(join "",map {$_->{bra}.$_->{value}.$self->{cket}->{$_->{bra}}->{join}} @{$self->{dic}->{$_}->[0]}),"\n" for sort keys %{$self->{dic}};
129
 
        }
130
 
        return undef;
131
 
}
132
 
=cut
133
65
 
134
66
sub size{
135
67
        my $self = shift;
151
83
        return if $self->{attr}->{$entry_name} eq 'readonly';
152
84
        my $value = shift;
153
85
        my $index = shift;
154
 
        defined $index ? $self->{dictionary}->{$entry_name}->[$index] = $value : $self->{dictionary}->{$entry_name} = [$value];
 
86
        defined $index ? ($self->{dictionary}->{$entry_name}->[$index] = $value) : ($self->{dictionary}->{$entry_name} = [$value]);
155
87
}
156
88
 
157
89
sub get{