Class Scanner
- java.lang.Object
-
- org.codehaus.janino.Scanner
-
public class Scanner extends java.lang.ObjectSplits up a character stream into tokens and returns them asStringobjects.
-
-
Field Summary
Fields Modifier and Type Field Description private booleancrLfPendingprivate java.lang.StringfileNameprivate booleanignoreWhiteSpaceprivate java.io.Readerinprivate static java.util.Set<java.lang.String>JAVA_KEYWORDSprivate static java.util.Set<java.lang.String>JAVA_OPERATORSprivate intnextButOneCharprivate intnextCharprivate intnextCharColumnNumberprivate intnextCharLineNumberprivate java.lang.StringBuildersbHolds the characters of the currently scanned token.static java.lang.StringSYSTEM_PROPERTY_SOURCE_DEBUGGING_DIRIf the source code is not read from a file, debuggers have a hard time locating the source file for source-level debugging.static java.lang.StringSYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLESetting this system property to 'true' enables source-level debugging.static java.lang.StringSYSTEM_PROPERTY_SOURCE_DEBUGGING_KEEPIf set to "true", then the temporary source code files are not deleted on exit.private inttokenColumnNumberColumn number of the first character of the previously produced token (1 if token is immediately preceded by a line break).private inttokenLineNumberLine number of the previously produced token (typically starting at one).
-
Constructor Summary
Constructors Constructor Description Scanner(java.io.File file)Deprecated.This method is deprecated because it leaves the input file openScanner(java.io.File file, java.lang.String encoding)Deprecated.This method is deprecated because it leaves the input file openScanner(java.lang.String fileName)Deprecated.This method is deprecated because it leaves the input file openScanner(java.lang.String fileName, java.io.InputStream is)Sets up a scanner that reads tokens from the givenInputStreamin the platform default encoding.Scanner(java.lang.String fileName, java.io.InputStream is, java.lang.String encoding)Sets up a scanner that reads tokens from the givenInputStreamwith the given encoding (nullmeans platform default encoding).Scanner(java.lang.String fileName, java.io.Reader in)Sets up a scanner that reads tokens from the givenReader.Scanner(java.lang.String fileName, java.io.Reader in, int initialLineNumber, int initialColumnNumber)Creates aScannerthat counts lines and columns from non-default initial values.Scanner(java.lang.String fileName, java.lang.String encoding)Deprecated.This method is deprecated because it leaves the input file open
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Deprecated.This method is deprecated, because the concept described above is confusing.java.lang.StringgetFileName()private intinternalRead()private static booleanisBinaryDigit(int c)private static booleanisDecimalDigit(int c)To comply with the JLS, this method does not allow for non-latin digit (likeCharacter.isDigit(char)does).private static booleanisHexDigit(int c)To comply with the JLS, this method does not allow for non-latin digit (likeCharacter.isDigit(char)does).private static booleanisOctalDigit(int c)Locationlocation()private intpeek()Returns the next character, but does not consume it.private booleanpeek(java.lang.String expectedCharacters)Checks whether the next character is one of the expectedCharacters.private intpeekButOne()Returns the next-but-one character, but does not consume any characters.private booleanpeekRead(int expected)Consumes the next character iff it equals the expected character.private booleanpeekRead(java.lang.String expectedCharacters)Consumes the next character iff it is one of the expectedCharactersTokenproduce()Produces and returns the next token.private charread()Consumes and returns the next character.private TokenTypescan()private voidscanLiteralCharacter()Scans the next literal character into aStringBuilder.private TokenTypescanNumericLiteral()voidsetIgnoreWhiteSpace(boolean value)If value istrue, then white space in the input stream is ignored, rather than scanned as aTokenType.WHITE_SPACEtoken.private Tokentoken(TokenType type, java.lang.String value)
-
-
-
Field Detail
-
SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE
public static final java.lang.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".- See Also:
- Constant Field Values
-
SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR
public static final java.lang.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), Constant Field Values
-
SYSTEM_PROPERTY_SOURCE_DEBUGGING_KEEP
public static final java.lang.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.- See Also:
- Constant Field Values
-
sb
private final java.lang.StringBuilder sb
Holds the characters of the currently scanned token.
-
fileName
@Nullable private final java.lang.String fileName
-
in
private final java.io.Reader in
-
ignoreWhiteSpace
private boolean ignoreWhiteSpace
-
nextChar
private int nextChar
-
nextButOneChar
private int nextButOneChar
-
crLfPending
private boolean crLfPending
-
nextCharLineNumber
private int nextCharLineNumber
-
nextCharColumnNumber
private int nextCharColumnNumber
-
tokenLineNumber
private int tokenLineNumber
Line number of the previously produced token (typically starting at one).
-
tokenColumnNumber
private int tokenColumnNumber
Column number of the first character of the previously produced token (1 if token is immediately preceded by a line break).
-
JAVA_KEYWORDS
private static final java.util.Set<java.lang.String> JAVA_KEYWORDS
-
JAVA_OPERATORS
private static final java.util.Set<java.lang.String> JAVA_OPERATORS
-
-
Constructor Detail
-
Scanner
@Deprecated public Scanner(java.lang.String fileName) throws java.io.IOExceptionDeprecated.This method is deprecated because it leaves the input file open- Throws:
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.lang.String fileName, java.lang.String encoding) throws java.io.IOExceptionDeprecated.This method is deprecated because it leaves the input file open- Throws:
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.io.File file) throws java.io.IOExceptionDeprecated.This method is deprecated because it leaves the input file open- Throws:
java.io.IOException
-
Scanner
@Deprecated public Scanner(java.io.File file, @Nullable java.lang.String encoding) throws java.io.IOExceptionDeprecated.This method is deprecated because it leaves the input file open- Throws:
java.io.IOException
-
Scanner
public Scanner(@Nullable java.lang.String fileName, java.io.InputStream is) throws java.io.IOException
Sets up a scanner that reads tokens from the givenInputStreamin the platform default encoding.The fileName is solely used for reporting in thrown exceptions.
- Throws:
java.io.IOException
-
Scanner
public Scanner(@Nullable java.lang.String fileName, java.io.InputStream is, @Nullable java.lang.String encoding) throws java.io.IOException
Sets up a scanner that reads tokens from the givenInputStreamwith the given encoding (nullmeans platform default encoding).The fileName is used for reporting errors during compilation and for source level debugging, and should name an existing file. If
nullis passed, and the system propertyorg.codehaus.janino.source_debugging.enableis set to "true", then a temporary file inorg.codehaus.janino.source_debugging.diror the system's default temp dir is created in order to make the source code available to a debugger.- Throws:
java.io.IOException
-
Scanner
public Scanner(@Nullable java.lang.String fileName, java.io.Reader in) throws java.io.IOException
Sets up a scanner that reads tokens from the givenReader.The fileName is used for reporting errors during compilation and for source level debugging, and should name an existing file. If
nullis passed, and the system propertyorg.codehaus.janino.source_debugging.enableis set to "true", then a temporary file inorg.codehaus.janino.source_debugging.diror the system's default temp dir is created in order to make the source code available to a debugger.- Throws:
java.io.IOException
-
-
Method Detail
-
setIgnoreWhiteSpace
public void setIgnoreWhiteSpace(boolean value)
If value istrue, then white space in the input stream is ignored, rather than scanned as aTokenType.WHITE_SPACEtoken. Since white space is typically quite numerous, this optimization may save considerable overhead.
-
getFileName
@Nullable public java.lang.String getFileName()
- Returns:
- The file name optionally passed to the constructor
-
close
@Deprecated public void close() throws java.io.IOExceptionDeprecated.This method is deprecated, because the concept described above is confusing. An application should close the underlyingInputStreamorReaderitselfCloses the character source (file,InputStream,Reader) associated with this object. The results of future calls toproduce()are undefined.- Throws:
java.io.IOException
-
produce
public Token produce() throws CompileException, java.io.IOException
Produces and returns the next token. Notice that end-of-input is not signalized with anullproduct, but by anTokenType.END_OF_INPUT-type token.- Throws:
CompileExceptionjava.io.IOException
-
scan
private TokenType scan() throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
scanNumericLiteral
private TokenType scanNumericLiteral() throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
isDecimalDigit
private static boolean isDecimalDigit(int c)
To comply with the JLS, this method does not allow for non-latin digit (likeCharacter.isDigit(char)does).
-
isHexDigit
private static boolean isHexDigit(int c)
To comply with the JLS, this method does not allow for non-latin digit (likeCharacter.isDigit(char)does).
-
isOctalDigit
private static boolean isOctalDigit(int c)
-
isBinaryDigit
private static boolean isBinaryDigit(int c)
-
scanLiteralCharacter
private void scanLiteralCharacter() throws CompileException, java.io.IOExceptionScans the next literal character into aStringBuilder.- Throws:
CompileExceptionjava.io.IOException
-
peek
private int peek() throws CompileException, java.io.IOExceptionReturns the next character, but does not consume it.- Returns:
- -1 iff there is no next character, i.e the input stream is at end-of-input
- Throws:
CompileExceptionjava.io.IOException
-
peek
private boolean peek(java.lang.String expectedCharacters) throws CompileException, java.io.IOExceptionChecks whether the next character is one of the expectedCharacters.It is not possible to peek exactly for "end-of-input"; use
peekRead(-1)instead.- Throws:
CompileExceptionjava.io.IOException
-
peekButOne
private int peekButOne() throws CompileException, java.io.IOExceptionReturns the next-but-one character, but does not consume any characters.- Throws:
CompileExceptionjava.io.IOException
-
read
private char read() throws CompileException, java.io.IOExceptionConsumes and returns the next character.- Throws:
CompileException- There is no next charcter, i.e. the input stream is at end-of-inputjava.io.IOException
-
peekRead
private boolean peekRead(int expected) throws CompileException, java.io.IOExceptionConsumes the next character iff it equals the expected character.- Returns:
- Whether the next character equalled the expected character
- Throws:
CompileExceptionjava.io.IOException
-
peekRead
private boolean peekRead(java.lang.String expectedCharacters) throws CompileException, java.io.IOExceptionConsumes the next character iff it is one of the expectedCharacters- Returns:
- Whether the next character was one of the expectedCharacters
- Throws:
CompileExceptionjava.io.IOException
-
internalRead
private int internalRead() throws java.io.IOException, CompileException- Returns:
- -1 iff there is no next character, i.e the input stream is at end-of-input
- Throws:
java.io.IOExceptionCompileException
-
-