Java Code Examples for org.antlr.runtime.TokenRewriteStream#toString()

The following examples show how to use org.antlr.runtime.TokenRewriteStream#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testDisjointInserts() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "x");
	tokens.insertBefore(2, "y");
	tokens.insertBefore(0, "z");
	String result = tokens.toString();
	String expecting = "zaxbyc";
	assertEquals(expecting, result);
}
 
Example 2
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testCombine3Inserts() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "x");
	tokens.insertBefore(0, "y");
	tokens.insertBefore(1, "z");
	String result = tokens.toString();
	String expecting = "yazxbc";
	assertEquals(expecting, result);
}
 
Example 3
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceRangeThenInsertAtLeftEdge() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcccba");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, 4, "x");
	tokens.insertBefore(2, "y");
	String result = tokens.toString();
	String expecting = "abyxba";
	assertEquals(expecting, result);
}
 
Example 4
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenInsertAfterLastIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, "x");
	tokens.insertAfter(2, "y");
	String result = tokens.toString();
	String expecting = "abxy";
	assertEquals(expecting, result);
}
 
Example 5
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testInsertThenReplaceLastIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(2, "y");
	tokens.replace(2, "x");
	String result = tokens.toString();
	String expecting = "abx";
	assertEquals(expecting, result);
}
 
Example 6
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testOverlappingReplace2() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(0, 3, "bar");
	tokens.replace(1, 2, "foo"); // cannot split earlier replace
	Exception exc = null;
	try {
		tokens.toString();
	}
	catch (IllegalArgumentException iae) {
		exc = iae;
	}
	String expecting = "replace op boundaries of <[email protected]:\"foo\"> overlap with previous <[email protected]:\"bar\">";
	assertNotNull(exc);
	assertEquals(expecting, exc.getMessage());
}
 
Example 7
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testDropPrevCoveredInsert() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "foo");
	tokens.replace(1, 2, "foo"); // kill prev insert
	String result = tokens.toString();
	String expecting = "afooc";
	assertEquals(expecting, result);
}
 
Example 8
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testDropIdenticalReplace() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(1, 2, "foo");
	tokens.replace(1, 2, "foo"); // drop previous, identical
	String result = tokens.toString();
	String expecting = "afooc";
	assertEquals(expecting, result);
}
 
Example 9
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testInsertThenReplaceSameIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(0, "0");
	tokens.replace(0, "x"); // supercedes insert at 0
	String result = tokens.toString();
	String expecting = "xbc";
	assertEquals(expecting, result);
}
 
Example 10
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testCombineInsertOnLeftWithDelete() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.delete(0, 2);
	tokens.insertBefore(0, "z"); // combine with left edge of rewrite
	String result = tokens.toString();
	String expecting = "z"; // make sure combo is not znull
	assertEquals(expecting, result);
}
 
Example 11
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void test2ReplaceMiddleIndex1InsertBefore() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
       tokens.insertBefore(0, "_");
       tokens.replace(1, "x");
	tokens.replace(1, "y");
	String result = tokens.toString();
	String expecting = "_ayc";
	assertEquals(expecting, result);
}
 
Example 12
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void test2ReplaceMiddleIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(1, "x");
	tokens.replace(1, "y");
	String result = tokens.toString();
	String expecting = "ayc";
	assertEquals(expecting, result);
}
 
Example 13
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testLeaveAloneDisjointInsert2() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, 3, "foo");
	tokens.insertBefore(1, "x");
	String result = tokens.toString();
	String expecting = "axbfoo";
	assertEquals(expecting, result);
}
 
Example 14
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testOverlappingReplace3() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(1, 2, "foo");
	tokens.replace(0, 2, "bar"); // wipes prior nested replace
	String result = tokens.toString();
	String expecting = "barc";
	assertEquals(expecting, result);
}
 
Example 15
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testCombineInsertOnLeftWithReplace() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(0, 2, "foo");
	tokens.insertBefore(0, "z"); // combine with left edge of rewrite
	String result = tokens.toString();
	String expecting = "zfoo";
	assertEquals(expecting, result);
}
 
Example 16
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void test2InsertBeforeAfterMiddleIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "x");
	tokens.insertAfter(1, "x");
	String result = tokens.toString();
	String expecting = "axbxc";
	assertEquals(expecting, result);
}
 
Example 17
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReplaceAll() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcccba");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(0, 6, "x");
	String result = tokens.toString();
	String expecting = "x";
	assertEquals(expecting, result);
}
 
Example 18
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReplaceLastIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, "x");
	String result = tokens.toString();
	String expecting = "abx";
	assertEquals(expecting, result);
}
 
Example 20
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testInsertBeforeIndex0() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(0, "0");
	String result = tokens.toString();
	String expecting = "0abc";
	assertEquals(expecting, result);
}