ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/Switcher.pm
Revision: 1.1
Committed: Mon Mar 1 10:35:01 1999 UTC (26 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
Log Message:
Utility modules

File Contents

# User Rev Content
1 williamc 1.1 #!/usr/local/bin/perl5
2     #
3     # Switcher Module
4     #
5     # Look for elements given in input has in a string
6     # If found then call a routine name with the same name
7     # Implemented as an object to maintain state info between each line
8     # processed.
9    
10     package Switcher;
11     require Exporter;
12     use Carp;
13     @ISA = qw(Exporter);
14    
15     sub new {
16     my $class=shift;
17     my $hash=shift;
18     my $file=shift;
19     my $self = {};
20     bless $self, $class;
21     $self->_initialise($hash,$file);
22     return $self;
23     }
24    
25     sub _initialise (hash1) {
26     my $self=shift;
27     my $inlabelhash=shift;
28     my $newkey;
29     my $key;
30     $self->{filename}=shift;
31     $self->{labelhash}={};
32     $self->{Strict_no_cr}='yes'; # set to 'no' to retain \n's
33    
34     # setup SGML type tag definitions
35     # Others may be added without problems but ensure to provide
36     # a closure with a hash value of the correct type
37     # No capitals thanks.
38     %{$self->{opencontext}}=(
39     '<' => 'starttag',
40     '</' => 'endtag'
41     );
42     %{$self->{closecontext}}= (
43     ">" => 'tag'
44     );
45    
46     # Fill in the blanks in the user supplied hash
47     if ( ! ( defined $$inlabelhash{'none'} )) { # check for none target
48     $$inlabelhash{'none'}='none';
49     }
50     block1: foreach $key ( keys %$inlabelhash ) {
51     ($newkey=$key)=~tr[A-Z][a-z];
52     ${$self->{labelhash}}{$newkey}=$$inlabelhash{$key};
53     foreach $context ( values %{$self->{opencontext}} ) {
54     next block1 if $newkey=~/$context/i;
55     }
56     foreach $context ( values %{$self->{opencontext}} ) {
57     if ( defined ${$self->{labelhash}}{$newkey."_".$context} ) {
58     next block1;
59     }
60     # print "Setting ".$newkey."_".$context." = 'none'\n";
61     ${$self->{labelhash}}{$newkey."_".$context}='none';
62     }
63     }
64    
65    
66     foreach $key ( %$self->{labelhash} ) {
67     ${$self->{ContextHash}}{$key} = 0;
68     }
69     $self->{InTag}="none";
70     $taglabel="";
71     $self->{tagblock}=[];
72     @{$self->{lastcon}}= qw(none);
73     }
74    
75    
76     sub parse {
77     my $self=shift;
78     my $mykey="";
79     my $key;
80     my $word;
81     my $filein="";
82     $self->{linecount}=0;
83     use FileHandle;
84     my $filehandle=FileHandle->new();
85     open( $filehandle , "$self->{filename}" )
86     or carp "Switcher: Cannot open $self->{filename} $! \n";
87     while ( <$filehandle> ) {
88     if ( $self->{Strict_no_cr} eq 'yes' ) {
89     chomp;
90     }
91     @{$self->{words}}=split /\b/;
92     $self->{linecount}++;
93     WHILELOOP: while ( ($word=shift @{$self->{words}}) ne "" ) {
94     # print $word." --- Word\n";
95     if ( $word=~/(>)(<.*)/ ) {
96     unshift @{$self->{words}}, $2;
97     $word=$1;
98     }
99     #test against opencontext
100     foreach $mykey ( keys ( %{$self->{opencontext}}) ) {
101     if ( $word=~/$mykey/i ) {
102     # Send current tagblock (if its not empty)
103     # to the Lastest Context Routine
104     $self->_closeprevious();
105     # Now carry on the parse
106     $self->{InTag}=${$self->{opencontext}}{$mykey};
107     $#{$self->{tagblock}}=-1;
108     next WHILELOOP;
109     }
110     }
111     #test against closed context
112     foreach $key ( keys %{$self->{closecontext}} ) {
113     if ( $word=~/$key/i ) {
114     if ( $self->{InTag}=~/${$self->{closecontext}}{$key}/ ) {
115     $temp=(shift @{$self->{tagblock}});
116     $temp=~tr[A-Z][a-z];
117     $rtname=($temp)."_".$self->{InTag};
118     $self->{InTag}="none";
119     # Do we call the tag init routine?
120     if ( ( defined ( ${$self->{labelhash}}{$rtname} )) &&
121     ( ! ( ${$self->{labelhash}}{$rtname}=~/none/i )) ) {
122     &{${$self->{labelhash}}{$rtname}}( $temp,
123     @{$self->{tagblock}});
124     }
125     $self->_flipcontext($temp);
126     $#{$self->{tagblock}}= -1;
127     next WHILELOOP;
128     }
129     else {
130     die "Unmatched \"$key\" on line $self->{linecount}"
131     if $self->{InTag} eq "none";
132     die "Error: Wrong closure \"$key\" on line $self->{linecount}";
133     }
134     }
135     }
136     push @{$self->{tagblock}}, $word;
137     } #end word while
138     } #end fileglob while
139     $self->_closeprevious();
140     close $filehandle;
141     }
142    
143     sub context ($key) {
144     my $self=shift;
145     my $key=shift;
146     $key=~tr[A-Z][a-z];
147     return ( ${$self->{ContextHash}}{$key} );
148     }
149    
150    
151     # convert array of value=constants to a hash
152     # returns reference to the array
153     sub SetupValueHash(reftoarray) {
154     my $self=shift;
155     my $arrayref=shift;
156     my $side="r";
157     my $key='';
158     my $thiskey;
159     my $wordto="";
160     my $word;
161     my $quotes=0;
162     local $varhash;
163    
164     $varhash = {};
165     foreach $word ( @$arrayref ) {
166     chomp;
167     if ( $word=~/=/) {
168     $side="l";
169     if ( $word=~s/^=// ) {
170     unshift @$arrayref, $word;
171     }
172     if ( $word=~s/^.*=// ) {
173     ($thiskey=$word)=~tr[A-Z][a-z]; #keys all lower case
174     $key=$key.$thiskey;
175     }
176     next;
177     }
178     if ( $word=~/\"/ ) {
179     $quotes= ( ! $quotes);
180     if ( $word=~s/^"// ) {
181     unshift @$arrayref, $word;
182     }
183     next;
184     }
185     if ( ( $word=~/\s/ ) && ( ! $quotes) ) {
186     $side="r";
187     if ( $key=~/./) {
188     $$varhash{$key}=$wordto;
189     }
190     $key='';
191     $wordto="";
192     next;
193     }
194     if ( $side=~/r/) {
195     ($thiskey=$word)=~tr[A-Z][a-z]; #keys all lower case
196     $key=$key.$thiskey;
197     }
198     else {
199     $wordto=$wordto.$word;
200     }
201     }
202     if ( $side=~/l/ ) { $$varhash{$key}=$wordto; }
203     return $varhash;
204     }
205    
206     sub _flipcontext(Hashkey) {
207     my $self=shift;
208     my $key = shift;
209     ${$self->{ContextHash}}{$key}=(! ${$self->{ContextHash}}{$key});
210     # sort out the context stack
211     if ( ! ${$self->{ContextHash}}{$key} ) {
212     pop @{$self->{lastcon}};
213     # print "** poping ".$key."\n";
214     }
215     else {
216     # print "** pushing ".$key."\n";
217     push @{$self->{lastcon}}, $key;
218     }
219     }
220     sub _closeprevious {
221     my $self=shift;
222     if ( $#{$self->{tagblock}} != -1 ) {
223     if (( defined
224     ${$self->{labelhash}}{${$self->{lastcon}}[$#{$self->{lastcon}}]} ) &&
225     ( ! ( ${$self->{labelhash}}{${$self->{lastcon}}[$#{$self->{lastcon}}]}
226     =~/none/i )) ) {
227     &{ ${$self->{labelhash}}{${$self->{lastcon}}[$#{$self->{lastcon}}]}}(
228     ${$self->{lastcon}}[$#{$self->{lastcon}}], @{$self->{tagblock}});
229     }
230     }
231     }
232    
233     sub checkparam($hash, $name, $key) {
234     my $self=shift;
235     my $hashref=shift;
236     my $name=shift;
237     my $key=shift;
238    
239     if ( ! defined $$hashref{$key} ) {
240     print "BootParser: Badly formed $name tag -".
241     " undefined $key parameter\n";
242     exit 1;
243     }
244     $$hashref{$key}=~s/["']//;
245     }
246