コード例 #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);
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: DFA.cs プロジェクト: mahanteshck/antlrcs
 public virtual int Predict( IIntStream input )
 {
     Interpreter interp = new Interpreter( Nfa.Grammar, input );
     return interp.Predict( this );
 }
コード例 #4
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);
        }
コード例 #5
0
ファイル: Interpreter.cs プロジェクト: bszafko/antlrcs
 public LexerActionGetTokenType( Interpreter outer, Grammar g )
 {
     this.outer = outer;
     this.g = g;
 }