Use a stack to keep track of "open" styles in order to determine if a new style is a child or a parent.
use strict;
use warnings;
my @styles = (
'\(\d+\)', '\d+\)', '\d+\.',
'\([a-h]\)', '[a-h]\)', '\([A-H]\)', '[A-H]\)',
'\([IVX]+\)', '[IVX]+\)', '\([ivx]+\)', '[ivx]+\)',
'-',
);
my @stack;
while (<>) {
for my $i (reverse 0..$#stack) {
if (/$stack[$i]/) {
splice(@stack, $i+1);
goto DONE_LINE;
}
}
for my $style (@styles) {
if (my ($spaces) = /^( *)$style/) {
push @stack, qr/^$spaces$style/;
goto DONE_LINE;
}
}
die "Unrecognized format at line $. - $_";
DONE_LINE:
s/^ *//;
printf("<<LIST%d>>%s", 0+@stack, $_);
}
To avoid recompiling the same regex patterns over and over again, add
my %re_cache = map { $_ => qr/^( *)$_/ } @styles;
and change
/^( *)$style/
to
/$re_cache{$style}/