File: explode

package info (click to toggle)
binutils 2.32.51.20190821-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 309,676 kB
  • sloc: ansic: 1,201,497; asm: 658,869; cpp: 130,432; exp: 69,370; makefile: 56,804; sh: 23,583; lisp: 14,519; yacc: 14,467; perl: 2,111; ada: 1,681; lex: 1,649; pascal: 1,446; python: 991; cs: 879; sed: 195; xml: 95; awk: 25
file content (199 lines) | stat: -rwxr-xr-x 3,909 bytes parent folder | download | duplicates (13)
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
#!/usr/bin/perl
# -*- perl -*-

#  Copyright (C) 2006 Red Hat Inc.
#
#  This file is part of GAS, the GNU Assembler.
#
#  GAS 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, or (at your option)
#  any later version.
#
#  GAS 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; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
#  MA 02110-1301, USA.

%myfiles = ();

$incdir = ".";

while ($ARGV[0] =~ /^-/) {
    $opt = shift;
    if ($opt eq "-I") {
	$incdir = shift;
    }
}

$infile = shift;
$outfile = shift;

$inbase = $infile;
$inbase =~ s@.*/@@;
$inbase =~ s@[^a-zA-Z0-9].*@@;

$t = 0;
$errors = 0;

if ($outfile) {
    open(OUT, ">$outfile");
} else {
    open(OUT, ">&STDOUT");
}

open(I, "$incdir/macros.inc") || die("$incdir/macros.inc: $!");
&read_file();
close I;
open(I, $infile) || die("$infile: $!");
&read_file();
close I;

sub read_file {
    while (<I>) {
	$line ++;
	next if /^;/;
    s/[\r\n]+$//;
	if (/^macro\s+(\S+)\s+(.*)/) {
	    ($name, $val) = ($1,$2);
	    print "set macro \"$name\" to \"$val\"\n" if $t;
	    $macro{$name} = $val;
	} elsif (/\S/) {
	    &explode($_);
	}
    }
}

exit ($errors);

# There's no way to quote braces so you can output them :-P

sub explode {
    my ($s) = @_;
    my ($a, $b, $p, $e, @params);

    print "explode($s)\n" if $t;

    ($b, $a, @params) = &split_braces($s);
    @params = explode_params (@params);
    if (! $a && ! @params) {
	if ($t) {
	    print "\033[33m$s\033[0m\n";
	} else {
	    print OUT "$s\n";
	}
	return;
    }
    if (@params == 1 && defined $macro{$params[0]}) {
	$p = $macro{$params[0]};
	&explode ("$b$p$a");
    } else {
	for $p (@params) {
	    &explode ("$b$p$a");
	}
    }
}

sub explode_params {
    my (@p) = @_;
    my ($p,@r);

    @r = ();
    while (@p) {
	$p = shift @p;
	($b,$a,@e) = split_braces ($p);
	if (defined $a) {
	    for $e (reverse @e) {
		unshift (@p, "$b$e$a");
	    }
	} else {
	    push (@r, $p);
	}
    }
    return @r;
}

sub getmacro {
    my ($v) = $macro{$_[0]};
    if (! defined $v) {
	print STDERR "$line: Error: macro $_[0] not defined\n";
	$errors ++;
    }
    return $v;
}

sub expand_macros {
    my ($l) = @_;
    0 while $l =~ s/{([^{};]+)}/&getmacro($1)/ge;
    return $l;
}

# returns (before, after, list of variances)
sub split_braces {
    my ($l) = @_;
    my (@l, $i, $a, @parms, $b, $n,$p);

    print "split_braces($l) = (" if $t;

    $l = &expand_macros ($l);

    if ($l !~ /\{.*\}/) {
	print "nothing)\n" if $t;
	return ($l);
    }
    if ($l =~ /^{([^{};]+)}/) {
	print "macro:", $macro{$1}, ")\n" if $t;
	return (&getmacro($1), "");
    }

    $n = 0;
    @parms = ('');
    $p = 0;

    ($a, $l) = $l =~ m@^([^\{]*)\{(.*)@;
    @l = split(//, $l);

    while (defined ($i = shift @l)) {
	if ($n == 0) {
	    print "\033[32m$i" if $t;
	    if ($i eq '}') {
		print "\033[0m$a, ", join('', @l), ", (", join("\033[31m;\033[0m", @parms), ")\n" if $t;
		return ($a, join('',@l), @parms);
	    } elsif ($i eq ';') {
		$p ++;
		$parms[$p] = '';
	    } else {
		$parms[$p] .= $i;
		$n ++ if $i eq '{';
	    }
	} else {
	    print "\033[34m$i" if $t;
	    $n ++ if $i eq '{';
	    $n -- if $i eq '}';
	    $parms[$p] .= $i;
	}
    }
    print "$a, <null>, (", join(';', @parms), ")\n" if $t;
    return ($a, "", @parms);
}

__END__;

macro rest c,d
	foo {a;b},{{rest};e;}

expands to:

        foo a,c
        foo a,d
        foo a,e
        foo a,
        foo b,c
        foo b,d
        foo b,e
        foo b,