org.antlr.runtime.TokenRewriteStream Java Examples

The following examples show how to use org.antlr.runtime.TokenRewriteStream. 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 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 #2
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenReplaceSuperset() 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(3, 5, "foo"); // overlaps, 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 #3
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 #4
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceSingleMiddleThenOverlappingSuperset() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcba");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, 2, "xyz");
	tokens.replace(0, 3, "foo");
	String result = tokens.toString();
	String expecting = "fooa";
	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 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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testCombineInserts() 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");
	String result = tokens.toString();
	String expecting = "yxabc";
	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 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 #15
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testLeaveAloneDisjointInsert() 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, "x");
	tokens.replace(2, 3, "foo");
	String result = tokens.toString();
	String expecting = "axbfoo";
	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 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 #17
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/** Test basic || expression */
@Test
public void selectAll() throws RecognitionException {

    String queryString = "select * where a = 1 or b > 2";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    Collection<SelectFieldMapping> identifiers = query.getSelectFieldMappings();

    assertEquals( 0, identifiers.size() );
}
 
Example #18
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectGeo() throws RecognitionException {
    String queryString = "select * where a within .1 of -40.343666, 175.630917";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    WithinOperand operand = ( WithinOperand ) query.getRootOperand();

    assertEquals( "a", operand.getProperty().getValue() );
    assertEquals( .1f, operand.getDistance().getFloatValue(), 0 );
    assertEquals( -40.343666f, operand.getLatitude().getFloatValue(), 0 );
    assertEquals( 175.630917f, operand.getLongitude().getFloatValue(), 0 );
}
 
Example #19
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectGeoWithInt() throws RecognitionException {
    String queryString = "select * where a within 1 of -40.343666, 175.630917";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    WithinOperand operand = ( WithinOperand ) query.getRootOperand();

    assertEquals( "a", operand.getProperty().getValue() );
    assertEquals( 1f, operand.getDistance().getFloatValue(), 0 );
    assertEquals( -40.343666f, operand.getLatitude().getFloatValue(), 0 );
    assertEquals( 175.630917f, operand.getLongitude().getFloatValue(), 0 );
}
 
Example #20
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectGeoWithAnd() throws RecognitionException {
    String queryString = "select * where location within 20000 of 37,-75 "
            + "and created > 1407776999925 and created < 1407777000266";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    AndOperand andOp1 = ( AndOperand ) query.getRootOperand();
    AndOperand andOp2 = ( AndOperand ) andOp1.getLeft();
    WithinOperand withinOperand = ( WithinOperand ) andOp2.getLeft();

    assertEquals( "location", withinOperand.getProperty().getValue() );
    assertEquals( 20000, withinOperand.getDistance().getFloatValue(), 0 );
    assertEquals( 37f, withinOperand.getLatitude().getFloatValue(), 0 );
    assertEquals( -75f, withinOperand.getLongitude().getFloatValue(), 0 );


}
 
Example #21
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectDistance() throws RecognitionException {
    String queryString = "select * where a contains 'foo'";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    ContainsOperand operand = ( ContainsOperand ) query.getRootOperand();

    assertEquals( "a", operand.getProperty().getValue() );
    assertEquals( "foo", operand.getString().getValue() );
}
 
Example #22
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void stringLower() throws Exception {
    String queryString = "select * where  title = 'Hot'";


    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    Equal rootNode = ( Equal ) query.getRootOperand();

    assertEquals( "title", rootNode.getProperty().getValue() );
    assertEquals( "hot", ( ( StringLiteral ) rootNode.getLiteral() ).getValue() );
}
 
Example #23
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void uuidParse() throws RecognitionException {
    String queryString = "select * where  title = c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    Equal rootNode = ( Equal ) query.getRootOperand();

    assertEquals( "title", rootNode.getProperty().getValue() );
    assertEquals( UUID.fromString( "c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0" ),
            ( ( UUIDLiteral ) rootNode.getLiteral() ).getValue() );
}
 
Example #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
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);
}