public void TestLookback() { 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); BufferedTokenStream tokens = new BufferedTokenStream(lexEngine); tokens.Consume(); // get x into buffer IToken t = tokens.LT(-1); Assert.AreEqual("x", t.Text); tokens.Consume(); tokens.Consume(); // consume '=' t = tokens.LT(-3); Assert.AreEqual("x", t.Text); t = tokens.LT(-2); Assert.AreEqual(" ", t.Text); t = tokens.LT(-1); Assert.AreEqual("=", t.Text); }
public void TestCompleteBufferAfterConsuming() { 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); BufferedTokenStream tokens = new BufferedTokenStream(lexEngine); IToken t = tokens.LT(1); while (t.Type != CharStreamConstants.EndOfFile) { tokens.Consume(); t = tokens.LT(1); } tokens.Consume(); tokens.LT(1); // push it past end tokens.Consume(); tokens.LT(1); string result = tokens.ToString(); string expecting = "x = 3 * 0 + 2 * 0;"; Assert.AreEqual(expecting, result); }