| 1 | | /* |
| 2 | | * xtc - The eXTensible Compiler |
| 3 | | * Copyright (C) 2005-2006 Princeton University |
| 4 | | * |
| 5 | | * This program is free software; you can redistribute it and/or |
| 6 | | * modify it under the terms of the GNU General Public License |
| 7 | | * version 2 as published by the Free Software Foundation. |
| 8 | | * |
| 9 | | * This program is distributed in the hope that it will be useful, |
| 10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | | * GNU General Public License for more details. |
| 13 | | * |
| 14 | | * You should have received a copy of the GNU General Public License |
| 15 | | * along with this program; if not, write to the Free Software |
| 16 | | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 17 | | * USA. |
| 18 | | */ |
| 19 | | package xtc.lang.c4; |
| 20 | | |
| 21 | | import java.util.ArrayList; |
| 22 | | import java.util.HashMap; |
| 23 | | import java.util.Iterator; |
| 24 | | |
| 25 | | import xtc.lang.c4.C4State.Context; |
| 26 | | import xtc.tree.GNode; |
| 27 | | import xtc.tree.LineMarker; |
| 28 | | import xtc.tree.Location; |
| 29 | | import xtc.tree.Node; |
| 30 | | import xtc.tree.Pragma; |
| 31 | | import xtc.tree.SourceIdentity; |
| 32 | | import xtc.tree.Visitor; |
| 33 | | import xtc.util.State; |
| 34 | | |
| 35 | | |
| 36 | | /** |
| 37 | | * State for parsing C4. |
| 38 | | * |
| 39 | | * @author Laurent Burgy |
| 40 | | * @version $Revision: 1.3 $ |
| 41 | | */ |
| 42 | | public class C4ParserState implements State { |
| 43 | | |
| 44 | | private class MacroUndefException extends RuntimeException { |
| 45 | | /** |
| | 1 | /* |
| | 2 | * xtc - The eXTensible Compiler |
| | 3 | * Copyright (C) 2005-2006 Princeton University |
| | 4 | * |
| | 5 | * This program is free software; you can redistribute it and/or |
| | 6 | * modify it under the terms of the GNU General Public License |
| | 7 | * version 2 as published by the Free Software Foundation. |
| | 8 | * |
| | 9 | * This program is distributed in the hope that it will be useful, |
| | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 12 | * GNU General Public License for more details. |
| | 13 | * |
| | 14 | * You should have received a copy of the GNU General Public License |
| | 15 | * along with this program; if not, write to the Free Software |
| | 16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| | 17 | * USA. |
| | 18 | */ |
| | 19 | package xtc.lang.c4; |
| | 20 | |
| | 21 | import java.util.ArrayList; |
| | 22 | import java.util.HashMap; |
| | 23 | import java.util.Iterator; |
| | 24 | |
| | 25 | import xtc.lang.c4.C4State.Context; |
| | 26 | import xtc.tree.GNode; |
| | 27 | import xtc.tree.LineMarker; |
| | 28 | import xtc.tree.Location; |
| | 29 | import xtc.tree.Node; |
| | 30 | import xtc.tree.Pragma; |
| | 31 | import xtc.tree.SourceIdentity; |
| | 32 | import xtc.tree.Visitor; |
| | 33 | import xtc.util.State; |
| | 34 | |
| | 35 | |
| | 36 | /** |
| | 37 | * State for parsing C4. |
| | 38 | * |
| | 39 | * @author Laurent Burgy |
| | 40 | * @version $Revision: 1.3 $ |
| | 41 | */ |
| | 42 | public class C4ParserState implements State { |
| | 43 | |
| | 44 | private class MacroUndefException extends RuntimeException { |
| | 45 | /** |
| | 46 | * |
| | 47 | */ |
| | 48 | private static final long serialVersionUID = -4091350260433333109L; |
| | 49 | private String macroName; |
| | 50 | |
| | 51 | MacroUndefException(String name) { |
| | 52 | super(); |
| | 53 | macroName = name; |
| | 54 | } |
| | 55 | |
| | 56 | MacroUndefException() { |
| | 57 | super(); |
| | 58 | } |
| | 59 | |
| | 60 | public void printStackTrace() { |
| | 61 | System.out.println("Undefined Macro " + macroName); |
| | 62 | super.printStackTrace(); |
| | 63 | } |
| | 64 | |
| | 65 | public String getMessage() { |
| | 66 | return ("Undefined Macro" + macroName + " " + super.getMessage()); |
| | 67 | } |
| | 68 | |
| | 69 | public String getMacroName() { |
| | 70 | if(null != macroName) |
| | 71 | return macroName; |
| | 72 | return ""; |
| | 73 | } |
| | 74 | |
| | 75 | } |
| | 76 | |
| | 77 | |
| | 78 | // ========================================================================== |
| | 79 | |
| | 80 | /** |
| | 81 | * Add the specified number of fresh contexts to the pool. |
| | 82 | * |
| | 83 | * @param n The number to add. |
| | 84 | */ |
| | 85 | protected void fillPool(int n) { |
| | 86 | preState.fillPool(n); |
| | 87 | } |
| | 88 | |
| | 89 | /** |
| | 90 | * Take a context from the pool, refilling the pool if necessary. |
| | 91 | * |
| | 92 | * @return A fresh context. |
| | 93 | */ |
| | 94 | protected Context takeFromPool() { |
| | 95 | return preState.takeFromPool(); |
| | 96 | } |
| | 97 | |
| | 98 | /** |
| | 99 | * Return the specified context to the pool, clearing it along the |
| | 100 | * way. |
| | 101 | * |
| | 102 | * @param c The context to return. |
| | 103 | */ |
| | 104 | protected void addToPool(Context c) { |
| | 105 | preState.addToPool(c); |
| | 106 | } |
| | 107 | |
| | 108 | // ========================================================================== |
| | 109 | |
| | 110 | /** |
| | 111 | * Push the specified context onto the context stack. |
| | 112 | * |
| | 113 | * @param c The context to push. |
| | 114 | */ |
| | 115 | protected void push(Context c) { |
| | 116 | preState.push(c); |
| | 117 | } |
| | 118 | |
| | 119 | /** |
| | 120 | * Pop a context from the context stack. |
| | 121 | * |
| | 122 | * @return The top-most context. |
| | 123 | */ |
| | 124 | protected Context pop() { |
| | 125 | return preState.pop(); |
| | 126 | } |
| | 127 | |
| | 128 | public void start() { |
| | 129 | preState.start(); |
| | 130 | } |
| | 131 | |
| | 132 | public void commit() { |
| | 133 | preState.commit(); |
| | 134 | } |
| | 135 | |
| | 136 | public void abort() { |
| | 137 | preState.abort(); |
| | 138 | } |
| | 139 | |
| | 140 | // ========================================================================== |
| | 141 | |
| | 142 | /** Record a typedef storage class specifier. */ |
| | 143 | public void typedef() { |
| | 144 | preState.typedef(); |
| | 145 | } |
| | 146 | |
| | 147 | /** Record a function parameter list. */ |
| | 148 | public void parameters() { |
| | 149 | preState.parameters(); |
| | 150 | } |
| | 151 | |
| | 152 | /** Record a function declarator. */ |
| | 153 | public void functionDeclarator() { |
| | 154 | preState.functionDeclarator(); |
| | 155 | } |
| | 156 | |
| | 157 | /** Enter a new scope. */ |
| | 158 | public void pushScope() { |
| | 159 | preState.pushScope(); |
| | 160 | } |
| | 161 | |
| | 162 | /** Exit the last scope. */ |
| | 163 | public void popScope() { |
| | 164 | preState.popScope(); |
| | 165 | } |
| | 166 | |
| | 167 | /** Enter a structure declaration list. */ |
| | 168 | public void enterStructure() { |
| | 169 | preState.enterStructure(); |
| | 170 | } |
| | 171 | |
| | 172 | /** Exit a structure declaration list. */ |
| | 173 | public void exitStructure() { |
| | 174 | preState.exitStructure(); |
| | 175 | } |
| | 176 | |
| | 177 | |
| | 178 | |
| | 179 | /** |
| | 180 | * Determine whether a declaration actually is a declaration. This |
| | 181 | * method determines whether the sequence |
| | 182 | * <pre> |
| | 183 | * DeclarationSpecifiers l:InitializedDeclaratorList? |
| | 184 | * </pre> |
| | 185 | * can actually represent a declaration. It assumes that any type |
| | 186 | * specifier encountered while parsing |
| | 187 | * <code>DeclarationSpecifiers</code> has been marked through {@link |
| | 188 | * #typeSpecifier()}. |
| | 189 | * |
| | 190 | * @param idl The result of parsing the optional initialized |
| | 191 | * declarator list. |
| | 192 | * @return <code>true</code> if the declaration is a declaration. |
| | 193 | */ |
| | 194 | public boolean isValid(Node idl) { |
| | 195 | return preState.isValid(idl); |
| | 196 | } |
| | 197 | |
| | 198 | // ========================================================================== |
| | 199 | |
| | 200 | /** |
| | 201 | * Record a line marker. Note that string values for the four flags |
| | 202 | * are interpreted as follows: Any non-null string counts for |
| | 203 | * <code>true</code>, while null counts for <code>false</code>. |
| | 204 | * |
| | 205 | * @see LineMarker |
| | 206 | * |
| | 207 | * @param file The file name (without quotes). |
| | 208 | * @param line The line number. |
| | 209 | * @param isStartFile The start file flag. |
| | 210 | * @param isReturnToFile The return to file flag. |
| | 211 | * @param isSystemHeader The system header flag. |
| | 212 | * @param isExternC The extern C flag. |
| | 213 | * @param location The line marker's source location. |
| | 214 | */ |
| | 215 | public void lineMarker(String file, int line, String isStartFile, |
| | 216 | String isReturnToFile, String isSystemHeader, |
| | 217 | String isExternC, Location location) { |
| | 218 | preState.lineMarker(file, line, isStartFile, isReturnToFile, isSystemHeader, isExternC, location); |
| | 219 | } |
| | 220 | |
| | 221 | /** |
| | 222 | * Record a pragma. |
| | 223 | * |
| | 224 | * @see Pragma |
| | 225 | * |
| | 226 | * @param directive The actual directive. |
| | 227 | * @param location The pragma's source location. |
| | 228 | */ |
| | 229 | public void pragma(String directive, Location location) { |
| | 230 | preState.pragma(directive, location); |
| | 231 | } |
| | 232 | |
| | 233 | /** |
| | 234 | * Record an ident directive. |
| | 235 | * |
| | 236 | * @see SourceIdentity |
| | 237 | * |
| | 238 | * @param ident The actual identity marker. |
| | 239 | * @param location The ident directive's source location. |
| | 240 | */ |
| | 241 | public void ident(String ident, Location location) { |
| | 242 | preState.ident(ident, location); |
| | 243 | } |
| | 244 | |
| | 245 | /** |
| | 246 | * Mark the current annotation. This method must be called before |
| | 247 | * recognizing the nonterminals to be annotated. Furthermore, it |
| | 248 | * must be called within the context of a stateful production. |
| | 249 | */ |
| | 250 | public void mark() { |
| | 251 | preState.mark(); |
| | 252 | } |
| | 253 | |
| | 254 | /** |
| | 255 | * Annotate the specified node. If any annotations have been |
| | 256 | * recorded and {@link #mark() marked}, the specified node is |
| | 257 | * wrapped by those annotations and the outer-most annotation is |
| | 258 | * returned. Otherwise, the specified node is returned. |
| | 259 | * |
| | 260 | * @param node The node. |
| | 261 | * @return The annotated node. |
| | 262 | */ |
| | 263 | public Node annotate(Node node) { |
| | 264 | return preState.annotate(node); |
| | 265 | } |
| | 266 | |
| | 267 | //=========================================================================== |
| | 268 | /** The C4 State to manage everything |
| | 269 | * before everything was collecting using C4ParserState |
| | 270 | * but it's no longer possible |
| | 271 | * I've added a static field that manages all the state |
| | 272 | */ |
| | 273 | static C4State preState = new C4State(); |
| | 274 | |
| | 275 | /** The current Object MacroId */ |
| | 276 | String omId ; |
| | 277 | |
| | 278 | /** |
| | 279 | * Expression Evaluator |
| | 280 | */ |
| | 281 | /** Left Value for a If directive */ |
| | 282 | String leftValue ; |
| | 283 | |
| | 284 | boolean leftValueSet; |
| | 285 | |
| | 286 | boolean isLeft = false; |
| | 287 | |
| | 288 | boolean isRight = false; |
| | 289 | |
| | 290 | /** Right Value for a If directive */ |
| | 291 | String rightValue; |
| | 292 | |
| | 293 | /** To check if the macro currently defined is an attribute */ |
| | 294 | boolean isAttribute = false; |
| | 295 | |
| | 296 | /** |
| | 297 | * Variable that stores the temp value of the IfSection Conditional (if arithmetic included) |
| | 298 | */ |
| | 299 | long tmpCondValue = 0; |
| | 300 | |
| | 301 | boolean tmpCondValueSet = false; |
| | 302 | |
| | 303 | // Logger dbgLogger; |
| | 304 | |
| | 305 | /** |
| | 306 | * Default constructor. |
| | 307 | */ |
| | 308 | public C4ParserState() { |
| | 309 | super(); |
| | 310 | } |
| | 311 | |
| | 312 | /** |
| | 313 | * Constructor with a macro manager. |
| | 314 | * |
| | 315 | * @param macrosManager An instance of the C4MacrosManager. |
| | 316 | */ |
| | 317 | public C4ParserState(C4MacrosManager macrosManager) { |
| | 318 | this(); |
| | 319 | if(preState == null) //If we do not use the wrapper |
| | 320 | resetPreState(); // we need to instantiate the preState |
| | 321 | |
| | 322 | preState.setMacrosManager(macrosManager); |
| | 323 | } |
| | 324 | |
| | 325 | /** |
| | 326 | * Sets the include path manager. |
| 47 | | */ |
| 48 | | private static final long serialVersionUID = -4091350260433333109L; |
| 49 | | private String macroName; |
| 50 | | |
| 51 | | MacroUndefException(String name) { |
| 52 | | super(); |
| 53 | | macroName = name; |
| 54 | | } |
| 55 | | |
| 56 | | MacroUndefException() { |
| 57 | | super(); |
| 58 | | } |
| 59 | | |
| 60 | | public void printStackTrace() { |
| 61 | | System.out.println("Undefined Macro " + macroName); |
| 62 | | super.printStackTrace(); |
| 63 | | } |
| 64 | | |
| 65 | | public String getMessage() { |
| 66 | | return ("Undefined Macro" + macroName + " " + super.getMessage()); |
| 67 | | } |
| 68 | | |
| 69 | | public String getMacroName() { |
| 70 | | if(null != macroName) |
| 71 | | return macroName; |
| 72 | | return ""; |
| 73 | | } |
| 74 | | |
| 75 | | } |
| 76 | | |
| 77 | | |
| 78 | | // ========================================================================== |
| 79 | | |
| 80 | | /** |
| 81 | | * Add the specified number of fresh contexts to the pool. |
| 82 | | * |
| 83 | | * @param n The number to add. |
| 84 | | */ |
| 85 | | protected void fillPool(int n) { |
| 86 | | preState.fillPool(n); |
| 87 | | } |
| 88 | | |
| 89 | | /** |
| 90 | | * Take a context from the pool, refilling the pool if necessary. |
| 91 | | * |
| 92 | | * @return A fresh context. |
| 93 | | */ |
| 94 | | protected Context takeFromPool() { |
| 95 | | return preState.takeFromPool(); |
| 96 | | } |
| 97 | | |
| 98 | | /** |
| 99 | | * Return the specified context to the pool, clearing it along the |
| 100 | | * way. |
| 101 | | * |
| 102 | | * @param c The context to return. |
| 103 | | */ |
| 104 | | protected void addToPool(Context c) { |
| 105 | | preState.addToPool(c); |
| 106 | | } |
| 107 | | |
| 108 | | // ========================================================================== |
| 109 | | |
| 110 | | /** |
| 111 | | * Push the specified context onto the context stack. |
| 112 | | * |
| 113 | | * @param c The context to push. |
| 114 | | */ |
| 115 | | protected void push(Context c) { |
| 116 | | preState.push(c); |
| 117 | | } |
| 118 | | |
| 119 | | /** |
| 120 | | * Pop a context from the context stack. |
| 121 | | * |
| 122 | | * @return The top-most context. |
| 123 | | */ |
| 124 | | protected Context pop() { |
| 125 | | return preState.pop(); |
| 126 | | } |
| 127 | | |
| 128 | | public void start() { |
| 129 | | preState.start(); |
| 130 | | } |
| 131 | | |
| 132 | | public void commit() { |
| 133 | | preState.commit(); |
| 134 | | } |
| 135 | | |
| 136 | | public void abort() { |
| 137 | | preState.abort(); |
| 138 | | } |
| 139 | | |
| 140 | | // ========================================================================== |
| 141 | | |
| 142 | | /** Record a typedef storage class specifier. */ |
| 143 | | public void typedef() { |
| 144 | | preState.typedef(); |
| 145 | | } |
| 146 | | |
| 147 | | /** Record a function parameter list. */ |
| 148 | | public void parameters() { |
| 149 | | preState.parameters(); |
| 150 | | } |
| 151 | | |
| 152 | | /** Record a function declarator. */ |
| 153 | | public void functionDeclarator() { |
| 154 | | preState.functionDeclarator(); |
| 155 | | } |
| 156 | | |
| 157 | | /** Enter a new scope. */ |
| 158 | | public void pushScope() { |
| 159 | | preState.pushScope(); |
| 160 | | } |
| 161 | | |
| 162 | | /** Exit the last scope. */ |
| 163 | | public void popScope() { |
| 164 | | preState.popScope(); |
| 165 | | } |
| 166 | | |
| 167 | | /** Enter a structure declaration list. */ |
| 168 | | public void enterStructure() { |
| 169 | | preState.enterStructure(); |
| 170 | | } |
| 171 | | |
| 172 | | /** Exit a structure declaration list. */ |
| 173 | | public void exitStructure() { |
| 174 | | preState.exitStructure(); |
| 175 | | } |
| 176 | | |
| 177 | | |
| 178 | | |
| 179 | | /** |
| 180 | | * Determine whether a declaration actually is a declaration. This |
| 181 | | * method determines whether the sequence |
| 182 | | * <pre> |
| 183 | | * DeclarationSpecifiers l:InitializedDeclaratorList? |
| 184 | | * </pre> |
| 185 | | * can actually represent a declaration. It assumes that any type |
| 186 | | * specifier encountered while parsing |
| 187 | | * <code>DeclarationSpecifiers</code> has been marked through {@link |
| 188 | | * #typeSpecifier()}. |
| 189 | | * |
| 190 | | * @param idl The result of parsing the optional initialized |
| 191 | | * declarator list. |
| 192 | | * @return <code>true</code> if the declaration is a declaration. |
| 193 | | */ |
| 194 | | public boolean isValid(Node idl) { |
| 195 | | return preState.isValid(idl); |
| 196 | | } |
| 197 | | |
| 198 | | // ========================================================================== |
| 199 | | |
| 200 | | /** |
| 201 | | * Record a line marker. Note that string values for the four flags |
| 202 | | * are interpreted as follows: Any non-null string counts for |
| 203 | | * <code>true</code>, while null counts for <code>false</code>. |
| 204 | | * |
| 205 | | * @see LineMarker |
| 206 | | * |
| 207 | | * @param file The file name (without quotes). |
| 208 | | * @param line The line number. |
| 209 | | * @param isStartFile The start file flag. |
| 210 | | * @param isReturnToFile The return to file flag. |
| 211 | | * @param isSystemHeader The system header flag. |
| 212 | | * @param isExternC The extern C flag. |
| 213 | | * @param location The line marker's source location. |
| 214 | | */ |
| 215 | | public void lineMarker(String file, int line, String isStartFile, |
| 216 | | String isReturnToFile, String isSystemHeader, |
| 217 | | String isExternC, Location location) { |
| 218 | | preState.lineMarker(file, line, isStartFile, isReturnToFile, isSystemHeader, isExternC, location); |
| 219 | | } |
| 220 | | |
| 221 | | /** |
| 222 | | * Record a pragma. |
| 223 | | * |
| 224 | | * @see Pragma |
| 225 | | * |
| 226 | | * @param directive The actual directive. |
| 227 | | * @param location The pragma's source location. |
| 228 | | */ |
| 229 | | public void pragma(String directive, Location location) { |
| 230 | | preState.pragma(directive, location); |
| 231 | | } |
| 232 | | |
| 233 | | /** |
| 234 | | * Record an ident directive. |
| 235 | | * |
| 236 | | * @see SourceIdentity |
| 237 | | * |
| 238 | | * @param ident The actual identity marker. |
| 239 | | * @param location The ident directive's source location. |
| 240 | | */ |
| 241 | | public void ident(String ident, Location location) { |
| 242 | | preState.ident(ident, location); |
| 243 | | } |
| 244 | | |
| 245 | | /** |
| 246 | | * Mark the current annotation. This method must be called before |
| 247 | | * recognizing the nonterminals to be annotated. Furthermore, it |
| 248 | | * must be called within the context of a stateful production. |
| 249 | | */ |
| 250 | | public void mark() { |
| 251 | | preState.mark(); |
| 252 | | } |
| 253 | | |
| 254 | | /** |
| 255 | | * Annotate the specified node. If any annotations have been |
| 256 | | * recorded and {@link #mark() marked}, the specified node is |
| 257 | | * wrapped by those annotations and the outer-most annotation is |
| 258 | | * returned. Otherwise, the specified node is returned. |
| 259 | | * |
| 260 | | * @param node The node. |
| 261 | | * @return The annotated node. |
| 262 | | */ |
| 263 | | public Node annotate(Node node) { |
| 264 | | return preState.annotate(node); |
| 265 | | } |
| 266 | | |
| 267 | | //=========================================================================== |
| 268 | | /** The C4 State to manage everything |
| 269 | | * before everything was collecting using C4ParserState |
| 270 | | * but it's no longer possible |
| 271 | | * I've added a static field that manages all the state |
| 272 | | */ |
| 273 | | static C4State preState = new C4State(); |
| 274 | | |
| 275 | | /** The current Object MacroId */ |
| 276 | | String omId ; |
| 277 | | |
| 278 | | /** |
| 279 | | * Expression Evaluator |
| 280 | | */ |
| 281 | | /** Left Value for a If directive */ |
| 282 | | String leftValue ; |
| 283 | | |
| 284 | | boolean leftValueSet; |
| 285 | | |
| 286 | | boolean isLeft = false; |
| 287 | | |
| 288 | | boolean isRight = false; |
| 289 | | |
| 290 | | /** Right Value for a If directive */ |
| 291 | | String rightValue; |
| 292 | | |
| 293 | | /** To check if the macro currently defined is an attribute */ |
| 294 | | boolean isAttribute = false; |
| 295 | | |
| 296 | | /** |
| 297 | | * Variable that stores the temp value of the IfSection Conditional (if arithmetic included) |
| 298 | | */ |
| 299 | | long tmpCondValue = 0; |
| 300 | | |
| 301 | | boolean tmpCondValueSet = false; |
| 302 | | |
| 303 | | // Logger dbgLogger; |
| 304 | | |
| 305 | | /** |
| 306 | | * Default constructor. |
| 307 | | */ |
| 308 | | public C4ParserState() { |
| 309 | | super(); |
| 310 | | } |
| 311 | | |
| 312 | | /** |
| 313 | | * Constructor with a macro manager. |
| 314 | | * |
| 315 | | * @param macrosManager An instance of the C4MacrosManager. |
| 316 | | */ |
| 317 | | public C4ParserState(C4MacrosManager macrosManager) { |
| 318 | | this(); |
| 319 | | if(preState == null) //If we do not use the wrapper |
| 320 | | resetPreState(); // we need to instantiate the preState |
| | 328 | * @param manager |
| | 329 | * An instance of the include path manager. |
| | 330 | */ |
| | 331 | public void setIncludePathManager(C4IncludePathManager manager) { |
| | 332 | preState.setIncludePathManager(manager); |
| | 333 | } |
| | 334 | |
| | 335 | /** |
| | 336 | * Sets the parseTree flag |
| | 337 | * @param parseTreeFlag |
| | 338 | * A boolean |
| | 339 | */ |
| | 340 | public void setParseTree(boolean parseTree) { |
| | 341 | preState.setParseTree(parseTree); |
| | 342 | } |
| 322 | | preState.setMacrosManager(macrosManager); |
| 323 | | } |
| 324 | | |
| 325 | | /** |
| 326 | | * Sets the include path manager. |
| 327 | | * |
| 328 | | * @param manager |
| 329 | | * An instance of the include path manager. |
| 330 | | */ |
| 331 | | public void setIncludePathManager(C4IncludePathManager manager) { |
| 332 | | preState.setIncludePathManager(manager); |
| 333 | | } |
| 334 | | |
| 335 | | /** |
| 336 | | * Returns the include path manager. |
| 337 | | * |
| 338 | | * @return The include path manager. |
| 339 | | */ |
| 340 | | public C4IncludePathManager getIncludePathManager() { |
| 341 | | return preState.getIncludePathManager(); |
| 342 | | } |
| 343 | | |
| 344 | | /** |
| 345 | | * Increments the include level. |
| 346 | | */ |
| 347 | | public void incIncludeLevel() { |
| 348 | | preState.incIncludeLevel(); |
| 349 | | } |
| 350 | | |
| 351 | | /** |
| 352 | | * Decrements the include level. |
| 353 | | */ |
| 354 | | public void decIncludeLevel() { |
| 355 | | preState.decIncludeLevel(); |
| 356 | | } |
| 357 | | |
| 358 | | public void reset(String file) { |
| 359 | | preState.reset(file); |
| 360 | | } |
| 361 | | |
| 362 | | //--------------------------------------------------------------------------- |
| 363 | | // Macros |
| 364 | | //--------------------------------------------------------------------------- |
| 365 | | /** |
| 366 | | * for the String Constant with macro parameters |
| 367 | | */ |
| 368 | | public boolean isMacroParameter(String id) { |
| 369 | | return preState.isMacroParameter(id); |
| 370 | | } |
| 371 | | |
| 372 | | /** |
| 373 | | * Tells whether or not the token is a function Macro |
| 374 | | * @param token |
| 375 | | * @return true if the token is a function macro |
| 376 | | */ |
| 377 | | public boolean isFunctionMacro(String token) { |
| 378 | | return preState.isFunctionMacro(token); |
| 379 | | } |
| 380 | | |
| 381 | | /** |
| 382 | | * Gets the object macro associated with the token |
| 383 | | * @param token |
| 384 | | * @return Object representing the object macro |
| 385 | | */ |
| 386 | | public Object getObjectMacro(String token) { |
| 387 | | return preState.getObjectMacro(token); |
| 388 | | } |
| 389 | | |
| 390 | | /** |
| 391 | | * Tells whether or not the token is an object macro |
| 392 | | * @param token |
| 393 | | * @return true if the token is an object macro |
| 394 | | */ |
| 395 | | public boolean isObjectMacro(String token) { |
| 396 | | return preState.isObjectMacro(token); |
| 397 | | } |
| 398 | | |
| 399 | | /** |
| 400 | | * Tells whether or not the token is a macro |
| 401 | | * @param token |
| 402 | | * @return true if the token is a macro name |
| 403 | | */ |
| 404 | | public boolean isMacro(String token) { |
| 405 | | return preState.isMacro(token); |
| 406 | | } |
| 407 | | |
| 408 | | /** |
| 409 | | * Gets the function macro associated with the token |
| 410 | | * @param token |
| 411 | | * @return Object representing the object macro |
| 412 | | */ |
| 413 | | public Object getFunctionMacro(String token) { |
| 414 | | return preState.getFunctionMacro(token); |
| 415 | | } |
| 416 | | |
| 417 | | /** |
| 418 | | * Removes a macro |
| 419 | | * @param id The name of the macro to remove |
| 420 | | */ |
| 421 | | public void removeMacro(String id) { |
| 422 | | preState.removeMacro(id); |
| 423 | | } |
| 424 | | |
| 425 | | /** |
| 426 | | * Gets a macro by its name |
| 427 | | * @param macroName |
| 428 | | * @return Node of the macro |
| 429 | | */ |
| 430 | | public Node getMacro(String macroName) { |
| 431 | | return preState.getMacro(macroName); |
| 432 | | } |
| 433 | | |
| 434 | | /** |
| 435 | | * Stores a new macro |
| 436 | | * @param macroNode Node of the macro to store |
| 437 | | */ |
| 438 | | public void storeMacro(Node macroNode){ |
| 439 | | preState.getMacrosManager().storeMacro(macroNode); |
| 440 | | } |
| 441 | | |
| 442 | | /** |
| 443 | | * To store the parameters of a function Macro |
| 444 | | * @param n The parameters node |
| 445 | | */ |
| 446 | | public void storeParameters(Node n) { |
| 447 | | preState.storeParameters(n); |
| 448 | | } |
| 449 | | |
| 450 | | /** |
| 451 | | * Clean-up function |
| 452 | | */ |
| 453 | | public void purgeParameters() { |
| 454 | | preState.purgeParameters(); |
| 455 | | } |
| 456 | | |
| 457 | | |
| 458 | | /** |
| 459 | | * A commodity method to convert a string as an integer |
| 460 | | * @param tmpS1 String to be converted |
| 461 | | * @return Integer representing the string parameter |
| 462 | | */ |
| 463 | | public long string2long(String tmpS1) { |
| 464 | | long tmp1; |
| 465 | | try { |
| 466 | | tmp1 = Long.valueOf(tmpS1); |
| 467 | | } catch (NumberFormatException e) { |
| 468 | | //System.err.println("NumberFormatException " + tmpS1); |
| 469 | | tmp1 = 0; |
| 470 | | } |
| 471 | | return tmp1; |
| 472 | | |
| 473 | | } |
| 474 | | |
| 475 | | /** |
| 476 | | * Get The parameter at the index associated with the macro |
| 477 | | * @param macroName |
| 478 | | * @param paramIndex |
| 479 | | * @return String representing the parameter |
| 480 | | */ |
| 481 | | public String retrieveFunctionMacroParamName(String macroName, int paramIndex) { |
| 482 | | String paramName = "param"; |
| 483 | | Node macro = preState.getMacrosManager().retrieveMacro(macroName); |
| 484 | | Node parameterList = ((Node)((Node)macro.get(1)).get(0)); |
| 485 | | |
| 486 | | paramName = parameterList.get(paramIndex).toString(); |
| 487 | | |
| 488 | | return paramName; |
| 489 | | } |
| 490 | | |
| 491 | | /** |
| 492 | | * Evaluates a parameter given to a Macro |
| 493 | | * @param parameter |
| 494 | | * @return String representing the parameter |
| 495 | | */ |
| 496 | | @SuppressWarnings("unused") |
| 497 | | public String evalParameter(Node parameter) { |
| 498 | | String tmpString = new Visitor() { |
| 499 | | |
| 500 | | public String visitObjectMacroValueSupported(GNode n) { |
| 501 | | return dispatch((Node)n.get(0)).toString(); |
| 502 | | } |
| 503 | | |
| 504 | | public String visitObjectMacroValue(GNode n) { |
| 505 | | return dispatch((Node)n.get(0)).toString(); |
| 506 | | } |
| 507 | | |
| 508 | | public String visitMacroFunctionExpression(GNode n) { |
| 509 | | evalMacroFuncCall(n); |
| 510 | | System.out.println("EvalParameter " + n); |
| 511 | | return "1"; |
| 512 | | } |
| 513 | | |
| 514 | | public String visitMacroReference(GNode n) { |
| 515 | | try { |
| 516 | | return getMacroValue(n.get(0).toString()); |
| 517 | | } catch(MacroUndefException e) { |
| 518 | | //The macro that has been given as parameter is not defined |
| 519 | | //This is weird. |
| 520 | | //TODO check when a macro given as a parameter could be undefined. |
| 521 | | e.printStackTrace(); |
| 522 | | return "0"; |
| 523 | | } |
| 524 | | } |
| 525 | | |
| 526 | | public String visitShiftExpression(GNode n) { |
| 527 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 528 | | String tmpS1, tmpS2; |
| 529 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 530 | | tmp1 = string2long(tmpS1); |
| 531 | | |
| 532 | | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| 533 | | tmp2 = string2long(tmpS2); |
| 534 | | |
| 535 | | |
| 536 | | if(n.get(1) == "<<") |
| 537 | | result = tmp1 << tmp2; |
| 538 | | |
| 539 | | return Long.toString(result); |
| 540 | | } |
| 541 | | |
| 542 | | public String visitLogicalOrExpression(GNode n) { |
| 543 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 544 | | String tmpS1, tmpS2; |
| 545 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 546 | | tmp1 = string2long(tmpS1); |
| 547 | | |
| 548 | | tmpS2 = dispatch((Node)n.get(1)).toString(); |
| 549 | | tmp2 = string2long(tmpS2); |
| 550 | | |
| 551 | | result = ( tmp1 != 0 ) || ( tmp2 != 0 ) ? 1 : 0; |
| 552 | | |
| 553 | | return Long.toString(result); |
| 554 | | } |
| 555 | | |
| 556 | | public String visitLogicalAndExpression(GNode n) { |
| 557 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 558 | | String tmpS1, tmpS2; |
| 559 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 560 | | tmp1 = string2long(tmpS1); |
| 561 | | |
| 562 | | tmpS2 = dispatch((Node)n.get(1)).toString(); |
| 563 | | tmp2 = string2long(tmpS2); |
| 564 | | |
| 565 | | result = ( tmp1 != 0 ) && ( tmp2 != 0 ) ? 1 : 0; |
| 566 | | |
| 567 | | return Long.toString(result); |
| 568 | | |
| 569 | | } |
| 570 | | |
| 571 | | public String visitRelationalExpression(GNode n) { |
| 572 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 573 | | String tmpS1, tmpS2; |
| 574 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 575 | | tmp1 = string2long(tmpS1); |
| 576 | | |
| 577 | | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| 578 | | tmp2 = string2long(tmpS2); |
| 579 | | |
| 580 | | if(n.get(1) == "<") |
| 581 | | result = tmp1 < tmp2 ? 1 : 0; |
| 582 | | else if (n.get(1) == ">") |
| 583 | | result = tmp1 > tmp2 ? 1 : 0; |
| 584 | | else if (n.get(1) == ">=") |
| 585 | | result = tmp1 >= tmp2 ? 1 : 0; |
| 586 | | else if (n.get(1) == ">=") |
| 587 | | result = tmp1 >= tmp2 ? 1 : 0; |
| 588 | | |
| 589 | | return Long.toString(result); |
| 590 | | } |
| 591 | | |
| 592 | | public String visitEqualityExpression(GNode n) { |
| 593 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 594 | | String tmpS1, tmpS2; |
| 595 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 596 | | tmp1 = string2long(tmpS1); |
| 597 | | |
| 598 | | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| 599 | | tmp2 = string2long(tmpS2); |
| 600 | | if(n.get(1) == "==") |
| 601 | | result = tmp1 == tmp2 ? 1 : 0; |
| 602 | | else if (n.get(1) == "!=") |
| 603 | | result = tmp1 != tmp2 ? 1 : 0; |
| 604 | | |
| 605 | | return Long.toString(result); |
| 606 | | |
| 607 | | } |
| 608 | | |
| 609 | | public String visitAdditiveExpression(GNode n) { |
| 610 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 611 | | String tmpS1, tmpS2; |
| 612 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 613 | | tmp1 = string2long(tmpS1); |
| 614 | | |
| 615 | | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| 616 | | tmp2 = string2long(tmpS2); |
| 617 | | |
| 618 | | if(n.get(1) == "+") |
| 619 | | result = tmp1+tmp2; |
| 620 | | else if (n.get(1) == "-") |
| 621 | | result = tmp1-tmp2; |
| 622 | | |
| 623 | | return Long.toString(result); |
| 624 | | } |
| 625 | | |
| 626 | | public String visitMultiplicativeExpression(GNode n) { |
| 627 | | long tmp1 = 0, tmp2 = 0, result = 0; |
| 628 | | String tmpS1, tmpS2; |
| 629 | | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| 630 | | tmp1 = string2long(tmpS1); |
| 631 | | |
| 632 | | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| 633 | | tmp2 = string2long(tmpS2); |
| 634 | | |
| 635 | | |
| 636 | | if(n.get(1) == "*") |
| 637 | | result = tmp1*tmp2; |
| 638 | | else if (n.get(1) == "/") |
| 639 | | result = tmp1/tmp2; |
| 640 | | else if (n.get(1) == "%") |
| 641 | | result = tmp1%tmp2; |
| 642 | | return Long.toString(result); |
| 643 | | } |
| 644 | | |
| 645 | | public String visitIntegerConstant(GNode n) { |
| 646 | | long intValueI; |
| 647 | | try { |
| 648 | | String tmpS = n.get(0).toString(); |
| 649 | | |
| 650 | | if(tmpS.startsWith("0x")) |
| 651 | | intValueI = Long.valueOf(tmpS.substring(2), 16); |
| 652 | | else |
| 653 | | intValueI = Long.valueOf(tmpS.split("u|U|l|L")[0]); |
| 654 | | } |
| 655 | | catch (NumberFormatException nfe) { |
| 656 | | //System.err.println("NumberFormatException " + nfe.getLocalizedMessage()); |
| 657 | | intValueI = 0; |
| 658 | | } |
| 659 | | return Long.toString(intValueI); |
| 660 | | } |
| 661 | | |
| 662 | | public String visitStringConstant(GNode n) { |
| 663 | | return dispatch((Node)n.get(0)).toString(); |
| 664 | | } |
| 665 | | |
| 666 | | public String visitCPPLineTerminator(GNode n) { |
| 667 | | return ""; |
| 668 | | } |
| 669 | | |
| 670 | | public String visitFunctionMacroCallParameters(GNode n) { |
| 671 | | return dispatch(n.getNode(0)).toString(); |
| 672 | | } |
| 673 | | |
| 674 | | //TODO Check why we need that |
| 675 | | public String visit(GNode n) { |
| 676 | | return "1"; |
| 677 | | } |
| 678 | | |
| 679 | | }.dispatch(parameter).toString(); |
| 680 | | return tmpString; |
| 681 | | } |
| 682 | | |
| 683 | | /** |
| 684 | | * Evaluates the function macro call |
| 685 | | * @param n |
| 686 | | * @return String result of the evaluation of a macro function call |
| 687 | | */ |
| 688 | | public String evalMacroFuncCall(GNode n) { |
| 689 | | String result = "1"; |
| 690 | | int i = 0; |
| 691 | | Node parameters = (Node) ((Node) n.get(1)); |
| 692 | | HashMap<String, String> paramValue = new HashMap<String, String>(); |
| 693 | | for (Object o : parameters) { |
| 694 | | Node parameter = (Node) o; |
| 695 | | String paramName = retrieveFunctionMacroParamName(n.getString(0), i++); |
| 696 | | paramValue.put(paramName,evalParameter(parameter)); |
| 697 | | } |
| 698 | | return result; |
| 699 | | } |
| 700 | | |
| 701 | | /** |
| 702 | | * Gets the macro value |
| 703 | | * @param macroName |
| 704 | | * @return |
| 705 | | */ |
| 706 | | @SuppressWarnings("unused") |
| 707 | | public String getMacroValue(String macroName) throws MacroUndefException { |
| 708 | | Node macroNode = preState.getMacrosManager().retrieveMacro(macroName); |
| 709 | | String macroValue = "0"; |
| 710 | | if( macroNode != null ){ |
| 711 | | macroValue = new Visitor() { |
| | 344 | /** |
| | 345 | * Sets the parseTree flag |
| | 346 | * @param parseTreeFlag |
| | 347 | * A boolean |
| | 348 | */ |
| | 349 | public boolean getParseTree() { |
| | 350 | return preState.getParseTree(); |
| | 351 | } |
| | 352 | |
| | 353 | /** |
| | 354 | * Returns the include path manager. |
| | 355 | * |
| | 356 | * @return The include path manager. |
| | 357 | */ |
| | 358 | public C4IncludePathManager getIncludePathManager() { |
| | 359 | return preState.getIncludePathManager(); |
| | 360 | } |
| | 361 | |
| | 362 | /** |
| | 363 | * Increments the include level. |
| | 364 | */ |
| | 365 | public void incIncludeLevel() { |
| | 366 | preState.incIncludeLevel(); |
| | 367 | } |
| | 368 | |
| | 369 | /** |
| | 370 | * Decrements the include level. |
| | 371 | */ |
| | 372 | public void decIncludeLevel() { |
| | 373 | preState.decIncludeLevel(); |
| | 374 | } |
| | 375 | |
| | 376 | public void reset(String file) { |
| | 377 | preState.reset(file); |
| | 378 | } |
| | 379 | |
| | 380 | //--------------------------------------------------------------------------- |
| | 381 | // Macros |
| | 382 | //--------------------------------------------------------------------------- |
| | 383 | /** |
| | 384 | * for the String Constant with macro parameters |
| | 385 | */ |
| | 386 | public boolean isMacroParameter(String id) { |
| | 387 | return preState.isMacroParameter(id); |
| | 388 | } |
| | 389 | |
| | 390 | /** |
| | 391 | * Tells whether or not the token is a function Macro |
| | 392 | * @param token |
| | 393 | * @return true if the token is a function macro |
| | 394 | */ |
| | 395 | public boolean isFunctionMacro(String token) { |
| | 396 | return preState.isFunctionMacro(token); |
| | 397 | } |
| | 398 | |
| | 399 | /** |
| | 400 | * Gets the object macro associated with the token |
| | 401 | * @param token |
| | 402 | * @return Object representing the object macro |
| | 403 | */ |
| | 404 | public Object getObjectMacro(String token) { |
| | 405 | return preState.getObjectMacro(token); |
| | 406 | } |
| | 407 | |
| | 408 | /** |
| | 409 | * Tells whether or not the token is an object macro |
| | 410 | * @param token |
| | 411 | * @return true if the token is an object macro |
| | 412 | */ |
| | 413 | public boolean isObjectMacro(String token) { |
| | 414 | return preState.isObjectMacro(token); |
| | 415 | } |
| | 416 | |
| | 417 | /** |
| | 418 | * Tells whether or not the token is a macro |
| | 419 | * @param token |
| | 420 | * @return true if the token is a macro name |
| | 421 | */ |
| | 422 | public boolean isMacro(String token) { |
| | 423 | return preState.isMacro(token); |
| | 424 | } |
| | 425 | |
| | 426 | /** |
| | 427 | * Gets the function macro associated with the token |
| | 428 | * @param token |
| | 429 | * @return Object representing the object macro |
| | 430 | */ |
| | 431 | public Object getFunctionMacro(String token) { |
| | 432 | return preState.getFunctionMacro(token); |
| | 433 | } |
| | 434 | |
| | 435 | /** |
| | 436 | * Removes a macro |
| | 437 | * @param id The name of the macro to remove |
| | 438 | */ |
| | 439 | public void removeMacro(String id) { |
| | 440 | preState.removeMacro(id); |
| | 441 | } |
| | 442 | |
| | 443 | /** |
| | 444 | * Gets a macro by its name |
| | 445 | * @param macroName |
| | 446 | * @return Node of the macro |
| | 447 | */ |
| | 448 | public Node getMacro(String macroName) { |
| | 449 | return preState.getMacro(macroName); |
| | 450 | } |
| | 451 | |
| | 452 | /** |
| | 453 | * Stores a new macro |
| | 454 | * @param macroNode Node of the macro to store |
| | 455 | */ |
| | 456 | public void storeMacro(Node macroNode){ |
| | 457 | preState.getMacrosManager().storeMacro(macroNode); |
| | 458 | } |
| | 459 | |
| | 460 | /** |
| | 461 | * To store the parameters of a function Macro |
| | 462 | * @param n The parameters node |
| | 463 | */ |
| | 464 | public void storeParameters(Node n) { |
| | 465 | preState.storeParameters(n); |
| | 466 | } |
| | 467 | |
| | 468 | /** |
| | 469 | * Clean-up function |
| | 470 | */ |
| | 471 | public void purgeParameters() { |
| | 472 | preState.purgeParameters(); |
| | 473 | } |
| | 474 | |
| | 475 | |
| | 476 | /** |
| | 477 | * A commodity method to convert a string as an integer |
| | 478 | * @param tmpS1 String to be converted |
| | 479 | * @return Integer representing the string parameter |
| | 480 | */ |
| | 481 | public long string2long(String tmpS1) { |
| | 482 | long tmp1; |
| | 483 | try { |
| | 484 | tmp1 = Long.valueOf(tmpS1); |
| | 485 | } catch (NumberFormatException nfe) { |
| | 486 | System.err.println("NumberFormatException " + tmpS1); |
| | 487 | nfe.printStackTrace(); |
| | 488 | tmp1 = 0; |
| | 489 | } |
| | 490 | return tmp1; |
| | 491 | |
| | 492 | } |
| | 493 | |
| | 494 | /** |
| | 495 | * Get The parameter at the index associated with the macro |
| | 496 | * @param macroName |
| | 497 | * @param paramIndex |
| | 498 | * @return String representing the parameter |
| | 499 | */ |
| | 500 | public String retrieveFunctionMacroParamName(String macroName, int paramIndex) { |
| | 501 | String paramName = "param"; |
| | 502 | Node macro = preState.getMacrosManager().retrieveMacro(macroName); |
| | 503 | Node parameterList = ((Node)((Node)macro.get(1)).get(0)); |
| | 504 | |
| | 505 | paramName = parameterList.get(paramIndex).toString(); |
| | 506 | |
| | 507 | return paramName; |
| | 508 | } |
| | 509 | |
| | 510 | /** |
| | 511 | * Evaluates a parameter given to a Macro |
| | 512 | * @param parameter |
| | 513 | * @return String representing the parameter |
| | 514 | */ |
| | 515 | @SuppressWarnings("unused") |
| | 516 | public String evalParameter(Node parameter) { |
| | 517 | String tmpString = new Visitor() { |
| | 518 | |
| 931 | | return dispatch((Node)n.get(0)).toString(); |
| 932 | | } |
| 933 | | |
| 934 | | }.dispatch(macroNode).toString(); |
| 935 | | } |
| 936 | | else |
| 937 | | { |
| 938 | | throw new MacroUndefException(macroName); |
| 939 | | } |
| 940 | | |
| 941 | | return macroValue; |
| 942 | | } |
| 943 | | |
| 944 | | |
| 945 | | /** |
| 946 | | * Adds a new parent when nesting a new conditional sections |
| 947 | | */ |
| 948 | | public void addNewParent() { |
| 949 | | preState.getMacrosManager().addNewParent(); |
| 950 | | } |
| 951 | | |
| 952 | | /** |
| 953 | | * Pushes a new macro branch |
| 954 | | */ |
| 955 | | public void pushMacroBranch() { |
| 956 | | preState.getMacrosManager().pushMacroBranch(); |
| 957 | | } |
| 958 | | |
| 959 | | /** |
| 960 | | * Pops the last Macro Branch |
| 961 | | */ |
| 962 | | public void popMacroBranch() { |
| 963 | | preState.getMacrosManager().popMacroBranch(); |
| 964 | | } |
| 965 | | |
| 966 | | /** |
| 967 | | * Determines the given header file has already been parsed or not. |
| 968 | | * |
| 969 | | * @param headerPath The path of the header file. |
| 970 | | * @return True or false. |
| 971 | | */ |
| 972 | | public boolean hasParsed(String headerPath) { |
| 973 | | return preState.hasParsed(headerPath); |
| 974 | | } |
| 975 | | |
| 976 | | /** |
| 977 | | * Adds a header file to the list of parsed headers. |
| 978 | | * |
| 979 | | * @param header Absolute path of the header file. |
| 980 | | */ |
| 981 | | public void parsed(String header) { |
| 982 | | preState.parsed(header); |
| 983 | | } |
| 984 | | |
| 985 | | /** |
| 986 | | * Flushes the macro keyword to make them visible |
| 987 | | */ |
| 988 | | public void flushMacroKeyword() { |
| 989 | | preState.macrosManager.flushMacroKeyword(); |
| 990 | | } |
| 991 | | |
| 992 | | /** |
| 993 | | * Adds everything to be flushed |
| 994 | | */ |
| 995 | | public void addToFlush() { |
| 996 | | preState.macrosManager.addToFlush(); |
| 997 | | } |
| 998 | | |
| 999 | | /** |
| 1000 | | * Tells whether the parameter is a macro keyword |
| 1001 | | * @param id |
| 1002 | | * @return true if the param is a macro keyword |
| 1003 | | */ |
| 1004 | | public boolean isMacroKeyword(String id) { |
| 1005 | | return (preState.isMacroKeyword(id)); |
| 1006 | | } |
| 1007 | | |
| 1008 | | public boolean isObjectMacroForId(String id) { |
| 1009 | | return(preState.isObjectMacroForId(id)); |
| 1010 | | } |
| 1011 | | |
| 1012 | | public boolean isKernelMacroObjectKeyword(String id) { |
| 1013 | | return (isMacroKeyword(id) && isObjectMacro(id) && (!isBlockBeginning(id)) && (!isBlockEnding(id))); |
| 1014 | | } |
| 1015 | | |
| 1016 | | /** |
| 1017 | | * Adds a macro keyword |
| 1018 | | * @param id |
| 1019 | | */ |
| 1020 | | public void addMacroKeyword(String id) { |
| 1021 | | preState.getMacrosManager().addMacroKeyword(id); |
| 1022 | | } |
| 1023 | | |
| 1024 | | /** |
| 1025 | | * Tells whether the macro is an attribute |
| 1026 | | * @return |
| 1027 | | */ |
| 1028 | | public boolean isAttribute() { |
| 1029 | | return isAttribute; |
| 1030 | | } |
| 1031 | | |
| 1032 | | /** |
| 1033 | | * Resets the attribute boo |
| 1034 | | */ |
| 1035 | | public void resetAttribute() { |
| 1036 | | isAttribute = false; |
| 1037 | | } |
| 1038 | | |
| 1039 | | /** |
| 1040 | | * Sets the attribute to true |
| 1041 | | */ |
| 1042 | | public void setAttribute() { |
| 1043 | | isAttribute = true; |
| 1044 | | } |
| 1045 | | |
| 1046 | | /** |
| 1047 | | * Enters the Function Declaration Context |
| 1048 | | */ |
| 1049 | | public void setInFunctionDecl() { |
| 1050 | | preState.setInFunctionDecl(true); |
| 1051 | | } |
| 1052 | | |
| 1053 | | /** |
| 1054 | | * Exits the Function Declaration Context |
| 1055 | | */ |
| 1056 | | public void unsetInFunctionDecl() { |
| 1057 | | preState.setInFunctionDecl(false); |
| 1058 | | } |
| 1059 | | |
| 1060 | | /** |
| 1061 | | * Checks the Function Declaration Context |
| 1062 | | */ |
| 1063 | | public boolean isInFunctionDecl() { |
| 1064 | | return preState.isInFunctionDecl(); |
| 1065 | | } |
| 1066 | | |
| 1067 | | /** |
| 1068 | | * Enters the macro context. |
| 1069 | | */ |
| 1070 | | public void macro() { |
| 1071 | | preState.macro(); |
| 1072 | | } |
| 1073 | | |
| 1074 | | /** |
| 1075 | | * Exits the macro context. |
| 1076 | | */ |
| 1077 | | public void exitMacro() { |
| 1078 | | preState.exitMacro(); |
| 1079 | | } |
| 1080 | | |
| 1081 | | /** |
| 1082 | | * Enters the function Defintion context. |
| 1083 | | */ |
| 1084 | | public void enterFunctionDefinition() { |
| 1085 | | preState.funcDef(); |
| 1086 | | } |
| 1087 | | |
| 1088 | | public void exitFunctionDefinition() { |
| 1089 | | preState.exitFuncDef(); |
| 1090 | | } |
| 1091 | | |
| 1092 | | public boolean inFuncDef() { |
| 1093 | | return preState.inFuncDefContext(); |
| 1094 | | } |
| 1095 | | /** |
| 1096 | | * Checks if top is in macro context. |
| 1097 | | * |
| 1098 | | * @return True or False. |
| 1099 | | */ |
| 1100 | | public boolean inMacroContext() { |
| 1101 | | return preState.inMacroContext(); |
| 1102 | | } |
| 1103 | | |
| 1104 | | /** |
| 1105 | | * To unparse not defined Macro |
| 1106 | | * |
| 1107 | | */ |
| 1108 | | public void unparseIfDef() { |
| 1109 | | preState.unparseIfDef(); |
| 1110 | | } |
| 1111 | | |
| 1112 | | /** |
| 1113 | | * To unparse an endif |
| 1114 | | */ |
| 1115 | | public void unparseEndif() { |
| 1116 | | preState.unparseEndif(); |
| 1117 | | } |
| 1118 | | |
| 1119 | | /** |
| 1120 | | * Tells whether the endif must be parsed |
| 1121 | | * @return |
| 1122 | | */ |
| 1123 | | public boolean toParseEndif() { |
| 1124 | | return (preState.toParseEndif()); |
| 1125 | | } |
| 1126 | | |
| 1127 | | |
| 1128 | | //-------------------------------------------------------------------------- |
| 1129 | | // Registering and Retrieving the State before and after ifSection |
| 1130 | | // -------------------------------------------------------------------------- |
| 1131 | | public void registerStateIfSection() { |
| 1132 | | preState.getStateIfSectionStack().add(isDefined()); |
| 1133 | | //BUG correction Before: |
| 1134 | | //this.stateIfSectionStack.add(isElseParsing()); |
| 1135 | | preState.getStateElseStack().add(isElseParsing()); |
| 1136 | | } |
| 1137 | | |
| 1138 | | /** |
| 1139 | | * Retrieve the state associated with the current IfSection |
| 1140 | | */ |
| 1141 | | public void retrieveStateIfSection() { |
| 1142 | | preState.retrieveStateIfSection(); |
| 1143 | | } |
| 1144 | | |
| 1145 | | //----------------------------------- |
| 1146 | | // Header Name resolution when given as a Macro |
| 1147 | | //----------------------------------- |
| 1148 | | |
| 1149 | | |
| 1150 | | /** |
| 1151 | | * Concate all the elements of a concatenation expresionn. |
| 1152 | | * Starts from a bad parsing (should be only one String Constant..) |
| 1153 | | *TODO improve the parsing of this type of concatenation |
| 1154 | | */ |
| 1155 | | @SuppressWarnings("unused") |
| 1156 | | private String concatParameter(GNode funcMacroCallParams, final ArrayList<String> formal, final ArrayList<String> actual, final boolean isStringify) { |
| 1157 | | String concatenated = ""; |
| 1158 | | concatenated = (String) new Visitor() { |
| 1159 | | |
| 1160 | | public String visitStringConstant(GNode stringConst) { |
| 1161 | | if(stringConst.size() == 1) { |
| 1162 | | if(stringConst.get(0) instanceof String) |
| 1163 | | return stringConst.getString(0); |
| | 764 | |
| | 765 | } |
| | 766 | |
| | 767 | public String visitUnaryMinusExpression(GNode n) { |
| | 768 | return "-"+dispatch(n.getGeneric(0)).toString(); |
| | 769 | } |
| | 770 | |
| | 771 | public String visitFunctionCall(GNode n) { |
| | 772 | String functionName = n.getGeneric(0).getString(0); |
| | 773 | GNode parameters = n.getGeneric(2); |
| | 774 | if(isMacro(functionName)) { |
| | 775 | GNode tmpMacroRef = GNode.create("MacroReference", functionName,getMacro(functionName)); |
| | 776 | GNode tmpFuncMac = GNode.create("FunctionMacroCallExpression", tmpMacroRef, parameters); |
| | 777 | String tmpRes = computeVisitor(tmpFuncMac).toString(); |
| | 778 | return tmpRes; |
| | 779 | } |
| | 780 | //Should not occur |
| | 781 | return "1"; |
| | 782 | } |
| | 783 | |
| | 784 | public String visitDeclarationWithoutColon(GNode n) { |
| | 785 | return "1"; |
| | 786 | } |
| | 787 | |
| | 788 | public String visitMacroFunctionExpression(GNode n) { |
| | 789 | return evalMacroFuncCall(n); |
| | 790 | } |
| | 791 | |
| | 792 | public String visitMacroReference(GNode n) { |
| | 793 | try { |
| | 794 | return getMacroValue(n.getString(0)); |
| | 795 | } catch(MacroUndefException e) { |
| | 796 | e.printStackTrace(); |
| | 797 | return "0"; |
| | 798 | } |
| | 799 | } |
| | 800 | |
| | 801 | public String visitMacroObjectAttrib(GNode n) { |
| | 802 | try { |
| | 803 | return getMacroValue(n.getString(0)); |
| | 804 | } catch (MacroUndefException e) { |
| | 805 | e.printStackTrace(); |
| | 806 | return "0"; |
| | 807 | } |
| | 808 | } |
| | 809 | |
| | 810 | public String visitShiftExpression(GNode n) { |
| | 811 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 812 | String tmpS1, tmpS2; |
| | 813 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 814 | tmp1 = string2long(tmpS1); |
| | 815 | |
| | 816 | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| | 817 | tmp2 = string2long(tmpS2); |
| | 818 | |
| | 819 | |
| | 820 | if(n.getString(1) == "<<") |
| | 821 | result = tmp1 << tmp2; |
| | 822 | |
| | 823 | return Long.toString(result); |
| | 824 | } |
| | 825 | |
| | 826 | public String visitLogicalOrExpression(GNode n) { |
| | 827 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 828 | String tmpS1, tmpS2; |
| | 829 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 830 | tmp1 = string2long(tmpS1); |
| | 831 | |
| | 832 | tmpS2 = dispatch((Node)n.get(1)).toString(); |
| | 833 | tmp2 = string2long(tmpS2); |
| | 834 | |
| | 835 | result = ( tmp1 != 0 ) || ( tmp2 != 0 ) ? 1 : 0; |
| | 836 | |
| | 837 | return Long.toString(result); |
| | 838 | } |
| | 839 | |
| | 840 | public String visitLogicalAndExpression(GNode n) { |
| | 841 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 842 | String tmpS1, tmpS2; |
| | 843 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 844 | tmp1 = string2long(tmpS1); |
| | 845 | |
| | 846 | tmpS2 = dispatch((Node)n.get(1)).toString(); |
| | 847 | tmp2 = string2long(tmpS2); |
| | 848 | |
| | 849 | result = ( tmp1 != 0 ) && ( tmp2 != 0 ) ? 1 : 0; |
| | 850 | |
| | 851 | return Long.toString(result); |
| | 852 | |
| | 853 | } |
| | 854 | |
| | 855 | public String visitRelationalExpression(GNode n) { |
| | 856 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 857 | String tmpS1, tmpS2; |
| | 858 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 859 | tmp1 = string2long(tmpS1); |
| | 860 | |
| | 861 | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| | 862 | tmp2 = string2long(tmpS2); |
| | 863 | |
| | 864 | if(n.getString(1) == "<") |
| | 865 | result = tmp1 < tmp2 ? 1 : 0; |
| | 866 | else if (n.getString(1) == ">") |
| | 867 | result = tmp1 > tmp2 ? 1 : 0; |
| | 868 | else if (n.getString(1) == ">=") |
| | 869 | result = tmp1 >= tmp2 ? 1 : 0; |
| | 870 | else if (n.getString(1) == ">=") |
| | 871 | result = tmp1 >= tmp2 ? 1 : 0; |
| | 872 | |
| | 873 | return Long.toString(result); |
| | 874 | } |
| | 875 | |
| | 876 | public String visitEqualityExpression(GNode n) { |
| | 877 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 878 | String tmpS1, tmpS2; |
| | 879 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 880 | tmp1 = string2long(tmpS1); |
| | 881 | |
| | 882 | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| | 883 | tmp2 = string2long(tmpS2); |
| | 884 | if(n.getString(1) == "==") |
| | 885 | result = tmp1 == tmp2 ? 1 : 0; |
| | 886 | else if (n.getString(1) == "!=") |
| | 887 | result = tmp1 != tmp2 ? 1 : 0; |
| | 888 | |
| | 889 | return Long.toString(result); |
| | 890 | |
| | 891 | } |
| | 892 | |
| | 893 | public String visitAdditiveExpression(GNode n) { |
| | 894 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 895 | String tmpS1, tmpS2; |
| | 896 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 897 | tmp1 = string2long(tmpS1); |
| | 898 | |
| | 899 | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| | 900 | tmp2 = string2long(tmpS2); |
| | 901 | |
| | 902 | |
| | 903 | if(n.getString(1) == "+") |
| | 904 | result = tmp1+tmp2; |
| | 905 | else if (n.getString(1) == "-") |
| | 906 | result = tmp1-tmp2; |
| | 907 | |
| | 908 | return Long.toString(result); |
| | 909 | } |
| | 910 | |
| | 911 | public String visitMultiplicativeExpression(GNode n) { |
| | 912 | long tmp1 = 0, tmp2 = 0, result = 0; |
| | 913 | String tmpS1, tmpS2; |
| | 914 | tmpS1 = dispatch((Node)n.get(0)).toString(); |
| | 915 | tmp1 = string2long(tmpS1); |
| | 916 | |
| | 917 | tmpS2 = dispatch((Node)n.get(2)).toString(); |
| | 918 | tmp2 = string2long(tmpS2); |
| | 919 | |
| | 920 | |
| | 921 | if(n.getString(1) == "*") |
| | 922 | result = tmp1*tmp2; |
| | 923 | else if (n.getString(1) == "/") |
| | 924 | result = tmp1/tmp2; |
| | 925 | else if (n.getString(1) == "%") |
| | 926 | result = tmp1%tmp2; |
| | 927 | |
| | 928 | |
| | 929 | return Long.toString(result); |
| | 930 | } |
| | 931 | |
| | 932 | public String visitIntegerConstant(GNode n) { |
| | 933 | long intValueI; |
| | 934 | try { |
| | 935 | String tmpS = n.getString(0); |
| | 936 | if(tmpS.startsWith("0x")) |
| | 937 | intValueI = Long.valueOf(tmpS.substring(2), 16); |
| | 938 | else |
| | 939 | intValueI = Long.valueOf(tmpS.split("u|U|l|L")[0]); |
| | 940 | } |
| | 941 | catch (NumberFormatException nfe) { |
| | 942 | intValueI = 0; |
| | 943 | } |
| | 944 | return Long.toString(intValueI); |
| | 945 | } |
| | 946 | |
| | 947 | public String visitStringConstant(GNode n) { |
| | 948 | return dispatch((Node)n.get(0)).toString(); |
| | 949 | } |
| | 950 | |
| | 951 | public String visitCPPLineTerminator(GNode n) { |
| | 952 | return ""; |
| | 953 | } |
| | 954 | |
| | 955 | public String visit(GNode n) { |
| | 956 | |
| | 957 | if (n.isEmpty()) |
| | 958 | return "1"; |
| | 959 | else { |
| | 960 | return dispatch((Node)n.get(0)).toString(); |
| | 961 | } |
| | 962 | } |
| | 963 | |
| | 964 | }.dispatch(macroNode).toString(); |
| | 965 | } |
| | 966 | else |
| | 967 | { |
| | 968 | throw new MacroUndefException(macroName); |
| | 969 | } |
| | 970 | return macroValue; |
| | 971 | } |
| | 972 | |
| | 973 | |
| | 974 | /** |
| | 975 | * Adds a new parent when nesting a new conditional sections |
| | 976 | */ |
| | 977 | public void addNewParent() { |
| | 978 | preState.getMacrosManager().addNewParent(); |
| | 979 | } |
| | 980 | |
| | 981 | /** |
| | 982 | * Pushes a new macro branch |
| | 983 | */ |
| | 984 | public void pushMacroBranch() { |
| | 985 | preState.getMacrosManager().pushMacroBranch(); |
| | 986 | } |
| | 987 | |
| | 988 | /** |
| | 989 | * Pops the last Macro Branch |
| | 990 | */ |
| | 991 | public void popMacroBranch() { |
| | 992 | preState.getMacrosManager().popMacroBranch(); |
| | 993 | } |
| | 994 | |
| | 995 | /** |
| | 996 | * Determines the given header file has already been parsed or not. |
| | 997 | * |
| | 998 | * @param headerPath The path of the header file. |
| | 999 | * @return True or false. |
| | 1000 | */ |
| | 1001 | public boolean hasParsed(String headerPath) { |
| | 1002 | return preState.hasParsed(headerPath); |
| | 1003 | } |
| | 1004 | |
| | 1005 | /** |
| | 1006 | * Adds a header file to the list of parsed headers. |
| | 1007 | * |
| | 1008 | * @param header Absolute path of the header file. |
| | 1009 | */ |
| | 1010 | public void parsed(String header) { |
| | 1011 | preState.parsed(header); |
| | 1012 | } |
| | 1013 | |
| | 1014 | /** |
| | 1015 | * Flushes the macro keyword to make them visible |
| | 1016 | */ |
| | 1017 | public void flushMacroKeyword() { |
| | 1018 | preState.macrosManager.flushMacroKeyword(); |
| | 1019 | } |
| | 1020 | |
| | 1021 | /** |
| | 1022 | * Adds everything to be flushed |
| | 1023 | */ |
| | 1024 | public void addToFlush() { |
| | 1025 | preState.macrosManager.addToFlush(); |
| | 1026 | } |
| | 1027 | |
| | 1028 | /** |
| | 1029 | * Tells whether the parameter is a macro keyword |
| | 1030 | * @param id |
| | 1031 | * @return true if the param is a macro keyword |
| | 1032 | */ |
| | 1033 | public boolean isMacroKeyword(String id) { |
| | 1034 | return (preState.isMacroKeyword(id)); |
| | 1035 | } |
| | 1036 | |
| | 1037 | public boolean isObjectMacroForId(String id) { |
| | 1038 | return(preState.isObjectMacroForId(id)); |
| | 1039 | } |
| | 1040 | |
| | 1041 | public boolean isKernelMacroObjectKeyword(String id) { |
| | 1042 | return (isMacroKeyword(id) && isObjectMacro(id) && (!isBlockBeginning(id)) && (!isBlockEnding(id))); |
| | 1043 | } |
| | 1044 | |
| | 1045 | /** |
| | 1046 | * Adds a macro keyword |
| | 1047 | * @param id |
| | 1048 | */ |
| | 1049 | public void addMacroKeyword(String id) { |
| | 1050 | preState.getMacrosManager().addMacroKeyword(id); |
| | 1051 | } |
| | 1052 | |
| | 1053 | /** |
| | 1054 | * Tells whether the macro is an attribute |
| | 1055 | * @return |
| | 1056 | */ |
| | 1057 | public boolean isAttribute() { |
| | 1058 | return isAttribute; |
| | 1059 | } |
| | 1060 | |
| | 1061 | /** |
| | 1062 | * Resets the attribute boo |
| | 1063 | */ |
| | 1064 | public void resetAttribute() { |
| | 1065 | isAttribute = false; |
| | 1066 | } |
| | 1067 | |
| | 1068 | /** |
| | 1069 | * Sets the attribute to true |
| | 1070 | */ |
| | 1071 | public void setAttribute() { |
| | 1072 | isAttribute = true; |
| | 1073 | } |
| | 1074 | |
| | 1075 | /** |
| | 1076 | * Enters the Function Declaration Context |
| | 1077 | */ |
| | 1078 | public void setInFunctionDecl() { |
| | 1079 | preState.setInFunctionDecl(true); |
| | 1080 | } |
| | 1081 | |
| | 1082 | /** |
| | 1083 | * Exits the Function Declaration Context |
| | 1084 | */ |
| | 1085 | public void unsetInFunctionDecl() { |
| | 1086 | preState.setInFunctionDecl(false); |
| | 1087 | } |
| | 1088 | |
| | 1089 | /** |
| | 1090 | * Checks the Function Declaration Context |
| | 1091 | */ |
| | 1092 | public boolean isInFunctionDecl() { |
| | 1093 | return preState.isInFunctionDecl(); |
| | 1094 | } |
| | 1095 | |
| | 1096 | /** |
| | 1097 | * Enters the macro context. |
| | 1098 | */ |
| | 1099 | public void macro() { |
| | 1100 | preState.macro(); |
| | 1101 | } |
| | 1102 | |
| | 1103 | /** |
| | 1104 | * Exits the macro context. |
| | 1105 | */ |
| | 1106 | public void exitMacro() { |
| | 1107 | preState.exitMacro(); |
| | 1108 | } |
| | 1109 | |
| | 1110 | /** |
| | 1111 | * Enters the function Defintion context. |
| | 1112 | */ |
| | 1113 | public void enterFunctionDefinition() { |
| | 1114 | preState.funcDef(); |
| | 1115 | } |
| | 1116 | |
| | 1117 | public void exitFunctionDefinition() { |
| | 1118 | preState.exitFuncDef(); |
| | 1119 | } |
| | 1120 | |
| | 1121 | public boolean inFuncDef() { |
| | 1122 | return preState.inFuncDefContext(); |
| | 1123 | } |
| | 1124 | /** |
| | 1125 | * Checks if top is in macro context. |
| | 1126 | * |
| | 1127 | * @return True or False. |
| | 1128 | */ |
| | 1129 | public boolean inMacroContext() { |
| | 1130 | return preState.inMacroContext(); |
| | 1131 | } |
| | 1132 | |
| | 1133 | /** |
| | 1134 | * To unparse not defined Macro |
| | 1135 | * |
| | 1136 | */ |
| | 1137 | public void unparseIfDef() { |
| | 1138 | preState.unparseIfDef(); |
| | 1139 | } |
| | 1140 | |
| | 1141 | /** |
| | 1142 | * To unparse an endif |
| | 1143 | */ |
| | 1144 | public void unparseEndif() { |
| | 1145 | preState.unparseEndif(); |
| | 1146 | } |
| | 1147 | |
| | 1148 | /** |
| | 1149 | * Tells whether the endif must be parsed |
| | 1150 | * @return |
| | 1151 | */ |
| | 1152 | public boolean toParseEndif() { |
| | 1153 | return (preState.toParseEndif()); |
| | 1154 | } |
| | 1155 | |
| | 1156 | |
| | 1157 | //-------------------------------------------------------------------------- |
| | 1158 | // Registering and Retrieving the State before and after ifSection |
| | 1159 | // -------------------------------------------------------------------------- |
| | 1160 | public void registerStateIfSection() { |
| | 1161 | preState.getStateIfSectionStack().add(isDefined()); |
| | 1162 | //BUG correction Before: |
| | 1163 | //this.stateIfSectionStack.add(isElseParsing()); |
| | 1164 | preState.getStateElseStack().add(isElseParsing()); |
| | 1165 | } |
| | 1166 | |
| | 1167 | /** |
| | 1168 | * Retrieve the state associated with the current IfSection |
| | 1169 | */ |
| | 1170 | public void retrieveStateIfSection() { |
| | 1171 | preState.retrieveStateIfSection(); |
| | 1172 | } |
| | 1173 | |
| | 1174 | //----------------------------------- |
| | 1175 | // Header Name resolution when given as a Macro |
| | 1176 | //----------------------------------- |
| | 1177 | |
| | 1178 | |
| | 1179 | /** |
| | 1180 | * Concate all the elements of a concatenation expresionn. |
| | 1181 | * Starts from a bad parsing (should be only one String Constant..) |
| | 1182 | *TODO improve the parsing of this type of concatenation |
| | 1183 | */ |
| | 1184 | @SuppressWarnings("unused") |
| | 1185 | private String concatParameter(GNode funcMacroCallParams, final ArrayList<String> formal, final ArrayList<String> actual, final boolean isStringify) { |
| | 1186 | String concatenated = ""; |
| | 1187 | concatenated = (String) new Visitor() { |
| | 1188 | |
| | 1189 | public String visitStringConstant(GNode stringConst) { |
| | 1190 | if(stringConst.size() == 1) { |
| | 1191 | if(stringConst.get(0) instanceof String) |
| | 1192 | return stringConst.getString(0); |
| | 1193 | else |
| | 1194 | return (String) dispatch(stringConst.getGeneric(0)); |
| | 1195 | } |
| | 1196 | else return ""; |
| | 1197 | } |
| | 1198 | |
| | 1199 | public String visitMacroReference(GNode macroRef) { |
| | 1200 | if(isStringify) |
| | 1201 | return (macroRef.getString(0)); |
| 1165 | | return (String) dispatch(stringConst.getGeneric(0)); |
| 1166 | | } |
| 1167 | | else return ""; |
| 1168 | | } |
| 1169 | | |
| 1170 | | public String visitMacroReference(GNode macroRef) { |
| 1171 | | if(isStringify) |
| 1172 | | return (macroRef.getString(0)); |
| 1173 | | else |
| 1174 | | return ((String) dispatch(macroRef.getGeneric(1))); |
| 1175 | | } |
| 1176 | | |
| 1177 | | public String visitMacroIdentifierConstant(GNode macroIdConst) { |
| 1178 | | String tmpParam = macroIdConst.getString(0); |
| 1179 | | |
| 1180 | | if(formal.contains(tmpParam)) |
| 1181 | | return actual.get(formal.indexOf(tmpParam)); |
| 1182 | | else |
| 1183 | | return ""; |
| 1184 | | } |
| 1185 | | |
| 1186 | | public String visitDirectComponentSelection(GNode directComp) { |
| 1187 | | return dispatch(directComp.getGeneric(0)) + "." + directComp.getString(1); |
| 1188 | | } |
| 1189 | | |
| 1190 | | public String visitVariadicConcatenateExpression(GNode varConc) { |
| 1191 | | return (String) dispatch(varConc.getGeneric(0)) + dispatch(varConc.getGeneric(2)); |
| 1192 | | } |
| 1193 | | |
| 1194 | | public String visitPrimaryIdentifier(GNode pId) { |
| 1195 | | return pId.getString(0); |
| 1196 | | } |
| 1197 | | |
| 1198 | | public String visitMultiplicativeExpression(GNode addExpr) { |
| 1199 | | return dispatch(addExpr.getGeneric(0)) + addExpr.getString(1) + dispatch(addExpr.getGeneric(2)); |
| 1200 | | } |
| 1201 | | |
| 1202 | | public String visitAdditiveExpression(GNode addExpr) { |
| 1203 | | return dispatch(addExpr.getGeneric(0)) + addExpr.getString(1) + dispatch(addExpr.getGeneric(2)); |
| 1204 | | } |
| 1205 | | |
| 1206 | | public String visitFunctionMacroCallParameters(GNode funcMacroCallParams) { |
| 1207 | | String concatenateParam = ""; |
| 1208 | | for (Iterator<Object> iterator = funcMacroCallParams.iterator(); iterator.hasNext();) { |
| 1209 | | Object childo = (Object) iterator.next(); |
| 1210 | | if(childo instanceof Node) |
| 1211 | | concatenateParam = concatenateParam + dispatch((GNode) childo); |
| 1212 | | else |
| 1213 | | concatenateParam = concatenateParam + (String) childo; |
| 1214 | | |
| 1215 | | } |
| 1216 | | return concatenateParam; |
| 1217 | | } |
| 1218 | | }.dispatch(funcMacroCallParams); |
| 1219 | | return concatenated; |
| 1220 | | } |
| 1221 | | |
| 1222 | | /** |
| 1223 | | *tells whether the FunctionMacroCallParameters given to the macro should be concatenated. |
| 1224 | | */ |
| 1225 | | @SuppressWarnings("unused") |
| 1226 | | private boolean isConcat(GNode funcMacroCallParams) { |
| 1227 | | boolean isConcat = false; |
| 1228 | | for (Iterator<Object> iterator = funcMacroCallParams.iterator(); iterator.hasNext();) { |
| 1229 | | Object childo = (Object) iterator.next(); |
| 1230 | | if(childo instanceof Node) { |
| 1231 | | |
| 1232 | | boolean tmpBool = ((Boolean) new Visitor() { |
| 1233 | | boolean tmpConcat = false; |
| 1234 | | |
| 1235 | | public Boolean visit(GNode node) { |
| 1236 | | for(int i = 0; i< node.size(); i++) { |
| 1237 | | if(node.get(i) instanceof GNode) { |
| 1238 | | GNode tmpGen = node.getGeneric(i); |
| 1239 | | if(tmpGen.getName().equals("VariadicConcatenateExpression") || tmpGen.getName().equals("StringConcatenateExpression") ) { |
| 1240 | | return true; |
| 1241 | | } |
| | 1203 | return ((String) dispatch(macroRef.getGeneric(1))); |
| | 1204 | } |
| | 1205 | |
| | 1206 | public String visitMacroIdentifierConstant(GNode macroIdConst) { |
| | 1207 | String tmpParam = macroIdConst.getString(0); |
| | 1208 | |
| | 1209 | if(formal.contains(tmpParam)) |
| | 1210 | return actual.get(formal.indexOf(tmpParam)); |
| | 1211 | else |
| | 1212 | return ""; |
| | 1213 | } |
| | 1214 | |
| | 1215 | public String visitDirectComponentSelection(GNode directComp) { |
| | 1216 | return dispatch(directComp.getGeneric(0)) + "." + directComp.getString(1); |
| | 1217 | } |
| | 1218 | |
| | 1219 | public String visitVariadicConcatenateExpression(GNode varConc) { |
| | 1220 | return (String) dispatch(varConc.getGeneric(0)) + dispatch(varConc.getGeneric(2)); |
| | 1221 | } |
| | 1222 | |
| | 1223 | public String visitPrimaryIdentifier(GNode pId) { |
| | 1224 | return pId.getString(0); |
| | 1225 | } |
| | 1226 | |
| | 1227 | public String visitMultiplicativeExpression(GNode addExpr) { |
| | 1228 | return dispatch(addExpr.getGeneric(0)) + addExpr.getString(1) + dispatch(addExpr.getGeneric(2)); |
| | 1229 | } |
| | 1230 | |
| | 1231 | public String visitAdditiveExpression(GNode addExpr) { |
| | 1232 | return dispatch(addExpr.getGeneric(0)) + addExpr.getString(1) + dispatch(addExpr.getGeneric(2)); |
| | 1233 | } |
| | 1234 | |
| | 1235 | public String visitFunctionMacroCallParameters(GNode funcMacroCallParams) { |
| | 1236 | String concatenateParam = ""; |
| | 1237 | for (Iterator<Object> iterator = funcMacroCallParams.iterator(); iterator.hasNext();) { |
| | 1238 | Object childo = (Object) iterator.next(); |
| | 1239 | if(childo instanceof Node) |
| | 1240 | concatenateParam = concatenateParam + dispatch((GNode) childo); |
| | 1241 | else |
| | 1242 | concatenateParam = concatenateParam + (String) childo; |
| | 1243 | |
| | 1244 | } |
| | 1245 | return concatenateParam; |
| | 1246 | } |
| | 1247 | }.dispatch(funcMacroCallParams); |
| | 1248 | return concatenated; |
| | 1249 | } |
| | 1250 | |
| | 1251 | /** |
| | 1252 | *tells whether the FunctionMacroCallParameters given to the macro should be concatenated. |
| | 1253 | */ |
| | 1254 | @SuppressWarnings("unused") |
| | 1255 | private boolean isConcat(GNode funcMacroCallParams) { |
| | 1256 | boolean isConcat = false; |
| | 1257 | for (Iterator<Object> iterator = funcMacroCallParams.iterator(); iterator.hasNext();) { |
| | 1258 | Object childo = (Object) iterator.next(); |
| | 1259 | if(childo instanceof GNode) { |
| | 1260 | |
| | 1261 | boolean tmpBool = ((Boolean) new Visitor() { |
| | 1262 | boolean tmpConcat = false; |
| | 1263 | |
| | 1264 | public Boolean visit(GNode node) { |
| | 1265 | for(int i = 0; i< node.size(); i++) { |
| | 1266 | if(node.get(i) instanceof GNode) { |
| | 1267 | GNode tmpGen = node.getGeneric(i); |
| | 1268 | if(tmpGen.getName().equals("VariadicConcatenateExpression") || tmpGen.getName().equals("StringConcatenateExpression") ) { |
| | 1269 | return true; |
| | 1270 | } |
| | 1271 | else |
| | 1272 | tmpConcat = tmpConcat || ((Boolean) dispatch(node.getGeneric(i))).booleanValue(); |
| | 1273 | |
| | 1274 | } |
| | 1275 | } |
| | 1276 | |
| | 1277 | return tmpConcat; |
| | 1278 | |
| | 1279 | } |
| | 1280 | }.dispatch((GNode) childo)).booleanValue(); |
| | 1281 | |
| | 1282 | isC |