JSForInStatementImpl.java

1    /* 
2     * Copyright 2000-2005 JetBrains s.r.o. 
3     * 
4     * Licensed under the Apache License, Version 2.0 (the "License"); 
5     * you may not use this file except in compliance with the License. 
6     * You may obtain a copy of the License at 
7     * 
8     * http://www.apache.org/licenses/LICENSE-2.0 
9     * 
10    * Unless required by applicable law or agreed to in writing, software 
11    * distributed under the License is distributed on an "AS IS" BASIS, 
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13    * See the License for the specific language governing permissions and 
14    * limitations under the License. 
15    */ 
16   package com.intellij.lang.javascript.psi.impl; 
17    
18   import com.intellij.lang.ASTNode; 
19   import com.intellij.lang.javascript.JSElementTypes; 
20   import com.intellij.lang.javascript.JSTokenTypes; 
21   import com.intellij.lang.javascript.psi.*; 
22   import com.intellij.psi.PsiElement; 
23   import com.intellij.psi.PsiElementVisitor; 
24   import com.intellij.psi.PsiSubstitutor; 
25   import com.intellij.psi.scope.PsiScopeProcessor; 
26    
27   /** 
28    * Created by IntelliJ IDEA. 
29    * User: max 
30    * Date: Jan 30, 2005 
31    * Time: 11:20:30 PM 
32    * To change this template use File | Settings | File Templates. 
33    */ 
34   public class JSForInStatementImpl extends JSElementImpl implements JSForInStatement { 
35     public JSForInStatementImpl(final ASTNode node) { 
36       super(node); 
37     } 
38    
39     public JSVarStatement getDeclarationStatement() { 
40       final ASTNode childNode = getNode().findChildByType(JSElementTypes.VAR_STATEMENT); 
41       return childNode == null ? null : (JSVarStatement)childNode.getPsi(); 
42     } 
43    
44     public JSExpression getVariableExpression() { 
45       ASTNode child = getNode().getFirstChildNode(); 
46       while (child != null) { 
47         if (child.getElementType() == JSTokenTypes.IN_KEYWORD) return null; 
48         if (JSElementTypes.EXPRESSIONS.isInSet(child.getElementType())) { 
49           return (JSExpression)child.getPsi(); 
50         } 
51       } 
52       return null; 
53     } 
54    
55     public JSExpression getCollectionExpression() { 
56       ASTNode child = getNode().getFirstChildNode(); 
57       boolean inPassed = false; 
58       while (child != null) { 
59         if (child.getElementType() == JSTokenTypes.IN_KEYWORD) { 
60           inPassed = true; 
61         } 
62         if (inPassed && JSElementTypes.EXPRESSIONS.isInSet(child.getElementType())) { 
63           return (JSExpression)child.getPsi(); 
64         } 
65       } 
66    
67       return null; 
68     } 
69    
70     public JSStatement getBody() { 
71       ASTNode child = getNode().getFirstChildNode(); 
72       boolean passedRParen = false; 
73       while (child != null) { 
74         if (child.getElementType() == JSTokenTypes.RPAR) { 
75           passedRParen = true; 
76         } 
77         else if (passedRParen && JSElementTypes.STATEMENTS.isInSet(child.getElementType())) { 
78           return (JSStatement)child.getPsi(); 
79         } 
80         child = child.getTreeNext(); 
81       } 
82    
83       return null; 
84     } 
85    
86     public boolean processDeclarations(PsiScopeProcessor processor, 
87                                        PsiSubstitutor substitutor, 
88                                        PsiElement lastParent, 
89                                        PsiElement place) { 
90       if (lastParent != null) { 
91         final JSVarStatement statement = getDeclarationStatement(); 
92         if (statement != null) return statement.processDeclarations(processor, substitutor, lastParent, place); 
93       } 
94       return true; 
95     } 
96    
97     public void accept(PsiElementVisitor visitor) { 
98       if (visitor instanceof JSElementVisitor) { 
99         ((JSElementVisitor)visitor).visitJSForInStatement(this); 
100      } 
101      else { 
102        visitor.visitElement(this); 
103      } 
104    } 
105   
106    public String toString() { 
107      return "JSForInStatement"; 
108    } 
109  } 
110