Class JavaccToken
java.lang.Object
net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken
- All Implemented Interfaces:
Comparable<JavaccToken>,GenericToken<JavaccToken>,Reportable
A generic token implementation for JavaCC parsers.
Largely has the same interface as the default generated token class. The main difference is that the position of the token is encoded as a start and end offset in the source file, instead of a (begin,end)x(line,column) 4-tuple. This offers two practical advantages:
- It allows retrieving easily the underlying text of a node (just need to cut a substring of the file text). Other attributes like lines and column bounds can be derived as well - though this should not be done systematically because it's costlier.
- It's a bit lighter. Token instances are one of the most numerous class in a typical PMD run and this may reduce GC pressure.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intKind for EOF tokens.static final intKind for implicit tokens.final intAn integer that describes the kind of this token.A reference to the next regular (non-special) token from the input stream.This field is used to access special tokens that occur prior to this token, but after the immediately preceding regular (non-special) token. -
Constructor Summary
ConstructorsConstructorDescriptionJavaccToken(int kind, String image, int startInclusive, int endExclusive, JavaccTokenDocument document) Constructor with aStringimage (seethe other ctor).JavaccToken(int kind, Chars image, int startInclusive, int endExclusive, JavaccTokenDocument document) Builds a new token of the specified kind. -
Method Summary
Modifier and TypeMethodDescriptionfinal JavaccTokenDocumentReturns the document owning this token.getImage()Returns the token's text as a string.Returns the text of the token as a char sequence.intgetKind()Gets a unique integer representing the kind of token this is.getNext()Obtain the next generic token according to the input stream which generated the instance of this token.Obtain a comment-type token which, according to the input stream which generated the instance of this token, precedes this instance token and succeeds the previous generic token (if there is any).final TextRegionReturns a text region with the coordinates of this token.Returns the location at which this element should be reported.getText()Returns the original text of the token.static JavaccTokenimplicitBefore(JavaccToken next) Creates an implicit token, with zero length, that is linked to the given token as its special predecessor.booleanisEof()Returns true if this token is an end-of-file token.booleanReturns true if this token is implicit, ie was inserted artificially and has a zero-length image.static JavaccTokennewImplicit(int offset, JavaccTokenDocument document) Returns a new implicit token, positioned at the given offset.replaceImage(CharStream charStream) Returns a new token with the same kind as this one, whose image is replaced by the one marked on the char stream.toString()withKind(int newKind) Returns a new token with the given kind, and all other parameters identical to this one.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.sourceforge.pmd.lang.ast.GenericToken
compareTo, imageEquals
-
Field Details
-
EOF
public static final int EOFKind for EOF tokens.- See Also:
-
IMPLICIT_TOKEN
public static final int IMPLICIT_TOKENKind for implicit tokens. Negative because JavaCC only picks positive numbers for token kinds.- See Also:
-
kind
public final int kindAn integer that describes the kind of this token. This numbering system is determined by JavaCCParser, and a table of these numbers is stored in the file ...Constants.java. -
next
A reference to the next regular (non-special) token from the input stream. If this is the last token from the input stream, or if the token manager has not read tokens beyond this one, this field is set to null. This is true only if this token is also a regular token. Otherwise, see below for a description of the contents of this field. -
specialToken
This field is used to access special tokens that occur prior to this token, but after the immediately preceding regular (non-special) token. If there are no such special tokens, this field is set to null. When there are more than one such special token, this field refers to the last of these special tokens, which in turn refers to the next previous special token through its specialToken field, and so on until the first special token (whose specialToken field is null). The next fields of special tokens refer to other special tokens that immediately follow it (without an intervening regular token). If there is no such token, this field is null.
-
-
Constructor Details
-
JavaccToken
public JavaccToken(int kind, Chars image, int startInclusive, int endExclusive, JavaccTokenDocument document) Builds a new token of the specified kind.- Parameters:
kind- Kind of tokenimage- Image of the token (after translating escapes if any)startInclusive- Start character of the token in the text file (before translating escapes)endExclusive- End of the token in the text file (before translating escapes)document- Document owning the token
-
JavaccToken
public JavaccToken(int kind, String image, int startInclusive, int endExclusive, JavaccTokenDocument document) Constructor with aStringimage (seethe other ctor).
-
-
Method Details
-
getDocument
Returns the document owning this token. -
isEof
public boolean isEof()Description copied from interface:GenericTokenReturns true if this token is an end-of-file token. This is the last token of token sequences that have been fully lexed.- Specified by:
isEofin interfaceGenericToken<JavaccToken>
-
getKind
public int getKind()Description copied from interface:GenericTokenGets a unique integer representing the kind of token this is. The semantics of this kind depend on the language.The returned constants can be looked up in the language's "*ParserConstants", e.g. CppParserConstants or JavaParserConstants. These constants are considered internal API and may change at any time when the language's grammar is changed.
- Specified by:
getKindin interfaceGenericToken<JavaccToken>
-
getNext
Description copied from interface:GenericTokenObtain the next generic token according to the input stream which generated the instance of this token.- Specified by:
getNextin interfaceGenericToken<JavaccToken>- Returns:
- the next generic token if it exists; null if it does not exist
-
getPreviousComment
Description copied from interface:GenericTokenObtain a comment-type token which, according to the input stream which generated the instance of this token, precedes this instance token and succeeds the previous generic token (if there is any).- Specified by:
getPreviousCommentin interfaceGenericToken<JavaccToken>- Returns:
- the comment-type token if it exists; null if it does not exist
-
getImageCs
Description copied from interface:GenericTokenReturns the text of the token as a char sequence. This should be preferred when you can use egStringUtilsto do some processing, without having to create a string.- Specified by:
getImageCsin interfaceGenericToken<JavaccToken>
-
getImage
Description copied from interface:GenericTokenReturns the token's text as a string.- Specified by:
getImagein interfaceGenericToken<JavaccToken>
-
getText
Returns the original text of the token. The image may be normalized, e.g. for case-insensitive languages.- Since:
- 7.3.0
-
getRegion
Description copied from interface:GenericTokenReturns a text region with the coordinates of this token.- Specified by:
getRegionin interfaceGenericToken<JavaccToken>
-
getReportLocation
Description copied from interface:ReportableReturns the location at which this element should be reported.Use this instead of
Node.getBeginColumn()/Node.getBeginLine(), etc.- Specified by:
getReportLocationin interfaceReportable
-
isImplicit
public boolean isImplicit()Description copied from interface:GenericTokenReturns true if this token is implicit, ie was inserted artificially and has a zero-length image.- Specified by:
isImplicitin interfaceGenericToken<JavaccToken>
-
toString
-
replaceImage
Returns a new token with the same kind as this one, whose image is replaced by the one marked on the char stream.- Parameters:
charStream- Char stream from which to start- Returns:
- A new token
-
withKind
Returns a new token with the given kind, and all other parameters identical to this one.- Parameters:
newKind- Char stream from which to start- Returns:
- A new token
-
implicitBefore
Creates an implicit token, with zero length, that is linked to the given token as its special predecessor.- Parameters:
next- Token before which to insert the new token- Returns:
- A new token
-
newImplicit
Returns a new implicit token, positioned at the given offset.- Parameters:
offset- Offset of the tokendocument- Document owning the token- Returns:
- A new token
-