texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

texinfo/tp Texinfo/Parser.pm t/60macro.t


From: Patrice Dumas
Subject: texinfo/tp Texinfo/Parser.pm t/60macro.t
Date: Thu, 30 Sep 2010 06:02:29 +0000

CVSROOT:        /sources/texinfo
Module name:    texinfo
Changes by:     Patrice Dumas <pertusus>        10/09/30 06:02:29

Modified files:
        tp/Texinfo     : Parser.pm 
        tp/t           : 60macro.t 

Log message:
        Expand arguments in macro body.
        Allow passing a context.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/Texinfo/Parser.pm?cvsroot=texinfo&r1=1.29&r2=1.30
http://cvs.savannah.gnu.org/viewcvs/texinfo/tp/t/60macro.t?cvsroot=texinfo&r1=1.2&r2=1.3

Patches:
Index: Texinfo/Parser.pm
===================================================================
RCS file: /sources/texinfo/texinfo/tp/Texinfo/Parser.pm,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- Texinfo/Parser.pm   30 Sep 2010 05:35:14 -0000      1.29
+++ Texinfo/Parser.pm   30 Sep 2010 06:02:28 -0000      1.30
@@ -67,6 +67,7 @@
   'test' => 0,
   'debug' => 0,
   'gettext' => sub {return $_[0];},
+  'context' => '_root',
   'aliases' => {},
   'indices' => [],
   'values' => {},
@@ -498,6 +499,7 @@
   }
   $parser->{'errors_warnings'} = [];
   $parser->{'errors_nrs'} = 0;
+  $parser->{'context_stack'} = [ $parser->{'context'} ];
   return $parser;
 }
 
@@ -665,7 +667,7 @@
   my $current = shift;
 
   if ((!$current->{'type'} or $current->{'type'} eq 'before_item') 
-      and !$no_paragraph_contexts{$self->{'context'}->[-1]}) {
+      and !$no_paragraph_contexts{$self->{'context_stack'}->[-1]}) {
     push @{$current->{'contents'}}, 
             { 'type' => 'paragraph', 'parent' => $current, 'contents' => [] };
     $current = $current->{'contents'}->[-1];
@@ -741,14 +743,14 @@
         and exists($block_commands{$current->{'cmdname'}})) {
       $self->_line_error(sprintf($self->__("No matching `%cend %s'"),
                                    ord('@'), $current->{'cmdname'}), $line_nr);
-      pop @{$self->{'context'}} if 
+      pop @{$self->{'context_stack'}} if 
          ($preformatted_commands{$current->{'cmdname'}}
            or $menu_commands{$current->{'cmdname'}});
       $current = $current->{'parent'};
     } elsif ($current->{'parent'}->{'cmdname'}
              and exists 
$context_brace_commands{$current->{'parent'}->{'cmdname'}}) {
       $current = _close_brace_command($self, $current, $line_nr);
-      pop @{$self->{'context'}};
+      pop @{$self->{'context_stack'}};
     } else { # silently close containers and @-commands without @end
       $current = $current->{'parent'};
     }
@@ -758,7 +760,7 @@
 
   if ($command and $current->{'cmdname'} 
     and $current->{'cmdname'} eq $command) {
-    pop @{$self->{'context'}} if 
+    pop @{$self->{'context_stack'}} if 
        ($preformatted_commands{$current->{'cmdname'}} 
          or $menu_commands{$current->{'cmdname'}});
     $current = $current->{'parent'}
@@ -880,8 +882,13 @@
       $arguments->[-1] .= $1;
       if ($separator eq '\\') {
         if ($line =~ s/^(.)//) {
-          $arguments->[-1] .= $1;
-          print STDERR "MACRO ARG: $separator: $1\n" if ($self->{'debug'});
+          my $protected_char = $1;
+          if ($protected_char !~ /[\\{},]/) {
+            $arguments->[-1] .= '\\';
+          }
+          $arguments->[-1] .= $protected_char;
+          
+          print STDERR "MACRO ARG: $separator: $protected_char\n" if 
($self->{'debug'});
         } else {
           $arguments->[-1] .= '\\';
           print STDERR "MACRO ARG: $separator\n" if ($self->{'debug'});
@@ -920,16 +927,43 @@
   return ($arguments, $line, $line_nr);
 }
 
-sub _expand_macro_body($$$$$$$) {
+sub _expand_macro_body($$$$) {
   my $self = shift;
   my $macro = shift;
-  my $arguments = shift;
-  my $line = shift;
+  my $args = shift;
   my $line_nr = shift;
-  my $text = shift;
-  my $lines_array = shift;
 
-  return ($line, $line_nr);
+  my $macrobody = $macro->{'special'}->{'macrobody'};
+  my $args_total = scalar(@{$macro->{'args'}}) -1;
+  my $args_index = $macro->{'special'}->{'args_index'};
+
+  my $i;
+  for ($i=0; $i<=$args_total; $i++) {
+    $args->[$i] = "" unless (defined($args->[$i]));
+  }
+#  line_error (sprintf(__("Macro `%s' called with too many args"), $name), 
$line_nr) if (defined($args->[$i + 1]));
+
+  my $result = '';
+  while ($macrobody ne '') {
+    if ($macrobody =~ s/^([^\\]*)\\//o) {
+      $result .= $1;
+      if ($macrobody =~ s/^\\//) {
+        $result .= '\\';
+      } elsif ($macrobody =~ s/^([^\\]*)\\//) {
+        my $arg = $1;
+        if (defined($args_index->{$arg})) {
+          $result .= $args->[$args_index->{$arg}];
+        } else {
+          _line_error ($self, sprintf($self->__("\\ in macro expansion 
followed `%s' instead of parameter name or \\"), $arg), $line_nr);
+          $result .= '\\' . $arg;
+        }
+      }
+      next;
+    }
+    $result .= $macrobody;
+    last;
+  }
+  return $result;
 }
 
 #c 'menu_entry'
@@ -967,7 +1001,6 @@
 
   my $root = { 'contents' => [] };
   $self->{'tree'} = $root;
-  $self->{'context'} = [ '_root' ];
   my $current = $root;
 
   my $line_nr;
@@ -982,7 +1015,7 @@
       local $Data::Dumper::Indent = 1;
       local $Data::Dumper::Purity = 1;
       print STDERR "".Data::Dumper->Dump([$root], ['$root']);
-      print STDERR "NEW LINE($self->{'context'}->[-1]): $line";
+      print STDERR "NEW LINE($self->{'context_stack'}->[-1]): $line";
       delete $current->{'HERE !!!!'};
     }
 
@@ -996,7 +1029,7 @@
             and $current->{'parent'}->{'cmdname'} eq 'verb')
           )
         # not in math or preformatted
-        and !$no_paragraph_contexts{$self->{'context'}->[-1]}) {
+        and !$no_paragraph_contexts{$self->{'context_stack'}->[-1]}) {
       print STDERR "EMPTY LINE\n" if ($self->{'debug'});
       $current = _end_paragraph($self, $current, $line_nr);
       push @{$current->{'contents'}}, { 'type' => 'normal_line', 
@@ -1131,10 +1164,11 @@
             $arguments = [$line];
             $line = "\n";
           } 
-          ($line, $line_nr) = _expand_macro_body ($self, 
-                                $expanded_macro, $arguments, 
-                                $line, $line_nr,
-                                $text, $lines_array);
+          my $expanded = _expand_macro_body ($self, $expanded_macro, 
+                                     $arguments, $line_nr);
+          print STDERR "MACROBODY: $expanded".'||||||'."\n" 
+             if ($self->{'debug'}); 
+          
           next;
         }
 
@@ -1167,7 +1201,7 @@
           }
         }
 
-        last if ($self->{'context'}->[-1] eq 'def' and $command eq "\n");
+        last if ($self->{'context_stack'}->[-1] eq 'def' and $command eq "\n");
 
         unless ($self->{'no_paragraph_commands'}->{$command}) {
           my $paragraph = _begin_paragraph($self, $current);
@@ -1262,7 +1296,7 @@
                    or $current->{'cmdname'} ne $base_command) {
                 $self->_line_error(sprintf($self->__("Must be in 
address@hidden' environment to use address@hidden'"), $base_command, $command), 
$line_nr);
               }
-              push @{$self->{'context'}}, 'def';
+              push @{$self->{'context_stack'}}, 'def';
               $current->{'contents'}->[-1]->{'type'} = 'def_line';
             }
               
@@ -1300,7 +1334,7 @@
             # definition line.  This allows to have a treatement similar
             # with def*x.
             if ($def_commands{$command}) {
-              push @{$self->{'context'}}, 'def';
+              push @{$self->{'context_stack'}}, 'def';
               push @{$current->{'contents'}}, { 
                                                 'parent' => $current,
                                                 'cmdname' => $command,
@@ -1334,13 +1368,13 @@
                 $current->{'cmdname'} = 'columnfractions';
               }
             } else {
-              push @{$self->{'context'}}, 'preformatted' 
+              push @{$self->{'context_stack'}}, 'preformatted' 
                 if ($preformatted_commands{$command});
               if ($menu_commands{$command}) {
                 push @{$current->{'contents'}}, {'type' => 'menu_comment',
                                                  'parent' => $current,
                                                  'contents' => [] };
-                push @{$self->{'context'}}, 'menu';
+                push @{$self->{'context_stack'}}, 'menu';
                 $current = $current->{'contents'}->[-1];
               }
               last unless ($line =~ /\S/);
@@ -1366,7 +1400,7 @@
           $current = $current->{'args'}->[-1];
           # FIXME don't use type to distinguish context_brace_commands.
           if ($context_brace_commands{$command}) {
-            push @{$self->{'context'}}, $command;
+            push @{$self->{'context_stack'}}, $command;
           } else {
             $current->{'type'} = 'brace_command_arg';
           }
@@ -1455,20 +1489,20 @@
                    and $current->{'parent'}->{'cmdname'}
                    and exists 
$brace_commands{$current->{'parent'}->{'cmdname'}}) {
              if ($context_brace_commands{$current->{'parent'}->{'cmdname'}}) {
-               pop @{$self->{'context'}};
+               pop @{$self->{'context_stack'}};
              }
              # first is the arg.
              print STDERR "CLOSING address@hidden>{'parent'}->{'cmdname'}\n" 
if ($self->{'debug'});
              $current = $current->{'parent'}->{'parent'};
           # footnote caption closing
-          } elsif ($context_brace_commands{$self->{'context'}->[-1]}) {
+          } elsif ($context_brace_commands{$self->{'context_stack'}->[-1]}) {
              $current = _end_paragraph($self, $current, $line_nr);
              if ($current->{'parent'}
                    and $current->{'parent'}->{'cmdname'}
                    and $brace_commands{$current->{'parent'}->{'cmdname'}}
                    and 
$context_brace_commands{$current->{'parent'}->{'cmdname'}}
-                   and 
$context_brace_commands{$current->{'parent'}->{'cmdname'}} eq 
$self->{'context'}->[-1]) {
-               pop @{$self->{'context'}};
+                   and 
$context_brace_commands{$current->{'parent'}->{'cmdname'}} eq 
$self->{'context_stack'}->[-1]) {
+               pop @{$self->{'context_stack'}};
                print STDERR "CLOSING address@hidden>{'parent'}->{'cmdname'}\n" 
if ($self->{'debug'});
                $current = $current->{'parent'}->{'parent'};
             }
@@ -1611,7 +1645,7 @@
         } elsif ($current->{'parent'}
              and $current->{'parent'}->{'type'}
                     and $current->{'parent'}->{'type'} eq 'def_line') {
-            my $def_context = pop @{$self->{'context'}};
+            my $def_context = pop @{$self->{'context_stack'}};
             die "BUG: def_context $def_context "._print_current($current) 
                if ($def_context ne 'def');
             $current = $current->{'parent'}->{'parent'};

Index: t/60macro.t
===================================================================
RCS file: /sources/texinfo/texinfo/tp/t/60macro.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- t/60macro.t 30 Sep 2010 05:35:14 -0000      1.2
+++ t/60macro.t 30 Sep 2010 06:02:29 -0000      1.3
@@ -45,7 +45,7 @@
 @end macro
 @end macro
 '],
-['macro_epansion','
+['macro_expansion','
 @macro macro1 {arg1, arg2 }
 result of a macro with \arg1\ and 
 @verbatim



reply via email to

[Prev in Thread] Current Thread [Next in Thread]