use of org.antlr.v4.runtime.LexerInterpreter 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);
}
use of org.antlr.v4.runtime.LexerInterpreter 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);
}
use of org.antlr.v4.runtime.LexerInterpreter 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);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceRangeThenInsertAfterRightEdge.
@Test
public void testReplaceRangeThenInsertAfterRightEdge() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abcccba";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.replace(2, 4, "x");
tokens.insertAfter(4, "y");
String result = tokens.getText();
String expecting = "abxyba";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceThenDeleteMiddleIndex.
@Test
public void testReplaceThenDeleteMiddleIndex() 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.delete(1);
String result = tokens.getText();
String expecting = "ac";
assertEquals(expecting, result);
}
Aggregations