List of usage examples for org.eclipse.jdt.internal.compiler.parser Scanner getCurrentTokenSource
public char[] getCurrentTokenSource()
From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java
License:Open Source License
private boolean checkSelection(char[] source, int selectionStart, int selectionEnd) { Scanner scanner = new Scanner(false /*comment*/, false /*whitespace*/, false /*nls*/, this.compilerOptions.sourceLevel, this.compilerOptions.complianceLevel, null/*taskTag*/, null/*taskPriorities*/, true /*taskCaseSensitive*/); scanner.setSource(source);/* ww w. j av a 2 s. c om*/ int lastIdentifierStart = -1; int lastIdentifierEnd = -1; char[] lastIdentifier = null; int token; if (selectionStart > selectionEnd) { int end = source.length - 1; // compute start position of current line int currentPosition = selectionStart - 1; int nextCharacterPosition = selectionStart; char currentCharacter = ' '; try { lineLoop: while (currentPosition > 0) { if (source[currentPosition] == '\\' && source[currentPosition + 1] == 'u') { int pos = currentPosition + 2; int c1 = 0, c2 = 0, c3 = 0, c4 = 0; while (source[pos] == 'u') { pos++; } int endOfUnicode = pos + 3; if (end < endOfUnicode) { if (endOfUnicode < source.length) { end = endOfUnicode; } else { return false; // not enough characters to decode an unicode } } if ((c1 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c1 < 0 || (c2 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c2 < 0 || (c3 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c3 < 0 || (c4 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c4 < 0) { return false; } else { currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4); nextCharacterPosition = pos; } } else { currentCharacter = source[currentPosition]; nextCharacterPosition = currentPosition + 1; } switch (currentCharacter) { case '\r': case '\n': case '/': case '"': case '\'': break lineLoop; } currentPosition--; } } catch (ArrayIndexOutOfBoundsException e) { return false; } // compute start and end of the last token scanner.resetTo(nextCharacterPosition, end); isolateLastName: do { try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return false; } switch (token) { case TerminalTokens.TokenNamethis: case TerminalTokens.TokenNamesuper: case TerminalTokens.TokenNamenew: case TerminalTokens.TokenNameIdentifier: if (scanner.startPosition <= selectionStart && selectionStart <= scanner.currentPosition) { if (scanner.currentPosition == scanner.eofPosition) { int temp = scanner.eofPosition; scanner.eofPosition = scanner.source.length; while (scanner.getNextCharAsJavaIdentifierPart()) { /*empty*/} scanner.eofPosition = temp; } lastIdentifierStart = scanner.startPosition; lastIdentifierEnd = scanner.currentPosition - 1; lastIdentifier = scanner.getCurrentTokenSource(); break isolateLastName; } break; } } while (token != TerminalTokens.TokenNameEOF); } else { if (selectionStart == selectionEnd) { // Widen the selection to scan -> || :: if needed. No unicode handling for now. if (selectionStart > 0 && selectionEnd < source.length - 1) { if ((source[selectionStart] == '>' && source[selectionStart - 1] == '-') || source[selectionStart] == ':' && source[selectionStart - 1] == ':') { selectionStart--; } else { if ((source[selectionStart] == '-' && source[selectionEnd + 1] == '>') || source[selectionStart] == ':' && source[selectionEnd + 1] == ':') { selectionEnd++; } } } } // there could be some innocuous widening, shouldn't matter. scanner.resetTo(selectionStart, selectionEnd); boolean expectingIdentifier = true; do { try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return false; } switch (token) { case TerminalTokens.TokenNamethis: case TerminalTokens.TokenNamesuper: case TerminalTokens.TokenNamenew: case TerminalTokens.TokenNameIdentifier: if (!expectingIdentifier) return false; lastIdentifier = scanner.getCurrentTokenSource(); lastIdentifierStart = scanner.startPosition; lastIdentifierEnd = scanner.currentPosition - 1; if (lastIdentifierEnd > selectionEnd) { lastIdentifierEnd = selectionEnd; lastIdentifier = CharOperation.subarray(lastIdentifier, 0, lastIdentifierEnd - lastIdentifierStart + 1); } expectingIdentifier = false; break; case TerminalTokens.TokenNameCOLON_COLON: if (selectionStart >= scanner.startPosition && selectionEnd < scanner.currentPosition) { this.actualSelectionStart = selectionStart; this.actualSelectionEnd = selectionEnd; this.selectedIdentifier = CharOperation.NO_CHAR; return true; } //$FALL-THROUGH$ case TerminalTokens.TokenNameDOT: if (expectingIdentifier) return false; expectingIdentifier = true; break; case TerminalTokens.TokenNameEOF: if (expectingIdentifier) return false; break; case TerminalTokens.TokenNameLESS: if (!checkTypeArgument(scanner)) return false; break; case TerminalTokens.TokenNameAT: if (scanner.startPosition != scanner.initialPosition) return false; break; case TerminalTokens.TokenNameARROW: if (selectionStart >= scanner.startPosition && selectionEnd < scanner.currentPosition) { this.actualSelectionStart = selectionStart; this.actualSelectionEnd = selectionEnd; this.selectedIdentifier = CharOperation.NO_CHAR; return true; } return false; default: return false; } } while (token != TerminalTokens.TokenNameEOF); } if (lastIdentifierStart > 0) { this.actualSelectionStart = lastIdentifierStart; this.actualSelectionEnd = lastIdentifierEnd; this.selectedIdentifier = lastIdentifier; return true; } return false; }
From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java
License:Open Source License
private boolean checkTypeArgument(Scanner scanner) { int depth = 1; int token;//w ww .j ava 2 s . co m StringBuffer buffer = new StringBuffer(); do { try { token = scanner.getNextToken(); } catch (InvalidInputException e) { return false; } switch (token) { case TerminalTokens.TokenNameLESS: depth++; buffer.append(scanner.getCurrentTokenSource()); break; case TerminalTokens.TokenNameGREATER: depth--; buffer.append(scanner.getCurrentTokenSource()); break; case TerminalTokens.TokenNameRIGHT_SHIFT: depth -= 2; buffer.append(scanner.getCurrentTokenSource()); break; case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT: depth -= 3; buffer.append(scanner.getCurrentTokenSource()); break; case TerminalTokens.TokenNameextends: case TerminalTokens.TokenNamesuper: buffer.append(' '); buffer.append(scanner.getCurrentTokenSource()); buffer.append(' '); break; case TerminalTokens.TokenNameCOMMA: if (depth == 1) { int length = buffer.length(); char[] typeRef = new char[length]; buffer.getChars(0, length, typeRef, 0); try { Signature.createTypeSignature(typeRef, true); buffer = new StringBuffer(); } catch (IllegalArgumentException e) { return false; } } break; default: buffer.append(scanner.getCurrentTokenSource()); break; } if (depth < 0) { return false; } } while (depth != 0 && token != TerminalTokens.TokenNameEOF); if (depth == 0) { int length = buffer.length() - 1; char[] typeRef = new char[length]; buffer.getChars(0, length, typeRef, 0); try { Signature.createTypeSignature(typeRef, true); return true; } catch (IllegalArgumentException e) { return false; } } return false; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Finds the accurate positions of the sequence of tokens given by qualifiedName * in the source and reports a reference to this this qualified name * to the search requestor.// ww w . j ava 2 s. c o m */ protected void reportAccurateTypeReference(SearchMatch match, ASTNode typeRef, char[] name) throws CoreException { if (match.getRule() == 0) return; if (!encloses((IJavaElement) match.getElement())) return; int sourceStart = typeRef.sourceStart; int sourceEnd = typeRef.sourceEnd; // Compute source positions of the qualified reference if (name != null) { Scanner scanner = this.parser.scanner; scanner.setSource(this.currentPossibleMatch.getContents()); scanner.resetTo(sourceStart, sourceEnd); int token = -1; int currentPosition; do { currentPosition = scanner.currentPosition; try { token = scanner.getNextToken(); } catch (InvalidInputException e) { // ignore } if (token == TerminalTokens.TokenNameIdentifier && this.pattern.matchesName(name, scanner.getCurrentTokenSource())) { int length = scanner.currentPosition - currentPosition; match.setOffset(currentPosition); match.setLength(length); report(match); return; } } while (token != TerminalTokens.TokenNameEOF); } // Report match match.setOffset(sourceStart); match.setLength(sourceEnd - sourceStart + 1); report(match); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
/** * Finds the accurate positions of each valid token in the source and * reports a reference to this token to the search requestor. * A token is valid if it has an accuracy which is not -1. */// w ww. jav a2 s .c o m protected void reportAccurateFieldReference(SearchMatch[] matches, QualifiedNameReference qNameRef) throws CoreException { if (matches == null) return; // there's nothing to accurate in this case int matchesLength = matches.length; int sourceStart = qNameRef.sourceStart; int sourceEnd = qNameRef.sourceEnd; char[][] tokens = qNameRef.tokens; // compute source positions of the qualified reference Scanner scanner = this.parser.scanner; scanner.setSource(this.currentPossibleMatch.getContents()); scanner.resetTo(sourceStart, sourceEnd); int sourceLength = sourceEnd - sourceStart + 1; int refSourceStart = -1, refSourceEnd = -1; int length = tokens.length; int token = -1; int previousValid = -1; int i = 0; int index = 0; do { int currentPosition = scanner.currentPosition; // read token try { token = scanner.getNextToken(); } catch (InvalidInputException e) { //ignore } if (token != TerminalTokens.TokenNameEOF) { char[] currentTokenSource = scanner.getCurrentTokenSource(); boolean equals = false; while (i < length && !(equals = this.pattern.matchesName(tokens[i++], currentTokenSource))) { /*empty*/} if (equals && (previousValid == -1 || previousValid == i - 2)) { previousValid = i - 1; if (refSourceStart == -1) refSourceStart = currentPosition; refSourceEnd = scanner.currentPosition - 1; } else { i = 0; refSourceStart = -1; previousValid = -1; } // read '.' try { token = scanner.getNextToken(); } catch (InvalidInputException e) { // ignore } } SearchMatch match = matches[index]; if (match != null && match.getRule() != 0) { if (!encloses((IJavaElement) match.getElement())) return; // accept reference if (refSourceStart != -1) { match.setOffset(refSourceStart); match.setLength(refSourceEnd - refSourceStart + 1); report(match); } else { match.setOffset(sourceStart); match.setLength(sourceLength); report(match); } i = 0; } refSourceStart = -1; previousValid = -1; if (index < matchesLength - 1) { index++; } } while (token != TerminalTokens.TokenNameEOF); }