summaryrefslogtreecommitdiff
path: root/ACE/MPC/modules/Parser.pm
blob: bca6dccddf56f846d0ce0dc34ce650db43782aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package Parser;

# ************************************************************
# Description   : A basic parser that requires a parse_line override
# Author        : Chad Elliott
# Create Date   : 5/16/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use FileHandle;

use OutputMessage;
use StringProcessor;
use DirectoryManager;

use vars qw(@ISA);
@ISA = qw(OutputMessage StringProcessor DirectoryManager);

# ************************************************************
# Data Section
# ************************************************************

my %filecache;

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class, $inc) = @_;
  my $self = $class->SUPER::new();

  ## Set up the internal data members.
  $self->{'line_number'} = 0;
  $self->{'include'}     = $inc;

  return $self;
}


sub strip_line {
  my($self, $line) = @_;

  ## Keep track of our line number
  ++$self->{'line_number'};

  ## Remove comments and leading and trailing white-space.
  $line =~ s/\/\/.*//;
  $line =~ s/^\s+//;
  $line =~ s/\s+$//;

  return $line;
}


sub preprocess_line {
  #my $self = shift;
  #my $fh   = shift;
  #my $line = shift;
  return $_[0]->strip_line($_[2]);
}


sub read_file {
  my($self, $input, $cache) = @_;
  my $ih = new FileHandle();
  my $status = 1;
  my $errorString;

  $self->{'line_number'} = 0;
  if (open($ih, $input)) {
    $self->debug("Open $input");
    if ($cache) {
      ## If we don't have an array for this file, then start one
      $filecache{$input} = [] if (!defined $filecache{$input});

      while(<$ih>) {
        ## Preprocess the line
        my $line = $self->preprocess_line($ih, $_);

        ## Push the line onto the array for this file
        push(@{$filecache{$input}}, $line);

        ## Parse the line
        ($status, $errorString) = $self->parse_line($ih, $line);

        ## Stop reading the file if we've encountered an error
        last if (!$status);
      }
    }
    else {
      ## We're not caching, so we just preprocess and parse in one call.
      while(<$ih>) {
        ($status, $errorString) = $self->parse_line(
                                    $ih, $self->preprocess_line($ih, $_));

        ## Stop reading the file if we've encountered an error
        last if (!$status);
      }
    }
    $self->debug("Close $input");
    close($ih);
  }
  else {
    $errorString = "Unable to open \"$input\" for reading";
    $status = 0;
  }

  return $status, $errorString;
}


sub cached_file_read {
  my($self, $input) = @_;
  my $lines = $filecache{$input};

  if (defined $lines) {
    my $status = 1;
    my $error;
    $self->{'line_number'} = 0;
    foreach my $line (@$lines) {
      ++$self->{'line_number'};
      ## Since we're "reading" a cached file, we must pass undef as the
      ## file handle to parse_line().
      ($status, $error) = $self->parse_line(undef, $line);

      ## Stop "reading" the file if we've encountered an error
      last if (!$status);
    }
    return $status, $error;
  }

  ## We haven't cached this file yet, read it and cache it.
  return $self->read_file($input, 1);
}


sub get_line_number {
  return $_[0]->{'line_number'};
}


sub set_line_number {
  my($self, $number) = @_;
  $self->{'line_number'} = $number;
}


sub slash_to_backslash {
  ## This method is here solely for convenience.  It's used to make the
  ## calling code look cleaner.
  my($self, $file) = @_;
  $file =~ s/\//\\/g;
  return $file;
}


sub get_include_path {
  return $_[0]->{'include'};
}


sub search_include_path {
  my($self, $file) = @_;

  foreach my $include ('.', @{$self->{'include'}}) {
    return "$include/$file" if (-r "$include/$file");
  }

  return undef;
}


sub escape_regex_special {
  my($self, $name) = @_;
  $name =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
  return $name;
}


# ************************************************************
# Virtual Methods To Be Overridden
# ************************************************************

sub parse_line {
  #my $self = shift;
  #my $ih   = shift;
  #my $line = shift;
}


1;