Search in sources :

Example 61 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUTF8CodePointDecoder method decodeLatinByteBufferWritesCodePoint.

@Test
public void decodeLatinByteBufferWritesCodePoint() throws Exception {
    UTF8CodePointDecoder decoder = new UTF8CodePointDecoder(CodingErrorAction.REPLACE);
    ByteBuffer utf8BytesIn = StandardCharsets.UTF_8.encode("X");
    IntBuffer codePointsOut = IntBuffer.allocate(1);
    IntBuffer result = decoder.decodeCodePointsFromBuffer(utf8BytesIn, codePointsOut, true);
    result.flip();
    assertEquals(1, result.remaining());
    assertEquals('X', result.get(0));
}
Also used : IntBuffer(java.nio.IntBuffer) UTF8CodePointDecoder(org.antlr.v4.runtime.UTF8CodePointDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 62 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUTF8CodePointDecoder method decodingInvalidLeadInReplaceModeWritesSubstitutionCharacter.

@Test
public void decodingInvalidLeadInReplaceModeWritesSubstitutionCharacter() throws Exception {
    UTF8CodePointDecoder decoder = new UTF8CodePointDecoder(CodingErrorAction.REPLACE);
    ByteBuffer utf8BytesIn = ByteBuffer.wrap(new byte[] { (byte) 0xF8 });
    IntBuffer codePointsOut = IntBuffer.allocate(1);
    IntBuffer result = decoder.decodeCodePointsFromBuffer(utf8BytesIn, codePointsOut, true);
    result.flip();
    assertEquals(1, result.remaining());
    assertEquals(0xFFFD, result.get(0));
}
Also used : IntBuffer(java.nio.IntBuffer) UTF8CodePointDecoder(org.antlr.v4.runtime.UTF8CodePointDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 63 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUTF8CodePointDecoder method decodeEmojiByteBufferWritesCodePoint.

@Test
public void decodeEmojiByteBufferWritesCodePoint() throws Exception {
    UTF8CodePointDecoder decoder = new UTF8CodePointDecoder(CodingErrorAction.REPLACE);
    ByteBuffer utf8BytesIn = StandardCharsets.UTF_8.encode(new StringBuilder().appendCodePoint(0x1F4A9).toString());
    IntBuffer codePointsOut = IntBuffer.allocate(1);
    IntBuffer result = decoder.decodeCodePointsFromBuffer(utf8BytesIn, codePointsOut, true);
    result.flip();
    assertEquals(1, result.remaining());
    assertEquals(0x1F4A9, result.get(0));
}
Also used : IntBuffer(java.nio.IntBuffer) UTF8CodePointDecoder(org.antlr.v4.runtime.UTF8CodePointDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 64 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUTF8CodePointDecoder method decodeEmptyByteBufferWritesNothing.

@Test
public void decodeEmptyByteBufferWritesNothing() throws Exception {
    UTF8CodePointDecoder decoder = new UTF8CodePointDecoder(CodingErrorAction.REPLACE);
    ByteBuffer utf8BytesIn = ByteBuffer.allocate(0);
    IntBuffer codePointsOut = IntBuffer.allocate(0);
    IntBuffer result = decoder.decodeCodePointsFromBuffer(utf8BytesIn, codePointsOut, true);
    result.flip();
    assertEquals(0, result.remaining());
}
Also used : IntBuffer(java.nio.IntBuffer) UTF8CodePointDecoder(org.antlr.v4.runtime.UTF8CodePointDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 65 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestTokenStreamRewriter method testCombineInserts.

@Test
public void testCombineInserts() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "abc";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.insertBefore(0, "x");
    tokens.insertBefore(0, "y");
    String result = tokens.getText();
    String expecting = "yxabc";
    assertEquals(expecting, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) TokenStreamRewriter(org.antlr.v4.runtime.TokenStreamRewriter) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)131 LexerGrammar (org.antlr.v4.tool.LexerGrammar)78 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)52 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)49 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)44 ATN (org.antlr.v4.runtime.atn.ATN)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)38 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)38 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)38 ParseTree (org.antlr.v4.runtime.tree.ParseTree)29 ATNState (org.antlr.v4.runtime.atn.ATNState)11 Grammar (org.antlr.v4.tool.Grammar)10 STGroupString (org.stringtemplate.v4.STGroupString)10 ByteBuffer (java.nio.ByteBuffer)8 IntBuffer (java.nio.IntBuffer)8 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)8 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 DOTGenerator (org.antlr.v4.tool.DOTGenerator)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 Rule (org.antlr.v4.tool.Rule)7