Example usage for org.eclipse.jdt.core.compiler IScanner getNextToken

List of usage examples for org.eclipse.jdt.core.compiler IScanner getNextToken

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler IScanner getNextToken.

Prototype

int getNextToken() throws InvalidInputException;

Source Link

Document

Read the next token in the source, and answers its ID as specified by ITerminalSymbols.

Usage

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.NodeFinder.java

License:Open Source License

/**
 * A visitor that maps a selection to a given ASTNode. The result node is
 * determined as follows:/*ww  w .j a  v a  2 s  . c om*/
 * <ul>
 *   <li>first the visitor tries to find a node that is covered by <code>start</code> and
 *       <code>length</code> where either <code>start</code> and <code>length</code> exactly
 *       matches the node or where the text covered before and after the node only consists
 *       of white spaces or comments.</li>
 *     <li>if no such node exists than the node that encloses the range defined by
 *       start and end is returned.</li>
 *   <li>if the length is zero than also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 * 
 * @param root the root node from which the search starts
 * @param start the start offset
 * @param length the length
 * @param source the source of the compilation unit
 * 
 * @return the result node
 * @throws JavaModelException if an error occurs in the Java model
 * 
 * @since      3.0
 */
public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException {
    NodeFinder finder = new NodeFinder(start, length);
    root.accept(finder);
    ASTNode result = finder.getCoveredNode();
    if (result == null)
        return null;
    Selection selection = Selection.createFromStartLength(start, length);
    if (selection.covers(result)) {
        IBuffer buffer = source.getBuffer();
        if (buffer != null) {
            IScanner scanner = ToolFactory.createScanner(false, false, false, false);
            scanner.setSource(buffer.getText(start, length).toCharArray());
            try {
                int token = scanner.getNextToken();
                if (token != ITerminalSymbols.TokenNameEOF) {
                    int tStart = scanner.getCurrentTokenStartPosition();
                    if (tStart == result.getStartPosition() - start) {
                        scanner.resetTo(tStart + result.getLength(), length - 1);
                        token = scanner.getNextToken();
                        if (token == ITerminalSymbols.TokenNameEOF)
                            return result;
                    }
                }
            } catch (InvalidInputException e) {
            }
        }
    }
    return finder.getCoveringNode();
}

From source file:ca.uvic.chisel.javasketch.internal.ast.NodeFinder.java

License:Open Source License

/**
 * Maps a selection to a given ASTNode, where the selection is given by a start and a length.
 * The result node is determined as follows:
 * <ul>/* www  . j  av  a2 s.c o m*/
 *   <li>first the visitor tries to find a node that is covered by <code>start</code> and
 *       <code>length</code> where either <code>start</code> and <code>length</code> exactly
 *       matches the node or where the text covered before and after the node only consists
 *       of white spaces or comments.</li>
 *   <li>if no such node exists then the node that encloses the range defined by
 *       <code>start</code> and <code>length</code> is returned.</li>
 *   <li>if the length is zero then also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 *
 * @param root the root node from which the search starts
 * @param start the given start
 * @param length the given length
 * @param source the source of the compilation unit
 *
 * @return the result node
 * @throws JavaModelException if an error occurs in the Java model
 */
public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException {
    NodeFinder finder = new NodeFinder(root, start, length);
    ASTNode result = finder.getCoveredNode();
    if (result == null)
        return null;
    int nodeStart = result.getStartPosition();
    if (start <= nodeStart && ((nodeStart + result.getLength()) <= (start + length))) {
        IBuffer buffer = source.getBuffer();
        if (buffer != null) {
            IScanner scanner = ToolFactory.createScanner(false, false, false, false);
            try {
                scanner.setSource(buffer.getText(start, length).toCharArray());
                int token = scanner.getNextToken();
                if (token != ITerminalSymbols.TokenNameEOF) {
                    int tStart = scanner.getCurrentTokenStartPosition();
                    if (tStart == result.getStartPosition() - start) {
                        scanner.resetTo(tStart + result.getLength(), length - 1);
                        token = scanner.getNextToken();
                        if (token == ITerminalSymbols.TokenNameEOF)
                            return result;
                    }
                }
            } catch (InvalidInputException e) {
                // ignore
            } catch (IndexOutOfBoundsException e) {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001
                return null;
            }
        }
    }
    return finder.getCoveringNode();
}

From source file:ca.uvic.cs.tagsea.extraction.TagExtractor.java

License:Open Source License

/**
 * Gets the comment regions associated with this document, restricted to the given offset and length, if no comment regions 
 * are found an empty array is returned//from w  w  w  . ja v  a 2  s.  co  m
 * @param document
 * @param offset
 * @param length
 * @param returnSingleLine Wither to collect single line commments
 * @return Array of tag regions
 */
public static IRegion[] getCommentRegions(IDocument document, int offset, int length) {
    try {
        StringBuffer content = new StringBuffer(document.get(offset, length));
        IRegion range = new Region(offset, length);
        List<IRegion> commentRegions = new ArrayList<IRegion>();
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        scanner.setSource(content.toString().toCharArray());

        int shift = range.getOffset();

        while (true) {
            int terminal = scanner.getNextToken();

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameEOF)
                break;
        }

        IRegion[] result = new IRegion[commentRegions.size()];
        commentRegions.toArray(result);
        return result;

    } catch (BadLocationException e) {
        TagSEAPlugin.log("", e);
    } catch (InvalidInputException e) {
        TagSEAPlugin.log("", e);
    }

    return new IRegion[0];
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringRefactoring.java

License:Open Source License

/**
 * Try to find the selected Java element in the compilation unit.
 *
 * If selection matches a string literal, capture it, otherwise add a fatal error
 * to the status./*  w  ww.  jav  a  2 s. c om*/
 *
 * On success, advance the monitor by 3.
 * Returns status.isOK().
 */
private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status,
        IProgressMonitor monitor) {
    try {
        IBuffer buffer = unit.getBuffer();

        IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments
                false, //tokenizeWhiteSpace
                false, //assertMode
                false //recordLineSeparator
        );
        scanner.setSource(buffer.getCharacters());
        monitor.worked(1);

        for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner
                .getNextToken()) {
            if (scanner.getCurrentTokenStartPosition() <= mSelectionStart
                    && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) {
                // found the token, but only keep if the right type
                if (token == ITerminalSymbols.TokenNameStringLiteral) {
                    mTokenString = new String(scanner.getCurrentTokenSource());
                }
                break;
            } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) {
                // scanner is past the selection, abort.
                break;
            }
        }
    } catch (JavaModelException e1) {
        // Error in unit.getBuffer. Ignore.
    } catch (InvalidInputException e2) {
        // Error in scanner.getNextToken. Ignore.
    } finally {
        monitor.worked(1);
    }

    if (mTokenString != null) {
        // As a literal string, the token should have surrounding quotes. Remove them.
        // Note: unquoteAttrValue technically removes either " or ' paired quotes, whereas
        // the Java token should only have " quotes. Since we know the type to be a string
        // literal, there should be no confusion here.
        mTokenString = unquoteAttrValue(mTokenString);

        // We need a non-empty string literal
        if (mTokenString.length() == 0) {
            mTokenString = null;
        }
    }

    if (mTokenString == null) {
        status.addFatalError("Please select a Java string literal.");
    }

    monitor.worked(1);
    return status.isOK();
}

From source file:com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringRefactoring.java

License:Open Source License

/**
 * Try to find the selected Java element in the compilation unit.
 * //from   w ww.j  a  v a2s  .c o  m
 * If selection matches a string literal, capture it, otherwise add a fatal error
 * to the status.
 * 
 * On success, advance the monitor by 3.
 */
private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status,
        IProgressMonitor monitor) {
    try {
        IBuffer buffer = unit.getBuffer();

        IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments
                false, //tokenizeWhiteSpace
                false, //assertMode
                false //recordLineSeparator
        );
        scanner.setSource(buffer.getCharacters());
        monitor.worked(1);

        for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner
                .getNextToken()) {
            if (scanner.getCurrentTokenStartPosition() <= mSelectionStart
                    && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) {
                // found the token, but only keep of the right type
                if (token == ITerminalSymbols.TokenNameStringLiteral) {
                    mTokenString = new String(scanner.getCurrentTokenSource());
                }
                break;
            } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) {
                // scanner is past the selection, abort.
                break;
            }
        }
    } catch (JavaModelException e1) {
        // Error in unit.getBuffer. Ignore.
    } catch (InvalidInputException e2) {
        // Error in scanner.getNextToken. Ignore.
    } finally {
        monitor.worked(1);
    }

    if (mTokenString != null) {
        // As a literal string, the token should have surrounding quotes. Remove them.
        int len = mTokenString.length();
        if (len > 0 && mTokenString.charAt(0) == '"' && mTokenString.charAt(len - 1) == '"') {
            mTokenString = mTokenString.substring(1, len - 1);
        }
        // We need a non-empty string literal
        if (mTokenString.length() == 0) {
            mTokenString = null;
        }
    }

    if (mTokenString == null) {
        status.addFatalError("Please select a Java string literal.");
    }

    monitor.worked(1);
    return status.isOK();
}

From source file:com.cb.eclipse.folding.java.calculation.JavaProjectionCalculator.java

License:Open Source License

/**
 * The core logic of this class./* w  ww.j  a va2s .  c  om*/
 * 
 * Computes using the strategy objects what regions are available as
 * projections for this particular element.
 * 
 * @param elem
 * @return @throws
 *         JavaModelException
 * @throws InvalidInputException
 */
private Set computeProjections(IJavaElement elem) throws JavaModelException, InvalidInputException {
    if (!(elem instanceof ISourceReference))
        return null;

    RegionCalculationStrategy strategy = StrategyFactory.instance(this, elem);
    strategy.initialize();
    Set regionSet;
    if (!strategy.shouldScan(elem)) {
        regionSet = strategy.result(); // call immediately...
    } else {
        ISourceReference reference = (ISourceReference) elem;
        ISourceRange range = reference.getSourceRange();

        // All other element types require some parsing...
        String contents = reference.getSource();

        if (contents == null)
            return null;

        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        scanner.setSource(contents.toCharArray());

        int shift = range.getOffset();
        int start = shift;

        while (true) {
            int token = scanner.getNextToken();

            start = shift + scanner.getCurrentTokenStartPosition();
            int end = shift + scanner.getCurrentTokenEndPosition() + 1;

            if (!strategy.keepProcessing(token) || token == ITerminalSymbols.TokenNameEOF) {
                break; // end case.
            }

            strategy.handle(token, start, end, elem);

        }

        strategy.postScan(start, elem);

        regionSet = strategy.result();

    }

    if (regionSet == null) {
        regionSet = Collections.EMPTY_SET;
    }

    strategy.dispose();

    return regionSet;

}

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

License:Open Source License

public ISourceRange getJavadocRange() throws JavaModelException {
    ISourceRange range = getSourceRange();
    if (range == null)
        return null;
    IBuffer buf = null;// w w  w.  ja  v  a 2 s . c o  m
    if (isBinary()) {
        buf = getClassFile().getBuffer();
    } else {
        ICompilationUnit compilationUnit = getCompilationUnit();
        if (!compilationUnit.isConsistent()) {
            return null;
        }
        buf = compilationUnit.getBuffer();
    }
    final int start = range.getOffset();
    final int length = range.getLength();
    if (length > 0 && buf.getChar(start) == '/') {
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        try {
            scanner.setSource(buf.getText(start, length).toCharArray());
            int docOffset = -1;
            int docEnd = -1;

            int terminal = scanner.getNextToken();
            loop: while (true) {
                switch (terminal) {
                case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
                    docOffset = scanner.getCurrentTokenStartPosition();
                    docEnd = scanner.getCurrentTokenEndPosition() + 1;
                    terminal = scanner.getNextToken();
                    break;
                case ITerminalSymbols.TokenNameCOMMENT_LINE:
                case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
                    terminal = scanner.getNextToken();
                    continue loop;
                default:
                    break loop;
                }
            }
            if (docOffset != -1) {
                return new SourceRange(docOffset + start, docEnd - docOffset);
            }
        } catch (InvalidInputException ex) {
            // try if there is inherited Javadoc
        } catch (IndexOutOfBoundsException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001
        }
    }
    return null;
}

From source file:com.github.elucash.lambda4jdt.FoldingStructureProvider.java

License:Open Source License

/**
 * Computes the projection ranges for a given <code>ISourceReference</code>. More than one range
 * or none at all may be returned. If there are no foldable regions, an empty array is returned.
 * <p>//ww  w .  ja va  2  s  .  c om
 * The last region in the returned array (if not empty) describes the region for the java element
 * that implements the source reference. Any preceding regions describe javadoc comments of that
 * java element.
 * </p>
 * @param reference a java element that is a source reference
 * @param ctx the folding context
 * @return the regions to be folded
 */
protected final IRegion[] computeProjectionRanges(ISourceReference reference,
        FoldingStructureComputationContext ctx) {
    try {
        ISourceRange range = reference.getSourceRange();
        if (!SourceRange.isAvailable(range))
            return new IRegion[0];

        String contents = reference.getSource();
        if (contents == null)
            return new IRegion[0];

        List<IRegion> regions = new ArrayList<IRegion>();
        if (!ctx.hasFirstType() && reference instanceof IType) {
            ctx.setFirstType((IType) reference);
            IRegion headerComment = computeHeaderComment(ctx);
            if (headerComment != null) {
                regions.add(headerComment);
                ctx.setHasHeaderComment();
            }
        }

        final int shift = range.getOffset();
        IScanner scanner = ctx.getScanner();
        scanner.resetTo(shift, shift + range.getLength());

        int start = shift;
        while (true) {

            int token = scanner.getNextToken();
            start = scanner.getCurrentTokenStartPosition();

            switch (token) {
            case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
            case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
                int end = scanner.getCurrentTokenEndPosition() + 1;
                regions.add(new Region(start, end - start));
                continue;
            }
            case ITerminalSymbols.TokenNameCOMMENT_LINE:
                continue;
            }

            break;
        }

        regions.add(new Region(start, shift + range.getLength() - start));

        return regions.toArray(new IRegion[regions.size()]);
    } catch (JavaModelException e) {
    } catch (InvalidInputException e) {
    }

    return new IRegion[0];
}

From source file:com.github.elucash.lambda4jdt.FoldingStructureProvider.java

License:Open Source License

private IRegion computeHeaderComment(FoldingStructureComputationContext ctx) throws JavaModelException {
    // search at most up to the first type
    ISourceRange range = ctx.getFirstType().getSourceRange();
    if (range == null)
        return null;
    int start = 0;
    int end = range.getOffset();

    /* code adapted from CommentFormattingStrategy:
     * scan the header content up to the first type. Once a comment is
     * found, accumulate any additional comments up to the stop condition.
     * The stop condition is reaching a package declaration, import container,
     * or the end of the input./*  w  w  w.java2  s.c  om*/
     */
    IScanner scanner = ctx.getScanner();
    scanner.resetTo(start, end);

    int headerStart = -1;
    int headerEnd = -1;
    try {
        boolean foundComment = false;
        int terminal = scanner.getNextToken();
        while (terminal != ITerminalSymbols.TokenNameEOF && !(terminal == ITerminalSymbols.TokenNameclass
                || terminal == ITerminalSymbols.TokenNameinterface || terminal == ITerminalSymbols.TokenNameenum
                || (foundComment && (terminal == ITerminalSymbols.TokenNameimport
                        || terminal == ITerminalSymbols.TokenNamepackage)))) {

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                if (!foundComment)
                    headerStart = scanner.getCurrentTokenStartPosition();
                headerEnd = scanner.getCurrentTokenEndPosition();
                foundComment = true;
            }
            terminal = scanner.getNextToken();
        }

    } catch (InvalidInputException ex) {
        return null;
    }

    if (headerEnd != -1) {
        return new Region(headerStart, headerEnd - headerStart);
    }
    return null;
}

From source file:com.google.gdt.eclipse.core.TypeCreator.java

License:Open Source License

private boolean isValidComment(String template) {
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    scanner.setSource(template.toCharArray());
    try {//w w  w. java2  s  . c  o m
        int next = scanner.getNextToken();
        while (TokenScanner.isComment(next)) {
            next = scanner.getNextToken();
        }
        return next == ITerminalSymbols.TokenNameEOF;
    } catch (InvalidInputException e) {
        // If there are lexical errors, the comment is invalid
    }
    return false;
}