JSArrayLiteralExpressionImpl.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.JSArrayLiteralExpression; 
22   import com.intellij.lang.javascript.psi.JSElementVisitor; 
23   import com.intellij.lang.javascript.psi.JSExpression; 
24   import com.intellij.psi.PsiElementVisitor; 
25   import com.intellij.psi.tree.IElementType; 
26    
27   import java.util.ArrayList; 
28   import java.util.List; 
29    
30   /** 
31    * Created by IntelliJ IDEA. 
32    * User: max 
33    * Date: Jan 30, 2005 
34    * Time: 11:32:23 PM 
35    * To change this template use File | Settings | File Templates. 
36    */ 
37   public class JSArrayLiteralExpressionImpl extends JSElementImpl implements JSArrayLiteralExpression { 
38     public JSArrayLiteralExpressionImpl(final ASTNode node) { 
39       super(node); 
40     } 
41    
42     public JSExpression[] getExpressions() { 
43       List<JSExpression> result = new ArrayList<JSExpression>(); 
44       ASTNode child = getNode().getFirstChildNode(); 
45       boolean wasExpression = false; 
46       while (child != null) { 
47         final IElementType type = child.getElementType(); 
48         if (JSElementTypes.EXPRESSIONS.isInSet(type)) { 
49           result.add((JSExpression)child.getPsi()); 
50           wasExpression = true; 
51         } 
52         else if (type == JSTokenTypes.COMMA) { 
53           if (wasExpression) { 
54             wasExpression = false; 
55           } 
56           else { 
57             result.add(null); // Skipped expression like [a,,b] 
58           } 
59         } 
60         child = child.getTreeNext(); 
61       } 
62    
63       return result.toArray(new JSExpression[result.size()]); 
64     } 
65    
66     public String toString() { 
67       return "JSArrayLiteralExpression"; 
68     } 
69    
70     public void accept(PsiElementVisitor visitor) { 
71       if (visitor instanceof JSElementVisitor) { 
72         ((JSElementVisitor)visitor).visitJSArrayLiteralExpression(this); 
73       } 
74       else { 
75         visitor.visitElement(this); 
76       } 
77     } 
78   } 
79