Coverage Report - net.sf.jmatchparser.template.engine.template.command.plain.LoopCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
LoopCommand
100%
13/13
100%
2/2
1,25
LoopCommand$LoopCommandState
100%
11/11
N/A
1,25
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * All rights reserved.
 4  
  * 
 5  
  * This program is free software: you can redistribute it and/or modify
 6  
  * it under the terms of the GNU General Public License as published by
 7  
  * the Free Software Foundation, either version 2 of the License, or
 8  
  * (at your option) any later version.
 9  
  * 
 10  
  * This program is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 package net.sf.jmatchparser.template.engine.template.command.plain;
 19  
 
 20  
 import net.sf.jmatchparser.template.engine.operation.CounterOperation;
 21  
 import net.sf.jmatchparser.template.engine.operation.IncrementCounterOperation;
 22  
 import net.sf.jmatchparser.template.engine.operation.JumpOperation;
 23  
 import net.sf.jmatchparser.template.engine.operation.Label;
 24  
 import net.sf.jmatchparser.template.engine.operation.LoopForkOperation;
 25  
 import net.sf.jmatchparser.template.engine.parameter.Parameter;
 26  
 import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState;
 27  
 import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl;
 28  
 
 29  1
 public class LoopCommand extends Command {
 30  
 
 31  
         @Override
 32  
         public PlainBlockCommandState parse(MatchTemplateImpl template, String parameters) {
 33  
                 // Continue after the loop and fork inside the loop.
 34  
                 // The other way round is easier to code, but will cause
 35  
                 // a huge pile of queued of states to pile up if the loop repeats
 36  
                 // a lot of times.
 37  22
                 Parameter[] params = Parameter.parseParameters(parameters, 0, 2, 1, template.getSpecialTags());
 38  22
                 if (params[0] == null)
 39  12
                         params[0] = Parameter.getLiteralParameter("0");
 40  22
                 Label startLabel = new Label(), endLabel = new Label(), forkLabel = new Label();
 41  22
                 CounterOperation co = new CounterOperation(template.getCurrentTemplatePosition());
 42  22
                 template.appendOperation(co);
 43  22
                 startLabel.setDestinationToNextCommand(template);
 44  22
                 template.appendOperation(new LoopForkOperation(template.getCurrentTemplatePosition(), forkLabel, co, params[0], params[1]));
 45  22
                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel));
 46  22
                 forkLabel.setDestinationToNextCommand(template);
 47  22
                 return new LoopCommandState(template, co, startLabel, endLabel);
 48  
         }
 49  
 
 50  1
         private class LoopCommandState extends PlainBlockCommandState {
 51  
 
 52  
                 private final CounterOperation co;
 53  
                 private final Label startLabel;
 54  
                 private final Label endLabel;
 55  
 
 56  22
                 public LoopCommandState(MatchTemplateImpl template, CounterOperation co, Label startLabel, Label endLabel) {
 57  22
                         super(template);
 58  22
                         this.co = co;
 59  22
                         this.startLabel = startLabel;
 60  22
                         this.endLabel = endLabel;
 61  22
                 }
 62  
 
 63  
                 @Override
 64  
                 public boolean canParse(String commandName) {
 65  20
                         return commandName.equals("endloop");
 66  
                 }
 67  
 
 68  
                 @Override
 69  
                 public PlainBlockCommandState parseCommand(MatchTemplateImpl template,
 70  
                                 String commandName, String parameters) {
 71  20
                         template.appendOperation(new IncrementCounterOperation(template.getCurrentTemplatePosition(), co));
 72  20
                         template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), startLabel));
 73  20
                         endLabel.setDestinationToNextCommand(template);
 74  20
                         return null;
 75  
                 }
 76  
         }
 77  
 }