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: 10:17:30 PM 32 * To change this template use File | Settings | File Templates. 33 */ 34 public class JSForStatementImpl extends JSElementImpl implements JSForStatement { 35 public JSForStatementImpl(final ASTNode node) { 36 super(node); 37 } 38 39 public JSVarStatement getVarDeclaration() { 40 final ASTNode node = getNode().findChildByType(JSElementTypes.VAR_STATEMENT); 41 return (JSVarStatement)(node != null ? node.getPsi() : null); 42 } 43 44 public JSExpression getInitialization() { 45 ASTNode child = getNode().getFirstChildNode(); 46 while (child != null) { 47 if (child.getElementType() == JSTokenTypes.SEMICOLON) return null; 48 if (JSElementTypes.EXPRESSIONS.isInSet(child.getElementType())) return (JSExpression)child.getPsi(); 49 child = child.getTreeNext(); 50 } 51 return null; 52 } 53 54 public JSExpression getCondition() { 55 ASTNode child = getNode().getFirstChildNode(); 56 int semicolonCount = 0; 57 while (child != null) { 58 if (child.getElementType() == JSTokenTypes.SEMICOLON) { 59 semicolonCount++; 60 if (semicolonCount == 2) return null; 61 } 62 else if (semicolonCount == 1 && JSElementTypes.EXPRESSIONS.isInSet(child.getElementType())) { 63 return (JSExpression)child.getPsi(); 64 } 65 child = child.getTreeNext(); 66 } 67 return null; 68 } 69 70 public JSExpression getUpdate() { 71 ASTNode child = getNode().getFirstChildNode(); 72 int semicolonCount = 0; 73 while (child != null) { 74 if (child.getElementType() == JSTokenTypes.SEMICOLON) { 75 semicolonCount++; 76 } 77 else if (semicolonCount == 2 && JSElementTypes.EXPRESSIONS.isInSet(child.getElementType())) { 78 return (JSExpression)child.getPsi(); 79 } 80 child = child.getTreeNext(); 81 } 82 return null; 83 } 84 85 public JSStatement getBody() { 86 ASTNode child = getNode().getFirstChildNode(); 87 boolean passedRParen = false; 88 while (child != null) { 89 if (child.getElementType() == JSTokenTypes.RPAR) { 90 passedRParen = true; 91 } 92 else if (passedRParen && JSElementTypes.STATEMENTS.isInSet(child.getElementType())) { 93 return (JSStatement)child.getPsi(); 94 } 95 child = child.getTreeNext(); 96 } 97 98 return null; 99 } 100 101 public boolean processDeclarations(PsiScopeProcessor processor, 102 PsiSubstitutor substitutor, 103 PsiElement lastParent, 104 PsiElement place) { 105 if (lastParent != null) { 106 final JSVarStatement statement = getVarDeclaration(); 107 if (statement != null) return statement.processDeclarations(processor, substitutor, lastParent, place); 108 } 109 return true; 110 } 111 112 public void accept(PsiElementVisitor visitor) { 113 if (visitor instanceof JSElementVisitor) { 114 ((JSElementVisitor)visitor).visitJSForStatement(this); 115 } 116 else { 117 visitor.visitElement(this); 118 } 119 } 120 121 public String toString() { 122 return "JSForStatement"; 123 } 124 } 125