tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/FunctionReturnValueParser.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Parser for function return values
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.Collections;
       
    22 import java.util.List;
       
    23 
       
    24 /**
       
    25  * Parser for function return values
       
    26  * 
       
    27  */
       
    28 class FunctionReturnValueParser {
       
    29 
       
    30 	/**
       
    31 	 * Source parser
       
    32 	 */
       
    33 	private SourceParser parser;
       
    34 
       
    35 	/**
       
    36 	 * Comparator for source return values
       
    37 	 */
       
    38 	private PositionArrayComparator comparator = new PositionArrayComparator();
       
    39 
       
    40 	/**
       
    41 	 * Macros representing return statement
       
    42 	 */
       
    43 	private List<String> returnExtensions;
       
    44 
       
    45 	/**
       
    46 	 * Constructor
       
    47 	 * 
       
    48 	 * @param parser
       
    49 	 *            the source parser
       
    50 	 */
       
    51 	FunctionReturnValueParser(SourceParser parser) {
       
    52 		this.parser = parser;
       
    53 	}
       
    54 
       
    55 	/**
       
    56 	 * Parses the return values of given source context
       
    57 	 * 
       
    58 	 * @param context
       
    59 	 *            the context to be parsed
       
    60 	 * @param list
       
    61 	 *            the list of return values
       
    62 	 */
       
    63 	void parseReturnValues(SourceContext context, List<SourceReturn> list) {
       
    64 		String statement = SourceConstants.RETURN;
       
    65 		try {
       
    66 			parseReturnValues(context, list, statement);
       
    67 		} catch (SourceParserException e) {
       
    68 		}
       
    69 		if (returnExtensions != null && returnExtensions.size() > 0) {
       
    70 			for (int i = 0; i < returnExtensions.size(); i++) {
       
    71 				try {
       
    72 					parseReturnValues(context, list, returnExtensions.get(i));
       
    73 				} catch (SourceParserException e) {
       
    74 				}
       
    75 			}
       
    76 			Collections.sort(list, comparator);
       
    77 		}
       
    78 		if (context.isVoid()) {
       
    79 			boolean addToEnd = true;
       
    80 			// If there were no return statements, the trace is added to end
       
    81 			// Otherwise the last return statement needs to be checked. If it is
       
    82 			// at the end of the function, the return trace is not added to
       
    83 			// the end
       
    84 			if (!list.isEmpty()) {
       
    85 				SourceReturn ret = list.get(list.size() - 1);
       
    86 				SourceIterator itr = parser.createIterator(ret.getOffset()
       
    87 						+ ret.getLength() + 1, SourceParser.SKIP_ALL);
       
    88 				try {
       
    89 					itr.next();
       
    90 					// If the next character after return statement is the end
       
    91 					// of function, the return is not added
       
    92 					if (itr.currentIndex() == context.getOffset()
       
    93 							+ context.getLength() - 1) {
       
    94 						addToEnd = false;
       
    95 					}
       
    96 				} catch (SourceParserException e) {
       
    97 					addToEnd = false;
       
    98 				}
       
    99 			}
       
   100 			if (addToEnd) {
       
   101 				list.add(new SourceReturn(parser, context.getOffset()
       
   102 						+ context.getLength() - 1, 0));
       
   103 			}
       
   104 		}
       
   105 	}
       
   106 
       
   107 	/**
       
   108 	 * Parses the return values of given source context that use the given
       
   109 	 * return statement
       
   110 	 * 
       
   111 	 * @param context
       
   112 	 *            the context to be parsed
       
   113 	 * @param list
       
   114 	 *            the list of return values
       
   115 	 * @param statement
       
   116 	 *            the return statement
       
   117 	 * @throws SourceParserException
       
   118 	 *             if processing fails
       
   119 	 */
       
   120 	private void parseReturnValues(SourceContext context,
       
   121 			List<SourceReturn> list, String statement)
       
   122 			throws SourceParserException {
       
   123 		FunctionReturnValueSearchData data = new FunctionReturnValueSearchData();
       
   124 		int offset = context.getOffset();
       
   125 		int end = offset + context.getLength();
       
   126 		SourceSearch search = parser.startStringSearch(statement, offset, end,
       
   127 				SourceParser.MATCH_WHOLE_WORD | SourceParser.SKIP_ALL);
       
   128 		boolean looping = true;
       
   129 		do {
       
   130 			data.index = search.findNext();
       
   131 			if (data.index != -1) {
       
   132 				data.itr = parser.createIterator(data.index
       
   133 						+ statement.length(), SourceParser.SKIP_ALL);
       
   134 				locateReturnStatement(data);
       
   135 				if (data.endOffset != -1 && data.startOffset != -1) {
       
   136 					SourceReturn ret = createReturnStatement(data);
       
   137 					list.add(ret);
       
   138 				} else {
       
   139 					// End of return statement missing
       
   140 					looping = false;
       
   141 				}
       
   142 			} else {
       
   143 				looping = false;
       
   144 			}
       
   145 		} while (looping);
       
   146 	}
       
   147 
       
   148 	/**
       
   149 	 * Locates the start and end offsets for the return statement
       
   150 	 * 
       
   151 	 * @param data
       
   152 	 *            the search data
       
   153 	 * @throws SourceParserException
       
   154 	 *             if parser fails
       
   155 	 */
       
   156 	private void locateReturnStatement(FunctionReturnValueSearchData data)
       
   157 			throws SourceParserException {
       
   158 		data.startOffset = -1;
       
   159 		data.endOffset = -1;
       
   160 		boolean found = false;
       
   161 		boolean colonAllowed = false;
       
   162 		while (data.itr.hasNext() && !found) {
       
   163 			char c = data.itr.next();
       
   164 			if (c == ';') {
       
   165 				data.endOffset = data.itr.previousIndex() + 1;
       
   166 				found = true;
       
   167 			} else if (c == '}') {
       
   168 				found = true;
       
   169 			} else if (c == '?') {
       
   170 				colonAllowed = true;
       
   171 			} else if (c == ':') {
       
   172 				if (data.itr.hasNext() && data.itr.peek() == ':') {
       
   173 					// Skips over ::
       
   174 					c = data.itr.next();
       
   175 				} else {
       
   176 					if (colonAllowed) {
       
   177 						colonAllowed = false;
       
   178 					} else {
       
   179 						data.endOffset = data.itr.previousIndex() + 1;
       
   180 						found = true;
       
   181 					}
       
   182 				}
       
   183 			}
       
   184 			if (data.startOffset == -1) {
       
   185 				data.startOffset = data.itr.currentIndex();
       
   186 			}
       
   187 		}
       
   188 	}
       
   189 
       
   190 	/**
       
   191 	 * Creates a return statement
       
   192 	 * 
       
   193 	 * @param data
       
   194 	 *            the parser data
       
   195 	 * @return the new statement
       
   196 	 * @throws SourceParserException
       
   197 	 *             if parser fails
       
   198 	 */
       
   199 	private SourceReturn createReturnStatement(
       
   200 			FunctionReturnValueSearchData data) throws SourceParserException {
       
   201 		SourceReturn ret = new SourceReturn(parser, data.startOffset,
       
   202 				data.endOffset - data.startOffset);
       
   203 		if (checkTag(data.startOffset, data.endOffset - data.startOffset)) {
       
   204 			ret.setTagHazard();
       
   205 		}
       
   206 		if (checkPreviousChar(data.index - 1)) {
       
   207 			ret.setPreviousCharHazard();
       
   208 		}
       
   209 		return ret;
       
   210 	}
       
   211 
       
   212 	/**
       
   213 	 * Checks if the tag is hazardous
       
   214 	 * 
       
   215 	 * @param start
       
   216 	 *            start offset
       
   217 	 * @param length
       
   218 	 *            tag length
       
   219 	 * @return true if there is a problem
       
   220 	 */
       
   221 	private boolean checkTag(int start, int length) {
       
   222 		boolean hazard = false;
       
   223 		boolean previous = false;
       
   224 		// Function calls and increment / decrement operators are not safe
       
   225 		for (int i = start; i < start + length && !hazard; i++) {
       
   226 			char c = parser.getData(i);
       
   227 			if (c == '(') {
       
   228 				// If return statement is within brackets, it is not hazardous
       
   229 				if (i != start || parser.getData(start + length - 1) != ')') {
       
   230 					hazard = true;
       
   231 				}
       
   232 			} else if (c == '?') {
       
   233 				hazard = true;
       
   234 			} else if (c == '-' || c == '+') {
       
   235 				if (previous) {
       
   236 					hazard = true;
       
   237 				} else {
       
   238 					previous = true;
       
   239 				}
       
   240 			} else {
       
   241 				previous = false;
       
   242 			}
       
   243 		}
       
   244 		return hazard;
       
   245 	}
       
   246 
       
   247 	/**
       
   248 	 * Checks if previous character is hazardous
       
   249 	 * 
       
   250 	 * @param index
       
   251 	 *            the index
       
   252 	 * @return true if hazard, false if not
       
   253 	 * @throws SourceParserException
       
   254 	 *             if processing fails
       
   255 	 */
       
   256 	private boolean checkPreviousChar(int index) throws SourceParserException {
       
   257 		boolean hazard = false;
       
   258 		SourceIterator previtr = parser.createIterator(index,
       
   259 				SourceParser.SKIP_ALL | SourceParser.BACKWARD_SEARCH);
       
   260 		char prevchar = previtr.next();
       
   261 		if (prevchar != ';' && prevchar != '{' && prevchar != '}') {
       
   262 			hazard = true;
       
   263 		}
       
   264 		return hazard;
       
   265 	}
       
   266 
       
   267 	/**
       
   268 	 * Finds the last return statement from the given context
       
   269 	 * 
       
   270 	 * @param context
       
   271 	 *            the context
       
   272 	 * @return the index to beginning of the return statement
       
   273 	 */
       
   274 	int findLast(SourceContext context) {
       
   275 		String statement = SourceConstants.RETURN;
       
   276 		int retval = findLast(context, statement);
       
   277 		if (returnExtensions != null && returnExtensions.size() > 0) {
       
   278 			int res;
       
   279 			for (int i = 0; i < returnExtensions.size(); i++) {
       
   280 				res = findLast(context, returnExtensions.get(i));
       
   281 				if (res > retval) {
       
   282 					retval = res;
       
   283 				}
       
   284 			}
       
   285 		}
       
   286 		if (retval == -1) {
       
   287 			retval = context.getOffset() + context.getLength();
       
   288 		}
       
   289 		return retval;
       
   290 	}
       
   291 
       
   292 	/**
       
   293 	 * Finds the last return statement from the given context
       
   294 	 * 
       
   295 	 * @param context
       
   296 	 *            the context
       
   297 	 * @param statement
       
   298 	 *            the statement to be searched
       
   299 	 * @return the index to beginning of the return statement
       
   300 	 */
       
   301 	private int findLast(SourceContext context, String statement) {
       
   302 		// TODO: Backwards string search
       
   303 		int start = context.getOffset();
       
   304 		int end = start + context.getLength();
       
   305 		SourceSearch search = parser.startStringSearch(statement, start, end,
       
   306 				SourceParser.MATCH_WHOLE_WORD | SourceParser.SKIP_ALL);
       
   307 		int index = 0;
       
   308 		int retval = -1;
       
   309 		do {
       
   310 			index = search.findNext();
       
   311 			if (index != -1) {
       
   312 				retval = index;
       
   313 			}
       
   314 		} while (index != -1);
       
   315 		return retval;
       
   316 	}
       
   317 }