use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testInsertAfterLastIndex.
@Test
public void testInsertAfterLastIndex() 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.insertAfter(2, "x");
String result = tokens.getText();
String expecting = "abcx";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.TokenStreamRewriter in project antlr4 by antlr.
the class TestTokenStreamRewriter method test2InsertMiddleIndex.
@Test
public void test2InsertMiddleIndex() 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, "x");
tokens.insertBefore(1, "y");
String result = tokens.getText();
String expecting = "ayxbc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.TokenStreamRewriter 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.TokenStreamRewriter 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.TokenStreamRewriter 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());
}
Aggregations