-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[Java] Add a new CharStream that converts the symbols to upper or lower case. #2048
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
49 changes: 49 additions & 0 deletions
49
runtime-testsuite/test/org/antlr/v4/runtime/TestCaseChangingCharStream.java
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package org.antlr.v4.runtime; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.antlr.v4.runtime.CharStreams.fromString; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class TestCaseChangingCharStream { | ||
|
|
||
| /** | ||
| * Helper function to return a complete list of symbols read from the stream. | ||
| * @param stream | ||
| * @return | ||
| */ | ||
| private static List<Integer> readAll(CharStream stream) { | ||
| List<Integer> symbols = Lists.newArrayList(); | ||
|
|
||
| for (int i = 1; i <= stream.size()+1; i++) { | ||
| symbols.add( stream.LA(i) ); | ||
| } | ||
|
|
||
| return symbols; | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpper() { | ||
| List<Integer> expected = Lists.newArrayList((int)'A', (int)'B', (int)'C', (int)'D', IntStream.EOF); | ||
|
|
||
| CharStream stream = CharStreams.toUpper(fromString("abcd")); | ||
| assertEquals(expected, readAll(stream)); | ||
|
|
||
| stream = CharStreams.toUpper(fromString("ABCD")); | ||
| assertEquals(expected, readAll(stream)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLower() { | ||
| List<Integer> expected = Lists.newArrayList((int)'a', (int)'b', (int)'c', (int)'d', IntStream.EOF); | ||
|
|
||
| CharStream stream = CharStreams.toLower(fromString("abcd")); | ||
| assertEquals(expected, readAll(stream)); | ||
|
|
||
| stream = CharStreams.toLower(fromString("ABCD")); | ||
| assertEquals(expected, readAll(stream)); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
runtime/Java/src/org/antlr/v4/runtime/CaseChangingCharStream.java
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package org.antlr.v4.runtime; | ||
|
|
||
| import org.antlr.v4.runtime.misc.Interval; | ||
|
|
||
| /** | ||
| * This class supports case-insensitive lexing by wrapping an existing | ||
| * {@link CharStream} and forcing the lexer to see either upper or | ||
| * lowercase characters. Grammar literals should then be either upper or | ||
| * lower case such as 'BEGIN' or 'begin'. The text of the character | ||
| * stream is unaffected. Example: input 'BeGiN' would match lexer rule | ||
| * 'BEGIN' if constructor parameter upper=true but getText() would return | ||
| * 'BeGiN'. | ||
| */ | ||
| public class CaseChangingCharStream implements CharStream { | ||
|
|
||
| final CharStream stream; | ||
| final boolean upper; | ||
|
|
||
| /** | ||
| * Constructs a new CaseChangingCharStream wrapping the given {@link CharStream} forcing | ||
| * all characters to upper case or lower case. | ||
| * @param stream The stream to wrap. | ||
| * @param upper If true force each symbol to upper case, otherwise force to lower. | ||
| */ | ||
| public CaseChangingCharStream(CharStream stream, boolean upper) { | ||
| this.stream = stream; | ||
| this.upper = upper; | ||
| } | ||
|
|
||
| @Override | ||
| public String getText(Interval interval) { | ||
| return stream.getText(interval); | ||
| } | ||
|
|
||
| @Override | ||
| public void consume() { | ||
| stream.consume(); | ||
| } | ||
|
|
||
| @Override | ||
| public int LA(int i) { | ||
| int c = stream.LA(i); | ||
| if (c <= 0) { | ||
| return c; | ||
| } | ||
| if (upper) { | ||
| return Character.toUpperCase(c); | ||
| } | ||
| return Character.toLowerCase(c); | ||
| } | ||
|
|
||
| @Override | ||
| public int mark() { | ||
| return stream.mark(); | ||
| } | ||
|
|
||
| @Override | ||
| public void release(int marker) { | ||
| stream.release(marker); | ||
| } | ||
|
|
||
| @Override | ||
| public int index() { | ||
| return stream.index(); | ||
| } | ||
|
|
||
| @Override | ||
| public void seek(int index) { | ||
| stream.seek(index); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return stream.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getSourceName() { | ||
| return stream.getSourceName(); | ||
| } | ||
| } | ||
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.
Perhaps we could change the comment as follows?
This class supports case-insensitive lexing by wrapping an existing
{@link CharStream} and forcing the lexer to see either upper or
lowercase characters. Grammar literals should then be either upper or
lower case such as 'BEGIN' or 'begin'. The text of the character
stream is unaffected. Example: input BeGiN would match lexer rule
'BEGIN' if constructor parameter upper=true but getText() would return
BeGiN.
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.
SGTM.