chiark / gitweb /
Big comment with syntax diagram thingy.
[ian-dotfiles.git] / gpt
diff --git a/gpt b/gpt
index c50443ad910460820896cf4758290a40d896ce07..ce93196499dfb99039e317ab4be9bc96d0bc4087 100755 (executable)
--- a/gpt
+++ b/gpt
@@ -1,5 +1,37 @@
 #!/usr/bin/perl
 
+#
+#                          =====deeper=nesting===>>
+#
+#                     end of Perl fragment
+#                      \n@@ \_@@  ;@@ )@@ }@@  \w@@
+#                   <------------------------------------
+#
+#                   ------------------------------------>
+#                      if    eval    eval+     eval+
+#                       @@?   @@\_   ignore    include
+#                             @@$     @@-      file
+#    ___                      @@\n              @@<
+#  ,'   `
+#  |      processing                                       processing
+#  `--->  as TEXT                                          as PERL
+#  endif
+#   @@;     vA      <------------------------------------
+#           ||         then
+#   comment ||          ?@@
+#       @@# ||
+#           ||       included text fragment as
+#      else ||         string value    code to write out
+#       @@: ||          (@@             {@@
+#           ||      <------------------------------------
+#           `'
+#                   ------------------------------------>
+#                     end of included text fragment
+#                       @@)  @@}
+#
+#                          <<====deeper=nesting====
+#
+
 package GenericPrepTemplate;
 
 #BEGIN {
@@ -19,7 +51,7 @@ use IO::File;
 
 open DEBUG, ">/dev/null" or die "gpt: /dev/null: $!\n";
 
-while (@main::ARGV =~ m/^-/) {
+while ($main::ARGV[0] =~ m/^-/) {
     $_= shift @main::ARGV;
     next if m/^--$/;
     while (m/^-./) {
@@ -41,15 +73,19 @@ if (!@main::ARGV) {
 } else {
     for ($i=0; $i<@main::ARGV; $i++) {
        $lno= '<arg#'.($i+1).'>';
-       $value= $main::ARGV[$i];
-       $fh= new IO::File $value,'r' or err("cannot open file \`$value': $!");
-       process_fh($fh,$value);
-       $fh->close or err("gpt: cannot close file \`$value': $!");
+       process_file($main::ARGV[$i]);
     }
 }
 close STDOUT or die "gpt: close stdout: $!\n";
 exit 0;
 
+sub process_file ($) {
+    my ($value) = @_;
+    $fh= new IO::File $value,'r' or err("cannot open file \`$value': $!");
+    process_fh($fh,$value);
+    $fh->close or err("gpt: cannot close file \`$value': $!");
+}
+
 sub err ($) { die "gpt: $fn:$lno: $_[0]\n"; }
 
 sub add_perl ($) {
@@ -85,10 +121,10 @@ sub process_input () {
        print DEBUG "L $fn:$lno:",join('',@oplist),
                    ":`$l' e\`$expr' s\`$substr'\n";
        if (@oplist & 1) { # scanning perl
-           if ($l =~ m/(.)\@\@/m || $l =~ m/^()\@\@/m) {
+           if ($l =~ m/^()\@\@/ || $l =~ m/([^\000])\@\@/) {
                ($to,$op,$esc,$l) = ($`,$1,$&,$');
                add_perl($to);
-               if ($op =~ m/[ 0-9a-zA-Z_\;\)\}?]/ || $op eq '') {
+               if ($op =~ m/[ \t\n0-9a-zA-Z_\;\)\}?]/ || $op eq '') {
                    $top= pop @oplist;
                    if (!@oplist) {
                        # And evaluate.
@@ -119,8 +155,12 @@ sub process_input () {
            if ($l =~ m/\@\@(.|\n)/) {
                ($to,$op,$esc,$l) = ($`,$1,$&,$');
                add_text($to);
-               if ($op =~ m/[-< \$?]/) {
-                   $substr.= $esc if @oplist;
+               if ($op =~ m/[-< \$?\n]/) {
+                   if (!@oplist) {
+                       add_perl($op) if $op =~ m/[\$]/;
+                   } else {
+                       $substr.= $esc;
+                   }
                    push @oplist, $op;
                } elsif ($op =~ m/[\)\}\;\:]/) {
                    err("unmatched closing \@\@$op") unless @oplist;
@@ -160,6 +200,8 @@ sub process_input () {
                    } else {
                        $substr.= $esc;
                    }
+               } elsif ($op eq '#') {
+                   $l =~ s/^[^\n]*//; $l =~ s/^\n//;
                } else {
                    err("bad escape sequence \@\@$op in text part");
                }
@@ -171,6 +213,26 @@ sub process_input () {
     }
 }
 
+sub close_nesteds () {
+    my ($op);
+    while (@oplist) {
+       $op= $oplist[$#oplist];
+       if (@oplist & 1) {
+           err("unterminated \@\@?") if $op eq '?';
+           $l=' @@';
+       } else {
+           if ($op =~ y/({/)}/) {
+               $l= '@@'.$op;
+           } elsif ($op =~ m/^[;:]+/) {
+               $l= '@@;';
+           } else {
+               die "intern /@oplist/";
+           }
+       }
+       process_input();
+    }
+}
+
 sub process_fh ($$) {
     local ($fh,$fn) = @_;
     local ($l,$lno, @oplist,$substr,$expr,$to,$op);
@@ -180,7 +242,9 @@ sub process_fh ($$) {
        $lno= $.;
        process_input();
     }
+    
     die "gpt: $fn: read error: $!" if $fh->error;
+    close_nesteds();
 }
 
 sub output ($) {
@@ -199,5 +263,6 @@ sub process_i ($) {
     local ($outbuf, @oplist,$substr,$expr,$to,$op);
     $outbuf='';
     process_input();
+    close_nesteds();
     return $outbuf;
 }