use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceLastIndex.
@Test
public void testReplaceLastIndex() 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");
String result = tokens.getText();
String expecting = "abx";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testInsertBeforeIndex0.
@Test
public void testInsertBeforeIndex0() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream("abc"));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.insertBefore(0, "0");
String result = tokens.getText();
String expecting = "0abc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testOverlappingReplace2.
@Test
public void testOverlappingReplace2() 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(0, 3, "bar");
tokens.replace(1, 2, "foo");
stream.fill();
// cannot split earlier replace
Exception exc = null;
try {
tokens.getText();
} catch (IllegalArgumentException iae) {
exc = iae;
}
String expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<2>,1:1]..[@2,2:2='c',<3>,1:2]:\"foo\"> overlap with previous <ReplaceOp@[@0,0:0='a',<1>,1:0]..[@3,3:3='c',<3>,1:3]:\"bar\">";
assertNotNull(exc);
assertEquals(expecting, exc.getMessage());
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method test2InsertThenReplaceIndex0.
@Test
public void test2InsertThenReplaceIndex0() 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");
tokens.replace(0, "z");
String result = tokens.getText();
String expecting = "yxzbc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testOverlappingReplace4.
@Test
public void testOverlappingReplace4() 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(1, 3, "bar");
stream.fill();
// wipes prior nested replace
String result = tokens.getText();
String expecting = "abar";
assertEquals(expecting, result);
}
Aggregations