Class JavaccToken
- java.lang.Object
-
- net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken
-
- All Implemented Interfaces:
Comparable<JavaccToken>,GenericToken<JavaccToken>,Reportable
public class JavaccToken extends Object implements GenericToken<JavaccToken>
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
Fields Modifier and Type Field Description static intEOFKind for EOF tokens.static intIMPLICIT_TOKENKind for implicit tokens.intkindAn integer that describes the kind of this token.JavaccTokennextA reference to the next regular (non-special) token from the input stream.JavaccTokenspecialTokenThis field is used to access special tokens that occur prior to this token, but after the immediately preceding regular (non-special) token.
-
Constructor Summary
Constructors Constructor Description JavaccToken(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JavaccTokenDocumentgetDocument()Returns the document owning this token.StringgetImage()Returns the token's text as a string.CharsgetImageCs()Returns the text of the token as a char sequence.intgetKind()Gets a unique integer representing the kind of token this is.JavaccTokengetNext()Obtain the next generic token according to the input stream which generated the instance of this token.JavaccTokengetPreviousComment()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).TextRegiongetRegion()Returns a text region with the coordinates of this token.FileLocationgetReportLocation()Returns the location at which this element should be reported.CharsgetText()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.booleanisImplicit()Returns 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.JavaccTokenreplaceImage(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.StringtoString()JavaccTokenwithKind(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, wait
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.GenericToken
compareTo, imageEquals
-
-
-
-
Field Detail
-
EOF
public static final int EOF
Kind for EOF tokens.- See Also:
- Constant Field Values
-
IMPLICIT_TOKEN
public static final int IMPLICIT_TOKEN
Kind for implicit tokens. Negative because JavaCC only picks positive numbers for token kinds.- See Also:
- Constant Field Values
-
kind
public final int kind
An 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
public JavaccToken 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
public JavaccToken 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 Detail
-
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 Detail
-
getDocument
public final JavaccTokenDocument 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
public 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
public JavaccToken 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
public Chars 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
public String getImage()
Description copied from interface:GenericTokenReturns the token's text as a string.- Specified by:
getImagein interfaceGenericToken<JavaccToken>
-
getText
public Chars getText()
Returns the original text of the token. The image may be normalized, e.g. for case-insensitive languages.- Since:
- 7.3.0
-
getRegion
public final TextRegion getRegion()
Description copied from interface:GenericTokenReturns a text region with the coordinates of this token.- Specified by:
getRegionin interfaceGenericToken<JavaccToken>
-
getReportLocation
public FileLocation 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>
-
replaceImage
public JavaccToken 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.- Parameters:
charStream- Char stream from which to start- Returns:
- A new token
-
withKind
public JavaccToken withKind(int newKind)
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
public static JavaccToken implicitBefore(JavaccToken next)
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
public static JavaccToken newImplicit(int offset, JavaccTokenDocument document)
Returns a new implicit token, positioned at the given offset.- Parameters:
offset- Offset of the tokendocument- Document owning the token- Returns:
- A new token
-
-