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

The following examples show how to use org.antlr.runtime.TokenRewriteStream#replace() . 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 test2InsertThenReplaceIndex0() 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, "x");
	tokens.insertBefore(0, "y");
	tokens.replace(0, "z");
	String result = tokens.toString();
	String expecting = "zbc";
	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 testOverlappingReplace4() 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, 3, "bar"); // wipes prior nested replace
	String result = tokens.toString();
	String expecting = "abar";
	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 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 4
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenReplaceLowerIndexedSuperset() 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, "xyz");
	tokens.replace(1, 3, "foo"); // overlap, error
	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]:\"xyz\">";
	assertNotNull(exc);
	assertEquals(expecting, exc.getMessage());
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenInsertBeforeLastIndex() 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.insertBefore(2, "y");
	String result = tokens.toString();
	String expecting = "abyx";
	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 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 11
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testInsertInPriorReplace() 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, "x");
	tokens.insertBefore(1, "0");
	Exception exc = null;
	try {
		tokens.toString();
	}
	catch (IllegalArgumentException iae) {
		exc = iae;
	}
	String expecting = "insert op <InsertBeforeOp@1:\"0\"> within boundaries of previous <[email protected]:\"x\">";
	assertNotNull(exc);
	assertEquals(expecting, exc.getMessage());
}
 
Example 12
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenDeleteMiddleIndex() 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.delete(1);
	String result = tokens.toString();
	String expecting = "ac";
	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 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 14
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testOverlappingReplace() 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, 3, "bar"); // wipes prior nested replace
	String result = tokens.toString();
	String expecting = "bar";
	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 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 16
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 17
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReplaceIndex0() 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, "x");
	String result = tokens.toString();
	String expecting = "xbc";
	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 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 19
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testReplaceMiddleIndex() 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");
	String result = tokens.toString();
	String expecting = "axc";
	assertEquals(expecting, result);
}
 
Example 20
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
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);
}