Merge pull request #440 from sharwell/clone-token-text

Improve CommonToken cloning performance, and update documentation
This commit is contained in:
Terence Parr 2014-01-23 08:30:16 -08:00
commit de43cc427c
1 changed files with 10 additions and 1 deletions

View File

@ -139,10 +139,17 @@ public class CommonToken implements WritableToken, Serializable {
/**
* Constructs a new {@link CommonToken} as a copy of another {@link Token}.
*
* <p>
* If {@code oldToken} is also a {@link CommonToken} instance, the newly
* constructed token will share a reference to the {@link #text} field and
* the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will
* be assigned the result of calling {@link #getText}, and {@link #source}
* will be constructed from the result of {@link Token#getTokenSource} and
* {@link Token#getInputStream}.</p>
*
* @param oldToken The token to copy.
*/
public CommonToken(@NotNull Token oldToken) {
text = oldToken.getText();
type = oldToken.getType();
line = oldToken.getLine();
index = oldToken.getTokenIndex();
@ -152,9 +159,11 @@ public class CommonToken implements WritableToken, Serializable {
stop = oldToken.getStopIndex();
if (oldToken instanceof CommonToken) {
text = ((CommonToken)oldToken).text;
source = ((CommonToken)oldToken).source;
}
else {
text = oldToken.getText();
source = new Pair<TokenSource, CharStream>(oldToken.getTokenSource(), oldToken.getInputStream());
}
}