001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.cfml.evaluator.impl;
020
021import lucee.commons.lang.StringUtil;
022import lucee.transformer.bytecode.cast.CastString;
023import lucee.transformer.bytecode.literal.LitString;
024import lucee.transformer.bytecode.statement.tag.Attribute;
025import lucee.transformer.bytecode.statement.tag.Tag;
026import lucee.transformer.bytecode.statement.tag.TagContinue;
027import lucee.transformer.bytecode.util.ASMUtil;
028import lucee.transformer.cfml.evaluator.EvaluatorException;
029import lucee.transformer.cfml.evaluator.EvaluatorSupport;
030import lucee.transformer.library.tag.TagLibTag;
031
032
033
034/**
035 * Prueft den Kontext des Tag continue.
036 * Das Tag <code>break</code> darf nur innerhalb des Tag <code>loop, while, foreach</code> liegen.
037 */
038public final class Continue extends EvaluatorSupport {
039
040        @Override
041        public void evaluate(Tag tag,TagLibTag libTag) throws EvaluatorException { 
042                String ns=libTag.getTagLib().getNameSpaceAndSeparator();
043                String loopName=ns+"loop";
044                String whileName=ns+"while";
045                
046                // label
047                String label=null;
048                Attribute attrLabel = tag.getAttribute("label");
049                if(attrLabel!=null){
050                        TagContinue tc=(TagContinue) tag;
051                        label=Break.variableToString(tag,attrLabel,null);
052                        if(label!=null){
053                                tc.setLabel(label=label.trim());
054                                tag.removeAttribute("label");
055                        }
056                        else if(ASMUtil.isLiteralAttribute(tag, attrLabel, ASMUtil.TYPE_STRING, false, true)) {
057                                LitString ls=(LitString) CastString.toExprString(tag.getAttribute("label").getValue());
058                                label = ls.getString();
059                                if(!StringUtil.isEmpty(label,true)) {
060                                        tc.setLabel(label=label.trim());
061                                        tag.removeAttribute("label");
062                                }
063                                else label=null;
064                        }
065                }
066                
067                if(ASMUtil.isLiteralAttribute(tag, "label", ASMUtil.TYPE_STRING, false, true)) {
068                        LitString ls=(LitString) CastString.toExprString(tag.getAttribute("label").getValue());
069                        TagContinue tc=(TagContinue) tag;
070                        label = ls.getString();
071                        if(!StringUtil.isEmpty(label,true)) {
072                                tc.setLabel(label=label.trim());
073                                tag.removeAttribute("label");
074                        }
075                        else label=null;
076                }
077                
078                if(!ASMUtil.hasAncestorContinueFCStatement(tag,label)) {
079                        if(tag.isScriptBase()) {
080                                if(StringUtil.isEmpty(label))
081                                        throw new EvaluatorException("Wrong Context, "+libTag.getName()+" must be inside a loop (for,while,loop ...)");
082                                throw new EvaluatorException("Wrong Context, "+libTag.getName()+" must be inside a loop (for,while,loop ...) with the label ["+label+"]");
083                                
084                        }
085                        if(StringUtil.isEmpty(label))
086                                throw new EvaluatorException("Wrong Context, tag "+libTag.getFullName()+" must be inside a "+loopName+" or "+whileName+" tag");
087                        throw new EvaluatorException("Wrong Context, tag "+libTag.getFullName()+" must be inside a "+loopName+" or "+whileName+" tag with the label ["+label+"]");
088                        
089                }
090        }
091}