Example #1
0
        public void TestCompleteBuffer()
        {
            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);

            int    i = 1;
            IToken t = tokens.LT(i);

            while (t.Type != CharStreamConstants.EndOfFile)
            {
                i++;
                t = tokens.LT(i);
            }
            tokens.LT(i++); // push it past end
            tokens.LT(i++);

            string result    = tokens.ToString();
            string expecting = "x = 3 * 0 + 2 * 0;";

            Assert.AreEqual(expecting, result);
        }
Example #2
0
        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);
        }
Example #3
0
        public void Test2ndToken()
        {
            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);

            string result    = tokens.LT(2).Text;
            string expecting = " ";

            Assert.AreEqual(expecting, result);
        }