Package org.codehaus.janino
Class TokenStreamImpl
- java.lang.Object
-
- org.codehaus.janino.TokenStreamImpl
-
- All Implemented Interfaces:
TokenStream
public class TokenStreamImpl extends java.lang.Object implements TokenStream
Standard implementation for theTokenStream.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringdocCommentThe optional JAVADOC comment preceding thenextToken.private TokennextButOneTokenprivate TokennextTokenprivate TokenpreviousTokenprivate Scannerscannerprivate WarningHandlerwarningHandler
-
Constructor Summary
Constructors Constructor Description TokenStreamImpl(Scanner scanner)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected CompileExceptioncompileException(java.lang.String message)Convenience method for throwing aCompileException.java.lang.Stringdoc()Gets the text of the doc comment (a.k.a.private static intindexOf(java.lang.String[] strings, java.lang.String subject)private static intindexOf(TokenType[] tta, TokenType subject)private static java.lang.Stringjoin(java.lang.Object[] oa, java.lang.String glue)Locationlocation()Tokenpeek()booleanpeek(java.lang.String suspected)intpeek(java.lang.String... suspected)Checks whether the value of the next token equals any of the suspected; does not consume the next token.booleanpeek(TokenType suspected)intpeek(TokenType... suspected)Checks whether the type of the next token is any of the suspected; does not consume the next token.TokenpeekNextButOne()booleanpeekNextButOne(java.lang.String suspected)booleanpeekRead(java.lang.String suspected)Checks whether the value of the next token equals the suspected; if so, consumes the token.intpeekRead(java.lang.String... suspected)Checks whether the value of the next token is one of the suspected; if so, consumes the token.java.lang.StringpeekRead(TokenType suspected)Checks whether the type of the next token is the suspected; if so, consumes the token.intpeekRead(TokenType... suspected)Checks whether the type of the next token is one of the suspected; if so, consumes the token.private TokenproduceToken()Tokenread()voidread(java.lang.String expected)Verifies that the value of the next token equals expected, and consumes the token.intread(java.lang.String... expected)Verifies that the value of the next token equals one of the expected, and consumes the token.java.lang.Stringread(TokenType expected)Verifies that the type of the next token is the expected, and consumes the token.intread(TokenType... expected)Verifies that the type of the next token is one of the expected, and consumes the token.voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install aWarningHandler.java.lang.StringtoString()private voidwarning(java.lang.String handle, java.lang.String message, Location location)Issues a warning with the given message and location and returns.
-
-
-
Field Detail
-
scanner
private final Scanner scanner
-
docComment
@Nullable private java.lang.String docComment
The optional JAVADOC comment preceding thenextToken.
-
warningHandler
@Nullable private WarningHandler warningHandler
-
-
Constructor Detail
-
TokenStreamImpl
public TokenStreamImpl(Scanner scanner)
-
-
Method Detail
-
produceToken
private Token produceToken() throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
doc
@Nullable public java.lang.String doc()
Gets the text of the doc comment (a.k.a. "JAVADOC comment") preceding the next token.- Returns:
nullif the next token is not preceded by a doc comment
-
peek
public Token peek() throws CompileException, java.io.IOException
- Specified by:
peekin interfaceTokenStream- Returns:
- The next token, but does not consume it
- Throws:
CompileExceptionjava.io.IOException
-
peekNextButOne
public Token peekNextButOne() throws CompileException, java.io.IOException
- Specified by:
peekNextButOnein interfaceTokenStream- Returns:
- The next-but-one token, but consumes neither the next nor the next-but-one token
- Throws:
CompileExceptionjava.io.IOException
-
peek
public boolean peek(java.lang.String suspected) throws CompileException, java.io.IOException- Specified by:
peekin interfaceTokenStream- Returns:
- Whether the value of the next token equals suspected; does not consume the next token
- Throws:
CompileExceptionjava.io.IOException
-
peek
public int peek(java.lang.String... suspected) throws CompileException, java.io.IOExceptionDescription copied from interface:TokenStreamChecks whether the value of the next token equals any of the suspected; does not consume the next token.- Specified by:
peekin interfaceTokenStream- Returns:
- The index of the first of the suspected that equals the value of the next token, or -1 if the value of the next token equals none of the suspected
- Throws:
CompileExceptionjava.io.IOException
-
peek
public boolean peek(TokenType suspected) throws CompileException, java.io.IOException
- Specified by:
peekin interfaceTokenStream- Returns:
- Whether the type of the next token is suspected
- Throws:
CompileExceptionjava.io.IOException
-
peek
public int peek(TokenType... suspected) throws CompileException, java.io.IOException
Description copied from interface:TokenStreamChecks whether the type of the next token is any of the suspected; does not consume the next token.- Specified by:
peekin interfaceTokenStream- Returns:
- The index of the first of the suspected types that is the next token's type, or -1 if the type of the next token is none of the suspected types
- Throws:
CompileExceptionjava.io.IOException
-
peekNextButOne
public boolean peekNextButOne(java.lang.String suspected) throws CompileException, java.io.IOException- Specified by:
peekNextButOnein interfaceTokenStream- Returns:
- Whether the value of the next-but-one token equals the suspected; consumes neither the next nor the next-but-one token
- Throws:
CompileExceptionjava.io.IOException
-
read
public Token read() throws CompileException, java.io.IOException
- Specified by:
readin interfaceTokenStream- Returns:
- The next token, which it also consumes, or
nulliff the scanner is at end-of-input - Throws:
CompileExceptionjava.io.IOException
-
read
public void read(java.lang.String expected) throws CompileException, java.io.IOExceptionDescription copied from interface:TokenStreamVerifies that the value of the next token equals expected, and consumes the token.- Specified by:
readin interfaceTokenStream- Throws:
CompileException- The value of the next token does not equal expected (this includes the case that the scanner is at end-of-input)java.io.IOException
-
read
public int read(java.lang.String... expected) throws CompileException, java.io.IOExceptionDescription copied from interface:TokenStreamVerifies that the value of the next token equals one of the expected, and consumes the token.- Specified by:
readin interfaceTokenStream- Returns:
- The index of the consumed token within expected
- Throws:
CompileException- The value of the next token does not equal any of the expected (this includes the case where the scanner is at end-of-input)java.io.IOException
-
read
public java.lang.String read(TokenType expected) throws CompileException, java.io.IOException
Description copied from interface:TokenStreamVerifies that the type of the next token is the expected, and consumes the token.- Specified by:
readin interfaceTokenStream- Returns:
- The value of the next token; an
internedString iff the token represents an identifier,true,false,null, or an operator - Throws:
CompileException- The next token's type is not the expectedjava.io.IOException
-
read
public int read(TokenType... expected) throws CompileException, java.io.IOException
Description copied from interface:TokenStreamVerifies that the type of the next token is one of the expected, and consumes the token.- Specified by:
readin interfaceTokenStream- Returns:
- The index of the first of the expected types that is the next token's type; -1 if the type of the next token is none of the expected
- Throws:
CompileException- The next token's type is none of the expectedjava.io.IOException
-
peekRead
public boolean peekRead(java.lang.String suspected) throws CompileException, java.io.IOExceptionDescription copied from interface:TokenStreamChecks whether the value of the next token equals the suspected; if so, consumes the token.- Specified by:
peekReadin interfaceTokenStream- Throws:
CompileExceptionjava.io.IOException
-
peekRead
public int peekRead(java.lang.String... suspected) throws CompileException, java.io.IOExceptionDescription copied from interface:TokenStreamChecks whether the value of the next token is one of the suspected; if so, consumes the token.- Specified by:
peekReadin interfaceTokenStream- Returns:
- The index of the first of the suspected that equals the next token's value; -1 iff the next token's value equals none of the suspected
- Throws:
CompileExceptionjava.io.IOException
-
peekRead
@Nullable public java.lang.String peekRead(TokenType suspected) throws CompileException, java.io.IOException
Description copied from interface:TokenStreamChecks whether the type of the next token is the suspected; if so, consumes the token.- Specified by:
peekReadin interfaceTokenStream- Returns:
- The value of the next token; an
internedString iff the token represents an identifier,true,false,null, or an operator - Throws:
CompileExceptionjava.io.IOException
-
peekRead
public int peekRead(TokenType... suspected) throws CompileException, java.io.IOException
Description copied from interface:TokenStreamChecks whether the type of the next token is one of the suspected; if so, consumes the token.- Specified by:
peekReadin interfaceTokenStream- Returns:
- The index of the elements that is the next token's type, or -1 iff the type of next token is none of the suspected
- Throws:
CompileExceptionjava.io.IOException
-
location
public Location location()
- Specified by:
locationin interfaceTokenStream- Returns:
- The location of the first character of the previously read (not peeked!) token
-
indexOf
private static int indexOf(java.lang.String[] strings, java.lang.String subject)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:TokenStreamBy default, warnings are discarded, but an application my install aWarningHandler.Notice that there is no
Parser.setErrorHandler()method, but parse errors always throw aCompileException. The reason being is that there is no reasonable way to recover from parse errors and continue parsing, so there is no need to install a custom parse error handler.- Specified by:
setWarningHandlerin interfaceTokenStream- Parameters:
warningHandler-nullto indicate that no warnings be issued
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
warning
private void warning(java.lang.String handle, java.lang.String message, @Nullable Location location) throws CompileExceptionIssues a warning with the given message and location and returns. This is done through aWarningHandlerthat was installed throughsetWarningHandler(WarningHandler).The
handleargument qualifies the warning and is typically used by theWarningHandlerto suppress individual warnings.- Throws:
CompileException- The optionally installedWarningHandlerdecided to throw aCompileException
-
compileException
protected final CompileException compileException(java.lang.String message)
Convenience method for throwing aCompileException.
-
join
private static java.lang.String join(@Nullable java.lang.Object[] oa, java.lang.String glue)
-
-