From 499e44b03ac2db0c7524a4d7af422452bd3ffcfd Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Mon, 30 Jan 2017 14:16:33 -0800 Subject: [PATCH] Pass through source name --- .../v4/test/runtime/java/TestCharStreams.java | 15 ++++++++++----- .../src/org/antlr/v4/runtime/CharStreams.java | 13 ++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestCharStreams.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestCharStreams.java index 8c232d741..f944fe5da 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestCharStreams.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestCharStreams.java @@ -60,6 +60,7 @@ public class TestCharStreams { assertEquals(5, s.size()); assertEquals(0, s.index()); assertEquals("hello", s.toString()); + assertEquals(p.toString(), s.getSourceName()); } @Test @@ -70,6 +71,7 @@ public class TestCharStreams { assertEquals(7, s.size()); assertEquals(0, s.index()); assertEquals("hello \uD83C\uDF0E", s.toString()); + assertEquals(p.toString(), s.getSourceName()); } @Test @@ -102,10 +104,11 @@ public class TestCharStreams { Files.write(p, "hello".getBytes(StandardCharsets.UTF_8)); try (SeekableByteChannel c = Files.newByteChannel(p)) { CodePointCharStream s = CharStreams.createWithUTF8Channel( - c, 4096, CodingErrorAction.REPLACE); + c, 4096, CodingErrorAction.REPLACE, "foo"); assertEquals(5, s.size()); assertEquals(0, s.index()); assertEquals("hello", s.toString()); + assertEquals("foo", s.getSourceName()); } } @@ -115,10 +118,11 @@ public class TestCharStreams { Files.write(p, "hello \uD83C\uDF0E".getBytes(StandardCharsets.UTF_8)); try (SeekableByteChannel c = Files.newByteChannel(p)) { CodePointCharStream s = CharStreams.createWithUTF8Channel( - c, 4096, CodingErrorAction.REPLACE); + c, 4096, CodingErrorAction.REPLACE, "foo"); assertEquals(7, s.size()); assertEquals(0, s.index()); assertEquals("hello \uD83C\uDF0E", s.toString()); + assertEquals("foo", s.getSourceName()); } } @@ -130,7 +134,7 @@ public class TestCharStreams { Files.write(p, toWrite); try (SeekableByteChannel c = Files.newByteChannel(p)) { CodePointCharStream s = CharStreams.createWithUTF8Channel( - c, 4096, CodingErrorAction.REPLACE); + c, 4096, CodingErrorAction.REPLACE, "foo"); assertEquals(3, s.size()); assertEquals(0, s.index()); assertEquals("\uFFFD\uFFFD\uFFFD", s.toString()); @@ -144,7 +148,7 @@ public class TestCharStreams { Files.write(p, toWrite); try (SeekableByteChannel c = Files.newByteChannel(p)) { thrown.expect(CharacterCodingException.class); - CharStreams.createWithUTF8Channel(c, 4096, CodingErrorAction.REPORT); + CharStreams.createWithUTF8Channel(c, 4096, CodingErrorAction.REPORT, "foo"); } } @@ -158,7 +162,8 @@ public class TestCharStreams { // Note this buffer size ensures the SMP code point // straddles the boundary of two buffers 8, - CodingErrorAction.REPLACE); + CodingErrorAction.REPLACE, + "foo"); assertEquals(7, s.size()); assertEquals(0, s.index()); assertEquals("hello \uD83C\uDF0E", s.toString()); diff --git a/runtime/Java/src/org/antlr/v4/runtime/CharStreams.java b/runtime/Java/src/org/antlr/v4/runtime/CharStreams.java index 2d0b4e33f..46bacc0fe 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/CharStreams.java +++ b/runtime/Java/src/org/antlr/v4/runtime/CharStreams.java @@ -49,7 +49,7 @@ public final class CharStreams { stringIdx += Character.charCount(codePoint); } codePointBuffer.flip(); - return new CodePointCharStream(codePointBuffer); + return new CodePointCharStream(codePointBuffer, IntStream.UNKNOWN_SOURCE_NAME); } public static CodePointCharStream createWithUTF8(Path path) throws IOException { @@ -57,7 +57,8 @@ public final class CharStreams { return createWithUTF8Channel( channel, DEFAULT_BUFFER_SIZE, - CodingErrorAction.REPLACE); + CodingErrorAction.REPLACE, + path.toString()); } } @@ -66,14 +67,16 @@ public final class CharStreams { return createWithUTF8Channel( channel, DEFAULT_BUFFER_SIZE, - CodingErrorAction.REPLACE); + CodingErrorAction.REPLACE, + IntStream.UNKNOWN_SOURCE_NAME); } } public static CodePointCharStream createWithUTF8Channel( ReadableByteChannel channel, int bufferSize, - CodingErrorAction decodingErrorAction + CodingErrorAction decodingErrorAction, + String sourceName ) throws IOException { ByteBuffer utf8BytesIn = ByteBuffer.allocateDirect(bufferSize); IntBuffer codePointsOut = IntBuffer.allocate(bufferSize); @@ -91,6 +94,6 @@ public final class CharStreams { } codePointsOut.limit(codePointsOut.position()); codePointsOut.flip(); - return new CodePointCharStream(codePointsOut); + return new CodePointCharStream(codePointsOut, sourceName); } }