<s> package org . eclipse . jdt . internal . core . dom . rewrite ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IStatus ; import org . eclipse . core . runtime . Status ; import org . eclipse . jdt . core . JavaCore ; import org . eclipse . jdt . core . compiler . InvalidInputException ; import org . eclipse . jdt . internal . compiler . parser . Scanner ; import org . eclipse . jdt . internal . compiler . parser . TerminalTokens ; public class TokenScanner { public static final int END_OF_FILE = 20001 ; public static final int LEXICAL_ERROR = 20002 ; public static final int DOCUMENT_ERROR = 20003 ; private final Scanner scanner ; private final int endPosition ; public TokenScanner ( Scanner scanner ) { this . scanner = scanner ; this . endPosition = this . scanner . getSource ( ) . length - 1 ; } public Scanner getScanner ( ) { return this . scanner ; } public void setOffset ( int offset ) { this . scanner . resetTo ( offset , this . endPosition ) ; } public int getCurrentEndOffset ( ) { return this . scanner . getCurrentTokenEndPosition ( ) + 1 ; } public int getCurrentStartOffset ( ) { return this . scanner . getCurrentTokenStartPosition ( ) ; } public int getCurrentLength ( ) { return getCurrentEndOffset ( ) - getCurrentStartOffset ( ) ; } public int readNext ( boolean ignoreComments ) throws CoreException { int curr = 0 ; do { try { curr = this . scanner . getNextToken ( ) ; if ( curr == TerminalTokens . TokenNameEOF ) { throw new CoreException ( createError ( END_OF_FILE , "End Of File" , null ) ) ; } } catch ( InvalidInputException e ) { throw new CoreException ( createError ( LEXICAL_ERROR , e . getMessage ( ) , e ) ) ; } } while ( ignoreComments && isComment ( curr ) ) ; return curr ; } public int readNext ( int offset , boolean ignoreComments ) throws CoreException { setOffset ( offset ) ; return readNext ( ignoreComments ) ; } public int getNextStartOffset ( int offset , boolean ignoreComments ) throws CoreException { readNext ( offset , ignoreComments ) ; return getCurrentStartOffset ( ) ; } public int getNextEndOffset ( int offset , boolean ignoreComments ) throws CoreException { readNext ( offset , ignoreComments ) ; return getCurrentEndOffset ( ) ; } public void readToToken ( int tok ) throws CoreException { int curr = 0 ; do { curr = readNext ( false ) ; } while ( curr != tok ) ; } public void readToToken ( int tok , int offset ) throws CoreException { setOffset ( offset ) ; readToToken ( tok ) ; } public int getTokenStartOffset ( int token , int startOffset ) throws CoreException { readToToken ( token , startOffset ) ; return getCurrentStartOffset ( ) ; } public int getTokenEndOffset ( int token , int startOffset ) throws CoreException { readToToken ( token , startOffset ) ; return getCurrentEndOffset ( ) ; } public int getPreviousTokenEndOffset ( int token , int startOffset ) throws CoreException { setOffset ( startOffset ) ; int res = startOffset ; int curr = readNext ( false ) ; while ( curr != token ) { res = getCurrentEndOffset ( ) ; curr = readNext ( false ) ; } return res ; } public static boolean isComment ( int token ) { return token == TerminalTokens . TokenNameCOMMENT_BLOCK || token == TerminalTokens . TokenNameCOMMENT_JAVADOC || token == TerminalTokens . TokenNameCOMMENT_LINE ; } public static boolean isModifier ( int token ) { switch ( token ) { case TerminalTokens . TokenNamepublic : case TerminalTokens . TokenNameprotected : case TerminalTokens . TokenNameprivate : case TerminalTokens . TokenNamestatic : case TerminalTokens . TokenNamefinal : case TerminalTokens . TokenNameabstract : case TerminalTokens . TokenNamenative : case TerminalTokens . TokenNamevolatile : case TerminalTokens . TokenNamestrictfp : case TerminalTokens . TokenNametransient : case TerminalTokens . TokenNamesynchronized : return true ; default : return false ; } } public static IStatus createError ( int code , String message , Throwable throwable ) { return new Status ( IStatus . ERROR , JavaCore . PLUGIN_ID , code , message , throwable ) ; } } </s>