tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/SourceParameterTokenizer.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2010 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 * Parameter tokenizer is used to parse function parameters lists
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 /**
       
    24  * Parameter tokenizer is used to parse function parameters lists
       
    25  * 
       
    26  */
       
    27 public class SourceParameterTokenizer {
       
    28 
       
    29 	/**
       
    30 	 * The source parser
       
    31 	 */
       
    32 	private SourceParser parser;
       
    33 
       
    34 	/**
       
    35 	 * The offset where to start the tokenizer
       
    36 	 */
       
    37 	private int offset;
       
    38 
       
    39 	/**
       
    40 	 * Slip next whitespace
       
    41 	 */
       
    42 	private boolean skipNextWhiteSpace;
       
    43 
       
    44 	/**
       
    45 	 * Constructor
       
    46 	 * 
       
    47 	 * @param parser
       
    48 	 *            the source parser
       
    49 	 * @param offset
       
    50 	 *            offset to the start of parameter
       
    51 	 */
       
    52 	public SourceParameterTokenizer(SourceParser parser, int offset) {
       
    53 		this.parser = parser;
       
    54 		this.offset = offset;
       
    55 	}
       
    56 
       
    57 	/**
       
    58 	 * Parses a list of parameters (a, b, c) and stores the values into the
       
    59 	 * given list.
       
    60 	 * 
       
    61 	 * @param list
       
    62 	 *            the list of parameters
       
    63 	 * @param findSeparator
       
    64 	 *            if true, the processing stops after ';' or '{' character. If
       
    65 	 *            false, processing stops after ')' at end of parameters
       
    66 	 * @return index at end of parameters
       
    67 	 * @throws SourceParserException
       
    68 	 *             if processing fails
       
    69 	 */
       
    70 	public int tokenize(List<String> list, boolean findSeparator)
       
    71 			throws SourceParserException {
       
    72 		TokenizerSearchData data = new TokenizerSearchData();
       
    73 		data.itr = parser.createIterator(offset, SourceParser.SKIP_WHITE_SPACES
       
    74 				| SourceParser.SKIP_COMMENTS);
       
    75 		boolean bracketsOpened = false;
       
    76 		boolean finished = false;
       
    77 		while (data.itr.hasNext() && !finished) {
       
    78 			data.value = data.itr.next();
       
    79 			if (!data.inQuotes && data.value == '\"') {
       
    80 				data.inQuotes = true;
       
    81 				data.hasData = true;
       
    82 			} else if (data.inQuotes) {
       
    83 				processInQuotesChar(data);
       
    84 			} else if (data.complete) {
       
    85 				processEndOfParametersChar(data);
       
    86 			} else if (data.value == '(') {
       
    87 				bracketsOpened = true;
       
    88 				processOpeningBracket(data);
       
    89 			} else if (data.value == ',' || data.value == ')') {
       
    90 				processCommaOrClosingBracket(list, data);
       
    91 			} else if (data.value == ';' && data.openBracketCount != 0 || data.value == '{'
       
    92 					|| data.value == '}' ) {
       
    93 				throw new SourceParserException(
       
    94 						SourceErrorCodes.UNEXPECTED_PARAMETER_SEPARATOR);
       
    95 			} else {
       
    96 				// Raises a flag that there is some data. processOpeningBracket
       
    97 				// no longer interprets the next bracket as opening bracket
       
    98 				if (data.openBracketCount > 0) {
       
    99 					data.hasData = true;
       
   100 				}
       
   101 			}
       
   102 			finished = ((data.complete && !findSeparator) || (data.endFound && findSeparator));
       
   103 			if (bracketsOpened && data.openBracketCount == 0) {
       
   104 				data.complete = true;
       
   105 				break;
       
   106 			}
       
   107 		}
       
   108 		if (!data.complete) {
       
   109 			throw new SourceParserException(
       
   110 					SourceErrorCodes.UNEXPECTED_END_OF_FILE);
       
   111 		}
       
   112 		if (data.openBracketCount != 0) {
       
   113 			throw new SourceParserException(SourceErrorCodes.BRACKET_MISMATCH);
       
   114 		}
       
   115 		
       
   116 		
       
   117 		return data.itr.currentIndex() + 1;
       
   118 	}
       
   119 
       
   120 	/**
       
   121 	 * Parses list of parameters with types (int a, int b, int c) and stores the
       
   122 	 * values into the given list.
       
   123 	 * 
       
   124 	 * @param list
       
   125 	 *            the list of parameters
       
   126 	 * @return index at end of parameters
       
   127 	 * @throws SourceParserException
       
   128 	 *             if processing fails
       
   129 	 */
       
   130 	public int tokenizeTyped(List<SourceParameter> list) 
       
   131 			throws SourceParserException {
       
   132 		TokenizerSearchData data = new TokenizerSearchData();
       
   133 
       
   134 		try {
       
   135 			data.itr = parser
       
   136 					.createIterator(offset, SourceParser.SKIP_WHITE_SPACES
       
   137 							| SourceParser.SKIP_COMMENTS);
       
   138 			data.sourceParameter = new SourceParameter();
       
   139 			while (data.itr.hasNext() && !data.complete) {
       
   140 				data.value = data.itr.next();
       
   141 
       
   142 				// Check if there was array start or end character and then
       
   143 				// space. It would mean that the parameter continues and more
       
   144 				// should be parsed.
       
   145 				if (skipNextWhiteSpace) {
       
   146 					skipNextWhiteSpace = false;
       
   147 					if (data.itr.hasSkipped()) {
       
   148 						data.value = data.itr.next();
       
   149 					}
       
   150 				}
       
   151 
       
   152 				if (data.value == '\"') {
       
   153 					throw new SourceParserException(
       
   154 							SourceErrorCodes.UNEXPECTED_QUOTE_CHARACTER);
       
   155 				} else if (data.value == '(') {
       
   156 					processOpeningBracket(data);
       
   157 				} else if (data.value == ',' || data.value == ')') {
       
   158 					processCommaOrClosingBracket(list, data);
       
   159 				} else if (data.value == ';' || data.value == '{'
       
   160 						|| data.value == '}') {
       
   161 					data.complete = true;
       
   162 					// Array start or end character.
       
   163 				} else if (data.value == '<' || data.value == '>') {
       
   164 					skipNextWhiteSpace = true;
       
   165 				} else if (data.itr.hasSkipped() && data.openBracketCount > 0) {
       
   166 					processNameValueSeparator(data);
       
   167 				}
       
   168 			}
       
   169 			if (!data.complete) {
       
   170 				throw new SourceParserException(
       
   171 						SourceErrorCodes.UNEXPECTED_END_OF_FILE);
       
   172 			}
       
   173 		} catch (SourceParserException e) {
       
   174 			// Resets all source locations if parser fails
       
   175 			for (int i = 0; i < list.size(); i++) {
       
   176 				list.get(i).getSourceLocation().dereference();
       
   177 			}
       
   178 			throw e;
       
   179 		}
       
   180 		return data.itr.currentIndex() + 1;
       
   181 	}
       
   182 
       
   183 	/**
       
   184 	 * Processes a separator character and updates the current SourceParameter
       
   185 	 * object in the search data
       
   186 	 * 
       
   187 	 * @param data
       
   188 	 *            the search data
       
   189 	 * @throws SourceParserException
       
   190 	 *             if processing fails
       
   191 	 */
       
   192 	private void processNameValueSeparator(TokenizerSearchData data)
       
   193 			throws SourceParserException {
       
   194 		// If the parameter is empty, the previous index will point
       
   195 		// to index preceeding tagStartIndex
       
   196 		int previous = data.itr.previousIndex();
       
   197 		if (previous >= data.tagStartIndex) {
       
   198 			int endIndex = previous + 1;
       
   199 			if (data.sourceParameter.getType() == null) {
       
   200 				processNameValueSeparatorNoType(data, endIndex);
       
   201 			} else if (data.sourceParameter.getName() == null) {
       
   202 				processNameValueSeparatorNoName(data, endIndex);
       
   203 			}
       
   204 			data.tagStartIndex = data.itr.currentIndex();
       
   205 		}
       
   206 	}
       
   207 
       
   208 	/**
       
   209 	 * Processes a name-value separator when there is no name
       
   210 	 * 
       
   211 	 * @param data
       
   212 	 *            the search data
       
   213 	 * @param endIndex
       
   214 	 *            the end index of the parameters
       
   215 	 * @throws SourceParserException
       
   216 	 *             if processing fails
       
   217 	 */
       
   218 	private void processNameValueSeparatorNoName(TokenizerSearchData data,
       
   219 			int endIndex) throws SourceParserException {
       
   220 		String name = parser.getSource().get(data.tagStartIndex,
       
   221 				endIndex - data.tagStartIndex);
       
   222 		boolean startFound = false;
       
   223 		int start = 0;
       
   224 		int end = name.length();
       
   225 		for (int i = 0; i < name.length(); i++) {
       
   226 			char c = name.charAt(i);
       
   227 			if (c == '&' || c == '*') {
       
   228 				if (c == '&') {
       
   229 					data.sourceParameter.setReference();
       
   230 				} else {
       
   231 					data.sourceParameter.addPointer();
       
   232 				}
       
   233 				if (!startFound) {
       
   234 					start++;
       
   235 				} else {
       
   236 					end--;
       
   237 				}
       
   238 			} else {
       
   239 				startFound = true;
       
   240 			}
       
   241 		}
       
   242 		name = name.substring(start, end);
       
   243 		if (name.length() > 0) {
       
   244 			if (isParameterTypeQualifier(name)) {
       
   245 				// Qualifiers between type and name are ignored
       
   246 				// For example TInt const* aValue
       
   247 			} else {
       
   248 				data.sourceParameter.setName(name);
       
   249 			}
       
   250 		}
       
   251 	}
       
   252 
       
   253 	/**
       
   254 	 * Processes a name-value separator when there is no value
       
   255 	 * 
       
   256 	 * @param data
       
   257 	 *            the search data
       
   258 	 * @param endIndex
       
   259 	 *            the end index of the parameters
       
   260 	 * @throws SourceParserException
       
   261 	 *             if processing fails
       
   262 	 */
       
   263 	private void processNameValueSeparatorNoType(TokenizerSearchData data,
       
   264 			int endIndex) throws SourceParserException {
       
   265 		String type = parser.getSource().get(data.tagStartIndex,
       
   266 				endIndex - data.tagStartIndex);
       
   267 		if (isParameterTypeQualifier(type)) {
       
   268 			data.sourceParameter.addQualifier(type);
       
   269 		} else {
       
   270 			for (int i = type.length() - 1; i >= 0; i--) {
       
   271 				if (type.charAt(i) == '&') {
       
   272 					data.sourceParameter.setReference();
       
   273 					if (i == 0) {
       
   274 						type = ""; //$NON-NLS-1$
       
   275 					}
       
   276 				} else if (type.charAt(i) == '*') {
       
   277 					data.sourceParameter.addPointer();
       
   278 					if (i == 0) {
       
   279 						type = ""; //$NON-NLS-1$
       
   280 					}
       
   281 				} else {
       
   282 					if (i != type.length() - 1) {
       
   283 						type = type.substring(0, i + 1);
       
   284 					}
       
   285 					i = -1;
       
   286 				}
       
   287 			}
       
   288 			if (type.length() > 0) {
       
   289 				// Remove spaces
       
   290 				type = type.replace(" ", ""); //$NON-NLS-1$ //$NON-NLS-2$
       
   291 				data.sourceParameter.setType(type);
       
   292 			}
       
   293 		}
       
   294 	}
       
   295 
       
   296 	/**
       
   297 	 * Checks if parameter type if a qualifier or not
       
   298 	 * 
       
   299 	 * @param type
       
   300 	 *            the type to be checked
       
   301 	 * @return true if qualifier, false if not
       
   302 	 */
       
   303 	private boolean isParameterTypeQualifier(String type) {
       
   304 		boolean retval = false;
       
   305 		for (String element : SourceConstants.PARAMETER_QUALIFIERS) {
       
   306 			if (type.equals(element)) {
       
   307 				retval = true;
       
   308 			}
       
   309 		}
       
   310 		return retval;
       
   311 	}
       
   312 
       
   313 	/**
       
   314 	 * Processes a parameter separator or closing bracket
       
   315 	 * 
       
   316 	 * @param list
       
   317 	 *            the list of existing parameters
       
   318 	 * @param data
       
   319 	 *            the search data
       
   320 	 * @throws SourceParserException
       
   321 	 *             if invalid character is encountered
       
   322 	 */
       
   323 	@SuppressWarnings("unchecked")
       
   324 	private void processCommaOrClosingBracket(List list,
       
   325 			TokenizerSearchData data) throws SourceParserException {
       
   326 		// This method is called from both tokenize functions. One uses
       
   327 		// List<String> and other List<SourceParameter>
       
   328 		// Thus this uses List and @SuppressWarnings
       
   329 		if (data.value == ')') {
       
   330 			data.openBracketCount--;
       
   331 		}
       
   332 		
       
   333 		if (data.value == ',') {
       
   334 			//we may have the case of OstTraceDef1( OST_TRACE_CATEGORY_ALL, TRACE_FATAL, TEST_OstTraceDef1, "EOstTraceDef1 - %u" , f(a,b));
       
   335 			//when processing the comma in f(a,b) that should not count as parameter separator
       
   336 			if (data.openBracketCount >1 ) {
       
   337 				return;
       
   338 			}
       
   339 		}
       
   340 		
       
   341 		if (data.value == ',' || data.openBracketCount == 0) {
       
   342 			//we have another parameter
       
   343 			if (data.sourceParameter != null) {
       
   344 				// If processing typed parameter list, the name and type are
       
   345 				// stored into a SourceParameter object, which is then added
       
   346 				// to list
       
   347 				processNameValueSeparator(data);
       
   348 				if (data.sourceParameter.getType() != null) {
       
   349 					SourceLocation location = new SourceLocation(parser,
       
   350 							data.paramStartIndex, data.itr.currentIndex()
       
   351 									- data.paramStartIndex);
       
   352 					data.sourceParameter.setSourceLocation(location);
       
   353 					list.add(data.sourceParameter);
       
   354 					data.sourceParameter = new SourceParameter();
       
   355 				}
       
   356 			} else {
       
   357 				// In this case the list contains strings.
       
   358 				int previous = data.itr.previousIndex();
       
   359 				String tracepoint = ""; //$NON-NLS-1$
       
   360 				if (previous >= data.tagStartIndex) {
       
   361 					int endIndex = data.itr.previousIndex() + 1;
       
   362 					tracepoint = parser.getSource().get(data.tagStartIndex,
       
   363 							endIndex - data.tagStartIndex);
       
   364 					list.add(tracepoint);
       
   365 				} else {
       
   366 					list.add(tracepoint);
       
   367 				}	
       
   368 			}
       
   369 			
       
   370 			// In case like below we have parsed all parameters and data is completed if next character after ')' is ':' 
       
   371 			// and open bracket count is 0:
       
   372 			// 		CNpeSendData::CNpeSendData(RMeDriver* aDriver, TUint16 aMaxMsgLength): CActive(EPriorityStandard),
       
   373 			//				iDriver(aDriver),
       
   374 			//				iMaxMsgLength(aMaxMsgLength)
       
   375 			if (data.itr.hasNext()){
       
   376 				char nextChar = data.itr.peek();
       
   377 				
       
   378 				if (data.value == ')' &&  nextChar == ':' && data.openBracketCount == 0) {
       
   379 					data.complete = true;
       
   380 				}
       
   381 			}
       
   382 			
       
   383 			if (data.value == ',') {
       
   384 				data.tagStartIndex = data.itr.nextIndex();
       
   385 				data.paramStartIndex = data.tagStartIndex;
       
   386 			}
       
   387 		}
       
   388 	}
       
   389 
       
   390 	/**
       
   391 	 * Processes an opening bracket
       
   392 	 * 
       
   393 	 * @param data
       
   394 	 *            the search data
       
   395 	 */
       
   396 	private void processOpeningBracket(TokenizerSearchData data) {
       
   397 		data.openBracketCount++;
       
   398 		if (!data.hasData && data.openBracketCount == 1 ) {
       
   399 			// The number of initial '(' characters is stored. The
       
   400 			// parameters are assumed to end when the corresponding ')'
       
   401 			// character is encountered
       
   402 			data.tagStartIndex = data.itr.nextIndex();
       
   403 			data.paramStartIndex = data.tagStartIndex;
       
   404 			data.initialBracketCount = data.openBracketCount;
       
   405 		}
       
   406 	}
       
   407 
       
   408 	/**
       
   409 	 * Process a character when in quotes
       
   410 	 * 
       
   411 	 * @param data
       
   412 	 *            the search data
       
   413 	 */
       
   414 	private void processInQuotesChar(TokenizerSearchData data) {
       
   415 		if (data.value == '\"' && data.previousValue != '\\') {
       
   416 			data.inQuotes = false;
       
   417 		}
       
   418 		data.previousValue = data.value;
       
   419 	}
       
   420 
       
   421 	/**
       
   422 	 * Processes a character found after the closing bracket
       
   423 	 * 
       
   424 	 * @param data
       
   425 	 *            the data
       
   426 	 * @throws SourceParserException
       
   427 	 *             if invalid characters are found
       
   428 	 */
       
   429 	private void processEndOfParametersChar(TokenizerSearchData data)
       
   430 			throws SourceParserException {
       
   431 		if (data.value == ';' || data.value == '{') {
       
   432 			data.endFound = true;
       
   433 		} else if (data.value == ')') {
       
   434 			data.openBracketCount--;
       
   435 		} else if (!Character.isWhitespace(data.value)) {
       
   436 			throw new SourceParserException(SourceErrorCodes.BRACKET_MISMATCH);
       
   437 		}
       
   438 	}
       
   439 
       
   440 }