예제 #1
0
        public void LexerBackTrackingThrowsIfCalledWithNonFullBufferTooManyTimes()
        {
            var reporter = new ErrorReporter();
            var lexer = new Lexer(@"..\..\example_program.txt", reporter);

            bool throws = false;
            try
            {
               lexer.Backtrack();
            }
            catch (InternalCompilerError e)
            {
                throws = true;
            }

            Assert.IsTrue(throws);
            Assert.AreEqual(0, reporter.Errors.Count);
        }
예제 #2
0
        public void LexerPeekingWorksWithBacktracking()
        {
            var reporter = new ErrorReporter();
            var lexer = new Lexer(@"..\..\example_program.txt", reporter);

            lexer.NextToken();
            lexer.NextToken();
            lexer.NextToken();
            Assert.AreEqual(new VarToken(), lexer.PeekToken());

            lexer.Backtrack();
            Assert.AreEqual(new SemicolonToken(), lexer.PeekToken());
            Assert.AreEqual(new SemicolonToken(), lexer.NextToken());
            Assert.AreEqual(new VarToken(), lexer.PeekToken());
            Assert.AreEqual(new VarToken(), lexer.PeekToken());
            lexer.Backtrack();
            lexer.Backtrack();

            Assert.AreEqual(new TextToken("Give a number"), lexer.PeekToken());
            Assert.AreEqual(new TextToken("Give a number"), lexer.NextToken());
            Assert.AreEqual(0, reporter.Errors.Count);
        }
예제 #3
0
        public void LexerBackTrackingThrowsIfCalledTooManyTimes()
        {
            var reporter = new ErrorReporter();
            var lexer = new Lexer(@"..\..\example_program.txt", reporter);

            Assert.AreEqual(new PrintToken(), lexer.NextToken());
            Assert.AreEqual(new TextToken("Give a number"), lexer.NextToken());
            Assert.AreEqual(new SemicolonToken(), lexer.NextToken());
            lexer.NextToken();

            for (int i = 0; i < Lexer.BACKTRACK_BUFFER_SIZE; ++i)
            {
                lexer.Backtrack();
            }

            bool throws = false;
            try
            {
               lexer.Backtrack();
            } catch (InternalCompilerError e)
            {
                throws = true;
            }

            Assert.IsTrue(throws);
            Assert.AreEqual(0, reporter.Errors.Count);
        }