1 /**********************************************************************
2 Copyright (c) 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v0.5
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v05.html
9 IBM Corporation - initial API and implementation
10 **********************************************************************/
12 package net.sourceforge.phpdt.core.compiler;
15 * Definition of a Java scanner, as returned by the <code>ToolFactory</code>.
16 * The scanner is responsible for tokenizing a given source, providing information about
17 * the nature of the token read, its positions and source equivalent.
19 * When the scanner has finished tokenizing, it answers an EOF token (<code>
20 * ITerminalSymbols#TokenNameEOF</code>.
22 * When encountering lexical errors, an <code>InvalidInputException</code> is thrown.
24 * @see org.eclipse.jdt.core.ToolFactory
25 * @see ITerminalSymbols
28 public interface IScanner {
31 * Answers the current identifier source, after unicode escape sequences have
32 * been translated into unicode characters.
33 * e.g. if original source was <code>\\u0061bc</code> then it will answer <code>abc</code>.
35 * @return the current identifier source, after unicode escape sequences have
36 * been translated into unicode characters
38 char[] getCurrentTokenSource();
41 * Answers the starting position of the current token inside the original source.
42 * This position is zero-based and inclusive. It corresponds to the position of the first character
43 * which is part of this token. If this character was a unicode escape sequence, it points at the first
44 * character of this sequence.
46 * @return the starting position of the current token inside the original source
48 int getCurrentTokenStartPosition();
51 * Answers the ending position of the current token inside the original source.
52 * This position is zero-based and inclusive. It corresponds to the position of the last character
53 * which is part of this token. If this character was a unicode escape sequence, it points at the last
54 * character of this sequence.
56 * @return the ending position of the current token inside the original source
58 int getCurrentTokenEndPosition();
61 * Answers the starting position of a given line number. This line has to have been encountered
62 * already in the tokenization process (i.e. it cannot be used to compute positions of lines beyond
63 * current token). Once the entire source has been processed, it can be used without any limit.
64 * Line starting positions are zero-based, and start immediately after the previous line separator (if any).
66 * @param lineNumber the given line number
67 * @return the starting position of a given line number
69 int getLineStart(int lineNumber);
72 * Answers the ending position of a given line number. This line has to have been encountered
73 * already in the tokenization process (i.e. it cannot be used to compute positions of lines beyond
74 * current token). Once the entire source has been processed, it can be used without any limit.
75 * Line ending positions are zero-based, and correspond to the last character of the line separator
76 * (in case multi-character line separators).
78 * @param lineNumber the given line number
79 * @return the ending position of a given line number
81 int getLineEnd(int lineNumber);
84 * Answers an array of the ending positions of the lines encountered so far. Line ending positions
85 * are zero-based, and correspond to the last character of the line separator (in case multi-character
88 * @return an array of the ending positions of the lines encountered so far
93 * Answers a 1-based line number using the lines which have been encountered so far. If the position
94 * is located beyond the current scanned line, then the last line number will be answered.
96 * @param charPosition the given character position
97 * @return a 1-based line number using the lines which have been encountered so far
99 int getLineNumber(int charPosition);
102 * Read the next token in the source, and answers its ID as specified by <code>ITerminalSymbols</code>.
103 * Note that the actual token ID values are subject to change if new keywords were added to the language
104 * (i.e. 'assert' keyword in 1.4).
106 * @throws InvalidInputException - in case a lexical error was detected while reading the current token
108 int getNextToken() throws InvalidInputException;
111 * Answers the original source being processed (not a copy of it).
113 * @return the original source being processed
118 * Reposition the scanner on some portion of the original source. Once reaching the given <code>endPosition</code>
119 * it will answer EOF tokens (<code>ITerminalSymbols.TokenNameEOF</code>).
121 * @param startPosition the given start position
122 * @param endPosition the given end position
124 void resetTo(int startPosition, int endPosition);
127 * Set the scanner source to process. By default, the scanner will consider starting at the beginning of the
128 * source until it reaches its end.
130 * @param source the given source
132 void setSource(char[] source);