use of org.antlr.v4.runtime.ANTLRInputStream 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.ANTLRInputStream 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);
}
use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceSingleMiddleThenOverlappingSuperset.
@Test
public void testReplaceSingleMiddleThenOverlappingSuperset() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abcba";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.replace(2, 2, "xyz");
tokens.replace(0, 3, "foo");
String result = tokens.getText();
String expecting = "fooa";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceAll.
@Test
public void testReplaceAll() 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(0, 6, "x");
String result = tokens.getText();
String expecting = "x";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceThenReplaceSuperset.
@Test
public void testReplaceThenReplaceSuperset() 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");
tokens.replace(3, 5, "foo");
stream.fill();
// overlaps, error
Exception exc = null;
try {
tokens.getText();
} catch (IllegalArgumentException iae) {
exc = iae;
}
String expecting = "replace op boundaries of <ReplaceOp@[@3,3:3='c',<3>,1:3]..[@5,5:5='b',<2>,1:5]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">";
assertNotNull(exc);
assertEquals(expecting, exc.getMessage());
}
Aggregations