Search in sources :

Example 36 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.

the class TestCharStreams method fromSMPUTF32LEPathSMPHasExpectedSize.

@Test
public void fromSMPUTF32LEPathSMPHasExpectedSize() throws Exception {
    File p = folder.newFile();
    Utils.writeFile(p, "hello \uD83C\uDF0E".getBytes(Charset.forName("UTF-32LE")));
    CharStream s = CharStreams.fromFile(p, Charset.forName("UTF-32LE"));
    assertEquals(7, s.size());
    assertEquals(0, s.index());
    assertEquals("hello \uD83C\uDF0E", s.toString());
    assertEquals(p.toString(), s.getSourceName());
}
Also used : File(java.io.File) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 37 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.

the class TestCharStreams method fromSMPUTF8SequenceStraddlingBufferBoundary.

@Test
public void fromSMPUTF8SequenceStraddlingBufferBoundary() throws Exception {
    File p = folder.newFile();
    Utils.writeFile(p, "hello \uD83C\uDF0E".getBytes(Charset.forName("UTF-8")));
    ReadableByteChannel c = Channels.newChannel(new FileInputStream(p));
    try {
        CharStream s = CharStreams.fromChannel(c, // straddles the boundary of two buffers
        8, CodingErrorAction.REPLACE, "foo");
        assertEquals(7, s.size());
        assertEquals(0, s.index());
        assertEquals("hello \uD83C\uDF0E", s.toString());
    } finally {
        c.close();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) File(java.io.File) FileInputStream(java.io.FileInputStream) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 38 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.

the class TestCharStreams method fromBMPUTF8InputStreamHasExpectedSize.

@Test
public void fromBMPUTF8InputStreamHasExpectedSize() throws Exception {
    File p = folder.newFile();
    Utils.writeFile(p, "hello".getBytes(Charset.forName("UTF-8")));
    InputStream is = new FileInputStream(p);
    try {
        CharStream s = CharStreams.fromStream(is);
        assertEquals(5, s.size());
        assertEquals(0, s.index());
        assertEquals("hello", s.toString());
    } finally {
        is.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 39 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.

the class TestCharStreams method fromBMPUTF8PathHasExpectedSize.

@Test
public void fromBMPUTF8PathHasExpectedSize() throws Exception {
    File p = folder.newFile();
    Utils.writeFile(p, "hello".getBytes(Charset.forName("UTF-8")));
    CharStream s = CharStreams.fromFile(p);
    assertEquals(5, s.size());
    assertEquals(0, s.index());
    assertEquals("hello", s.toString());
    assertEquals(p.toString(), s.getSourceName());
}
Also used : File(java.io.File) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 40 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.

the class TestCharStreams method fromBMPStringHasExpectedSize.

@Test
public void fromBMPStringHasExpectedSize() {
    CharStream s = CharStreams.fromString("hello");
    assertEquals(5, s.size());
    assertEquals(0, s.index());
    assertEquals("hello", s.toString());
}
Also used : CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14