#!/usr/bin/perl use strict ; use warnings ; use Text::Balanced qw(extract_multiple) ; die "Uso: $0 filtro\n" unless @ARGV ; my ($begop,$begin,$end) = (qr/\([&|!]\s*/, qr/\(\s*/, qr/\)\s*/) ; my $filter = shift @ARGV ; my @blocks = extract_multiple($filter,[$begop]) ; my $step = 0 ; foreach my $block (@blocks) { if ($block =~ $begop) { # Inizia un operatore print_chunk($step++,$block,1) ; } else { # E` un blocco di match, probabilmente sbilanciato my @matches = extract_multiple($block,[$begin,$end]) ; # Questi sono match while (@matches >= 3) { my @chunks = splice(@matches,0,3) ; # Fai check sui "chunk" e agisci di conseguenza: if ($chunks[1] =~ /=/) { # E` un match: print_chunk($step,join("",@chunks),1) ; } else { # Sfiga while (my $chunk = shift @chunks) { if ($chunk =~ $end) { print_chunk(--$step,$chunk,1) ; } else { # Ricarica gli elementi in @matches e riparti unshift @matches,$chunk,@chunks ; last ; } } } } # Queste sono parentesi che si chiudono drop_parenses(@matches) ; } } sub print_chunk { my ($step,$string,$newline) = @_ ; print " "x$step ; print $string ; print "\n" if $newline ; } sub drop_parenses { while (my $parens = shift @_) { print_chunk(--$step,$parens,1) ; } }