Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
107 bj 1
package Parser;
2
 
3
# ************************************************************
4
# Description   : A basic parser that requires a parse_line override
5
# Author        : Chad Elliott
6
# Create Date   : 5/16/2002
7
# ************************************************************
8
 
9
# ************************************************************
10
# Pragmas
11
# ************************************************************
12
 
13
use strict;
14
use FileHandle;
15
 
16
use OutputMessage;
17
use StringProcessor;
18
use DirectoryManager;
19
 
20
use vars qw(@ISA);
21
@ISA = qw(OutputMessage StringProcessor DirectoryManager);
22
 
23
# ************************************************************
24
# Data Section
25
# ************************************************************
26
 
27
my(%filecache) = ();
28
my($silent)    = 'MPC_SILENT';
29
my($logging)   = 'MPC_LOGGING';
30
 
31
# ************************************************************
32
# Subroutine Section
33
# ************************************************************
34
 
35
sub new {
36
  my($class) = shift;
37
  my($inc)   = shift;
38
  my($log)   = $ENV{$logging};
39
 
40
  ## The order of these array variables must correspond to the
41
  ## order of the parameters to OutputMessage::new().
42
  my($params) = (defined $ENV{$silent} ||
43
                 defined $log ? [0, 0, 0, 0] : [0, 1, 1, 1]);
44
 
45
  if (defined $log) {
46
    if ($log =~ /info(rmation)?\s*=\s*(\d+)/i) {
47
      $$params[0] = $2;
48
    }
49
    if ($log =~ /warn(ing)?\s*=\s*(\d+)/i) {
50
      $$params[1] = $2;
51
    }
52
    if ($log =~ /diag(nostic)?\s*=\s*(\d+)/i) {
53
      $$params[2] = $2;
54
    }
55
    if ($log =~ /detail(s)?\s*=\s*(\d+)/i) {
56
      $$params[3] = $2;
57
    }
58
  }
59
 
60
  my($self) = $class->SUPER::new(@$params);
61
 
62
  $self->{'line_number'} = 0;
63
  $self->{'include'}     = $inc;
64
 
65
  return $self;
66
}
67
 
68
 
69
sub strip_line {
70
  my($self) = shift;
71
  my($line) = shift;
72
 
73
  ++$self->{'line_number'};
74
  $line =~ s/\/\/.*//;
75
  $line =~ s/^\s+//;
76
  $line =~ s/\s+$//;
77
 
78
  return $line;
79
}
80
 
81
 
82
sub preprocess_line {
83
  #my($self) = shift;
84
  #my($fh)   = shift;
85
  #my($line) = shift;
86
  return $_[0]->strip_line($_[2]);
87
}
88
 
89
 
90
sub read_file {
91
  my($self)        = shift;
92
  my($input)       = shift;
93
  my($cache)       = shift;
94
  my($ih)          = new FileHandle();
95
  my($status)      = 1;
96
  my($errorString) = undef;
97
 
98
  $self->{'line_number'} = 0;
99
  if (open($ih, $input)) {
100
    if ($cache) {
101
      ## If we don't have an array for this file, then start one
102
      if (!defined $filecache{$input}) {
103
        $filecache{$input} = [];
104
      }
105
 
106
      while(<$ih>) {
107
        my($line) = $self->preprocess_line($ih, $_);
108
 
109
        ## Push the line onto the array for this file
110
        push(@{$filecache{$input}}, $line);
111
 
112
        ($status, $errorString) = $self->parse_line($ih, $line);
113
 
114
        if (!$status) {
115
          last;
116
        }
117
      }
118
    }
119
    else {
120
      while(<$ih>) {
121
        ($status, $errorString) = $self->parse_line(
122
                                    $ih, $self->preprocess_line($ih, $_));
123
 
124
        if (!$status) {
125
          last;
126
        }
127
      }
128
    }
129
    close($ih);
130
  }
131
  else {
132
    $errorString = "Unable to open \"$input\" for reading";
133
    $status = 0;
134
  }
135
 
136
  return $status, $errorString;
137
}
138
 
139
 
140
sub cached_file_read {
141
  my($self)  = shift;
142
  my($input) = shift;
143
  my($lines) = $filecache{$input};
144
 
145
  if (defined $lines) {
146
    my($status) = 1;
147
    my($error)  = undef;
148
    $self->{'line_number'} = 0;
149
    foreach my $line (@$lines) {
150
      ++$self->{'line_number'};
151
      ($status, $error) = $self->parse_line(undef, $line);
152
 
153
      if (!$status) {
154
        last;
155
      }
156
    }
157
    return $status, $error;
158
  }
159
 
160
  return $self->read_file($input, 1);
161
}
162
 
163
 
164
sub get_line_number {
165
  my($self) = shift;
166
  return $self->{'line_number'};
167
}
168
 
169
 
170
sub set_line_number {
171
  my($self)   = shift;
172
  my($number) = shift;
173
  $self->{'line_number'} = $number;
174
}
175
 
176
 
177
sub slash_to_backslash {
178
  my($self) = shift;
179
  my($file) = shift;
180
  $file =~ s/\//\\/g;
181
  return $file;
182
}
183
 
184
 
185
sub get_include_path {
186
  my($self) = shift;
187
  return $self->{'include'};
188
}
189
 
190
 
191
sub search_include_path {
192
  my($self)  = shift;
193
  my($file)  = shift;
194
 
195
  foreach my $include ('.', @{$self->{'include'}}) {
196
    if (-r "$include/$file") {
197
      return "$include/$file";
198
    }
199
  }
200
 
201
  return undef;
202
}
203
 
204
 
205
sub escape_regex_special {
206
  my($self) = shift;
207
  my($name) = shift;
208
 
209
  $name =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
210
  return $name;
211
}
212
 
213
 
214
# ************************************************************
215
# Virtual Methods To Be Overridden
216
# ************************************************************
217
 
218
sub parse_line {
219
  #my($self) = shift;
220
  #my($ih)   = shift;
221
  #my($line) = shift;
222
}
223
 
224
 
225
1;