Java Code Examples for org.eclipse.jdt.core.compiler.ITerminalSymbols#TokenNameStringLiteral

The following examples show how to use org.eclipse.jdt.core.compiler.ITerminalSymbols#TokenNameStringLiteral . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefactoringScanner.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void doScan() {
	try{
		int token = fScanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF) {
			switch (token) {
				case ITerminalSymbols.TokenNameStringLiteral :
				case ITerminalSymbols.TokenNameCOMMENT_JAVADOC :
				case ITerminalSymbols.TokenNameCOMMENT_LINE :
				case ITerminalSymbols.TokenNameCOMMENT_BLOCK :
					parseCurrentToken();
			}
			token = fScanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
}
 
Example 2
Source File: RefactoringScanner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void doScan() {
	try{
		int token = fScanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF) {
			switch (token) {
				case ITerminalSymbols.TokenNameStringLiteral :
				case ITerminalSymbols.TokenNameCOMMENT_JAVADOC :
				case ITerminalSymbols.TokenNameCOMMENT_LINE :
				case ITerminalSymbols.TokenNameCOMMENT_BLOCK :
					parseCurrentToken();
			}
			token = fScanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
}
 
Example 3
Source File: SJavaTokenComparator.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns the length of the token that
 * initiates the given comment type.
 *
 * @param tokenType
 * @return the length of the token that start a comment
 * @since 3.3
 */
private static int getCommentStartTokenLength(int tokenType) {
  if (tokenType == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
    return 3;
  } else if (tokenType == ITerminalSymbols.TokenNameStringLiteral) {
    return 1;
  } else {
    return 2;
  }
}
 
Example 4
Source File: JavaTokenTypeTokenizer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param token
 * @return
 */
public static boolean isLiteralToken(final int token) {
	return token == ITerminalSymbols.TokenNameStringLiteral
			|| token == ITerminalSymbols.TokenNameCharacterLiteral
			|| token == ITerminalSymbols.TokenNameFloatingPointLiteral
			|| token == ITerminalSymbols.TokenNameIntegerLiteral
			|| token == ITerminalSymbols.TokenNameLongLiteral
			|| token == ITerminalSymbols.TokenNameDoubleLiteral;
}
 
Example 5
Source File: JavaTokenTypeTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param token
 * @return
 */
public static boolean isLiteralToken(final int token) {
	return token == ITerminalSymbols.TokenNameStringLiteral
			|| token == ITerminalSymbols.TokenNameCharacterLiteral
			|| token == ITerminalSymbols.TokenNameFloatingPointLiteral
			|| token == ITerminalSymbols.TokenNameIntegerLiteral
			|| token == ITerminalSymbols.TokenNameLongLiteral
			|| token == ITerminalSymbols.TokenNameDoubleLiteral;
}
 
Example 6
Source File: JavaTokenComparator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a token comparator for the given string.
 *
 * @param text the text to be tokenized
 */
public JavaTokenComparator(String text) {
	Assert.isLegal(text != null);

	fText= text;

	int length= fText.length();
	fStarts= new int[length];
	fLengths= new int[length];
	fCount= 0;

	IScanner scanner= ToolFactory.createScanner(true, true, false, false); // returns comments & whitespace
	scanner.setSource(fText.toCharArray());
	int endPos= 0;
	try {
		int tokenType;
		while ((tokenType= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
			int start= scanner.getCurrentTokenStartPosition();
			int end= scanner.getCurrentTokenEndPosition()+1;
			// Comments and strings should not be treated as a single token, see https://bugs.eclipse.org/78063
			if (TokenScanner.isComment(tokenType) || tokenType == ITerminalSymbols.TokenNameStringLiteral) {
				// Line comments are often commented code, so lets treat them as code. See https://bugs.eclipse.org/216707
				boolean parseAsJava= tokenType == ITerminalSymbols.TokenNameCOMMENT_LINE;
				int dl= parseAsJava ? getCommentStartTokenLength(tokenType) : 0;
				if (dl > 0)
					recordTokenRange(start, dl);
				parseSubrange(start + dl, text.substring(start + dl, end), parseAsJava);
			} else {
				recordTokenRange(start, end - start);
			}
			endPos= end;
		}
	} catch (InvalidInputException ex) {
		// We couldn't parse part of the input. Fall through and make the rest a single token
	}
	// Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=13907
	if (endPos < length) {
		recordTokenRange(endPos, length - endPos);
	}
}
 
Example 7
Source File: JavaTokenComparator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the length of the token that
 * initiates the given comment type.
 *
 * @param tokenType an {@link ITerminalSymbols} constant
 * @return the length of the token that start a comment
 * @since 3.3
 */
private static int getCommentStartTokenLength(int tokenType) {
	if (tokenType == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
		return 3;
	} else if (tokenType == ITerminalSymbols.TokenNameStringLiteral) {
		return 1;
	} else {
		return 2;
	}
}
 
Example 8
Source File: JavaTokenTypeTokenizer.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param token
 * @return
 */
public static boolean isLiteralToken(final int token) {
	return token == ITerminalSymbols.TokenNameStringLiteral
			|| token == ITerminalSymbols.TokenNameCharacterLiteral
			|| token == ITerminalSymbols.TokenNameFloatingPointLiteral
			|| token == ITerminalSymbols.TokenNameIntegerLiteral
			|| token == ITerminalSymbols.TokenNameLongLiteral
			|| token == ITerminalSymbols.TokenNameDoubleLiteral;
}
 
Example 9
Source File: NLSSearchResultRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Finds the key defined by the given match. The assumption is that the key is the only argument
 * and it is a string literal i.e. quoted ("...") or a string constant i.e. 'static final
 * String' defined in the same class.
 * 
 * @param keyPositionResult reference parameter: will be filled with the position of the found
 *            key
 * @param enclosingElement enclosing java element
 * @return a string denoting the key, {@link #NO_KEY} if no key can be found and
 *         <code>null</code> otherwise
 * @throws CoreException if a problem occurs while accessing the <code>enclosingElement</code>
 */
private String findKey(Position keyPositionResult, IJavaElement enclosingElement) throws CoreException {
	ICompilationUnit unit= (ICompilationUnit)enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
	if (unit == null)
		return null;

	String source= unit.getSource();
	if (source == null)
		return null;

	IJavaProject javaProject= unit.getJavaProject();
	IScanner scanner= null;
	if (javaProject != null) {
		String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
		String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
		scanner= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
	} else {
		scanner= ToolFactory.createScanner(false, false, false, false);
	}
	scanner.setSource(source.toCharArray());
	scanner.resetTo(keyPositionResult.getOffset() + keyPositionResult.getLength(), source.length());

	try {
		if (scanner.getNextToken() != ITerminalSymbols.TokenNameDOT)
			return null;

		if (scanner.getNextToken() != ITerminalSymbols.TokenNameIdentifier)
			return null;

		String src= new String(scanner.getCurrentTokenSource());
		int tokenStart= scanner.getCurrentTokenStartPosition();
		int tokenEnd= scanner.getCurrentTokenEndPosition();

		if (scanner.getNextToken() == ITerminalSymbols.TokenNameLPAREN) {
			// Old school
			// next must be key string. Ignore methods which do not take a single String parameter (Bug 295040).
			int nextToken= scanner.getNextToken();
			if (nextToken != ITerminalSymbols.TokenNameStringLiteral && nextToken != ITerminalSymbols.TokenNameIdentifier)
				return null;

			tokenStart= scanner.getCurrentTokenStartPosition();
			tokenEnd= scanner.getCurrentTokenEndPosition();
			int token;
			while ((token= scanner.getNextToken()) == ITerminalSymbols.TokenNameDOT) {
				if ((nextToken= scanner.getNextToken()) != ITerminalSymbols.TokenNameIdentifier) {
						return null;
				}
				tokenStart= scanner.getCurrentTokenStartPosition();
				tokenEnd= scanner.getCurrentTokenEndPosition();
			}
			if (token != ITerminalSymbols.TokenNameRPAREN)
				return null;
			
			if (nextToken == ITerminalSymbols.TokenNameStringLiteral) {
				keyPositionResult.setOffset(tokenStart + 1);
				keyPositionResult.setLength(tokenEnd - tokenStart - 1);
				return source.substring(tokenStart + 1, tokenEnd);
			} else if (nextToken == ITerminalSymbols.TokenNameIdentifier) {
				keyPositionResult.setOffset(tokenStart);
				keyPositionResult.setLength(tokenEnd - tokenStart + 1);
				IType parentClass= (IType)enclosingElement.getAncestor(IJavaElement.TYPE);
				IField[] fields= parentClass.getFields();
				String identifier= source.substring(tokenStart, tokenEnd + 1);
				for (int i= 0; i < fields.length; i++) {
					if (fields[i].getElementName().equals(identifier)) {
						if (!Signature.getSignatureSimpleName(fields[i].getTypeSignature()).equals("String")) //$NON-NLS-1$
							return null;
						Object obj= fields[i].getConstant();
						return obj instanceof String ? ((String)obj).substring(1, ((String)obj).length() - 1) : NO_KEY;
					}
				}
			}
			return NO_KEY;
		} else {
			IJavaElement[] keys= unit.codeSelect(tokenStart, tokenEnd - tokenStart + 1);

			// an interface can't be a key
			if (keys.length == 1 && keys[0].getElementType() == IJavaElement.TYPE && ((IType) keys[0]).isInterface())
				return null;

			keyPositionResult.setOffset(tokenStart);
			keyPositionResult.setLength(tokenEnd - tokenStart + 1);
			return src;
		}
	} catch (InvalidInputException e) {
		throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
	}
}