2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.php.model.impl.factory;
43
import java.util.HashMap;
44
import java.util.HashSet;
48
import org.netbeans.api.languages.ASTNode;
49
import org.netbeans.api.lexer.TokenSequence;
50
import org.netbeans.modules.php.model.PhpModel;
51
import org.netbeans.modules.php.model.SourceElement;
52
import org.netbeans.modules.php.model.impl.ArrayExpressionImpl;
53
import org.netbeans.modules.php.model.impl.BinaryExpressionImpl;
54
import org.netbeans.modules.php.model.impl.NewExpressionImpl;
55
import org.netbeans.modules.php.model.impl.TernaryExpressionImpl;
56
import org.netbeans.modules.php.model.impl.UnaryExpressionImpl;
57
import org.netbeans.modules.php.model.impl.Utils;
58
import org.netbeans.modules.php.model.impl.VariableImpl;
59
import org.netbeans.modules.php.model.impl.Utils.NodeFinder;
66
public class ExpressionBuilder implements SourceElementBuilder {
68
private static final String LOGICAL_OR = "LogicalORExpression"; // NOI18N
70
private static final String LOGICAL_XOR = "LogicalXORExpression"; // NOI18N
72
private static final String LOGICAL_AND = "LogicalANDExpression"; // NOI18N
74
private static final String ASSIGN_EXPR = "AssignmentExpression"; // NOI18N
76
private static final String CONDITIION = "ConditionalExpression"; // NOI18N
78
private static final String LOGICAL_OR_OR = "LogicalORORExpression"; // NOI18N
80
private static final String LOGICAL_AND_AND = "LogicalANDANDExpression";// NOI18N
82
private static final String BITWISE_OR = "BitwiseORExpression"; // NOI18N
84
private static final String BITWISE_XOR = "BitwiseXORExpression"; // NOI18N
86
private static final String BITWISE_AND = "BitwiseANDExpression"; // NOI18N
88
private static final String EQUALITY_EXPR = "EqualityExpression"; // NOI18N
90
private static final String RELATION_EXPR = "RelationalExpression"; // NOI18N
92
private static final String BIT_SHIFT_EXPR = "BitShiftExpression"; // NOI18N
94
private static final String ADDITIVE_EXPR = "AdditiveExpression"; // NOI18N
96
private static final String MULT_EXPR = "MultiplicativeExpression";// NOI18N
98
private static final String INSTANCE_OF = "InstanceOfExpression"; // NOI18N
100
private static final String POSTFIX_EXPR = "PostfixExpression"; // NOI18N
102
private static final String UNARY = "UnaryExpression"; // NOI18N
105
private static final String NEW_EXPR = "NewExpression"; // NOI18N
107
public static final String BUILT_IN_CALL = "BuiltInCallExpression"; // NOI18N
109
public static final String INCLUDE = "IncludeExpression"; // NOI18N
111
private static final String ARRAY = "ArrayExpression"; // NOI18N
113
private static final String PRIMARY = "PrimaryExpression"; // NOI18N
115
public static final String CONSTANT = "ConstantExpression"; // NOI18N
118
* AST node types that are not represented by OM ( they should be tokens
119
* but they are nodes for simplicity of nbs file ).
122
public static final String ASSIGN_OP = "AssignmentOperator"; // NOI18N
124
public static final String EQUAL_OP = "EqualityOperator"; // NOI18N
126
public static final String REL_OP = "RelationalOperator"; // NOI18N
128
public static final String BIT_SHIFT_OP = "BitShiftOperator"; // NOI18N
130
public static final String ADD_OP = "AdditiveOperator"; // NOI18N
132
public static final String MULT_OP = "MultiplicativeOperator"; // NOI18N
134
public static final String UNARY_OP = "UnaryOperator"; // NOI18N
136
private ExpressionBuilder() {
140
* @see org.netbeans.modules.php.model.impl.factory.SourceElementBuilder#build(org.netbeans.modules.php.model.PhpModel, org.netbeans.api.languages.ASTNode, org.netbeans.api.lexer.TokenSequence)
142
public SourceElement build( PhpModel model, ASTNode node,
143
ASTNode realNode ,TokenSequence sequence )
150
* @see org.netbeans.modules.php.model.impl.factory.SourceElementBuilder#build(org.netbeans.modules.php.model.SourceElement, org.netbeans.api.languages.ASTNode, org.netbeans.api.lexer.TokenSequence)
152
public SourceElement build( SourceElement parent, ASTNode node,
153
ASTNode realNode ,TokenSequence sequence )
155
ASTNode startNode = realNode == null ? node : realNode;
156
ASTNode real = Utils.getNarrowNode( startNode );
157
String type = real.getNT();
158
if ( CONDITIION.equals( type )){
159
return new TernaryExpressionImpl( parent , node, real , sequence );
161
if ( BINARY_EXPRESSIONS.contains( type ) ) {
162
return new BinaryExpressionImpl( parent , node , real, sequence );
164
else if ( UNARY_EXPRESSIONS.contains( type )) {
165
return new UnaryExpressionImpl( parent , node , real, sequence ,
166
type.equals( POSTFIX_EXPR ));
168
else if ( OPERATORS.contains( type )){
172
NodeFinder finder = new NodeFinder( startNode , TYPES.keySet());
174
assert finder.isFound(): "Not found expected type inside " +
176
real = finder.getNode();
177
type = finder.getType();
178
return TYPES.get( type ).build(parent, node, real, sequence);
182
public SourceElementBuilder get( String type ) {
183
return TYPES.get(type);
186
public static ExpressionBuilder getInstance() {
190
private static final ExpressionBuilder INSTANCE = new ExpressionBuilder();
192
private static final Set<String> BINARY_EXPRESSIONS = new HashSet<String>();
194
private static final Set<String> UNARY_EXPRESSIONS = new HashSet<String>();
196
private static final Set<String> OPERATORS = new HashSet<String>();
198
private static final Map<String,SourceElementBuilder> TYPES
199
= new HashMap<String,SourceElementBuilder>();
202
BINARY_EXPRESSIONS.add( LOGICAL_AND );
203
BINARY_EXPRESSIONS.add( LOGICAL_AND_AND );
204
BINARY_EXPRESSIONS.add( LOGICAL_OR );
205
BINARY_EXPRESSIONS.add( LOGICAL_OR_OR );
206
BINARY_EXPRESSIONS.add( LOGICAL_XOR );
207
BINARY_EXPRESSIONS.add( ASSIGN_EXPR );
208
BINARY_EXPRESSIONS.add( BIT_SHIFT_EXPR );
209
BINARY_EXPRESSIONS.add( BITWISE_AND );
210
BINARY_EXPRESSIONS.add( BITWISE_OR );
211
BINARY_EXPRESSIONS.add( BITWISE_XOR );
212
BINARY_EXPRESSIONS.add( EQUALITY_EXPR );
213
BINARY_EXPRESSIONS.add( RELATION_EXPR );
214
BINARY_EXPRESSIONS.add( ADDITIVE_EXPR );
215
BINARY_EXPRESSIONS.add( MULT_EXPR );
216
BINARY_EXPRESSIONS.add( INSTANCE_OF );
218
UNARY_EXPRESSIONS.add( POSTFIX_EXPR );
219
UNARY_EXPRESSIONS.add( UNARY );
221
OPERATORS.add( ASSIGN_OP );
222
OPERATORS.add( EQUAL_OP );
223
OPERATORS.add( REL_OP );
224
OPERATORS.add( BIT_SHIFT_OP );
225
OPERATORS.add( ADD_OP );
226
OPERATORS.add( MULT_OP );
227
OPERATORS.add( UNARY_OP );
229
TYPES.put( CallExpressionBuilder.CALL_EXPR,
230
CallExpressionBuilder.getInstance() );
231
TYPES.put( NEW_EXPR , new NewExpressionBuilder() );
232
TYPES.put( BUILT_IN_CALL , CallExpressionBuilder.getInstance() );
233
TYPES.put( INCLUDE , CallExpressionBuilder.getInstance() );
234
TYPES.put( ARRAY , new ArrayExpressionBuilder() );
235
TYPES.put( PRIMARY , new PrimaryExpressionBuilder() );
236
TYPES.put( CONSTANT, LiteralBuilder.getInstance() );
237
TYPES.put( StaticExpressionBuilder.CLASS_STATIC, StaticExpressionBuilder
239
TYPES.put( CallExpressionFactory.IDENTIFIER ,
240
IdentifierBuilder.getInstance());
243
static class NewExpressionBuilder implements SourceElementBuilder {
245
public SourceElement build( PhpModel model, ASTNode node,
246
ASTNode realNode ,TokenSequence sequence )
253
* @see org.netbeans.modules.php.model.impl.factory.SourceElementBuilder#build(org.netbeans.modules.php.model.SourceElement, org.netbeans.api.languages.ASTNode, org.netbeans.api.lexer.TokenSequence)
255
public SourceElement build( SourceElement parent, ASTNode node,
256
ASTNode realNode ,TokenSequence sequence )
258
return new NewExpressionImpl( parent , node , realNode, sequence );
263
static class PrimaryExpressionBuilder implements SourceElementBuilder {
265
public SourceElement build( PhpModel model, ASTNode node,
266
ASTNode realNode ,TokenSequence sequence )
273
* @see org.netbeans.modules.php.model.impl.factory.SourceElementBuilder#build(org.netbeans.modules.php.model.SourceElement, org.netbeans.api.languages.ASTNode, org.netbeans.api.lexer.TokenSequence)
275
public SourceElement build( SourceElement parent, ASTNode node,
276
ASTNode realNode ,TokenSequence sequence )
278
ASTNode expr = realNode.getNode( ExpressionFactory.EXPRESSION );
279
if ( expr != null ) {
281
* This is not exactly unary epxression , this
282
* is expression surrounded by "(" , ")".
284
return new UnaryExpressionImpl( parent , node , realNode,
287
ASTNode var = realNode.getNode( StaticExpressionBuilder.VARIABLE );
289
return new VariableImpl( parent , node , var,
298
static class ArrayExpressionBuilder implements SourceElementBuilder {
300
public SourceElement build( PhpModel model, ASTNode node,
301
ASTNode realNode ,TokenSequence sequence )
308
* @see org.netbeans.modules.php.model.impl.factory.SourceElementBuilder#build(org.netbeans.modules.php.model.SourceElement, org.netbeans.api.languages.ASTNode, org.netbeans.api.lexer.TokenSequence)
310
public SourceElement build( SourceElement parent, ASTNode node,
311
ASTNode realNode ,TokenSequence sequence )
313
return new ArrayExpressionImpl( parent , node, realNode , sequence );