~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to scripting/php/srcmodel/src/org/netbeans/modules/php/model/impl/factory/ExpressionBuilder.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.php.model.impl.factory;
 
42
 
 
43
import java.util.HashMap;
 
44
import java.util.HashSet;
 
45
import java.util.Map;
 
46
import java.util.Set;
 
47
 
 
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;
 
60
 
 
61
 
 
62
/**
 
63
 * @author ads
 
64
 *
 
65
 */
 
66
public class ExpressionBuilder implements SourceElementBuilder {
 
67
    
 
68
    private static final String LOGICAL_OR      = "LogicalORExpression";    // NOI18N
 
69
    
 
70
    private static final String LOGICAL_XOR     = "LogicalXORExpression";   // NOI18N
 
71
    
 
72
    private static final String LOGICAL_AND     = "LogicalANDExpression";   // NOI18N
 
73
    
 
74
    private static final String ASSIGN_EXPR     = "AssignmentExpression";   // NOI18N
 
75
    
 
76
    private static final String CONDITIION      = "ConditionalExpression";  // NOI18N
 
77
    
 
78
    private static final String LOGICAL_OR_OR   = "LogicalORORExpression";  // NOI18N
 
79
    
 
80
    private static final String LOGICAL_AND_AND = "LogicalANDANDExpression";// NOI18N    
 
81
    
 
82
    private static final String BITWISE_OR      = "BitwiseORExpression";    // NOI18N
 
83
    
 
84
    private static final String BITWISE_XOR     = "BitwiseXORExpression";   // NOI18N
 
85
    
 
86
    private static final String BITWISE_AND     = "BitwiseANDExpression";   // NOI18N
 
87
    
 
88
    private static final String EQUALITY_EXPR   = "EqualityExpression";     // NOI18N
 
89
    
 
90
    private static final String RELATION_EXPR   = "RelationalExpression";   // NOI18N
 
91
    
 
92
    private static final String BIT_SHIFT_EXPR  = "BitShiftExpression";     // NOI18N
 
93
    
 
94
    private static final String ADDITIVE_EXPR   = "AdditiveExpression";     // NOI18N
 
95
    
 
96
    private static final String MULT_EXPR       = "MultiplicativeExpression";// NOI18N
 
97
    
 
98
    private static final String INSTANCE_OF     = "InstanceOfExpression";   //  NOI18N  
 
99
    
 
100
    private static final String POSTFIX_EXPR    = "PostfixExpression";      // NOI18N    
 
101
    
 
102
    private static final String UNARY           = "UnaryExpression";        // NOI18N
 
103
    
 
104
    
 
105
    private static final String NEW_EXPR        = "NewExpression";          // NOI18N
 
106
    
 
107
    public static final String BUILT_IN_CALL    = "BuiltInCallExpression";  // NOI18N
 
108
    
 
109
    public static final String INCLUDE          = "IncludeExpression";      // NOI18N
 
110
    
 
111
    private static final String ARRAY           = "ArrayExpression";        // NOI18N
 
112
    
 
113
    private static final String PRIMARY         = "PrimaryExpression";      // NOI18N
 
114
    
 
115
    public  static final String CONSTANT        = "ConstantExpression";     // NOI18N
 
116
    
 
117
    /*
 
118
     * AST node types that are not represented by OM ( they should be tokens
 
119
     * but they are nodes for simplicity of nbs file ).
 
120
     */
 
121
    
 
122
    public static final String ASSIGN_OP        = "AssignmentOperator";     // NOI18N
 
123
    
 
124
    public static final String EQUAL_OP         = "EqualityOperator";       // NOI18N
 
125
    
 
126
    public static final String REL_OP           = "RelationalOperator";     // NOI18N
 
127
    
 
128
    public static final String BIT_SHIFT_OP     = "BitShiftOperator";       // NOI18N
 
129
    
 
130
    public static final String ADD_OP           = "AdditiveOperator";       // NOI18N
 
131
    
 
132
    public static final String MULT_OP          = "MultiplicativeOperator"; // NOI18N
 
133
    
 
134
    public static final String UNARY_OP         = "UnaryOperator";          // NOI18N
 
135
    
 
136
    private ExpressionBuilder() {
 
137
    }
 
138
 
 
139
    /* (non-Javadoc)
 
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)
 
141
     */
 
142
    public SourceElement build( PhpModel model, ASTNode node,
 
143
            ASTNode realNode ,TokenSequence sequence )
 
144
    {
 
145
        assert false;
 
146
        return null;
 
147
    }
 
148
 
 
149
    /* (non-Javadoc)
 
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)
 
151
     */
 
152
    public SourceElement build( SourceElement parent, ASTNode node,
 
153
            ASTNode realNode ,TokenSequence sequence )
 
154
    {
 
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 );
 
160
        }
 
161
        if ( BINARY_EXPRESSIONS.contains( type ) ) {
 
162
            return new BinaryExpressionImpl( parent , node , real, sequence );
 
163
        }
 
164
        else if ( UNARY_EXPRESSIONS.contains( type )) {
 
165
            return new UnaryExpressionImpl( parent , node , real, sequence ,
 
166
                    type.equals( POSTFIX_EXPR ));
 
167
        }
 
168
        else if ( OPERATORS.contains( type )){
 
169
            return null;
 
170
        }
 
171
        else {
 
172
            NodeFinder finder = new NodeFinder( startNode , TYPES.keySet());
 
173
            finder.check();
 
174
            assert finder.isFound(): "Not found expected type inside " +
 
175
                startNode.getNT();
 
176
            real = finder.getNode();
 
177
            type = finder.getType();
 
178
            return TYPES.get( type ).build(parent, node, real, sequence);
 
179
        }
 
180
    }
 
181
    
 
182
    public SourceElementBuilder get( String type ) {
 
183
        return TYPES.get(type);
 
184
    }
 
185
    
 
186
    public static ExpressionBuilder getInstance() {
 
187
        return INSTANCE;
 
188
    }
 
189
    
 
190
    private static final ExpressionBuilder INSTANCE = new ExpressionBuilder();
 
191
 
 
192
    private static final Set<String>  BINARY_EXPRESSIONS = new HashSet<String>();
 
193
    
 
194
    private static final Set<String>  UNARY_EXPRESSIONS  = new HashSet<String>();
 
195
    
 
196
    private static final Set<String>  OPERATORS          = new HashSet<String>();
 
197
    
 
198
    private static final Map<String,SourceElementBuilder> TYPES 
 
199
                = new HashMap<String,SourceElementBuilder>();
 
200
 
 
201
    static {
 
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 );
 
217
        
 
218
        UNARY_EXPRESSIONS.add( POSTFIX_EXPR );
 
219
        UNARY_EXPRESSIONS.add( UNARY );
 
220
        
 
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 );
 
228
 
 
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
 
238
                .getInstance() );
 
239
        TYPES.put( CallExpressionFactory.IDENTIFIER , 
 
240
                IdentifierBuilder.getInstance());
 
241
    }
 
242
    
 
243
    static class NewExpressionBuilder implements SourceElementBuilder {
 
244
 
 
245
        public SourceElement build( PhpModel model, ASTNode node, 
 
246
                ASTNode realNode ,TokenSequence sequence ) 
 
247
        {
 
248
            assert false;
 
249
            return null;
 
250
        }
 
251
 
 
252
        /* (non-Javadoc)
 
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)
 
254
         */
 
255
        public SourceElement build( SourceElement parent, ASTNode node, 
 
256
                ASTNode realNode ,TokenSequence sequence ) 
 
257
        {
 
258
            return new NewExpressionImpl( parent , node , realNode, sequence );
 
259
        }
 
260
        
 
261
    }
 
262
    
 
263
    static class PrimaryExpressionBuilder implements SourceElementBuilder {
 
264
 
 
265
        public SourceElement build( PhpModel model, ASTNode node, 
 
266
                ASTNode realNode ,TokenSequence sequence ) 
 
267
        {
 
268
            assert false;
 
269
            return null;
 
270
        }
 
271
 
 
272
        /* (non-Javadoc)
 
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)
 
274
         */
 
275
        public SourceElement build( SourceElement parent, ASTNode node, 
 
276
                ASTNode realNode ,TokenSequence sequence ) 
 
277
        {
 
278
            ASTNode expr = realNode.getNode( ExpressionFactory.EXPRESSION );
 
279
            if ( expr != null ) {
 
280
                /*
 
281
                 * This is not exactly unary epxression , this 
 
282
                 * is expression surrounded by "(" , ")".
 
283
                 */
 
284
                return new UnaryExpressionImpl( parent , node , realNode,
 
285
                        sequence );
 
286
            }
 
287
            ASTNode var = realNode.getNode( StaticExpressionBuilder.VARIABLE );
 
288
            if ( var!= null ) {
 
289
                return new VariableImpl( parent , node , var, 
 
290
                        sequence );
 
291
            }
 
292
            assert false;
 
293
            return null;
 
294
        }
 
295
        
 
296
    }
 
297
    
 
298
    static class ArrayExpressionBuilder implements SourceElementBuilder {
 
299
 
 
300
        public SourceElement build( PhpModel model, ASTNode node, 
 
301
                ASTNode realNode ,TokenSequence sequence ) 
 
302
        {
 
303
            assert false;
 
304
            return null;
 
305
        }
 
306
 
 
307
        /* (non-Javadoc)
 
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)
 
309
         */
 
310
        public SourceElement build( SourceElement parent, ASTNode node, 
 
311
                ASTNode realNode ,TokenSequence sequence ) 
 
312
        {
 
313
            return new ArrayExpressionImpl( parent , node, realNode , sequence );
 
314
        }
 
315
        
 
316
    }
 
317
 
 
318
}