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 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; ICharStream input = new ANTLRStringStream("x = 3 * 0;"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(4, 8, "0"); // replace 3 * 0 with 0 string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(); expecting = "x = 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 9); expecting = "x = 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(4, 8); expecting = "0"; Assert.AreEqual(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 TestToStringStartStop() { 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; ICharStream input = new ANTLRStringStream( "x = 3 * 0;" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 4, 8, "0" ); // replace 3 * 0 with 0 string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString(); expecting = "x = 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 9 ); expecting = "x = 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 4, 8 ); expecting = "0"; Assert.AreEqual( expecting, result ); }