View Javadoc

    1   /*
    2    $Id: LabelVerifier.java 3419 2006-01-19 00:07:02Z blackdrag $
    3
    4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    5
    6    Redistribution and use of this software and associated documentation
    7    ("Software"), with or without modification, are permitted provided
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    44
    45   */
    46  package org.codehaus.groovy.control;
    47
    48  import java.util.Iterator;
    49  import java.util.LinkedList;
    50
    51  import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
    52  import org.codehaus.groovy.ast.stmt.BreakStatement;
    53  import org.codehaus.groovy.ast.stmt.ContinueStatement;
    54  import org.codehaus.groovy.ast.stmt.DoWhileStatement;
    55  import org.codehaus.groovy.ast.stmt.ForStatement;
    56  import org.codehaus.groovy.ast.stmt.Statement;
    57  import org.codehaus.groovy.ast.stmt.SwitchStatement;
    58  import org.codehaus.groovy.ast.stmt.WhileStatement;
    59
    60  /***
    61   * This class checks the handling of labels in the AST
    62   *
    63   * @author Jochen Theodorou
    64   */
    65  public class LabelVerifier extends ClassCodeVisitorSupport {
    66
    67      private SourceUnit source;
    68      private LinkedList visitedLabels;
    69      private LinkedList continueLabels;
    70      private LinkedList breakLabels;
    71      boolean inLoop=false;
    72      boolean inSwitch=false;
    73
    74      public LabelVerifier(SourceUnit src) {
    75          source = src;
    76      }
    77
    78      protected SourceUnit getSourceUnit() {
    79          return source;
    80      }
    81
    82      private void init(){
    83          visitedLabels = new LinkedList();
    84          continueLabels = new LinkedList();
    85          breakLabels = new LinkedList();
    86          inLoop=false;
    87          inSwitch=false;
    88      }
    89
    90      protected void visitClassCodeContainer(Statement code) {
    91          init();
    92          super.visitClassCodeContainer(code);
    93          assertNoLabelsMissed();
    94      }
    95
    96     public void visitStatement(Statement statement) {
    97         String label = statement.getStatementLabel();
    98
    99         if (label!=null) {
    100            for (Iterator iter = breakLabels.iterator(); iter.hasNext();) {
    101                BreakStatement element = (BreakStatement) iter.next();
    102                if (element.getLabel().equals(label)) iter.remove();
    103            }
    104
    105            for (Iterator iter = continueLabels.iterator(); iter.hasNext();) {
    106                ContinueStatement element = (ContinueStatement) iter.next();
    107                if (element.getLabel().equals(label)) iter.remove();
    108            }
    109
    110            visitedLabels.add(label);
    111        }
    112
    113        super.visitStatement(statement);
    114 }
    115
    116     public void visitForLoop(ForStatement forLoop) {
    117         boolean oldInLoop = inLoop;
    118         inLoop = true;
    119         super.visitForLoop(forLoop);
    120         inLoop = oldInLoop;
    121     }
    122
    123     public void visitDoWhileLoop(DoWhileStatement loop) {
    124         boolean oldInLoop = inLoop;
    125         inLoop = true;
    126         super.visitDoWhileLoop(loop);
    127         inLoop = oldInLoop;
    128     }
    129
    130     public void visitWhileLoop(WhileStatement loop) {
    131         boolean oldInLoop = inLoop;
    132         inLoop = true;
    133         super.visitWhileLoop(loop);
    134         inLoop = oldInLoop;
    135     }
    136
    137     public void visitBreakStatement(BreakStatement statement) {
    138         String label = statement.getLabel();
    139         boolean hasNamedLabel = label!=null;
    140         if (!hasNamedLabel && !inLoop && !inSwitch) {
    141             addError("the break statement is only allowed inside loops or switches",statement);
    142         } else if (hasNamedLabel && !inLoop) {
    143             addError("the break statement with named label is only allowed inside loops",statement);
    144         }
    145         if (label!=null) {
    146             boolean found=false;
    147             for (Iterator iter = visitedLabels.iterator(); iter.hasNext();) {
    148                 String element = (String) iter.next();
    149                 if (element.equals(label)) {
    150                     found = true;
    151                     break;
    152                 }
    153             }
    154             if (!found) breakLabels.add(statement);
    155         }
    156
    157         super.visitBreakStatement(statement);
    158     }
    159
    160     public void visitContinueStatement(ContinueStatement statement) {
    161         String label = statement.getLabel();
    162         boolean hasNamedLabel = label!=null;
    163         if (!hasNamedLabel && !inLoop) {
    164             addError("the continue statement is only allowed inside loops",statement);
    165         }
    166         if (label!=null) {
    167             boolean found=false;
    168             for (Iterator iter = visitedLabels.iterator(); iter.hasNext();) {
    169                 String element = (String) iter.next();
    170                 if (element.equals(label)) {
    171                     found = true;
    172                     break;
    173                 }
    174             }
    175             if (!found) continueLabels.add(statement);
    176         }
    177
    178         super.visitContinueStatement(statement);
    179     }
    180
    181     protected void assertNoLabelsMissed() {
    182         //TODO: report multiple missing labels of the same name only once
    183         for (Iterator iter = continueLabels.iterator(); iter.hasNext();) {
    184             ContinueStatement element = (ContinueStatement) iter.next();
    185             addError("continue to missing label",element);
    186         }
    187         for (Iterator iter = breakLabels.iterator(); iter.hasNext();) {
    188             BreakStatement element = (BreakStatement) iter.next();
    189             addError("break to missing label",element);
    190         }
    191     }
    192
    193     public void visitSwitch(SwitchStatement statement) {
    194         inSwitch=true;
    195         super.visitSwitch(statement);
    196     }
    197
    198 }