Search in sources :

Example 86 with CommonTokenStream

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

the class TestTokenStreamRewriter method testToStringStartStop2.

@Test
public void testToStringStartStop2() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    String input = "x = 3 * 0 + 2 * 0;";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    String result = tokens.getTokenStream().getText();
    String expecting = "x = 3 * 0 + 2 * 0;";
    assertEquals(expecting, result);
    tokens.replace(4, 8, "0");
    stream.fill();
    // replace 3 * 0 with 0
    result = tokens.getText();
    expecting = "x = 0 + 2 * 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(0, 17));
    expecting = "x = 0 + 2 * 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(4, 8));
    expecting = "0";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(0, 8));
    expecting = "x = 0";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(12, 16));
    expecting = "2 * 0";
    assertEquals(expecting, result);
    tokens.insertAfter(17, "// comment");
    result = tokens.getText(Interval.of(12, 18));
    expecting = "2 * 0;// comment";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(0, 8));
    stream.fill();
    // try again after insert at end
    expecting = "x = 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 87 with CommonTokenStream

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

the class TestTokenStreamRewriter method testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder.

// Test Fix for https://github.com/antlr/antlr4/issues/550
@Test
public void testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder() 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, "<b>");
    tokens.insertAfter(0, "</b>");
    tokens.insertBefore(1, "<b>");
    tokens.insertAfter(1, "</b>");
    String result = tokens.getText();
    // fails with <b>a<b></b>a</b>"
    String expecting = "<b>a</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 88 with CommonTokenStream

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

the class TestTokenStreamRewriter method test2ReplaceMiddleIndex.

@Test
public void test2ReplaceMiddleIndex() 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.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 89 with CommonTokenStream

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

the class TestTokenStreamRewriter method testCombineInsertOnLeftWithDelete.

@Test
public void testCombineInsertOnLeftWithDelete() 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.delete(0, 2);
    tokens.insertBefore(0, "z");
    stream.fill();
    // combine with left edge of rewrite
    String result = tokens.getText();
    String expecting = "z";
    stream.fill();
    // make sure combo is not znull
    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 90 with CommonTokenStream

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

the class TestTokenStreamRewriter method testLeaveAloneDisjointInsert2.

@Test
public void testLeaveAloneDisjointInsert2() 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(2, 3, "foo");
    tokens.insertBefore(1, "x");
    String result = tokens.getText();
    String expecting = "axbfoo";
    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

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)109 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)86 Test (org.junit.Test)54 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)52 LexerGrammar (org.antlr.v4.tool.LexerGrammar)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParseTree (org.antlr.v4.runtime.tree.ParseTree)20 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)12 IOException (java.io.IOException)10 RecognitionException (org.antlr.v4.runtime.RecognitionException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)7 ArrayList (java.util.ArrayList)7 Token (org.antlr.v4.runtime.Token)7 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)6 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)6 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)6 CommonToken (org.antlr.v4.runtime.CommonToken)5