File: parse.sub

package info (click to toggle)
mpich 3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 131,836 kB
  • sloc: ansic: 975,868; cpp: 57,437; f90: 53,762; perl: 19,562; xml: 12,464; sh: 12,303; fortran: 7,875; makefile: 7,078; ruby: 126; java: 100; python: 98; lisp: 19; php: 8; sed: 4
file content (225 lines) | stat: -rwxr-xr-x 6,168 bytes parent folder | download | duplicates (5)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! /usr/bin/env perl
#
# File of useful routines use to process the MPI source files.  This is 
# included by routines that process parameters and error messages, along with
# tools to check for proper usage (such as system routines and preprocessor
# tests).

#
# GetBalancedParen( FD, curline )
# Returns a balanced parenthesis string, starting at curline.  Reads from FD
# if necessary.  Skips any comments.
# Returns the pair (leading, result, remainder)
# Leading is anything before the opening paren.  If no opening paren in the
# line, returns the current line as "leading"
# Newlines are removed.
sub GetBalancedParen {
    my $paren_count = 1;
    my $result = "";
    my $count = 0;
    my $leading = "";
    my $maxcount = 200;
    $FD = $_[0];
    $curline = $_[1];
    # Remove escaped newlines
    $curline =~ s/\\$//;
    
    if ($curline =~ /^([^\(]*)\((.*)$/) {
	$leading = $1;
	$curline = $2;
	$result  = "(";
	print "Found open paren\n" if $debug;
    }
    else {
	$leading = $curline;
	return ($leading, "", "" );
    }

    while ($count < $maxcount && $paren_count > 0) {
	if ($curline =~ /^([^\(\)]*\()(.*$)/) {
	    # Found an opening paren
	    $result .= $1;
	    $curline = $2;
	    $paren_count++;
	    print "Found open paren\n" if $debug;
	}
        elsif ($curline =~ /^([^\(\)]*\))(.*$)/) {
	    # Found a closing paren
	    $result .= $1;
	    $curline = $2;
	    $paren_count--;
	    print "Found close paren\n" if $debug;
	}
	else {
	    # Need to read a new line
	    $result .= $curline;
	    $curline = <$FD>;
	    $curline =~ s/[\r]*\n//;
	    # Remove escaped newlines
	    $curline =~ s/\\$//;
	}
	$count ++;
    }
    return ($leading, $result, $curline);
}

# Like get balanced paren, but for a string.  Simpler because it does not need
# to handle balanced text.  
sub GetString {
    my $result = "";
    my $count = 0;
    my $leading = "";
    my $maxcount = 200;
    $FD = $_[0];
    $curline = $_[1];
    
    if ($curline =~ /^([^\"]*)\"(.*)$/) {
	$leading = $1;
	$curline = $2;
	$result  = "\"";
	print "Found quote\n" if $debug;
    }
    else {
	$leading = $curline;
	return ($leading, "", "" );
    }

    while ($count < $maxcount) {
	if ($curline =~ /^([^\"]*\\\")(.*$)/) {
	    # Found an escaped quote
	    $result .= $1;
	    $curline = $2;
	    print "Found escaped quote\n" if $debug;
	}
        elsif ($curline =~ /^([^\"]*\")(.*$)/) {
	    # Found the closing quote
	    $result .= $1;
	    $curline = $2;
	    print "Found closing quote\n" if $debug;
	    last;
	}
	else {
	    # Need to read a new line
	    $result .= $curline;
	    $curline = <$FD>;
	    $curline =~ s/[\r]*\n//;
	}
	$count ++;
    }
    return ($leading, $result, $curline);
}
#
# GetSubArgs( FD, curline ) returns an array of the arguments of a routine.
# Specifically, it converts (a,b,c) into an array containing "a", "b", and "c".
# The special feature of this is that any commas that are within balanced
# parenthesis are included within their argument.
# Actually returns
#  (leader, remainder, (@args) )
# in this order so the last values are always all of the args
# so you don't need to know
sub GetSubArgs {
    my @args = ();
    my $curline;
    my ($outer, $leader, $remainder, $arg);

    $FD = $_[0];
    $curline = $_[1];
    # Remove any embedded newlines
    $curline =~ s/[\r\n]//g;

    $curline =~ /^\(/ || die "No initial paren";
    ($leader, $outer, $remainder ) = &GetBalancedParen( $FD, $curline );


    # Strip off the first and last parens
    # Because of the greedy algorithm, the \s before the closing paren
    # won't be used.  To avoid problems with empty arguments, we remove
    # those blanks separately
    $outer =~ /^\s*\(\s*(.*)\s*\)\s*$/;
    $outer = $1;
    if ($outer =~ /(.*)\s+$/) { $outer = $1; }
    print "Line to tokenize is $outer\n" if $debug;
    $arg   = "";
    while ($outer ne "") {
	if ($outer =~ /^([^,\(\"]*)\s*,\s*(.*$)/) {
	    # simple arg
	    $arg .= $1;
	    $args[$#args+1] = $arg;
	    print "Found simple arg $arg (remainder $2)\n" if $debug;
	    $outer = $2;
	    $arg   = "";
	}
	elsif ($outer =~ /^([^,\"]*)\((.*$)/) { 
	    # arg with ()
	    ($startarg,$bal,$outer) = &GetBalancedParen( $FD, $outer );
	    $arg = $arg . $startarg . $bal;
	    # Rest of code will catch the rest
	}
        elsif ($outer =~ /^([^,\(]*)\"(.*$)/) {
	    # arg with ""
	    ($startarg,$string,$outer) = &GetString( $FD, $outer );
	    print "string is $string\n" if $debug;
	    $arg = $arg . $startarg . $string;
	    # Rest of code will catch the rest
	}
	else {
	    # no comma
	    print "Adding |$outer| to arg $arg\n" if $debug;
	    $arg .= $outer;
	    $outer = "";
	}
    }
    if ($arg ne "") {
	$args[$#args+1] = $arg;
    }
    print "Number of args is 1+$#args\n" if $debug;
    return ($leader, $remainder, @args );
}

# remainder = StripComments( FD, inputline )
# removes comments from a line and returns the line.  Read more if necessary
# Places the comment into $comment_line;
# The external "cxx_header" adds // to the comments stripped

# Set a default value for cxx_header
if (!defined($cxx_header)) {
    $cxx_header = 0;
}

sub StripComments {
    my $FD = $_[0];
    my $curline = $_[1];
    my $remainder = "";
    $comment_line = "";
    if ($cxx_header == 1 && $curline =~ /(\/\/.*)/) {
	$comment_line = $1;
	$curline =~ s/\/\/.*//;
	print "Removed C++ comment, now is $curline\n" if $debug;
	return $curline;
    }
    while ($curline =~ /\/\*/) {
	print "Curline = $curline\n" if $debug;
	if ($curline =~ /(\/\*.*?\*\/)/s) {
	    $comment_line = $1;
	    $curline =~ s/\/\*.*?\*\///s;
	    print "Removed comment, now is $curline\n" if $debug;
	    # Keep looking for comments incase the line has multiple 
	    # comments
	}
	else {
	    # Keep collecting until we find the end of the comment
	    if (eof($FD)) {
		print STDOUT "Unterminated comment found$errsrc!\n";
		my $line = $curline;
		if ($line =~ /(.*)\n/) { $line = "$1"; }
		print STDOUT "Comment begins with $line\n";
		return $curline;
	    }
	    $curline .= <$FD>;
	}
    }
    return $curline;
}

# Since this is a required package, indicate that we are successful.
return 1;