File: regexp.pm

package info (click to toggle)
filepp 1.7.1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,120 kB
  • ctags: 240
  • sloc: perl: 2,597; makefile: 489; sh: 174; ansic: 15
file content (148 lines) | stat: -rw-r--r-- 5,198 bytes parent folder | download | duplicates (2)
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
########################################################################
#
# regexp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#
########################################################################
#
#  Project      :  File Preprocessor - regexp module
#  Filename     :  $RCSfile: regexp.pm,v $
#  Author       :  $Author: darren $
#  Maintainer   :  Darren Miller: darren@cabaret.demon.co.uk
#  File version :  $Revision: 1.7 $
#  Last changed :  $Date: 2003/07/02 22:23:16 $
#  Description  :  This implements regular expression replacement routines
#  Licence      :  GNU copyleft
#
########################################################################
# THIS IS A FILEPP MODULE, YOU NEED FILEPP TO USE IT!!!
# usage: filepp -m regexp.pm <files>
########################################################################
package Regexp;

use strict;

# version number of module
my $VERSION = '1.0.2';

my @Regexp;  # list of regular expressions
my @Replace; # list of replacements for regular expressions

########################################################################
# Function to read regular expression
########################################################################
sub ParseRegexp
{
    my $input = shift;
    my @Args= split(/((?<!\\)\/)/, $input);
    if($#Args == 5 && $Args[1] eq "/" && $Args[3] eq "/" && $Args[5] eq "/") {
	return (1, $Args[2], $Args[4]);
    }    
    Filepp::Warning("Regexp: could not parse <$input>");
    return 0;
}

########################################################################
# Regular expression keyword
########################################################################
sub AddRegexp
{
    my $input = shift;
    my ($parseok, $regexp, $replace) = ParseRegexp($input);
    if($parseok) {
	Filepp::Debug("Regexp: Adding regexp /$regexp/$replace/");
	push(@Regexp, $regexp);
	push(@Replace, $replace);
    }
}
Filepp::AddKeyword("regexp", "Regexp::AddRegexp");

########################################################################
# Removes a regular expression
########################################################################
sub RmRegexp
{
    # Note: this only removes the first occurence of the regular expression
    # if the regular expression is multiply defined.
    my $input = shift;
    my ($parseok, $regexp, $replace) = ParseRegexp($input);
    if($parseok) {
	my $i = 0;
	# find regexp
	while($i<=$#Regexp &&
	      !($Regexp[$i] eq $regexp && $Replace[$i] eq $replace)) { $i++; }
	# remove regexp if found
	if($i<=$#Regexp) {
	    for(; $i<$#Regexp; $i++) {
		$Regexp[$i] = $Regexp[$i+1];
		$Replace[$i] = $Replace[$i+1];
	    }
	    Filepp::Debug("Regexp: Removed regexp /$regexp/$replace/");
	    pop(@Regexp);
	    pop(@Replace);
	}
    }
}
Filepp::AddKeyword("rmregexp", "Regexp::RmRegexp");

########################################################################
# Show current regexp list - only works in debug mode
########################################################################
sub ShowRegexp
{
    Filepp::Debug("Regexp: current regular expressions:");
    my $i;    
    for($i = 0; $i <= $#Regexp; $i++) {
	Filepp::Debug("Regexp ".$i.":\t/".$Regexp[$i]."/".$Replace[$i]."/");
    }    
}

########################################################################
# Regular expression replacement routine
########################################################################
sub ReplaceRegexp
{
    my $string = shift;
    my $i;
    
    for($i = 0; $i <= $#Regexp; $i++) {
	my $regexp = Filepp::RunProcessors($Regexp[$i], 2);
	my $replace = Filepp::RunProcessors($Replace[$i], 2);
	# eval is used to make sure all \1 etc. are converted correctly
 	my $evalstring = sprintf "\$string =~ s/$regexp/$replace/g";
	Filepp::Debug("Regexping: <$evalstring> with string = <$string>", 2);
 	eval $evalstring;
	Filepp::Debug("Regexp output: <$string>", 2);
    }
    
    return $string;
}
Filepp::AddProcessor("Regexp::ReplaceRegexp");

########################################################################
# Check for command line specifiled regexp (only one allowed)
########################################################################
if(Filepp::Ifdef("REGEXP")) {
    my $input = Filepp::ReplaceDefines("REGEXP");
    my ($parseok, $regexp, $replace) = ParseRegexp($input);
    if($parseok) {
	AddRegexp($input);
	Filepp::Undef("REGEXP");
    }
}

return 1;
########################################################################
# End of file
########################################################################