This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re-work the regcomp.sym to remove use of hard tabs. No data change.
[perl5.git] / regcomp.pl
CommitLineData
6294c161
DM
1#!/usr/bin/perl
2#
3# Regenerate (overwriting only if changed):
4#
5# regnodes.h
6#
7# from information stored in
8#
9# regcomp.sym
10# regexp.h
11#
12# Accepts the standard regen_lib -q and -v args.
13#
14# This script is normally invoked from regen.pl.
15
36bb303b
NC
16BEGIN {
17 # Get function prototypes
9ad884cb 18 require 'regen_lib.pl';
36bb303b 19}
d09b2d29 20#use Fatal qw(open close rename chmod unlink);
03363afd
YO
21use strict;
22use warnings;
23
d09b2d29 24open DESC, 'regcomp.sym';
d09b2d29 25
03363afd
YO
26my $ind = 0;
27my (@name,@rest,@type,@code,@args,@longj);
28my ($desc,$lastregop);
d09b2d29 29while (<DESC>) {
03363afd
YO
30 s/#.*$//;
31 next if /^\s*$/;
32 s/\s*\z//;
33 if (/^-+\s*$/) {
34 $lastregop= $ind;
35 next;
36 }
37 unless ($lastregop) {
38 $ind++;
f8abb37e 39 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+)\s*;\s*(.*)/;
03363afd
YO
40 ($type[$ind], $code[$ind], $args[$ind], $longj[$ind])
41 = split /[,\s]\s*/, $desc, 4;
42 } else {
f8abb37e 43 my ($type,@lists)=split /\s+/, $_;
03363afd
YO
44 die "No list? $type" if !@lists;
45 foreach my $list (@lists) {
46 my ($names,$special)=split /:/, $list , 2;
47 $special ||= "";
48 foreach my $name (split /,/,$names) {
49 my $real= $name eq 'resume'
50 ? "resume_$type"
51 : "${type}_$name";
52 my @suffix;
53 if (!$special) {
54 @suffix=("");
55 } elsif ($special=~/\d/) {
56 @suffix=(1..$special);
57 } elsif ($special eq 'FAIL') {
58 @suffix=("","_fail");
59 } else {
60 die "unknown :type ':$special'";
61 }
62 foreach my $suffix (@suffix) {
63 $ind++;
64 $name[$ind]="$real$suffix";
65 $type[$ind]=$type;
24b23f37 66 $rest[$ind]="state for $type";
03363afd
YO
67 }
68 }
69 }
70
71 }
72}
5d458dd8
YO
73# use fixed width to keep the diffs between regcomp.pl recompiles
74# as small as possible.
75my ($width,$rwidth,$twidth)=(22,12,9);
03363afd
YO
76$lastregop ||= $ind;
77my $tot = $ind;
d09b2d29 78close DESC;
03363afd
YO
79die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
80 if $lastregop>256;
d09b2d29 81
266db279 82my $tmp_h = 'regnodes.h-new';
d09b2d29
IZ
83
84unlink $tmp_h if -f $tmp_h;
85
424a4936 86my $out = safer_open($tmp_h);
d09b2d29 87
424a4936 88printf $out <<EOP,
37442d52
RGS
89/* -*- buffer-read-only: t -*-
90 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
9b155405 91 This file is built by regcomp.pl from regcomp.sym.
d09b2d29
IZ
92 Any changes made here will be lost!
93*/
94
6bda09f9
YO
95/* Regops and State definitions */
96
03363afd
YO
97#define %*s\t%d
98#define %*s\t%d
99
d09b2d29 100EOP
f9f4320a
YO
101 -$width, REGNODE_MAX => $lastregop - 1,
102 -$width, REGMATCH_STATE_MAX => $tot - 1
103;
d09b2d29 104
24b23f37
YO
105
106for ($ind=1; $ind <= $lastregop ; $ind++) {
03363afd 107 my $oind = $ind - 1;
424a4936 108 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
03363afd 109 -$width, $name[$ind], $ind-1, $ind-1, $rest[$ind];
24b23f37 110}
424a4936 111print $out "\t/* ------------ States ------------- */\n";
24b23f37 112for ( ; $ind <= $tot ; $ind++) {
424a4936 113 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
24b23f37 114 -$width, $name[$ind], $ind - $lastregop, $rest[$ind];
d09b2d29
IZ
115}
116
424a4936 117print $out <<EOP;
03363afd 118
6bda09f9 119/* PL_regkind[] What type of regop or state is this. */
d09b2d29
IZ
120
121#ifndef DOINIT
22c35a8c 122EXTCONST U8 PL_regkind[];
d09b2d29 123#else
22c35a8c 124EXTCONST U8 PL_regkind[] = {
d09b2d29
IZ
125EOP
126
127$ind = 0;
128while (++$ind <= $tot) {
424a4936 129 printf $out "\t%*s\t/* %*s */\n",
03363afd 130 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
424a4936 131 print $out "\t/* ------------ States ------------- */\n"
03363afd 132 if $ind == $lastregop and $lastregop != $tot;
d09b2d29
IZ
133}
134
424a4936 135print $out <<EOP;
d09b2d29
IZ
136};
137#endif
138
6bda09f9 139/* regarglen[] - How large is the argument part of the node (in regnodes) */
d09b2d29
IZ
140
141#ifdef REG_COMP_C
29de9391 142static const U8 regarglen[] = {
d09b2d29
IZ
143EOP
144
145$ind = 0;
03363afd
YO
146while (++$ind <= $lastregop) {
147 my $size = 0;
d09b2d29
IZ
148 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
149
424a4936 150 printf $out "\t%*s\t/* %*s */\n",
03363afd 151 -37, "$size,",-$rwidth,$name[$ind];
d09b2d29
IZ
152}
153
424a4936 154print $out <<EOP;
d09b2d29
IZ
155};
156
6bda09f9
YO
157/* reg_off_by_arg[] - Which argument holds the offset to the next node */
158
29de9391 159static const char reg_off_by_arg[] = {
d09b2d29
IZ
160EOP
161
162$ind = 0;
03363afd
YO
163while (++$ind <= $lastregop) {
164 my $size = $longj[$ind] || 0;
9b155405 165
424a4936 166 printf $out "\t%d,\t/* %*s */\n",
03363afd 167 $size, -$rwidth, $name[$ind]
d09b2d29
IZ
168}
169
424a4936 170print $out <<EOP;
d09b2d29 171};
9b155405 172
13d6edb4
NC
173#endif /* REG_COMP_C */
174
6bda09f9
YO
175/* reg_name[] - Opcode/state names in string form, for debugging */
176
22429478 177#ifndef DOINIT
13d6edb4 178EXTCONST char * PL_reg_name[];
22429478 179#else
4764e399 180EXTCONST char * const PL_reg_name[] = {
9b155405
IZ
181EOP
182
183$ind = 0;
24b23f37
YO
184my $ofs = 1;
185my $sym = "";
9b155405 186while (++$ind <= $tot) {
03363afd 187 my $size = $longj[$ind] || 0;
9b155405 188
424a4936 189 printf $out "\t%*s\t/* $sym%#04x */\n",
24b23f37
YO
190 -3-$width,qq("$name[$ind]",), $ind - $ofs;
191 if ($ind == $lastregop and $lastregop != $tot) {
424a4936 192 print $out "\t/* ------------ States ------------- */\n";
24b23f37
YO
193 $ofs = $lastregop;
194 $sym = 'REGNODE_MAX +';
195 }
196
9b155405
IZ
197}
198
424a4936 199print $out <<EOP;
9b155405 200};
22429478 201#endif /* DOINIT */
d09b2d29 202
f7819f85
A
203/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
204
205#ifndef DOINIT
206EXTCONST char * PL_reg_extflags_name[];
207#else
208EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
209EOP
210
f7819f85
A
211open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!";
212my %rxfv;
c8e4cf8b
NC
213my $val = 0;
214my %reverse;
f7819f85
A
215while (<$fh>) {
216 if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) {
c8e4cf8b
NC
217 my $newval = eval $2;
218 if($val & $newval) {
219 die sprintf "Both $1 and $reverse{$newval} use %08X", $newval;
220 }
221 $val|=$newval;
222 $rxfv{$1}= $newval;
223 $reverse{$newval} = $1;
f7819f85
A
224 }
225}
226my %vrxf=reverse %rxfv;
424a4936 227printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
f7819f85
A
228for (0..31) {
229 my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
230 $n=~s/^RXf_(PMf_)?//;
424a4936 231 printf $out qq(\t%-20s/* 0x%08x */\n),
f7819f85
A
232 qq("$n",),2**$_;
233}
234
424a4936 235print $out <<EOP;
f7819f85
A
236};
237#endif /* DOINIT */
238
239/* ex: set ro: */
240EOP
08858ed2 241safer_close($out);
d09b2d29 242
424a4936 243rename_if_different $tmp_h, 'regnodes.h';