#!/usr/bin/env perl

# Current limitation: only expands a single wildcard per pattern

while ( <> ) {
    
    chomp;

    if ( /^[\s]*%\(wild-card/) {

	($wildcard, $letterset) = /%\(wild-card[\s]+\(\?([^\s])[\s]+([^\s]+)[\s]*\)[\s]*\)[\s]*$/;
	@letters =  ($letterset =~ /([\\]?.)/g)  ;


	push @{$substitute{$wildcard}}, @letters;
    }
    elsif (/^[\s]*%(suf|pre)fix/) {
	
	if ( /^[\s]*%suffix[\s]+\(/ ) {
	    print "%suffix"
	}
	else {
	    print "%prefix"
	}

	@patterns = /\(([^\s]*[\s]+[^\s]*)\)/g;
	foreach $pat (@patterns) {
	    if ($pat =~ /[?].* .*/ ) {
		
		($wildcard) = ($pat =~ /[?]([^\s])/);
		
		
		foreach $subst (@{$substitute{$wildcard} }) {
		    
		    ($substpat = $pat) =~ s/[\?]$wildcard/$subst/;

		    print " (" . $substpat. ")";
		    
		    
		}
		
	    }
	    else {
		print " ($pat)";
	    }
	    
	}
	
    }
    else {
	print ; 
    }
    print "\n";

}
