Coverage Report - net.sf.jmatchparser.template.engine.template.command.plain.AlternativeCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
AlternativeCommand
100%
17/17
100%
2/2
2,2
AlternativeCommand$AlternativeCommandState
100%
21/21
90%
9/10
2,2
 
 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.ForkOperation;
 21  
 import net.sf.jmatchparser.template.engine.operation.JumpOperation;
 22  
 import net.sf.jmatchparser.template.engine.operation.Label;
 23  
 import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState;
 24  
 import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl;
 25  
 
 26  
 public class AlternativeCommand extends ParameterlessCommand {
 27  
 
 28  
         private final boolean optional;
 29  
 
 30  4
         public AlternativeCommand(boolean optional) {
 31  4
                 this.optional = optional;
 32  4
         }
 33  
 
 34  
         @Override
 35  
         protected PlainBlockCommandState parse(MatchTemplateImpl template) {
 36  65
                 Label endLabel = new Label(), nextPartLabel = new Label(), forkLabel = new Label();
 37  65
                 if (optional) {
 38  
                         // jump around here to make sure every path has at least one unique
 39  
                         // operation
 40  32
                         Label dummyEndLabel = new Label();
 41  32
                         Label dummyContLabel = new Label();
 42  32
                         template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), dummyEndLabel, false));
 43  32
                         template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), dummyContLabel));
 44  32
                         dummyEndLabel.setDestinationToNextCommand(template);
 45  32
                         template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel));
 46  32
                         dummyContLabel.setDestinationToNextCommand(template);
 47  
                 }
 48  65
                 int lastForkPoint = template.getNextOperationIndex();
 49  65
                 template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), forkLabel, false));
 50  65
                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), nextPartLabel));
 51  65
                 forkLabel.setDestinationToNextCommand(template);
 52  65
                 return new AlternativeCommandState(template, lastForkPoint, nextPartLabel, endLabel);
 53  
         }
 54  
 
 55  
         private class AlternativeCommandState extends PlainBlockCommandState {
 56  
 
 57  
                 private Label nextPartLabel;
 58  
                 private final Label endLabel;
 59  
                 private int lastForkPoint;
 60  
 
 61  
                 public AlternativeCommandState(MatchTemplateImpl template, int lastForkPoint,
 62  65
                                 Label nextPartLabel, Label endLabel) {
 63  65
                         super(template);
 64  65
                         this.lastForkPoint = lastForkPoint;
 65  65
                         this.nextPartLabel = nextPartLabel;
 66  65
                         this.endLabel = endLabel;
 67  65
                 }
 68  
 
 69  
                 @Override
 70  
                 public boolean canParse(String commandName) {
 71  124
                         return commandName.equals("nextalternative") || commandName.equals("nextalt") || commandName.equals("endalternative") || commandName.equals("endalt");
 72  
                 }
 73  
 
 74  
                 @Override
 75  
                 public PlainBlockCommandState parseCommand(MatchTemplateImpl template,
 76  
                                 String commandName, String parameters) {
 77  124
                         if (commandName.startsWith("end")) {
 78  64
                                 template.nopOperation(lastForkPoint);
 79  64
                                 template.nopOperation(lastForkPoint + 1);
 80  64
                                 endLabel.setDestinationToNextCommand(template);
 81  64
                                 return null;
 82  
                         } else {
 83  60
                                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel));
 84  60
                                 nextPartLabel.setDestinationToNextCommand(template);
 85  60
                                 nextPartLabel = new Label();
 86  60
                                 Label forkLabel = new Label();
 87  60
                                 lastForkPoint = template.getNextOperationIndex();
 88  60
                                 template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), forkLabel, false));
 89  60
                                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), nextPartLabel));
 90  60
                                 forkLabel.setDestinationToNextCommand(template);
 91  60
                                 return this;
 92  
                         }
 93  
                 }
 94  
         }
 95  
 }