-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Change UnbufferedCharStream to use code points #1796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
parrt
merged 1 commit into
antlr:master
from
bhamiltoncx:unbuffered-char-stream-code-points
Mar 29, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ public class UnbufferedCharStream implements CharStream { | |
* we keep adding to buffer. Otherwise, {@link #consume consume()} resets so | ||
* we start filling at index 0 again. | ||
*/ | ||
protected char[] data; | ||
protected int[] data; | ||
|
||
/** | ||
* The number of characters currently in {@link #data data}. | ||
|
@@ -82,7 +82,7 @@ public UnbufferedCharStream() { | |
/** Useful for subclasses that pull char from other than this.input. */ | ||
public UnbufferedCharStream(int bufferSize) { | ||
n = 0; | ||
data = new char[bufferSize]; | ||
data = new int[bufferSize]; | ||
} | ||
|
||
public UnbufferedCharStream(InputStream input) { | ||
|
@@ -145,13 +145,36 @@ protected void sync(int want) { | |
*/ | ||
protected int fill(int n) { | ||
for (int i=0; i<n; i++) { | ||
if (this.n > 0 && data[this.n - 1] == (char)IntStream.EOF) { | ||
if (this.n > 0 && data[this.n - 1] == IntStream.EOF) { | ||
return i; | ||
} | ||
|
||
try { | ||
int c = nextChar(); | ||
add(c); | ||
if (c > Character.MAX_VALUE || c == IntStream.EOF) { | ||
add(c); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reminder that our "style" is else starts a line ;) |
||
char ch = (char) c; | ||
if (Character.isLowSurrogate(ch)) { | ||
throw new RuntimeException("Invalid UTF-16 (low surrogate with no preceding high surrogate)"); | ||
} else if (Character.isHighSurrogate(ch)) { | ||
int lowSurrogate = nextChar(); | ||
if (lowSurrogate > Character.MAX_VALUE) { | ||
throw new RuntimeException("Invalid UTF-16 (high surrogate followed by code point > U+FFFF"); | ||
} else if (lowSurrogate == IntStream.EOF) { | ||
throw new RuntimeException("Invalid UTF-16 (dangling high surrogate at end of file)"); | ||
} else { | ||
char lowSurrogateChar = (char) lowSurrogate; | ||
if (Character.isLowSurrogate(lowSurrogateChar)) { | ||
add(Character.toCodePoint(ch, lowSurrogateChar)); | ||
} else { | ||
throw new RuntimeException("Invalid UTF-16 (dangling high surrogate"); | ||
} | ||
} | ||
} else { | ||
add(c); | ||
} | ||
} | ||
} | ||
catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
|
@@ -173,7 +196,7 @@ protected void add(int c) { | |
if ( n>=data.length ) { | ||
data = Arrays.copyOf(data, data.length * 2); | ||
} | ||
data[n++] = (char)c; | ||
data[n++] = c; | ||
} | ||
|
||
@Override | ||
|
@@ -183,8 +206,8 @@ public int LA(int i) { | |
int index = p + i - 1; | ||
if ( index < 0 ) throw new IndexOutOfBoundsException(); | ||
if ( index >= n ) return IntStream.EOF; | ||
char c = data[index]; | ||
if ( c==(char)IntStream.EOF ) return IntStream.EOF; | ||
int c = data[index]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, just realized this can be tidied up to just |
||
if ( c==IntStream.EOF ) return IntStream.EOF; | ||
return c; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note this ends up calling
new InputStreamReader(input)
without specifying aCharset
. That means this logic depends on the client's$LANG
environment variable or equivalent, which is usually not what anyone wants.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we should add an optional charset arg on one or more of the arguments. Also, doesn't the fill() method assume UTF-X? I.e., it would not work with some other encoding, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! We absolutely should add a
Charset
arg, or there's no way to use anything but the default in the calling environment. (We should probably also change to default to UTF-8..)fill()
uses theInputStreamReader
to get UTF-16 encodedchar
s. It will work with any encoding, although there's no way to specify an encoding other than the environment default with the currentUnbufferedCharStream
API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, great! could you please make the changes? Does that mean we change this field to InputStream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll make the changes! Nope, we'll still use a
Reader
, we'll just use a differentInputStreamReader
constructor which allows us to specify aCharset
.