|
JSConditionalExpressionImpl.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.JSConditionalExpression;
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 /**
28 * Created by IntelliJ IDEA.
29 * User: max
30 * Date: Jan 30, 2005
31 * Time: 11:47:35 PM
32 * To change this template use File | Settings | File Templates.
33 */
34 public class JSConditionalExpressionImpl extends JSElementImpl implements JSConditionalExpression {
35 public JSConditionalExpressionImpl(final ASTNode node) {
36 super(node);
37 }
38
39 public JSExpression getCondition() {
40 ASTNode child = getNode().getFirstChildNode();
41 while (child != null) {
42 final IElementType type = child.getElementType();
43 if (type == JSTokenTypes.QUEST) return null;
44 if (JSElementTypes.EXPRESSIONS.isInSet(type)) return (JSExpression)child.getPsi();
45 child = child.getTreeNext();
46 }
47 return null;
48 }
49
50 public JSExpression getThen() {
51 boolean questPassed = false;
52 ASTNode child = getNode().getFirstChildNode();
53 while (child != null) {
54 final IElementType type = child.getElementType();
55 if (type == JSTokenTypes.QUEST) {
56 questPassed = true;
57 }
58 if (type == JSTokenTypes.COLON) {
59 return null;
60 }
61 if (questPassed && JSElementTypes.EXPRESSIONS.isInSet(type)) {
62 return (JSExpression)child.getPsi();
63 }
64
65 child = child.getTreeNext();
66 }
67 return null;
68 }
69
70 public JSExpression getElse() {
71 boolean questPassed = false;
72 boolean colonPassed = false;
73 ASTNode child = getNode().getFirstChildNode();
74 while (child != null) {
75 final IElementType type = child.getElementType();
76 if (type == JSTokenTypes.QUEST) {
77 questPassed = true;
78 }
79 if (type == JSTokenTypes.COLON) {
80 colonPassed = true;
81 }
82 if (questPassed && colonPassed && JSElementTypes.EXPRESSIONS.isInSet(type)) {
83 return (JSExpression)child.getPsi();
84 }
85
86 child = child.getTreeNext();
87 }
88 return null;
89 }
90
91 public void accept(PsiElementVisitor visitor) {
92 if (visitor instanceof JSElementVisitor) {
93 ((JSElementVisitor)visitor).visitJSConditionalExpression(this);
94 }
95 else {
96 visitor.visitElement(this);
97 }
98 }
99
100 public String toString() {
101 return "JSConditionalExpression";
102 }
103 }
104