~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.core.lrparser/old/org/eclipse/cdt/internal/core/dom/lrparser/c99/action/deprecated/C99TypedefTrackerParserAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 *  Copyright (c) 2006, 2009 IBM Corporation and others.
3
 
 *  All rights reserved. This program and the accompanying materials
4
 
 *  are made available under the terms of the Eclipse Public License v1.0
5
 
 *  which accompanies this distribution, and is available at
6
 
 *  http://www.eclipse.org/legal/epl-v10.html
7
 
 * 
8
 
 *  Contributors:
9
 
 *     IBM Corporation - initial API and implementation
10
 
 *******************************************************************************/
11
 
package org.eclipse.cdt.internal.core.dom.lrparser.c99.action.deprecated;
12
 
 
13
 
import java.util.LinkedList;
14
 
 
15
 
import lpg.lpgjavaruntime.IToken;
16
 
 
17
 
import org.eclipse.cdt.core.dom.lrparser.action.ITokenStream;
18
 
import org.eclipse.cdt.core.parser.util.DebugUtil;
19
 
import org.eclipse.cdt.internal.core.dom.lrparser.symboltable.TypedefSymbolTable;
20
 
/**
21
 
 * A simple set of trial and undo actions that just keep track
22
 
 * of typedef names. This information is then fed back to the parser
23
 
 * in order to disambiguate certain parser grammar rules.
24
 
 * 
25
 
 * The command design pattern is used to implement undo actions.
26
 
 * 
27
 
 * @author Mike Kucera
28
 
 */
29
 
public class C99TypedefTrackerParserAction {
30
 
 
31
 
        private static final boolean DEBUG = true;
32
 
        
33
 
          
34
 
        // provides limited access to the token stream
35
 
        private final ITokenStream parser;
36
 
        
37
 
        // The symbolTable currently in use 
38
 
        private TypedefSymbolTable symbolTable = TypedefSymbolTable.EMPTY_TABLE;
39
 
        
40
 
        // A stack that keeps track of scopes in the symbol table, used to "close" scopes and to undo the opening of scopes
41
 
        private final LinkedList<TypedefSymbolTable> symbolTableScopeStack = new LinkedList<TypedefSymbolTable>();
42
 
        
43
 
        // keeps track of nested declarations
44
 
        private final LinkedList<DeclaratorFrame> declarationStack = new LinkedList<DeclaratorFrame>();
45
 
 
46
 
 
47
 
        // "For every action there is an equal and opposite reaction." - Newton's third law
48
 
        private final LinkedList<IUndoAction> undoStack = new LinkedList<IUndoAction>();
49
 
        
50
 
        
51
 
        /**
52
 
         * A command object that provides undo functionality.
53
 
         */
54
 
        private interface IUndoAction {
55
 
                void undo();
56
 
        }
57
 
        
58
 
        
59
 
        /**
60
 
         * Undoes the last fired action.
61
 
         */
62
 
        public void undo() {
63
 
                undoStack.removeLast().undo();
64
 
        }
65
 
        
66
 
        
67
 
        public C99TypedefTrackerParserAction(ITokenStream parser) {
68
 
                this.parser = parser;
69
 
        }
70
 
        
71
 
 
72
 
        /**
73
 
         * Lexer feedback hack, used by the parser to identify typedefname tokens.
74
 
         */
75
 
        public boolean isTypedef(String ident) {
76
 
                return symbolTable.contains(ident);
77
 
        }
78
 
        
79
 
        
80
 
        /**
81
 
         * Methods used by tests, package local access.
82
 
         */
83
 
        TypedefSymbolTable getSymbolTable() {
84
 
                return symbolTable;
85
 
        }
86
 
        
87
 
        int undoStackSize() {
88
 
                return undoStack.size();
89
 
        }
90
 
        
91
 
        LinkedList<DeclaratorFrame> getDeclarationStack() {
92
 
                return declarationStack;
93
 
        }
94
 
        
95
 
        
96
 
        /**
97
 
         * Called from the grammar file in places where a scope is created.
98
 
         * 
99
 
         * Scopes are created by compound statements, however special care
100
 
         * must also be taken with for loops because they may contain
101
 
         * declarations.
102
 
         * 
103
 
         * TODO: scope object now need to be handled explicitly
104
 
         */
105
 
        public void openSymbolScope() {
106
 
                if(DEBUG) DebugUtil.printMethodTrace();
107
 
                
108
 
                symbolTableScopeStack.add(symbolTable);
109
 
                
110
 
                undoStack.add(new IUndoAction() {
111
 
                        public void undo() {
112
 
                                if(DEBUG) DebugUtil.printMethodTrace();
113
 
                                
114
 
                                symbolTable = symbolTableScopeStack.removeLast();
115
 
                        }
116
 
                });
117
 
        }
118
 
        
119
 
 
120
 
        public void closeSymbolScope() {
121
 
                if(DEBUG) DebugUtil.printMethodTrace();
122
 
                
123
 
                final TypedefSymbolTable undoTable = symbolTable;
124
 
                symbolTable = symbolTableScopeStack.removeLast(); // close the scope
125
 
 
126
 
                undoStack.add(new IUndoAction() {
127
 
                        public void undo() {
128
 
                                if(DEBUG) DebugUtil.printMethodTrace();
129
 
                                
130
 
                                symbolTableScopeStack.add(symbolTable);
131
 
                                symbolTable = undoTable;
132
 
                        }
133
 
                });
134
 
        }
135
 
        
136
 
        
137
 
        
138
 
        /**
139
 
         * Called from the grammar before a declaration is about to be reduced.
140
 
         */
141
 
        public void openDeclarationScope() {
142
 
                if(DEBUG) DebugUtil.printMethodTrace();
143
 
                
144
 
                declarationStack.add(new DeclaratorFrame());
145
 
                
146
 
                undoStack.add(new IUndoAction() {
147
 
                        public void undo() {
148
 
                                if(DEBUG) DebugUtil.printMethodTrace();
149
 
                                
150
 
                                declarationStack.removeLast();
151
 
                        }
152
 
                });
153
 
        }
154
 
        
155
 
        
156
 
        public void closeDeclarationScope() {
157
 
                if(DEBUG) DebugUtil.printMethodTrace();
158
 
                
159
 
                final DeclaratorFrame undoFrame = declarationStack.removeLast();
160
 
                
161
 
                undoStack.add(new IUndoAction() {
162
 
                        public void undo() {
163
 
                                if(DEBUG) DebugUtil.printMethodTrace();
164
 
                                
165
 
                                declarationStack.add(undoFrame);
166
 
                        }
167
 
                });
168
 
        }
169
 
        
170
 
        
171
 
        public void consumeFunctionDefinition() {
172
 
                if(DEBUG) DebugUtil.printMethodTrace();
173
 
 
174
 
                final DeclaratorFrame frame = declarationStack.removeLast();
175
 
                
176
 
                final TypedefSymbolTable undoTable = symbolTable;
177
 
                symbolTable = symbolTableScopeStack.removeLast();
178
 
                
179
 
                
180
 
                undoStack.add(new IUndoAction() {
181
 
                        public void undo() {
182
 
                                if(DEBUG) DebugUtil.printMethodTrace();
183
 
                                
184
 
                                symbolTableScopeStack.add(symbolTable);
185
 
                                symbolTable = undoTable;
186
 
                                
187
 
                                declarationStack.add(frame);
188
 
                        }
189
 
                });
190
 
        }
191
 
 
192
 
 
193
 
 
194
 
 
195
 
        public void consumeDeclSpecToken() {
196
 
                if(DEBUG) DebugUtil.printMethodTrace();
197
 
                
198
 
                IToken token = parser.getRightIToken();
199
 
                final int kind = token.getKind();
200
 
                
201
 
                // creates a DeclSpec if there isn't one already
202
 
                DeclaratorFrame frame = declarationStack.getLast();
203
 
                final DeclSpec declSpec = frame.getDeclSpec();
204
 
                declSpec.add(kind);
205
 
                
206
 
                undoStack.add(new IUndoAction() {
207
 
                        public void undo() {
208
 
                                if(DEBUG) DebugUtil.printMethodTrace();
209
 
                                
210
 
                                declSpec.remove(kind);
211
 
                        }
212
 
                });
213
 
        }
214
 
        
215
 
        
216
 
        
217
 
        public void consumeDirectDeclaratorIdentifier() {
218
 
                if(DEBUG) DebugUtil.printMethodTrace();
219
 
                
220
 
                final DeclaratorFrame frame = declarationStack.getLast();
221
 
                frame.setDeclaratorName(parser.getRightIToken());
222
 
                
223
 
                undoStack.add(new IUndoAction() {
224
 
                        public void undo() {
225
 
                                if(DEBUG) DebugUtil.printMethodTrace();
226
 
                                
227
 
                                frame.setDeclaratorName(null);
228
 
                        }
229
 
                });
230
 
        }
231
 
        
232
 
        
233
 
        public void  consumeDeclaratorComplete() {
234
 
                if(DEBUG) DebugUtil.printMethodTrace();
235
 
                
236
 
                final DeclaratorFrame frame = declarationStack.getLast();
237
 
                
238
 
                IToken token       = frame.getDeclaratorName();
239
 
                DeclSpec declSpec  = frame.getDeclSpec();
240
 
 
241
 
                String ident = (token == null) ? null : token.toString();
242
 
                //System.out.println("declarator complete: " + ident);
243
 
                
244
 
                final TypedefSymbolTable oldTable = symbolTable;
245
 
                if(declSpec.isTypedef()) {
246
 
                        //System.out.println("adding typedef: " + ident);
247
 
                        symbolTable = symbolTable.add(ident);
248
 
                }
249
 
                
250
 
                declarationStack.removeLast();
251
 
                declarationStack.add(new DeclaratorFrame(frame.getDeclSpec())); // reset the declarator
252
 
                
253
 
                undoStack.add(new IUndoAction() {
254
 
                        public void undo() {
255
 
                                if(DEBUG) DebugUtil.printMethodTrace();
256
 
                                
257
 
                                declarationStack.removeLast();
258
 
                                declarationStack.add(frame);
259
 
                                symbolTable = oldTable;
260
 
                        }
261
 
                });
262
 
        }
263
 
        
264
 
        
265
 
        
266
 
        
267
 
        public void consumeDeclaratorCompleteParameter() {
268
 
                if(DEBUG) DebugUtil.printMethodTrace();
269
 
                
270
 
                final DeclaratorFrame frame = declarationStack.removeLast();
271
 
                
272
 
                //declarationStack.getLast().addNestedDeclaration(parameterBinding);
273
 
                
274
 
                // parameter declarations can only have one declarator, so don't reset
275
 
                //declarationStack.add(new DeclaratorFrame()); // reset
276
 
 
277
 
                
278
 
                undoStack.add(new IUndoAction() {
279
 
                        public void undo() {
280
 
                                if(DEBUG) DebugUtil.printMethodTrace();
281
 
                                //declarationStack.removeLast();
282
 
                                //declarationStack.getLast().removeLastNestedDeclaration();
283
 
                                declarationStack.add(frame);
284
 
                        }
285
 
                });
286
 
        }
287
 
        
288
 
        
289
 
        /**
290
 
         * This is a special case for the rule:
291
 
         *     parameter_declaration ::= declaration_specifiers
292
 
         *     
293
 
     * In this case there is no declarator at all
294
 
     * 
295
 
     * TODO: creating bindings that have no identifier seems really dumb,
296
 
     * why does it need to be done? Why not just have a null binding or
297
 
     * for that matter don't even have a name node
298
 
     * 
299
 
         */
300
 
        public void consumeParameterDeclarationWithoutDeclarator() {
301
 
                if(DEBUG) DebugUtil.printMethodTrace();
302
 
                
303
 
                final DeclaratorFrame frame = declarationStack.removeLast();
304
 
                
305
 
                undoStack.add(new IUndoAction() {
306
 
                        public void undo() {
307
 
                                if(DEBUG) DebugUtil.printMethodTrace();
308
 
                                
309
 
                                declarationStack.add(frame);
310
 
                        }
311
 
                });
312
 
        }
313
 
        
314
 
        
315
 
        public void consumeDeclaratorCompleteField() {
316
 
                if(DEBUG) DebugUtil.printMethodTrace();
317
 
                
318
 
                final DeclaratorFrame frame = declarationStack.removeLast();
319
 
 
320
 
                declarationStack.add(new DeclaratorFrame(frame.getDeclSpec()));  // reset the declarator
321
 
 
322
 
                undoStack.add(new IUndoAction() {
323
 
                        public void undo() {
324
 
                                if(DEBUG) DebugUtil.printMethodTrace();
325
 
                                
326
 
                                declarationStack.removeLast();
327
 
 
328
 
                                declarationStack.add(frame);
329
 
                        }
330
 
                });
331
 
        }
332
 
 
333
 
        
334
 
        /**
335
 
         * An abstract declarator used as part of an expression, eg) a cast.
336
 
         * Only need the type.
337
 
         * 
338
 
         * TODO: this isn't enough, I need a binding for the abstract declarator
339
 
         * what I really need is a consumeDeclaratorCompleteTypeId similar to above
340
 
         */
341
 
        public void consumeTypeId() {
342
 
                if(DEBUG) DebugUtil.printMethodTrace();
343
 
                
344
 
                final DeclaratorFrame frame = declarationStack.removeLast();
345
 
                
346
 
                undoStack.add(new IUndoAction() {
347
 
                        public void undo() {
348
 
                                if(DEBUG) DebugUtil.printMethodTrace();
349
 
 
350
 
                                declarationStack.add(frame);
351
 
                        }
352
 
                });
353
 
        }
354
 
        
355
 
}