本文整理汇总了Java中org.antlr.runtime.TokenRewriteStream.toOriginalString方法的典型用法代码示例。如果您正苦于以下问题:Java TokenRewriteStream.toOriginalString方法的具体用法?Java TokenRewriteStream.toOriginalString怎么用?Java TokenRewriteStream.toOriginalString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.antlr.runtime.TokenRewriteStream
的用法示例。
在下文中一共展示了TokenRewriteStream.toOriginalString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testToStringStartStop
import org.antlr.runtime.TokenRewriteStream; //导入方法依赖的package包/类
public void testToStringStartStop() throws Exception {
Grammar g = new Grammar(
"lexer grammar t;\n"+
"ID : 'a'..'z'+;\n" +
"INT : '0'..'9'+;\n" +
"SEMI : ';';\n" +
"MUL : '*';\n" +
"ASSIGN : '=';\n" +
"WS : ' '+;\n");
// Tokens: 0123456789
// Input: x = 3 * 0;
CharStream input = new ANTLRStringStream("x = 3 * 0;");
Interpreter lexEngine = new Interpreter(g, input);
TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
tokens.LT(1); // fill buffer
tokens.replace(4, 8, "0"); // replace 3 * 0 with 0
String result = tokens.toOriginalString();
String expecting = "x = 3 * 0;";
assertEquals(expecting, result);
result = tokens.toString();
expecting = "x = 0;";
assertEquals(expecting, result);
result = tokens.toString(0,9);
expecting = "x = 0;";
assertEquals(expecting, result);
result = tokens.toString(4,8);
expecting = "0";
assertEquals(expecting, result);
}
示例2: testToStringStartStop2
import org.antlr.runtime.TokenRewriteStream; //导入方法依赖的package包/类
public void testToStringStartStop2() throws Exception {
Grammar g = new Grammar(
"lexer grammar t;\n"+
"ID : 'a'..'z'+;\n" +
"INT : '0'..'9'+;\n" +
"SEMI : ';';\n" +
"ASSIGN : '=';\n" +
"PLUS : '+';\n" +
"MULT : '*';\n" +
"WS : ' '+;\n");
// Tokens: 012345678901234567
// Input: x = 3 * 0 + 2 * 0;
CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
Interpreter lexEngine = new Interpreter(g, input);
TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
tokens.LT(1); // fill buffer
String result = tokens.toOriginalString();
String expecting = "x = 3 * 0 + 2 * 0;";
assertEquals(expecting, result);
tokens.replace(4, 8, "0"); // replace 3 * 0 with 0
result = tokens.toString();
expecting = "x = 0 + 2 * 0;";
assertEquals(expecting, result);
result = tokens.toString(0,17);
expecting = "x = 0 + 2 * 0;";
assertEquals(expecting, result);
result = tokens.toString(4,8);
expecting = "0";
assertEquals(expecting, result);
result = tokens.toString(0,8);
expecting = "x = 0";
assertEquals(expecting, result);
result = tokens.toString(12,16);
expecting = "2 * 0";
assertEquals(expecting, result);
tokens.insertAfter(17, "// comment");
result = tokens.toString(12,17);
expecting = "2 * 0;// comment";
assertEquals(expecting, result);
result = tokens.toString(0,8); // try again after insert at end
expecting = "x = 0";
assertEquals(expecting, result);
}