3232
3333/** A wrapper around javac's lexer. */
3434final class JavacTokens {
35-
3635 /** The lexer eats terminal comments, so feed it one we don't care about. */
3736 // TODO(b/33103797): fix javac and remove the work-around
3837 private static final CharSequence EOF_COMMENT = "\n //EOF" ;
3938
39+ /** Represents a position in the source code with validation capabilities */
40+ static final class TokenPosition {
41+ private final int pos ;
42+ private final int endPos ;
43+
44+ TokenPosition (int pos , int endPos ) {
45+ this .pos = pos ;
46+ this .endPos = endPos ;
47+ }
48+
49+ int getPos () {
50+ return pos ;
51+ }
52+
53+ int getEndPos () {
54+ return endPos ;
55+ }
56+
57+ void validateIndex (int index ) {
58+ checkArgument (
59+ 0 <= index && index < (endPos - pos ),
60+ "Expected %s in the range [0, %s)" ,
61+ index ,
62+ endPos - pos );
63+ }
64+ }
65+
4066 /** An unprocessed input token, including whitespace and comments. */
4167 static class RawTok {
4268 private final String stringVal ;
4369 private final TokenKind kind ;
44- private final int pos ;
45- private final int endPos ;
70+ private final TokenPosition position ;
4671
4772 RawTok (String stringVal , TokenKind kind , int pos , int endPos ) {
4873 this .stringVal = stringVal ;
4974 this .kind = kind ;
50- this .pos = pos ;
51- this .endPos = endPos ;
75+ this .position = new TokenPosition (pos , endPos );
5276 }
5377
5478 /** The token kind, or {@code null} for whitespace and comments. */
@@ -58,12 +82,12 @@ public TokenKind kind() {
5882
5983 /** The start position. */
6084 public int pos () {
61- return pos ;
85+ return position . getPos () ;
6286 }
6387
6488 /** The end position. */
6589 public int endPos () {
66- return endPos ;
90+ return position . getEndPos () ;
6791 }
6892
6993 /** The escaped string value of a literal, or {@code null} for other tokens. */
@@ -74,7 +98,7 @@ public String stringVal() {
7498
7599 /** Lex the input and return a list of {@link RawTok}s. */
76100 public static ImmutableList <RawTok > getTokens (
77- String source , Context context , Set <TokenKind > stopTokens ) {
101+ String source , Context context , Set <TokenKind > stopTokens ) {
78102 if (source == null ) {
79103 return ImmutableList .of ();
80104 }
@@ -94,7 +118,7 @@ public static ImmutableList<RawTok> getTokens(
94118 tokens .add (new RawTok (null , null , last , c .getSourcePos (0 )));
95119 }
96120 tokens .add (
97- new RawTok (null , null , c .getSourcePos (0 ), c .getSourcePos (0 ) + c .getText ().length ()));
121+ new RawTok (null , null , c .getSourcePos (0 ), c .getSourcePos (0 ) + c .getText ().length ()));
98122 last = c .getSourcePos (0 ) + c .getText ().length ();
99123 }
100124 }
@@ -108,11 +132,11 @@ public static ImmutableList<RawTok> getTokens(
108132 tokens .add (new RawTok (null , null , last , t .pos ));
109133 }
110134 tokens .add (
111- new RawTok (
112- t .kind == TokenKind .STRINGLITERAL ? "\" " + t .stringVal () + "\" " : null ,
113- t .kind ,
114- t .pos ,
115- t .endPos ));
135+ new RawTok (
136+ t .kind == TokenKind .STRINGLITERAL ? "\" " + t .stringVal () + "\" " : null ,
137+ t .kind ,
138+ t .pos ,
139+ t .endPos ));
116140 last = t .endPos ;
117141 } while (scanner .token ().kind != TokenKind .EOF );
118142 if (last < end ) {
@@ -122,7 +146,7 @@ public static ImmutableList<RawTok> getTokens(
122146 }
123147
124148 private static ImmutableList <CommentWithTextAndPosition > getComments (
125- Token token , Map <Comment , CommentWithTextAndPosition > comments ) {
149+ Token token , Map <Comment , CommentWithTextAndPosition > comments ) {
126150 if (token .comments == null ) {
127151 return ImmutableList .of ();
128152 }
@@ -132,7 +156,6 @@ private static ImmutableList<CommentWithTextAndPosition> getComments(
132156
133157 /** A {@link JavaTokenizer} that saves comments. */
134158 static class CommentSavingTokenizer extends JavaTokenizer {
135-
136159 private final Map <Comment , CommentWithTextAndPosition > comments = new HashMap <>();
137160
138161 CommentSavingTokenizer (ScannerFactory fac , char [] buffer , int length ) {
@@ -148,7 +171,7 @@ protected Comment processComment(int pos, int endPos, CommentStyle style) {
148171 char [] buf = getRawCharactersReflectively (pos , endPos );
149172 Comment comment = super .processComment (pos , endPos , style );
150173 CommentWithTextAndPosition commentWithTextAndPosition =
151- new CommentWithTextAndPosition (pos , endPos , new String (buf ));
174+ new CommentWithTextAndPosition (pos , endPos , new String (buf ));
152175 comments .put (comment , commentWithTextAndPosition );
153176 return comment ;
154177 }
@@ -162,10 +185,10 @@ private char[] getRawCharactersReflectively(int beginIndex, int endIndex) {
162185 }
163186 try {
164187 return (char [])
165- instance
166- .getClass ()
167- .getMethod ("getRawCharacters" , int .class , int .class )
168- .invoke (instance , beginIndex , endIndex );
188+ instance
189+ .getClass ()
190+ .getMethod ("getRawCharacters" , int .class , int .class )
191+ .invoke (instance , beginIndex , endIndex );
169192 } catch (ReflectiveOperationException e ) {
170193 throw new LinkageError (e .getMessage (), e );
171194 }
@@ -174,30 +197,17 @@ private char[] getRawCharactersReflectively(int beginIndex, int endIndex) {
174197
175198 /** A {@link Comment} that saves its text and start position. */
176199 static class CommentWithTextAndPosition {
177-
178- private final int pos ;
179- private final int endPos ;
200+ private final TokenPosition position ;
180201 private final String text ;
181202
182203 public CommentWithTextAndPosition (int pos , int endPos , String text ) {
183- this .pos = pos ;
184- this .endPos = endPos ;
204+ this .position = new TokenPosition (pos , endPos );
185205 this .text = text ;
186206 }
187207
188- /**
189- * Returns the source position of the character at index {@code index} in the comment text.
190- *
191- * <p>The handling of javadoc comments in javac has more logic to skip over leading whitespace
192- * and '*' characters when indexing into doc comments, but we don't need any of that.
193- */
194208 public int getSourcePos (int index ) {
195- checkArgument (
196- 0 <= index && index < (endPos - pos ),
197- "Expected %s in the range [0, %s)" ,
198- index ,
199- endPos - pos );
200- return pos + index ;
209+ position .validateIndex (index );
210+ return position .getPos () + index ;
201211 }
202212
203213 public String getText () {
0 commit comments