Top Description Fields Constructors Methods
org.codehaus.janino

public Class Scanner

extends Object
Class Inheritance
Imports
java.io.File, .FileInputStream, .FileWriter, .IOException, .InputStream, .InputStreamReader, .Reader, java.util.Arrays, .HashSet, .Set, org.codehaus.commons.compiler.CompileException, .Location, org.codehaus.commons.compiler.io.Readers, org.codehaus.commons.nullanalysis.Nullable

Splits up a character stream into tokens and returns them as String objects.

Field Summary

Modifier and TypeField and Description
private boolean
private final String
private boolean
private final Reader
private static final Set<String>
private static final Set<String>
private int
private int
private int
private int
private final StringBuilder
sb

Holds the characters of the currently scanned token.

public static final String
SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR

If the source code is not read from a file, debuggers have a hard time locating the source file for source-level debugging.

public static final String
SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE

Setting this system property to 'true' enables source-level debugging.

public static final String
SYSTEM_PROPERTY_SOURCE_DEBUGGING_KEEP

If set to "true", then the temporary source code files are not deleted on exit.

private int
tokenColumnNumber

Column number of the first character of the previously produced token (1 if token is immediately preceded by a line break).

private int
tokenLineNumber

Line number of the previously produced token (typically starting at one).

Constructor Summary

AccessConstructor and Description
public
Scanner(String fileName)

Deprecated This method is deprecated because it leaves the input file open
public
Scanner(String fileName, String encoding)

Deprecated This method is deprecated because it leaves the input file open
public
Scanner(File file)

Deprecated This method is deprecated because it leaves the input file open
public
Scanner(File file, String encoding)

Deprecated This method is deprecated because it leaves the input file open
public
Scanner(String fileName, InputStream is)

Sets up a scanner that reads tokens from the given InputStream in the platform default encoding.

public
Scanner(String fileName, InputStream is, String encoding)

Sets up a scanner that reads tokens from the given InputStream with the given encoding (null means platform default encoding).

public
Scanner(String fileName, Reader in)

Sets up a scanner that reads tokens from the given Reader.

public
Scanner(String fileName, Reader in, int initialLineNumber, int initialColumnNumber)

Creates a Scanner that counts lines and columns from non-default initial values.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Deprecated This method is deprecated, because the concept described above is confusing.
Closes the character source (file, InputStream, Reader) associated with this object.
public String

Returns:

The file name optionally passed to the constructor
getFileName
()

private int

Returns:

-1 iff there is no next character, i.e the input stream is at end-of-input
internalRead
()

private static boolean
private static boolean
isDecimalDigit(int c)

To comply with the JLS, this method does not allow for non-latin digit (like Character#isDigit(char) does).

private static boolean
isHexDigit(int c)

To comply with the JLS, this method does not allow for non-latin digit (like Character#isDigit(char) does).

private static boolean
isOctalDigit(int c)

public Location

Returns:

The Location of the previously read (or peeked) token.
location
()

private int

Returns:

-1 iff there is no next character, i.e the input stream is at end-of-input
peek
()

Returns the next character, but does not consume it.

private boolean
peek(String expectedCharacters)

Checks whether the next character is one of the expectedCharacters.

private int
peekButOne()

Returns the next-but-one character, but does not consume any characters.

private boolean

Returns:

Whether the next character equalled the expected character
peekRead
(int expected)

Consumes the next character iff it equals the expected character.

private boolean

Returns:

Whether the next character was one of the expectedCharacters
peekRead
(String expectedCharacters)

Consumes the next character iff it is one of the expectedCharacters

public Token
produce()

Produces and returns the next token.

private char
read()

Consumes and returns the next character.

private TokenType
scan()

private void
scanLiteralCharacter()

Scans the next literal character into a StringBuilder.

private TokenType
public void
setIgnoreWhiteSpace(boolean value)

If value is true, then white space in the input stream is ignored, rather than scanned as a TokenType#WHITE_SPACE token.

private Token
token(TokenType type, String value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

crLfPendingback to summary
private boolean crLfPending
fileNameback to summary
private final String fileName
Annotations
@Nullable
ignoreWhiteSpaceback to summary
private boolean ignoreWhiteSpace
inback to summary
private final Reader in
JAVA_KEYWORDSback to summary
private static final Set<String> JAVA_KEYWORDS
JAVA_OPERATORSback to summary
private static final Set<String> JAVA_OPERATORS
nextButOneCharback to summary
private int nextButOneChar
nextCharback to summary
private int nextChar
nextCharColumnNumberback to summary
private int nextCharColumnNumber
nextCharLineNumberback to summary
private int nextCharLineNumber
sbback to summary
private final StringBuilder sb

Holds the characters of the currently scanned token.

SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIRback to summary
public static final String SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR

If the source code is not read from a file, debuggers have a hard time locating the source file for source-level debugging. As a workaround, a copy of the source code is written to a temporary file, which must be included in the debugger's source path. If this system property is set, the temporary source file is created in that directory, otherwise in the default temporary-file directory.

See Also
File#createTempFile(String, String, File)
SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLEback to summary
public static final String SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE

Setting this system property to 'true' enables source-level debugging. Typically, this means that compilation is executed with "-g:all" instead of "-g:none".

SYSTEM_PROPERTY_SOURCE_DEBUGGING_KEEPback to summary
public static final String SYSTEM_PROPERTY_SOURCE_DEBUGGING_KEEP

If set to "true", then the temporary source code files are not deleted on exit. That may be useful to interpret stack traces offline.

tokenColumnNumberback to summary
private int tokenColumnNumber

Column number of the first character of the previously produced token (1 if token is immediately preceded by a line break).

tokenLineNumberback to summary
private int tokenLineNumber

Line number of the previously produced token (typically starting at one).

Constructor Detail

Scannerback to summary
public Scanner(String fileName) throws IOException

Deprecated

This method is deprecated because it leaves the input file open

Annotations
@Deprecated
Scannerback to summary
public Scanner(String fileName, String encoding) throws IOException

Deprecated

This method is deprecated because it leaves the input file open

Annotations
@Deprecated
Scannerback to summary
public Scanner(File file) throws IOException

Deprecated

This method is deprecated because it leaves the input file open

Annotations
@Deprecated
Scannerback to summary
public Scanner(File file, String encoding) throws IOException

Deprecated

This method is deprecated because it leaves the input file open

Annotations
@Deprecated
Scannerback to summary
public Scanner(String fileName, InputStream is) throws IOException

Sets up a scanner that reads tokens from the given InputStream in the platform default encoding.

The fileName is solely used for reporting in thrown exceptions.

Scannerback to summary
public Scanner(String fileName, InputStream is, String encoding) throws IOException

Sets up a scanner that reads tokens from the given InputStream with the given encoding (null means platform default encoding).

The fileName is used for reporting errors during compilation and for source level debugging, and should name an existing file. If null is passed, and the system property org.codehaus.janino.source_debugging.enable is set to "true", then a temporary file in org.codehaus.janino.source_debugging.dir or the system's default temp dir is created in order to make the source code available to a debugger.

Scannerback to summary
public Scanner(String fileName, Reader in) throws IOException

Sets up a scanner that reads tokens from the given Reader.

The fileName is used for reporting errors during compilation and for source level debugging, and should name an existing file. If null is passed, and the system property org.codehaus.janino.source_debugging.enable is set to "true", then a temporary file in org.codehaus.janino.source_debugging.dir or the system's default temp dir is created in order to make the source code available to a debugger.

Scannerback to summary
public Scanner(String fileName, Reader in, int initialLineNumber, int initialColumnNumber) throws IOException

Creates a Scanner that counts lines and columns from non-default initial values.

Method Detail

closeback to summary
public void close() throws IOException

Deprecated

This method is deprecated, because the concept described above is confusing. An application should close the underlying InputStream or Reader itself

Closes the character source (file, InputStream, Reader) associated with this object. The results of future calls to produce() are undefined.

Annotations
@Deprecated
getFileNameback to summary
public String getFileName()
Returns:String

The file name optionally passed to the constructor

Annotations
@Nullable
internalReadback to summary
private int internalRead() throws IOException, CompileException
Returns:int

-1 iff there is no next character, i.e the input stream is at end-of-input

isBinaryDigitback to summary
private static boolean isBinaryDigit(int c)
isDecimalDigitback to summary
private static boolean isDecimalDigit(int c)

To comply with the JLS, this method does not allow for non-latin digit (like Character#isDigit(char) does).

isHexDigitback to summary
private static boolean isHexDigit(int c)

To comply with the JLS, this method does not allow for non-latin digit (like Character#isDigit(char) does).

isOctalDigitback to summary
private static boolean isOctalDigit(int c)
locationback to summary
public Location location()
Returns:Location

The Location of the previously read (or peeked) token.

peekback to summary
private int peek() throws CompileException, IOException

Returns the next character, but does not consume it.

Returns:int

-1 iff there is no next character, i.e the input stream is at end-of-input

peekback to summary
private boolean peek(String expectedCharacters) throws CompileException, IOException

Checks whether the next character is one of the expectedCharacters.

It is not possible to peek exactly for "end-of-input"; use peekRead(-1) instead.

peekButOneback to summary
private int peekButOne() throws CompileException, IOException

Returns the next-but-one character, but does not consume any characters.

peekReadback to summary
private boolean peekRead(int expected) throws CompileException, IOException

Consumes the next character iff it equals the expected character.

Returns:boolean

Whether the next character equalled the expected character

peekReadback to summary
private boolean peekRead(String expectedCharacters) throws CompileException, IOException

Consumes the next character iff it is one of the expectedCharacters

Returns:boolean

Whether the next character was one of the expectedCharacters

produceback to summary
public Token produce() throws CompileException, IOException

Produces and returns the next token. Notice that end-of-input is not signalized with a null product, but by an TokenType#END_OF_INPUT-type token.

readback to summary
private char read() throws CompileException, IOException

Consumes and returns the next character.

Exceptions
CompileException:
There is no next charcter, i.e. the input stream is at end-of-input
scanback to summary
private TokenType scan() throws CompileException, IOException
scanLiteralCharacterback to summary
private void scanLiteralCharacter() throws CompileException, IOException

Scans the next literal character into a StringBuilder.

scanNumericLiteralback to summary
private TokenType scanNumericLiteral() throws CompileException, IOException
setIgnoreWhiteSpaceback to summary
public void setIgnoreWhiteSpace(boolean value)

If value is true, then white space in the input stream is ignored, rather than scanned as a TokenType#WHITE_SPACE token. Since white space is typically quite numerous, this optimization may save considerable overhead.

tokenback to summary
private Token token(TokenType type, String value)