Example usage for org.eclipse.jdt.internal.compiler.parser TerminalTokens TokenNameIdentifier

List of usage examples for org.eclipse.jdt.internal.compiler.parser TerminalTokens TokenNameIdentifier

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.parser TerminalTokens TokenNameIdentifier.

Prototype

int TokenNameIdentifier

To view the source code for org.eclipse.jdt.internal.compiler.parser TerminalTokens TokenNameIdentifier.

Click Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.core.JavaConventions.java

License:Open Source License

private static synchronized char[] scannedIdentifier(String id, String sourceLevel, String complianceLevel) {
    if (id == null) {
        return null;
    }//w  ww.jav a 2s.com
    // Set scanner for given source and compliance levels
    SCANNER.sourceLevel = sourceLevel == null ? ClassFileConstants.JDK1_3
            : CompilerOptions.versionToJdkLevel(sourceLevel);
    SCANNER.complianceLevel = complianceLevel == null ? ClassFileConstants.JDK1_3
            : CompilerOptions.versionToJdkLevel(complianceLevel);

    try {
        SCANNER.setSource(id.toCharArray());
        int token = SCANNER.scanIdentifier();
        if (token != TerminalTokens.TokenNameIdentifier)
            return null;
        if (SCANNER.currentPosition == SCANNER.eofPosition) { // to handle case where we had an ArrayIndexOutOfBoundsException
            try {
                return SCANNER.getCurrentIdentifierSource();
            } catch (ArrayIndexOutOfBoundsException e) {
                return null;
            }
        } else {
            return null;
        }
    } catch (InvalidInputException e) {
        return null;
    }
}

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);//  www  . j a v  a 2s .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.core.search.indexing.AddJarFileToIndex.java

License:Open Source License

private boolean isIdentifier() throws InvalidInputException {
    switch (this.scanner.scanIdentifier()) {
    // assert and enum will not be recognized as java identifiers
    // in 1.7 mode, which are in 1.3.
    case TerminalTokens.TokenNameIdentifier:
    case TerminalTokens.TokenNameassert:
    case TerminalTokens.TokenNameenum:
        return true;
    default:/*from ww  w  . jav  a 2  s  .co  m*/
        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.//from  w  w w  .  j  a  v  a  2s .  co  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:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

/**
 * This method is used to retrieve the position just before the left bracket.
 * @return int the dimension found, -1 if none
 *///from   w  w w .  ja  va 2 s.  c o m
protected int retrieveEndOfElementTypeNamePosition(int start, int end) {
    this.scanner.resetTo(start, end);
    try {
        int token;
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            switch (token) {
            case TerminalTokens.TokenNameIdentifier:
            case TerminalTokens.TokenNamebyte:
            case TerminalTokens.TokenNamechar:
            case TerminalTokens.TokenNamedouble:
            case TerminalTokens.TokenNamefloat:
            case TerminalTokens.TokenNameint:
            case TerminalTokens.TokenNamelong:
            case TerminalTokens.TokenNameshort:
            case TerminalTokens.TokenNameboolean:
                return this.scanner.currentPosition - 1;
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
    return -1;
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

protected void retrieveIdentifierAndSetPositions(int start, int end, Name name) {
    this.scanner.resetTo(start, end);
    int token;//from   w w  w  .jav a2s  . co  m
    try {
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            if (token == TerminalTokens.TokenNameIdentifier) {
                int startName = this.scanner.startPosition;
                int endName = this.scanner.currentPosition - 1;
                name.setSourceRange(startName, endName - startName + 1);
                return;
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

/**
 * This method is used to retrieve the start position of the block.
 * @return int the dimension found, -1 if none
 */// ww w .j  a va 2s . co m
protected int retrieveIdentifierEndPosition(int start, int end) {
    this.scanner.resetTo(start, end);
    try {
        int token;
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            switch (token) {
            case TerminalTokens.TokenNameIdentifier://110
                return this.scanner.getCurrentTokenEndPosition();
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
    return -1;
}