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; ICharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0 + 2 * 0;"; Assert.AreEqual(expecting, result); tokens.Replace(4, 8, "0"); // replace 3 * 0 with 0 result = tokens.ToString(); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 17); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(4, 8); expecting = "0"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 8); expecting = "x = 0"; Assert.AreEqual(expecting, result); result = tokens.ToString(12, 16); expecting = "2 * 0"; Assert.AreEqual(expecting, result); tokens.InsertAfter(17, "// comment"); result = tokens.ToString(12, 18); expecting = "2 * 0;// comment"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 8); // try again after insert at end expecting = "x = 0"; Assert.AreEqual(expecting, result); }
public void TestInsertAfterLastIndex() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.InsertAfter(2, "x"); string result = tokens.ToString(); string expecting = "abcx"; Assert.AreEqual(expecting, result); }
public void Test2InsertBeforeAfterMiddleIndex() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.InsertBefore( 1, "x" ); tokens.InsertAfter( 1, "x" ); string result = tokens.ToString(); string expecting = "axbxc"; Assert.AreEqual( expecting, result ); }
public void TestReplaceRangeThenInsertAfterRightEdge() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 4, "x"); tokens.InsertAfter(4, "y"); string result = tokens.ToString(); string expecting = "abxyba"; Assert.AreEqual(expecting, result); }
public void Test2InsertBeforeAfterMiddleIndex() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.InsertBefore(1, "x"); tokens.InsertAfter(1, "x"); string result = tokens.ToString(); string expecting = "axbxc"; assertEquals(expecting, result); }
public void TestToStringStartStop2() { 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; ICharStream input = new ANTLRStringStream( "x = 3 * 0 + 2 * 0;" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); tokens.Replace( 4, 8, "0" ); // replace 3 * 0 with 0 result = tokens.ToString(); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 17 ); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 4, 8 ); expecting = "0"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 8 ); expecting = "x = 0"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 12, 16 ); expecting = "2 * 0"; Assert.AreEqual( expecting, result ); tokens.InsertAfter( 17, "// comment" ); result = tokens.ToString( 12, 18 ); expecting = "2 * 0;// comment"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 8 ); // try again after insert at end expecting = "x = 0"; Assert.AreEqual( expecting, result ); }
public void TestReplaceThenInsertAfterLastIndex() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, "x" ); tokens.InsertAfter( 2, "y" ); string result = tokens.ToString(); string expecting = "abxy"; Assert.AreEqual( expecting, result ); }
public void TestReplaceRangeThenInsertAfterRightEdge() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "x" ); tokens.InsertAfter( 4, "y" ); string result = tokens.ToString(); string expecting = "abxyba"; assertEquals( expecting, result ); }