Search in sources :

Example 31 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.

the class TestTokenStreamRewriter method testToStringStartStop.

@Test
public void testToStringStartStop() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "MUL : '*';\n" + "ASSIGN : '=';\n" + "WS : ' '+;\n");
    // Tokens: 0123456789
    // Input:  x = 3 * 0;
    String input = "x = 3 * 0;";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(4, 8, "0");
    stream.fill();
    // replace 3 * 0 with 0
    String result = tokens.getTokenStream().getText();
    String expecting = "x = 3 * 0;";
    assertEquals(expecting, result);
    result = tokens.getText();
    expecting = "x = 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(0, 9));
    expecting = "x = 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(4, 8));
    expecting = "0";
    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)

Example 32 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.

the class TestTokenStreamRewriter method test2ReplaceMiddleIndex1InsertBefore.

@Test
public void test2ReplaceMiddleIndex1InsertBefore() 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, "_");
    tokens.replace(1, "x");
    tokens.replace(1, "y");
    String result = tokens.getText();
    String expecting = "_ayc";
    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)

Example 33 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.

the class TestTokenStreamRewriter method testOverlappingReplace3.

@Test
public void testOverlappingReplace3() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "abcc";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(1, 2, "foo");
    tokens.replace(0, 2, "bar");
    stream.fill();
    // wipes prior nested replace
    String result = tokens.getText();
    String expecting = "barc";
    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)

Example 34 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.

the class TestTokenStreamRewriter method testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder2.

@Test
public void testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder2() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "aa";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.insertBefore(0, "<p>");
    tokens.insertBefore(0, "<b>");
    tokens.insertAfter(0, "</p>");
    tokens.insertAfter(0, "</b>");
    tokens.insertBefore(1, "<b>");
    tokens.insertAfter(1, "</b>");
    String result = tokens.getText();
    String expecting = "<b><p>a</p></b><b>a</b>";
    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)

Example 35 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.

the class TestTokenStreamRewriter method testInsertThenReplaceSameIndex.

@Test
public void testInsertThenReplaceSameIndex() 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, "0");
    tokens.replace(0, "x");
    stream.fill();
    // supercedes insert at 0
    String result = tokens.getText();
    String expecting = "0xbc";
    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

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)110 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)86 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)59 Test (org.junit.Test)59 LexerGrammar (org.antlr.v4.tool.LexerGrammar)52 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 CharStream (org.antlr.v4.runtime.CharStream)15 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 Token (org.antlr.v4.runtime.Token)12 TokenStream (org.antlr.v4.runtime.TokenStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 StringReader (java.io.StringReader)7 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 BufferedTokenStream (org.antlr.v4.runtime.BufferedTokenStream)6 IntegerList (org.antlr.v4.runtime.misc.IntegerList)6 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)5