use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceThenInsertAfterLastIndex.
@Test
public void testReplaceThenInsertAfterLastIndex() 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(2, "x");
tokens.insertAfter(2, "y");
String result = tokens.getText();
String expecting = "abxy";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceIndex0.
@Test
public void testReplaceIndex0() 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(0, "x");
String result = tokens.getText();
String expecting = "xbc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceRangeThenInsertAtRightEdge.
@Test
public void testReplaceRangeThenInsertAtRightEdge() 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.insertBefore(4, "y");
// no effect; within range of a replace
stream.fill();
Exception exc = null;
try {
tokens.getText();
} catch (IllegalArgumentException iae) {
exc = iae;
}
String expecting = "insert op <InsertBeforeOp@[@4,4:4='c',<3>,1:4]:\"y\"> within boundaries of previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"x\">";
assertNotNull(exc);
assertEquals(expecting, exc.getMessage());
}
use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceMiddleIndex.
@Test
public void testReplaceMiddleIndex() 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");
String result = tokens.getText();
String expecting = "axc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testDropPrevCoveredInsert.
@Test
public void testDropPrevCoveredInsert() 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(1, "foo");
tokens.replace(1, 2, "foo");
stream.fill();
// kill prev insert
String result = tokens.getText();
String expecting = "afoofoo";
assertEquals(expecting, result);
}
Aggregations