home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / pull_quotes < prev    next >
Encoding:
Text File  |  1992-10-31  |  2.9 KB  |  101 lines

  1. #!/usr/local/bin/perl
  2. # pull_quotes -- tom christiansen, tchrist@convex.com
  3. #
  4. ################################################################################
  5. sub show_quotes {
  6.     local($string, $qchars) = @_;
  7.     local($i) = $[;
  8.     local($_);
  9.  
  10.     local(@list) = &pull_quotes($string,$qchars);
  11.  
  12.     $qchars =~ s/(.)(.)/`$1'...`$2'/;
  13.  
  14.     print "Extracted ", 0+@list, " ", $qchars, " quote", 
  15.     (@list != 1)?'s':'', "\n";
  16.     #print "Extracted ", 0+@list, " ", $qchars, " quote", 
  17.     #(@list != 1)?'s':'', " from:<<", $_[0]. ">>\n";
  18.  
  19.     for (@list) {
  20.     print "Quote #$i is <<", $_, ">>\n";
  21.     $i++;
  22.     } 
  23.     print "\n";
  24.  
  25.  
  26. ################################################################################
  27. sub pull_quotes { # pull_quotes($string, $quotchars) => @quotestrings
  28.     local($_, $qchars) = @_;
  29.  
  30.     local($xlate);
  31.  
  32.     if ($qchars =~ tr/\173\175/\373\375/) {
  33.     $xlate++;
  34.     tr/\173\175/\373\375/;
  35.     } 
  36.  
  37.     local($qL, $qR);         # left and right quote chars, like `' or ()
  38.     local($quote_level);    # current quote level
  39.     local($max_quote);        # deepest we've gotten
  40.     local($qstring);        # tmp space for quote
  41.     local(@quotes);        # list of quotes to return
  42.     local($d) = '\$';        # not sure why this must be here
  43.     local($b) = '\\';        # nor this
  44.     local(@done);        # which quotes we've finished so far
  45.  
  46.     die "need two just quote chars" if length($qchars) != 2;
  47.  
  48.     $qL = substr($qchars, 0, 1);
  49.     $qR = substr($qchars, 1, 1);
  50.  
  51.     s/\\(.)/"\201".ord($1)."\202"/eg;   # protect from backslashes
  52.  
  53.     $max_quote = $quote_level = $[-1;
  54.  
  55.     while ( /[$qchars]/ ) {
  56.     if ($& eq $qL) {
  57.         do { ++$quote_level; } while $done[$quote_level];
  58.         s/$b$qL/\${QL${quote_level}}/;
  59.         $max_quote = $quote_level if $max_quote < $quote_level;
  60.     } elsif ($& eq $qR) {
  61.         s/$b$qR/\${QR${quote_level}}/;
  62.         $done[$quote_level]++;
  63.         do { --$quote_level; } while $done[$quote_level];
  64.     } else { 
  65.         die "unexpected quot char: $&";
  66.     }
  67.     } 
  68.     for ($quote_level = $[; $quote_level <= $max_quote; $quote_level++) {
  69.     ($qstring) = /${d}\{QL$quote_level\}([^\000]*)${d}\{QR$quote_level}/;
  70.     $qstring =~ s/\${QL\d+\}/$qL/g;
  71.     $qstring =~ s/\${QR\d+\}/$qR/g;
  72.     $qstring =~ s/\201(\d+)\202/pack('C',$1)/eg;
  73.     $quotes[$quote_level] = $qstring;
  74.     } 
  75.     grep (tr/\373\375/\173\175/, @quotes) if $xlate;
  76.     @quotes;
  77.  
  78. ################################################################################
  79. ################################################################################
  80. ################################################################################
  81. ################################################################################
  82. ################################################################################
  83.  
  84. # MAIN STARTS HERE
  85.  
  86.  
  87. &show_quotes(<<__END__, '()');
  88.  
  89.  param(zzz = (var0));
  90.  
  91.  param(yyy = ((var1=xx), var2(xx), (var3 = xx),
  92.              var4=xx, var5=xx, etc. etc.)) ;
  93.  
  94.  param(yyy = ((var1=xx), var2(xx), (var3 = xx),
  95.              var4=xx, var5=xx, etc. etc.))
  96.  
  97. __END__
  98.