Advertisement
KeplerBR

[Macro Plugin] Implementation of switch/case

Dec 12th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.93 KB | None | 0 0
  1. Index: Parser.pm
  2. ===================================================================
  3. --- Parser.pm   (revision 8776)
  4. +++ Parser.pm   (working copy)
  5. @@ -34,7 +34,8 @@
  6.  
  7.     my %block;
  8.     my $inBlock = 0;
  9. -   my $countBlockIf = 0;
  10. +   my $macroCountBlockIf = 0;
  11. +   my $macroCountBlockSwitch = 0;
  12.     my ($macro_subs, @perl_lines);
  13.     open my $fp, "<:utf8", $file or return 0;
  14.     while (<$fp>) {
  15. @@ -64,22 +65,33 @@
  16.  
  17.         if (%block && $block{type} eq "macro") {
  18.             if ($_ eq "}") {
  19. -               if ($countBlockIf) { # If the '}' is being used to terminate a block of commands from 'if'
  20. +               if ($macroCountBlockIf || $macroCountBlockSwitch) { # If the '}' is being used to terminate a block of commands from 'if'
  21.                     push(@{$macro{$block{name}}}, '}');
  22. -                   $countBlockIf--;
  23. +                  
  24. +                   if ($macroCountBlockIf) {
  25. +                       $macroCountBlockIf--;
  26. +                   } else {
  27. +                       $macroCountBlockSwitch--;
  28. +                   }
  29.                 } else {
  30.                     undef %block
  31.                 }
  32.             } else {
  33.                 if ($_ =~ /^if.*{$/) {
  34. -                   $countBlockIf++;
  35. -               } elsif (!$countBlockIf && ($_ =~ /^}\s*else\s*{$/ || $_ =~ /}\s*elsif\s*\(.*\)\s*{$/)) {
  36. +                   $macroCountBlockIf++;
  37. +               } elsif ($_ =~ /^switch.*{$/ || ($macroCountBlockSwitch && $_ =~ /^case.*{$/ || $_ =~ /^else.*{$/)) {
  38. +                   $macroCountBlockSwitch++;
  39. +               } elsif (!$macroCountBlockIf && ($_ =~ /^}\s*else\s*{$/ || $_ =~ /}\s*elsif\s*\(.*\)\s*{$/)) {
  40.                     warning "$file: ignoring '$_' in line $. (munch, munch, not found the 'if')\n";
  41.                     next
  42. +               } elsif (!$macroCountBlockSwitch && ($_ =~ /^case.*{$/ || $_ =~ /^else*{$/)) {
  43. +                   warning "$file: ignoring '$_' in line $. (munch, munch, not found the 'switch')\n";
  44. +                   next
  45.                 }
  46.  
  47.                 push(@{$macro{$block{name}}}, $_);
  48.             }
  49. +          
  50.             next
  51.         }
  52.  
  53. @@ -86,9 +98,13 @@
  54.         if (%block && $block{type} eq "auto") {
  55.             if ($_ eq "}") {
  56.                 if ($block{loadmacro}) {
  57. -                   if ($countBlockIf) {
  58. +                   if ($macroCountBlockIf || $macroCountBlockSwitch) {
  59.                         push(@{$macro{$block{loadmacro_name}}}, '}');
  60. -                       $countBlockIf--;
  61. +                       if ($macroCountBlockIf) {
  62. +                           $macroCountBlockIf--;
  63. +                       } else {
  64. +                           $macroCountBlockSwitch--;
  65. +                       }
  66.                     } else {
  67.                         undef $block{loadmacro}
  68.                     }
  69. @@ -102,10 +118,15 @@
  70.                 $macro{$block{loadmacro_name}} = []
  71.             } elsif ($block{loadmacro}) {
  72.                 if ($_ =~ /^if.*{$/) {
  73. -                   $countBlockIf++;
  74. -               } elsif (!$countBlockIf && ($_ =~ /^}\s*else\s*{$/ || $_ =~ /}\s*elsif\s*\(.*\)\s*{$/)) {
  75. +                   $macroCountBlockIf++;
  76. +               } elsif ($_ =~ /^switch.*{$/ || ($macroCountBlockSwitch && $_ =~ /^case.*{$/ || $_ =~ /^else.*{$/)) {
  77. +                   $macroCountBlockSwitch++;
  78. +               } elsif (!$macroCountBlockIf && ($_ =~ /^}\s*else\s*{$/ || $_ =~ /}\s*elsif\s*\(.*\)\s*{$/)) {
  79.                     warning "$file: ignoring '$_' in line $. (munch, munch, not found the 'if')\n";
  80.                     next
  81. +               } elsif (!$macroCountBlockSwitch && ($_ =~ /^case.*{$/ || $_ =~ /^else*{$/)) {
  82. +                   warning "$file: ignoring '$_' in line $. (munch, munch, not found the 'switch')\n";
  83. +                   next
  84.                 }
  85.  
  86.                 push(@{$macro{$block{loadmacro_name}}}, $_);
  87. @@ -123,6 +144,7 @@
  88.                     warning "$file: ignoring '$_' in line $. (munch, munch, unknown automacro keyword)\n"
  89.                 }
  90.             }
  91. +          
  92.             next
  93.         }
  94.        
  95. Index: Script.pm
  96. ===================================================================
  97. --- Script.pm   (revision 8776)
  98. +++ Script.pm   (working copy)
  99. @@ -256,7 +256,7 @@
  100.         if (multi($savetxt, $self, $errtpl)) {
  101.             newThen($then, $self, $errtpl);
  102.             if (defined $self->{error}) {$self->{error} = "$errtpl: $self->{error}"; return}
  103. -       } elsif ($then eq "{") { # If the condition is false and are using block of commands, will be skipped
  104. +       } elsif ($then eq "{") { # If the condition is false because "if" this is not using the command block
  105.             my $countBlockIf = 1;
  106.             while ($countBlockIf) {
  107.                 $self->{line}++;
  108. @@ -282,26 +282,67 @@
  109.         $self->{timeout} = 0
  110.  
  111.     ##########################################
  112. -   # If arriving at a line 'else' or 'elsif', it should be skipped -
  113. -   #  it will never be activated if coming from a false 'if'
  114. -   } elsif ($line =~ /^}\s*else\s*{/ || $line =~ /^}\s*elsif\s*\(.*\)\s*{$/) {
  115. -           my $countBlockIf = 1;
  116. -           while ($countBlockIf) {
  117. -               $self->{line}++;
  118. -               my $searchEnd = ${$macro{$self->{name}}}[$self->{line}];
  119. -              
  120. -               if ($searchEnd =~ /^if.*{$/) {
  121. -                   $countBlockIf++;
  122. -               } elsif (($searchEnd eq '}') ||
  123. -                   ($countBlockIf == 1 && ($searchEnd =~ /^}\s*else\s*{/ || $searchEnd =~ /^}\s*elsif\s*\(.*\)\s*{$/))) {
  124. -                   $countBlockIf--;
  125. -               }
  126. +   # If arriving at a line 'else', 'elsif' or 'case', it should be skipped -
  127. +   #  it will never be activated if coming from a false 'if' or a previous 'case' has not been called
  128. +   } elsif ($line =~ /^}\s*else\s*{/ || $line =~ /^}\s*elsif\s*\(.*\)\s*{$/ || $line =~ /^case/ || $line =~ /^else/) {
  129. +       my $countBlockIf = 1;
  130. +       while ($countBlockIf) {
  131. +           $self->{line}++;
  132. +           my $searchEnd = ${$macro{$self->{name}}}[$self->{line}];
  133. +          
  134. +           if ($searchEnd =~ /^if.*{$/ || $searchEnd =~ /^switch.*{$/ || $searchEnd =~ /^case.*{$/ || $searchEnd =~ /^else\s*{$/) {
  135. +               $countBlockIf++;
  136. +           } elsif (($searchEnd eq '}') ||
  137. +               ($countBlockIf == 1 && ($searchEnd =~ /^}\s*else\s*{/ || $searchEnd =~ /^}\s*elsif\s*\(.*\)\s*{$/))) {
  138. +               $countBlockIf--;
  139.             }
  140. +       }
  141.  
  142.         $self->{timeout} = 0
  143.  
  144.     ##########################################
  145. -   # end block of if
  146. +   # switch statement:
  147. +   } elsif ($line =~ /^switch.*{$/) {
  148. +       my ($firstPartCondition) = $line =~ /^switch\s*\(\s*(.*)\s*\)\s*{$/;
  149. +
  150. +       my $countBlocks = 1;
  151. +       while ($countBlocks) {
  152. +           $self->{line}++;
  153. +           my $searchNextCase = ${$macro{$self->{name}}}[$self->{line}];
  154. +              
  155. +           $countBlocks++ if ($searchNextCase =~ /^case.*{$/ || $searchNextCase =~ /^switch.*{$/ || $searchNextCase =~ /^}\s*else\s*{$/ || $searchNextCase =~ /}\s*elsif\s*{$/ || $searchNextCase =~ /\s*else\s*{$/);
  156. +           if ($searchNextCase =~ /^}$/) {
  157. +               last if ($countBlocks == 1);
  158. +               $countBlocks-- if ($countBlocks > 1);
  159. +           }
  160. +           next if ($countBlocks > 2);
  161. +          
  162. +           if ($searchNextCase =~ /^else/) {
  163. +               my ($then) = $searchNextCase =~ /^else\s*(.*)/;
  164. +               newThen($then, $self, $errtpl);
  165. +               if (defined $self->{error}) {$self->{error} = "$errtpl: $self->{error}"; return}
  166. +               last;
  167. +           }
  168. +          
  169. +           my ($secondPartCondition, $then) = $searchNextCase =~ /^case\s*\(\s*(.*)\s*\)\s*(.*)/;
  170. +           next if (!$secondPartCondition);
  171. +          
  172. +           my $completCondition = $firstPartCondition . ' ' . $secondPartCondition;
  173. +           my $text = parseCmd($completCondition, $self);
  174. +           if (defined $self->{error}) {$self->{error} = "$errtpl: $self->{error}"; return}
  175. +           my $savetxt = particle($text, $self, $errtpl);
  176. +           if (multi($savetxt, $self, $errtpl)) {
  177. +               newThen($then, $self, $errtpl);
  178. +               if (defined $self->{error}) {$self->{error} = "$errtpl: $self->{error}"; return}
  179. +               last;
  180. +           }
  181. +       }
  182. +      
  183. +       $self->{line}++;
  184. +       $self->{timeout} = 0
  185. +  
  186. +   ##########################################
  187. +   # end block of "if" or "switch"
  188.     } elsif ($line eq '}') {
  189.         $self->{line}++;
  190.         $self->{timeout} = 0
  191. Index: Wx/Debugger.pm
  192. ===================================================================
  193. --- Wx/Debugger.pm  (revision 8776)
  194. +++ Wx/Debugger.pm  (working copy)
  195. @@ -140,10 +140,11 @@
  196.         my $indentation = 0;
  197.         for ($i = 0; $i < @{$macro{$name}}; $i++) {
  198.             if (${$macro{$name}}[$i - 1] =~ /^if.*{$/ || ${$macro{$name}}[$i - 1] =~ /^}\s*else\s*{$/ ||
  199. -               ${$macro{$name}}[$i - 1] =~ /^}\s*elsif\s*\(.*\)\s*{$/) {
  200. +               ${$macro{$name}}[$i - 1] =~ /^}\s*elsif\s*\(.*\)\s*{$/ || ${$macro{$name}}[$i - 1] =~ /^switch.*{$/ ||
  201. +               ${$macro{$name}}[$i - 1] =~ /^case.*{$/ || ${$macro{$name}}[$i - 1] =~ /^else\s*{$/) {
  202.                 $indentation++;
  203. -           } elsif (${$macro{$name}}[$i] eq '}' || ${$macro{$name}}[$i] =~ /^}\s*else\s*{$/ ||
  204. -                    ${$macro{$name}}[$i] =~ /^}\s*elsif\s*\(.*\)\s*{$/) {
  205. +           } elsif (${$macro{$name}}[$i] eq '}' || ${$macro{$name}}[$i] =~ /^}\s*else\s*{/ ||
  206. +               ${$macro{$name}}[$i] =~ /^}\s*elsif\s*\(.*\)\s*{$/) {
  207.                 $indentation--;
  208.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement