/projects/aspectj-1.6.9/aspectjtools1.6.9/org/aspectj/org/eclipse/jdt/internal/core/dom/rewrite/TokenScanner.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 238 lines · 113 code · 23 blank · 102 comment · 14 complexity · 58aaa0149dffd625877de9c410850e3f MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2006 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.org.eclipse.jdt.internal.core.dom.rewrite;
  12. import org.eclipse.core.runtime.CoreException;
  13. import org.eclipse.core.runtime.IStatus;
  14. import org.eclipse.core.runtime.Status;
  15. import org.aspectj.org.eclipse.jdt.core.JavaCore;
  16. import org.aspectj.org.eclipse.jdt.core.compiler.IScanner;
  17. import org.aspectj.org.eclipse.jdt.core.compiler.ITerminalSymbols;
  18. import org.aspectj.org.eclipse.jdt.core.compiler.InvalidInputException;
  19. /**
  20. * Wraps a scanner and offers convenient methods for finding tokens
  21. */
  22. public class TokenScanner {
  23. public static final int END_OF_FILE= 20001;
  24. public static final int LEXICAL_ERROR= 20002;
  25. public static final int DOCUMENT_ERROR= 20003;
  26. private final IScanner scanner;
  27. private final int endPosition;
  28. /**
  29. * Creates a TokenScanner
  30. * @param scanner The scanner to be wrapped
  31. */
  32. public TokenScanner(IScanner scanner) {
  33. this.scanner= scanner;
  34. this.endPosition= this.scanner.getSource().length - 1;
  35. }
  36. /**
  37. * Returns the wrapped scanner
  38. * @return IScanner
  39. */
  40. public IScanner getScanner() {
  41. return this.scanner;
  42. }
  43. /**
  44. * Sets the scanner offset to the given offset.
  45. * @param offset The offset to set
  46. */
  47. public void setOffset(int offset) {
  48. this.scanner.resetTo(offset, this.endPosition);
  49. }
  50. /**
  51. * @return Returns the offset after the current token
  52. */
  53. public int getCurrentEndOffset() {
  54. return this.scanner.getCurrentTokenEndPosition() + 1;
  55. }
  56. /**
  57. * @return Returns the start offset of the current token
  58. */
  59. public int getCurrentStartOffset() {
  60. return this.scanner.getCurrentTokenStartPosition();
  61. }
  62. /**
  63. * @return Returns the length of the current token
  64. */
  65. public int getCurrentLength() {
  66. return getCurrentEndOffset() - getCurrentStartOffset();
  67. }
  68. /**
  69. * Reads the next token.
  70. * @param ignoreComments If set, comments will be overread
  71. * @return Return the token id.
  72. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  73. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  74. */
  75. public int readNext(boolean ignoreComments) throws CoreException {
  76. int curr= 0;
  77. do {
  78. try {
  79. curr= this.scanner.getNextToken();
  80. if (curr == ITerminalSymbols.TokenNameEOF) {
  81. throw new CoreException(createError(END_OF_FILE, "End Of File", null)); //$NON-NLS-1$
  82. }
  83. } catch (InvalidInputException e) {
  84. throw new CoreException(createError(LEXICAL_ERROR, e.getMessage(), e));
  85. }
  86. } while (ignoreComments && isComment(curr));
  87. return curr;
  88. }
  89. /**
  90. * Reads the next token from the given offset.
  91. * @param offset The offset to start reading from.
  92. * @param ignoreComments If set, comments will be overread.
  93. * @return Returns the token id.
  94. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  95. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  96. */
  97. public int readNext(int offset, boolean ignoreComments) throws CoreException {
  98. setOffset(offset);
  99. return readNext(ignoreComments);
  100. }
  101. /**
  102. * Reads the next token from the given offset and returns the start offset of the token.
  103. * @param offset The offset to start reading from.
  104. * @param ignoreComments If set, comments will be overread
  105. * @return Returns the start position of the next token.
  106. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  107. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  108. */
  109. public int getNextStartOffset(int offset, boolean ignoreComments) throws CoreException {
  110. readNext(offset, ignoreComments);
  111. return getCurrentStartOffset();
  112. }
  113. /**
  114. * Reads the next token from the given offset and returns the offset after the token.
  115. * @param offset The offset to start reading from.
  116. * @param ignoreComments If set, comments will be overread
  117. * @return Returns the start position of the next token.
  118. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  119. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  120. */
  121. public int getNextEndOffset(int offset, boolean ignoreComments) throws CoreException {
  122. readNext(offset, ignoreComments);
  123. return getCurrentEndOffset();
  124. }
  125. /**
  126. * Reads until a token is reached.
  127. * @param tok The token to read to.
  128. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  129. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  130. */
  131. public void readToToken(int tok) throws CoreException {
  132. int curr= 0;
  133. do {
  134. curr= readNext(false);
  135. } while (curr != tok);
  136. }
  137. /**
  138. * Reads until a token is reached, starting from the given offset.
  139. * @param tok The token to read to.
  140. * @param offset The offset to start reading from.
  141. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  142. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  143. */
  144. public void readToToken(int tok, int offset) throws CoreException {
  145. setOffset(offset);
  146. readToToken(tok);
  147. }
  148. /**
  149. * Reads from the given offset until a token is reached and returns the start offset of the token.
  150. * @param token The token to be found.
  151. * @param startOffset The offset to start reading from.
  152. * @return Returns the start position of the found token.
  153. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  154. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  155. */
  156. public int getTokenStartOffset(int token, int startOffset) throws CoreException {
  157. readToToken(token, startOffset);
  158. return getCurrentStartOffset();
  159. }
  160. /**
  161. * Reads from the given offset until a token is reached and returns the offset after the token.
  162. * @param token The token to be found.
  163. * @param startOffset Offset to start reading from
  164. * @return Returns the end position of the found token.
  165. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  166. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  167. */
  168. public int getTokenEndOffset(int token, int startOffset) throws CoreException {
  169. readToToken(token, startOffset);
  170. return getCurrentEndOffset();
  171. }
  172. /**
  173. * Reads from the given offset until a token is reached and returns the offset after the previous token.
  174. * @param token The token to be found.
  175. * @param startOffset The offset to start scanning from.
  176. * @return Returns the end offset of the token previous to the given token.
  177. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE)
  178. * or a lexical error was detected while scanning (code LEXICAL_ERROR)
  179. */
  180. public int getPreviousTokenEndOffset(int token, int startOffset) throws CoreException {
  181. setOffset(startOffset);
  182. int res= startOffset;
  183. int curr= readNext(false);
  184. while (curr != token) {
  185. res= getCurrentEndOffset();
  186. curr= readNext(false);
  187. }
  188. return res;
  189. }
  190. public static boolean isComment(int token) {
  191. return token == ITerminalSymbols.TokenNameCOMMENT_BLOCK || token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
  192. || token == ITerminalSymbols.TokenNameCOMMENT_LINE;
  193. }
  194. public static boolean isModifier(int token) {
  195. switch (token) {
  196. case ITerminalSymbols.TokenNamepublic:
  197. case ITerminalSymbols.TokenNameprotected:
  198. case ITerminalSymbols.TokenNameprivate:
  199. case ITerminalSymbols.TokenNamestatic:
  200. case ITerminalSymbols.TokenNamefinal:
  201. case ITerminalSymbols.TokenNameabstract:
  202. case ITerminalSymbols.TokenNamenative:
  203. case ITerminalSymbols.TokenNamevolatile:
  204. case ITerminalSymbols.TokenNamestrictfp:
  205. case ITerminalSymbols.TokenNametransient:
  206. case ITerminalSymbols.TokenNamesynchronized:
  207. return true;
  208. default:
  209. return false;
  210. }
  211. }
  212. public static IStatus createError(int code, String message, Throwable throwable) {
  213. return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, code, message, throwable);
  214. }
  215. }