Wed Jun 9 07:35:19 UTC 2010 Johnny Willemsen <jwillemsen@remedy.nl>
[MPC.git] / modules / Parser.pm
blobbca6dccddf56f846d0ce0dc34ce650db43782aa9
1 package Parser;
3 # ************************************************************
4 # Description : A basic parser that requires a parse_line override
5 # Author : Chad Elliott
6 # Create Date : 5/16/2002
7 # ************************************************************
9 # ************************************************************
10 # Pragmas
11 # ************************************************************
13 use strict;
14 use FileHandle;
16 use OutputMessage;
17 use StringProcessor;
18 use DirectoryManager;
20 use vars qw(@ISA);
21 @ISA = qw(OutputMessage StringProcessor DirectoryManager);
23 # ************************************************************
24 # Data Section
25 # ************************************************************
27 my %filecache;
29 # ************************************************************
30 # Subroutine Section
31 # ************************************************************
33 sub new {
34 my($class, $inc) = @_;
35 my $self = $class->SUPER::new();
37 ## Set up the internal data members.
38 $self->{'line_number'} = 0;
39 $self->{'include'} = $inc;
41 return $self;
45 sub strip_line {
46 my($self, $line) = @_;
48 ## Keep track of our line number
49 ++$self->{'line_number'};
51 ## Remove comments and leading and trailing white-space.
52 $line =~ s/\/\/.*//;
53 $line =~ s/^\s+//;
54 $line =~ s/\s+$//;
56 return $line;
60 sub preprocess_line {
61 #my $self = shift;
62 #my $fh = shift;
63 #my $line = shift;
64 return $_[0]->strip_line($_[2]);
68 sub read_file {
69 my($self, $input, $cache) = @_;
70 my $ih = new FileHandle();
71 my $status = 1;
72 my $errorString;
74 $self->{'line_number'} = 0;
75 if (open($ih, $input)) {
76 $self->debug("Open $input");
77 if ($cache) {
78 ## If we don't have an array for this file, then start one
79 $filecache{$input} = [] if (!defined $filecache{$input});
81 while(<$ih>) {
82 ## Preprocess the line
83 my $line = $self->preprocess_line($ih, $_);
85 ## Push the line onto the array for this file
86 push(@{$filecache{$input}}, $line);
88 ## Parse the line
89 ($status, $errorString) = $self->parse_line($ih, $line);
91 ## Stop reading the file if we've encountered an error
92 last if (!$status);
95 else {
96 ## We're not caching, so we just preprocess and parse in one call.
97 while(<$ih>) {
98 ($status, $errorString) = $self->parse_line(
99 $ih, $self->preprocess_line($ih, $_));
101 ## Stop reading the file if we've encountered an error
102 last if (!$status);
105 $self->debug("Close $input");
106 close($ih);
108 else {
109 $errorString = "Unable to open \"$input\" for reading";
110 $status = 0;
113 return $status, $errorString;
117 sub cached_file_read {
118 my($self, $input) = @_;
119 my $lines = $filecache{$input};
121 if (defined $lines) {
122 my $status = 1;
123 my $error;
124 $self->{'line_number'} = 0;
125 foreach my $line (@$lines) {
126 ++$self->{'line_number'};
127 ## Since we're "reading" a cached file, we must pass undef as the
128 ## file handle to parse_line().
129 ($status, $error) = $self->parse_line(undef, $line);
131 ## Stop "reading" the file if we've encountered an error
132 last if (!$status);
134 return $status, $error;
137 ## We haven't cached this file yet, read it and cache it.
138 return $self->read_file($input, 1);
142 sub get_line_number {
143 return $_[0]->{'line_number'};
147 sub set_line_number {
148 my($self, $number) = @_;
149 $self->{'line_number'} = $number;
153 sub slash_to_backslash {
154 ## This method is here solely for convenience. It's used to make the
155 ## calling code look cleaner.
156 my($self, $file) = @_;
157 $file =~ s/\//\\/g;
158 return $file;
162 sub get_include_path {
163 return $_[0]->{'include'};
167 sub search_include_path {
168 my($self, $file) = @_;
170 foreach my $include ('.', @{$self->{'include'}}) {
171 return "$include/$file" if (-r "$include/$file");
174 return undef;
178 sub escape_regex_special {
179 my($self, $name) = @_;
180 $name =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
181 return $name;
185 # ************************************************************
186 # Virtual Methods To Be Overridden
187 # ************************************************************
189 sub parse_line {
190 #my $self = shift;
191 #my $ih = shift;
192 #my $line = shift;