示例#1
0
        public void NestedBacktracking()
        {
            var tokens = new List <Token>
            {
                new IdentifierToken("x"),
                new SymbolToken(SymbolType.Equals),
                new IdentifierToken("3")
            };

            var enumerator = new TokenEnumerator(tokens.ToArray());

            Assert.AreEqual(new IdentifierToken("x"), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            enumerator.SetBacktrackPoint();
            Assert.AreEqual(new SymbolToken(SymbolType.Equals), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            enumerator.SetBacktrackPoint();
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            enumerator.Backtrack();
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            enumerator.Backtrack();
            Assert.AreEqual(new SymbolToken(SymbolType.Equals), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            Assert.AreEqual(null, enumerator.Current);
        }
示例#2
0
        private T ParseWith <T>(params Func <T>[] methods)
            where T : class
        {
            foreach (var method in methods)
            {
                tokens.SetBacktrackPoint();

                T result = method();

                if (!(result is null))
                {
                    return(result);
                }

                tokens.Backtrack();
            }

            return(null);
        }