Coverage Report - net.sf.jmatchparser.template.engine.template.command.plain.DefTemplateCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
DefTemplateCommand
100%
7/7
N/A
1
DefTemplateCommand$TemplateCommandState
100%
8/8
N/A
1
 
 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.JumpOperation;
 21  
 import net.sf.jmatchparser.template.engine.operation.Label;
 22  
 import net.sf.jmatchparser.template.engine.operation.ReturnOperation;
 23  
 import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl;
 24  
 import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState;
 25  
 
 26  1
 public class DefTemplateCommand extends Command {
 27  
 
 28  
         @Override
 29  
         public PlainBlockCommandState parse(MatchTemplateImpl template, String parameters) {
 30  
 
 31  
                 // Continue after the loop and fork inside the loop.
 32  
                 // The other way round is easier to code, but will cause
 33  
                 // a huge pile of queued of states to pile up if the loop repeats
 34  
                 // a lot of times.
 35  
 
 36  5
                 Label startLabel = new Label(), endLabel = new Label();
 37  5
                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel));
 38  5
                 startLabel.setDestinationToNextCommand(template);
 39  5
                 template.getDefLabels().put(parameters, startLabel);
 40  5
                 return new TemplateCommandState(template, endLabel);
 41  
         }
 42  
 
 43  1
         private class TemplateCommandState extends PlainBlockCommandState {
 44  
 
 45  
                 private final Label endLabel;
 46  
 
 47  5
                 public TemplateCommandState(MatchTemplateImpl template, Label endLabel) {
 48  5
                         super(template);
 49  5
                         this.endLabel = endLabel;
 50  5
                 }
 51  
 
 52  
                 @Override
 53  
                 public boolean canParse(String commandName) {
 54  5
                         return commandName.equals("endtemplate");
 55  
                 }
 56  
 
 57  
                 @Override
 58  
                 public PlainBlockCommandState parseCommand(MatchTemplateImpl template,
 59  
                                 String commandName, String parameters) {
 60  5
                         template.appendOperation(new ReturnOperation(template.getCurrentTemplatePosition()));
 61  5
                         endLabel.setDestinationToNextCommand(template);
 62  5
                         return null;
 63  
                 }
 64  
         }
 65  
 }