Class Scanner
- java.lang.Object
-
- org.codehaus.janino.Scanner
-
public class Scanner extends java.lang.Object
Splits up a character stream into tokens and returns them asString
objects.The
optionalFileName
parameter passed to many constructors should point
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
Scanner.Token
Representation of a Java™ token.
-
Field Summary
Fields Modifier and Type Field Description private boolean
crLfPending
private java.lang.String
docComment
The optional JAVADOC comment preceding the#nextToken
.private boolean
expectGreater
Whether the scanner is in 'expect greater' mode: If so, it parses character sequences like ">>>=" as ">", ">", ">", "=".private java.io.Reader
in
private static java.util.Map<java.lang.String,java.lang.String>
JAVA_KEYWORDS
private static java.util.Map<java.lang.String,java.lang.String>
JAVA_OPERATORS
private static java.util.Map<java.lang.String,java.lang.String>
JAVA_OPERATORS_EXPECT_GREATER
private int
nextChar
private short
nextCharColumnNumber
private short
nextCharLineNumber
private java.lang.String
optionalFileName
private WarningHandler
optionalWarningHandler
private short
tokenColumnNumber
Column number of the first character of the previously produced token (1 if token is immediately preceded by a line break).private short
tokenLineNumber
Line number of the previously produced token (typically starting at one).
-
Constructor Summary
Constructors Constructor Description Scanner(java.io.File file)
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedScanner(java.io.File file, java.lang.String optionalEncoding)
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedScanner(java.lang.String fileName)
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedScanner(java.lang.String optionalFileName, java.io.InputStream is)
Set up a scanner that reads tokens from the givenInputStream
in the platform default encoding.Scanner(java.lang.String optionalFileName, java.io.InputStream is, java.lang.String optionalEncoding)
Set up a scanner that reads tokens from the givenInputStream
with the givenoptionalEncoding
(null
means platform default encoding).Scanner(java.lang.String optionalFileName, java.io.Reader in)
Set up a scanner that reads tokens from the givenReader
.Scanner(java.lang.String optionalFileName, java.io.Reader in, short initialLineNumber, short initialColumnNumber)
Creates aScanner
that counts lines and columns from non-default initial values.Scanner(java.lang.String fileName, java.lang.String encoding)
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecated
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
close()
Deprecated.This method is deprecated, because the concept described above is confusing.java.lang.String
doc()
Get the text of the doc comment (a.k.a.boolean
getExpectGreater()
java.lang.String
getFileName()
Location
location()
Scanner.Token
produce()
Preduces and returns the next token.private void
readNextChar()
private void
scanLiteralCharacter(java.lang.StringBuilder sb)
Scans the next literal character into aStringBuilder
.private Scanner.Token
scanNumericLiteral(boolean hadDecimalPoint)
boolean
setExpectGreater(boolean value)
Sets or resets the 'expect greater' mode.void
setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install aWarningHandler
.private void
warning(java.lang.String handle, java.lang.String message, Location optionalLocation)
Issues a warning with the given message and location and returns.
-
-
-
Field Detail
-
optionalFileName
private final java.lang.String optionalFileName
-
in
private final java.io.Reader in
-
nextChar
private int nextChar
-
crLfPending
private boolean crLfPending
-
nextCharLineNumber
private short nextCharLineNumber
-
nextCharColumnNumber
private short nextCharColumnNumber
-
tokenLineNumber
private short tokenLineNumber
Line number of the previously produced token (typically starting at one).
-
tokenColumnNumber
private short tokenColumnNumber
Column number of the first character of the previously produced token (1 if token is immediately preceded by a line break).
-
docComment
private java.lang.String docComment
The optional JAVADOC comment preceding the#nextToken
.
-
expectGreater
private boolean expectGreater
Whether the scanner is in 'expect greater' mode: If so, it parses character sequences like ">>>=" as ">", ">", ">", "=".
-
JAVA_KEYWORDS
private static final java.util.Map<java.lang.String,java.lang.String> JAVA_KEYWORDS
-
JAVA_OPERATORS
private static final java.util.Map<java.lang.String,java.lang.String> JAVA_OPERATORS
-
JAVA_OPERATORS_EXPECT_GREATER
private static final java.util.Map<java.lang.String,java.lang.String> JAVA_OPERATORS_EXPECT_GREATER
-
optionalWarningHandler
private WarningHandler optionalWarningHandler
-
-
Constructor Detail
-
Scanner
@Deprecated public Scanner(java.lang.String fileName) throws CompileException, java.io.IOException
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedSet up a scanner that reads tokens from the given file in the default charset.This method is deprecated because it leaves the input file open.
- Throws:
CompileException
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.lang.String fileName, java.lang.String encoding) throws CompileException, java.io.IOException
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedSet up a scanner that reads tokens from the given file in the given encoding.This method is deprecated because it leaves the input file open.
- Throws:
CompileException
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.io.File file) throws CompileException, java.io.IOException
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedSet up a scanner that reads tokens from the given file in the platform default encoding.This method is deprecated because it leaves the input file open.
- Throws:
CompileException
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.io.File file, java.lang.String optionalEncoding) throws CompileException, java.io.IOException
Deprecated.// SUPPRESS CHECKSTYLE MissingDeprecatedSet up a scanner that reads tokens from the given file in the given encoding.This method is deprecated because it leaves the input file open.
- Throws:
CompileException
java.io.IOException
-
Scanner
public Scanner(java.lang.String optionalFileName, java.io.InputStream is) throws CompileException, java.io.IOException
Set up a scanner that reads tokens from the givenInputStream
in the platform default encoding.The
fileName
is solely used for reporting in thrown exceptions.- Throws:
CompileException
java.io.IOException
-
Scanner
public Scanner(java.lang.String optionalFileName, java.io.InputStream is, java.lang.String optionalEncoding) throws CompileException, java.io.IOException
Set up a scanner that reads tokens from the givenInputStream
with the givenoptionalEncoding
(null
means platform default encoding).The
optionalFileName
is used for reporting errors during compilation and for source level debugging, and should name an existing file. Ifnull
is passed, and the system propertyorg.codehaus.janino.source_debugging.enable
is set to "true", then a temporary file inorg.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.- Throws:
CompileException
java.io.IOException
-
Scanner
public Scanner(java.lang.String optionalFileName, java.io.Reader in) throws CompileException, java.io.IOException
Set up a scanner that reads tokens from the givenReader
.The
optionalFileName
is used for reporting errors during compilation and for source level debugging, and should name an existing file. Ifnull
is passed, and the system propertyorg.codehaus.janino.source_debugging.enable
is set to "true", then a temporary file inorg.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.- Throws:
CompileException
java.io.IOException
-
Scanner
public Scanner(java.lang.String optionalFileName, java.io.Reader in, short initialLineNumber, short initialColumnNumber) throws CompileException, java.io.IOException
Creates aScanner
that counts lines and columns from non-default initial values.- Throws:
CompileException
java.io.IOException
-
-
Method Detail
-
getFileName
public java.lang.String getFileName()
- Returns:
- The file name optionally passed to the constructor
-
close
@Deprecated public void close() throws java.io.IOException
Deprecated.This method is deprecated, because the concept described above is confusing. An application should close the underlyingInputStream
orReader
itself.Closes the character source (file,InputStream
,Reader
) associated with this object. The results of future calls toproduce()
are undefined.- Throws:
java.io.IOException
-
doc
public java.lang.String doc()
Get the text of the doc comment (a.k.a. "JAVADOC comment") preceeding the next token.- Returns:
null
if the next token is not preceeded by a doc comment
-
produce
public Scanner.Token produce() throws CompileException, java.io.IOException
Preduces and returns the next token. Notice that end-of-input is not signalized with anull
product, but by the specialScanner.Token.EOF
token.- Throws:
CompileException
java.io.IOException
-
getExpectGreater
public boolean getExpectGreater()
- Returns:
- Whether the scanner is currently in 'expect greater' mode
-
setExpectGreater
public boolean setExpectGreater(boolean value)
Sets or resets the 'expect greater' mode.- Returns:
- Whether the 'expect greater' mode was previously active
-
scanNumericLiteral
private Scanner.Token scanNumericLiteral(boolean hadDecimalPoint) throws CompileException, java.io.IOException
- Throws:
CompileException
java.io.IOException
-
scanLiteralCharacter
private void scanLiteralCharacter(java.lang.StringBuilder sb) throws CompileException, java.io.IOException
Scans the next literal character into aStringBuilder
.- Throws:
CompileException
java.io.IOException
-
readNextChar
private void readNextChar() throws java.io.IOException, CompileException
- Throws:
java.io.IOException
CompileException
-
setWarningHandler
public void setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install aWarningHandler
.Notice that there is no
Scanner.setErrorHandler()
method, but scan errors always throw aCompileException
. The reason being is that there is no reasonable way to recover from scan errors and continue scanning, so there is no need to install a custom scan error handler.- Parameters:
optionalWarningHandler
-null
to indicate that no warnings be issued
-
warning
private void warning(java.lang.String handle, java.lang.String message, Location optionalLocation) throws CompileException
Issues a warning with the given message and location and returns. This is done through aWarningHandler
that was installed throughsetWarningHandler(WarningHandler)
.The
handle
argument qulifies the warning and is typically used by theWarningHandler
to suppress individual warnings.- Throws:
CompileException
-
-