use of org.antlr.v4.runtime.LexerInterpreter 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);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testLeaveAloneDisjointInsert.
@Test
public void testLeaveAloneDisjointInsert() 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.insertBefore(1, "x");
tokens.replace(2, 3, "foo");
String result = tokens.getText();
String expecting = "axbfoo";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testPreservesOrderOfContiguousInserts.
// Test Fix for https://github.com/antlr/antlr4/issues/550
@Test
public void testPreservesOrderOfContiguousInserts() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "ab";
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.insertBefore(0, "<div>");
tokens.insertAfter(0, "</p>");
tokens.insertAfter(0, "</b>");
tokens.insertAfter(0, "</div>");
tokens.insertBefore(1, "!");
String result = tokens.getText();
String expecting = "<div><b><p>a</p></b></div>!b";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testDisjointInserts.
@Test
public void testDisjointInserts() 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(2, "y");
tokens.insertBefore(0, "z");
String result = tokens.getText();
String expecting = "zaxbyc";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceSubsetThenFetch.
@Test
public void testReplaceSubsetThenFetch() 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, "xyz");
String result = tokens.getText(Interval.of(0, 6));
String expecting = "abxyzba";
assertEquals(expecting, result);
}
Aggregations