Esempio n. 1
0
 public void TestRecursion()
 {
     {
         Placeholder expr = new Placeholder();
         expr.Expression = new FirstOf(
             new Sequence(new Literal('1'), expr),
             new Literal('0')
             );
         {
             var    parser = new Parser(expr, TextCursor.Create("11110"));
             Result r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.IsFalse(r.Cursor.CanPop());
             Assert.IsTrue(r.Value is IVector);
             IVector v = (IVector)r.Value;
             Assert.AreEqual(2, v.Length);
             Assert.AreEqual('1', v[0]);
             Assert.IsTrue(v[1] is IVector);
             while (v[1] is IVector)
             {
                 v = (IVector)v[1];
             }
             Assert.AreEqual('0', v[1]);
         }
         {
             var    parser = new Parser(expr, TextCursor.Create("0"));
             Result r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.IsFalse(r.Cursor.CanPop());
             Assert.AreEqual('0', r.Value);
         }
     }
 }
Esempio n. 2
0
 public void TestSomeMatcher()
 {
     {
         var digit = new LiteralAnyCharOf("0123456789");
         var q     = new Sequence(
             Some.Optional(new LiteralAnyCharOf("-")),
             Some.ZeroOrMore(digit),
             new LiteralAnyCharOf("."),
             Some.OneOrMore(digit)
             );
         {
             var parser = new Parser(q, TextCursor.Create("-.52"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(4, r.Cursor.Position);
             Assert.IsTrue(r.Value is IVector);
             IVector v = (IVector)r.Value;
             Assert.AreEqual(4, v.Length);
             Assert.IsTrue(v[0] is IVector);
             IVector v0 = (IVector)v[0];
             Assert.AreEqual(1, v0.Length);
             Assert.AreEqual('-', v0[0]);
             Assert.IsTrue(v[1] is IVector);
             Assert.AreEqual(0, ((IVector)v[1]).Length);
             Assert.AreEqual('.', v[2]);
             Assert.IsTrue(v[3] is IVector);
             IVector v3 = (IVector)v[3];
             Assert.AreEqual(2, v3.Length);
             Assert.AreEqual('5', v3[0]);
             Assert.AreEqual('2', v3[1]);
         }
     }
 }
Esempio n. 3
0
 public void TestFirstOf()
 {
     {
         var q = new FirstOf(new LiteralAnyCharOf("a"), new LiteralAnyCharOf("+-"), new LiteralAnyCharOf("z"));
         {
             var parser = new Parser(q, TextCursor.Create(""));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(0, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create("+"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('+', r.Value);
         }
         {
             var parser = new Parser(q, TextCursor.Create("z"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('z', r.Value);
         }
     }
 }
Esempio n. 4
0
 public void TestMatchPrimitives()
 {
     {
         var q = new LiteralAnyCharOf("0123456789");
         {
             var parser = new Parser(q, TextCursor.Create("7"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('7', r.Value);
         }
         {
             var parser = new Parser(q, TextCursor.Create("78"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('7', r.Value);
         }
     }
     {
         var q = new Literal('z');
         {
             var parser = new Parser(q, TextCursor.Create("z"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.IsNull(parser.FailCursor);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('z', r.Value);
         }
         {
             var parser = new Parser(q, TextCursor.Create("0123"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(0, parser.FailCursor.Position);
         }
     }
     {
         var q = new LiteralEOI();
         {
             var parser = new Parser(q, TextCursor.Create(""));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.IsNull(parser.FailCursor);
             Assert.AreEqual(0, r.Cursor.Position);
             Assert.IsTrue(!r.Cursor.CanPop());
             Assert.AreEqual(null, r.Value);
         }
         {
             var parser = new Parser(q, TextCursor.Create("0123"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(0, parser.FailCursor.Position);
         }
     }
 }
Esempio n. 5
0
 public void TestComposition()
 {
     {
         var q = new FirstOf(
             new Sequence(new LiteralAnyCharOf("a"), new LiteralAnyCharOf("+-"), new LiteralAnyCharOf("z")),
             new Sequence(new LiteralAnyCharOf("a"), new LiteralAnyCharOf("z")),
             new LiteralAnyCharOf("?")
             );
         {
             var parser = new Parser(q, TextCursor.Create(""));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(0, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create("a+z"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(3, r.Cursor.Position);
             var v = r.Value as IVector;
             Assert.IsTrue(r.Value is IVector);
             Assert.AreEqual(3, v.Length);
             Assert.AreEqual('a', v[0]);
             Assert.AreEqual('+', v[1]);
             Assert.AreEqual('z', v[2]);
         }
         {
             var parser = new Parser(q, TextCursor.Create("az"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(2, r.Cursor.Position);
             var v = r.Value as IVector;
             Assert.IsTrue(r.Value is IVector);
             Assert.AreEqual(2, v.Length);
             Assert.AreEqual('a', v[0]);
             Assert.AreEqual('z', v[1]);
         }
         {
             var parser = new Parser(q, TextCursor.Create("?"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(1, r.Cursor.Position);
             Assert.AreEqual('?', r.Value);
         }
         {
             var parser = new Parser(q, TextCursor.Create("a?"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(1, parser.FailCursor.Position);
         }
     }
 }
Esempio n. 6
0
        public void LookAheadTest()
        {
            /*
             *   expr:
             *     term '+' expr
             | term
             |   ;
             |
             |   term:
             |     '(' expr ')'
             | term '!'
             | NUMBER
             |   ;
             * '1+2'
             */
            Placeholder expr = new Placeholder();
            Placeholder term = new Placeholder();

            expr.Expression = new FirstOf(
                new Sequence(term, new Literal('+'), expr),
                term);
            //--left recursive
            //term.Expression = new First(
            //    new Sequence(new Literal('('), expr, new Literal(')')),
            //    new Sequence(term, new Literal('!')),
            //    new First(new Literal('1'), new Literal('2')));
            //--replaced:
            Placeholder term_r = new Placeholder();

            term.Expression = new Sequence(new FirstOf(
                                               new Sequence(new Literal('('), expr, new Literal(')')),
                                               new FirstOf(new Literal('1'), new Literal('2'))),
                                           term_r);
            term_r.Expression = new FirstOf(
                new Sequence(new Literal('!'), term_r),
                new EmptyRule(null));

            {
                var    parser = new Parser(expr, TextCursor.Create("1+2"));
                Result r      = parser.Run();
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("1+2!"));
                Result r      = parser.Run();
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
            }
        }
Esempio n. 7
0
 public void TestNotPredicateMatcher()
 {
     {//nested comments example
         var allchars = new LiteralAnyCharOf("0123456789/* ");
         var _open    = new Sequence(new LiteralAnyCharOf("/"), new LiteralAnyCharOf("*"));
         var _close   = new Sequence(new LiteralAnyCharOf("*"), new LiteralAnyCharOf("/"));
         var _nested  = new Placeholder();
         var _block   = new Sequence(_open, Some.ZeroOrMore(_nested), _close);
         _nested.Expression = new FirstOf(_block, new Sequence(Predicate.Not(_open), Predicate.Not(_close), allchars));
         var q = new Sequence(Some.ZeroOrMore(_nested), new LiteralEOI());
         {
             var parser = new Parser(q, TextCursor.Create("  ** /"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("  **/"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             if (System.Diagnostics.Debugger.IsAttached)
             {
                 System.Diagnostics.Debug.WriteLine(parser.GetError());
             }
             Assert.AreEqual(3, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create(" 12 34 /* 5 6 7*8/*9 0/**/ */* * */"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create(" 12 34 /* 5 6 7*8/*9 0/* */* * */"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             if (System.Diagnostics.Debugger.IsAttached)
             {
                 System.Diagnostics.Debug.WriteLine(parser.GetError());
             }
             Assert.AreEqual(33, parser.FailCursor.Position);
         }
     }
 }
Esempio n. 8
0
 public void TestComplex()
 {
     Function toString = delegate(object r) {
         IVector       v  = (IVector)r;
         StringBuilder sb = new StringBuilder();
         for (int i = 0; i < v.Length; i++)
         {
             sb.Append(v[i]);
         }
         return(sb.ToString());
     };
     {
         var _digits = new LiteralAnyCharOf("0123456789");
         //var _letters = new LiteralAnyCharOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
         var _letters = new FirstOf(
             new LiteralCharCategory(System.Globalization.UnicodeCategory.LowercaseLetter),
             new LiteralCharCategory(System.Globalization.UnicodeCategory.UppercaseLetter),
             new LiteralCharCategory(System.Globalization.UnicodeCategory.TitlecaseLetter),
             new LiteralCharCategory(System.Globalization.UnicodeCategory.LetterNumber),
             new LiteralCharCategory(System.Globalization.UnicodeCategory.ModifierLetter),
             new LiteralCharCategory(System.Globalization.UnicodeCategory.OtherLetter)
             );
         var _number     = new CallbackHandler(Some.OneOrMore(_digits), toString);
         var _ident      = new CallbackHandler(new Sequence(_letters, new CallbackHandler(Some.ZeroOrMore(new FirstOf(_digits, _letters)), toString)), toString);
         var _whitespace = new LiteralAnyCharOf(" \t\n\r\f\v");
         var _comment    = new Sequence(new LiteralString("/*"), Some.ZeroOrMore(new Sequence(Predicate.Not(new LiteralString("*/")), new LiteralAny())), new LiteralString("*/"));
         var _atmosphere = new FirstOf(_whitespace, _comment);
         var _lexeme     = new FirstOf(new LiteralAnyCharOf("{"), new LiteralAnyCharOf("}"), new LiteralAnyCharOf(";"),
                                       new ExtractOne(0, new Sequence(new FirstOf(_ident, _number), Predicate.And(new FirstOf(new LiteralAnyCharOf("{};"), _atmosphere, new LiteralEOI())))));
         var q = new ExtractOne(1, new Sequence(Some.ZeroOrMore(_atmosphere), Some.ZeroOrMore(new ExtractOne(0, new Sequence(_lexeme, Some.ZeroOrMore(_atmosphere)))), new LiteralEOI()));
         {
             var parser = new Parser(q, TextCursor.Create("   a1 set plus 32434 Ratio; /* ffwe34kfn3k4u3f$#$df \n34f2$F@$F$F#$DF#E#44  432#@$# $%@$***/ define edit {f 2 xz /*****/ e} /*\r*/\r;\r"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             IVector v = r.Value as IVector;
             Assert.IsNotNull(v);
             Assert.AreEqual(15, v.Length);
             Assert.AreEqual("a1", v[0]);
             Assert.AreEqual('{', v[8]);
             Assert.AreEqual('}', v[13]);
             Assert.AreEqual(';', v[14]);
         }
     }
 }
Esempio n. 9
0
 public void TestSequence()
 {
     {
         var q = new Sequence(new LiteralAnyCharOf("abc"), new LiteralAnyCharOf("+-"), new LiteralAnyCharOf("def"), new LiteralEOI());
         {
             var parser = new Parser(q, TextCursor.Create(""));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(0, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create("abf"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(1, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create("a+a"));
             var r      = parser.Run();
             Assert.IsNull(r);
             Assert.IsNotNull(parser.FailCursor);
             Assert.AreEqual(2, parser.FailCursor.Position);
         }
         {
             var parser = new Parser(q, TextCursor.Create("b-d"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
             Assert.AreEqual(3, r.Cursor.Position);
             var v = r.Value as IVector;
             Assert.IsTrue(r.Value is IVector);
             Assert.AreEqual(4, v.Length);
             Assert.AreEqual('d', v[2]);
             Assert.AreEqual(null, v[3]);
         }
     }
 }
Esempio n. 10
0
        public void TestCalc2Repeaters(bool useArray)
        {
            IVectorFactory factory = useArray ? (IVectorFactory) new ArrayVectorFactory() : (IVectorFactory) new BNodeVectorFactory();

            /*
             *  E ← T ((‘+’ / ‘-’) T)*
             *  T ← F (('*' / '/') F)*
             *  F ← N / '(' E ')'
             *  N ← [0-9]+
             */
            var eNumber = new CollapseToString(Some.OneOrMore(new LiteralAnyCharOf("0123456789")));
            var factor  = new Placeholder();
            var term    = new Placeholder();
            var expr    = new Placeholder();

            factor.Expression = new FirstOf(eNumber, new ExtractOne(1, new Sequence(new Literal('('), expr, new Literal(')'))));
            Function toPrefix = z => {
                IVector v  = (IVector)z;
                object  v0 = v[0];
                IVector v1 = (IVector)v[1];
                if (v1 == factory.Empty)
                {
                    return(v0);
                }
                for (int i = 0; i < v1.Length; i++)
                {
                    IVector vv = (IVector)v1[i];
                    v0 = factory.Create(vv[0], v0, vv[1]);
                }
                return(v0);
            };

            term.Expression = new CallbackHandler(new Sequence(factor, Some.ZeroOrMore(new Sequence(new LiteralAnyCharOf("*/"), factor))), toPrefix);
            expr.Expression = new CallbackHandler(new Sequence(term, Some.ZeroOrMore(new Sequence(new LiteralAnyCharOf("+-"), term))), toPrefix);

            {
                var    parser   = new Parser(expr, TextCursor.Create("12+3*45+6+789*(6+2)*9+0"), factory);
                Result r        = parser.Run();
                string expected = "(+ (+ (+ (+ 12 (* 3 45)) 6) (* (* 789 (+ 6 2)) 9)) 0)";
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
                Assert.AreEqual(expected, Convert.ToString(r.Value).Replace("\"", "").Replace("'", ""));
                object num = new CalculatorVisitor().Process(r.Value);
                Assert.AreEqual(Convert.ToDouble(12 + 3 * 45 + 6 + 789 * (6 + 2) * 9 + 0), num);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("12+3*4a5+6+7*(6+2)*9+0"), factory);
                Result r      = parser.Run();
                Assert.IsNotNull(r);
                Assert.IsTrue(r.Cursor.CanPop());
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(6, parser.FailCursor.Position);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("12+3*45+(6+7*(6+2)*9+0"), factory);
                Result r      = parser.Run();
                Assert.IsNotNull(r);
                Assert.IsTrue(r.Cursor.CanPop());
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(22, parser.FailCursor.Position);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("(12+3*45+(6+7*(6+2)*9+0)"), factory);
                Result r      = parser.Run();
                Assert.IsNull(r);
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(24, parser.FailCursor.Position);
            }
        }
Esempio n. 11
0
        public void TestCalc2(bool useArray)
        {
            if (useArray)
            {
                LispPrinter.PrintVectorsAsLists = true;         //required to compare the result
            }
            IVectorFactory factory = useArray ? (IVectorFactory) new ArrayVectorFactory() : (IVectorFactory) new BNodeVectorFactory();

            /*
             *  E ← T ((‘+’ / ‘-’) T)*
             *  T ← F (('*' / '/') F)*
             *  F ← N / '(' E ')'
             *  N ← [0-9]+
             */
            Rule        eDigit  = new LiteralAnyCharOf("0123456789");
            Placeholder eNumber = new Placeholder();

            eNumber.Expression = new FirstOf(new CollapseToString(new Sequence(eDigit, eNumber)), new CallbackHandler(eDigit, Convert.ToString));
            Placeholder eAdditive       = new Placeholder();
            Placeholder eMultiplicative = new Placeholder();
            Rule        eSingular       = new FirstOf(eNumber, new ExtractOne(1, new Sequence(new Literal('('), eAdditive, new Literal(')'))));
            //
            var BinaryInfixToPrefix = new Function(delegate(object v) {
                IVector a    = (IVector)v;
                IVector tail = (IVector)a[1];
                object x     = a[0];
                while (tail != factory.Empty)
                {
                    x    = factory.Create(tail[0], x, tail[1]);
                    tail = (IVector)tail[2];
                }
                return(x);
            });
            Placeholder eAdditiveSuffix = new Placeholder();

            eAdditiveSuffix.Expression = new FirstOf(
                new Sequence(new LiteralAnyCharOf("+-"), eMultiplicative, eAdditiveSuffix),
                new EmptyRule(factory.Empty));
            eAdditive.Expression = new CallbackHandler(new Sequence(eMultiplicative, eAdditiveSuffix), BinaryInfixToPrefix);
            Placeholder eMultiplicativeSuffix = new Placeholder();

            eMultiplicativeSuffix.Expression = new FirstOf(
                new Sequence(new LiteralAnyCharOf("*/"), eSingular, eMultiplicativeSuffix),
                new EmptyRule(factory.Empty));
            eMultiplicative.Expression = new CallbackHandler(new Sequence(eSingular, eMultiplicativeSuffix), BinaryInfixToPrefix);
            Rule expr = eAdditive;

            {
                var    parser   = new Parser(expr, TextCursor.Create("12+3*45+6+789*(6+2)*9+0"), factory);
                Result r        = parser.Run();
                string expected = "(+ (+ (+ (+ 12 (* 3 45)) 6) (* (* 789 (+ 6 2)) 9)) 0)";
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
                Assert.AreEqual(expected, Convert.ToString(r.Value).Replace("\"", "").Replace("'", ""));
                object num = new CalculatorVisitor().Process(r.Value);
                Assert.AreEqual(Convert.ToDouble(12 + 3 * 45 + 6 + 789 * (6 + 2) * 9 + 0), num);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("12+3*4a5+6+7*(6+2)*9+0"), factory);
                Result r      = parser.Run();
                //Assert.IsNull(r);
                Assert.IsTrue(r.Cursor.CanPop());
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(6, parser.FailCursor.Position);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("12+3*45+(6+7*(6+2)*9+0"), factory);
                Result r      = parser.Run();
                //Assert.IsNull(r);
                Assert.IsTrue(r.Cursor.CanPop());
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(22, parser.FailCursor.Position);
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("(12+3*45+(6+7*(6+2)*9+0)"), factory);
                Result r      = parser.Run();
                Assert.IsNull(r);
                //Assert.AreNotEqual(TextCursor.EOI, r.Cursor.Peek());
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(parser.GetError());
                }
                Assert.AreEqual(24, parser.FailCursor.Position);
            }
        }
Esempio n. 12
0
        public void TestCalc1(bool useArray)
        {
            if (useArray)
            {
                LispPrinter.PrintVectorsAsLists = true;          //required to compare the results
            }
            IVectorFactory factory = useArray? (IVectorFactory) new ArrayVectorFactory() : (IVectorFactory) new BNodeVectorFactory();

            /*
             *  Expression ← Term ((‘+’ / ‘-’) Term)*
             *  Term ← Factor (('*' / '/') Factor)*
             *  Factor ← Number / '(' Expression ')'
             *  Number ← [0-9]+
             */
            Rule        eDigit  = new LiteralAnyCharOf("0123456789");
            Placeholder eNumber = new Placeholder();

            //eNumber.Expression = new First(new CallbackHandler( new Sequence(eDigit, eNumber), delegate(object v){
            //    object[] a = (object[])v;
            //    return string.Concat(a[0], a[1]);
            //}), eDigit);
            eNumber.Expression = new FirstOf(new CollapseToString(new Sequence(eDigit, eNumber)), new CallbackHandler(eDigit, Convert.ToString));
            //eNumber.Expression = new CallbackHandler(
            //    new First(new CollapseToString(new Sequence(eDigit, eNumber)), new CallbackHandler(eDigit, Convert.ToString)),
            //    delegate(object v) { return Int64.Parse((string)v); });
            //eNumber.Expression = new CollapseToArray(new First(new Sequence(eDigit, eNumber), new Sequence(eDigit, new TailStub())));
            Placeholder eAdditive       = new Placeholder();
            Placeholder eMultiplicative = new Placeholder();
            Rule        eSingular       = new FirstOf(eNumber,
                                                      new ExtractOne(1, new Sequence(new Literal('('), eAdditive, new Literal(')'))));
            //
            Placeholder eAdditiveSuffix = new Placeholder();

            eAdditiveSuffix.Expression = new FirstOf(
                new CallbackHandler(new Sequence(new LiteralAnyCharOf("+-"), eMultiplicative, eAdditiveSuffix), delegate(object v) {
                IVector a    = (IVector)v;
                IVector tail = (IVector)a[2];
                return(factory.InsertBefore(a[0], factory.InsertBefore(a[1], tail)));
                //return v;
                //return ((object[])v)[1];
            }),
                //new EmptyExpression(null));
                new EmptyRule(factory.Empty));
            //eAdditive.Expression = new CallbackHandler( new Sequence(eMultiplicative, eAdditiveSuffix), DoAdd);
            eAdditive.Expression = new CallbackHandler(new Sequence(eMultiplicative, eAdditiveSuffix), delegate(object v) {
                IVector a    = (IVector)v;
                IVector tail = (IVector)a[1];
                return(factory.InsertBefore(a[0], tail));
            });
            Placeholder eMultiplicativeSuffix = new Placeholder();

            eMultiplicativeSuffix.Expression = new FirstOf(
                new CallbackHandler(new Sequence(new LiteralAnyCharOf("*/"), eSingular, eMultiplicativeSuffix), delegate(object v) {
                IVector a    = (IVector)v;
                IVector tail = (IVector)a[2];
                return(factory.InsertBefore(a[0], factory.InsertBefore(a[1], tail)));
                //return v;
                //return ((object[])v)[1];
            }),
                //new EmptyExpression(null));
                new EmptyRule(factory.Empty));
            //eMultiplicative.Expression = new CallbackHandler(new Sequence(eSingular, eMultiplicativeSuffix), DoMultiply);
            eMultiplicative.Expression = new CallbackHandler(new Sequence(eSingular, eMultiplicativeSuffix), delegate(object v) {
                IVector a    = (IVector)v;
                IVector tail = (IVector)a[1];
                if (tail == factory.Empty)
                {
                    return(a[0]);
                }
                return(factory.InsertBefore(a[0], tail));
            });
            Rule expr = eAdditive;

            {
                var    parser = new Parser(expr, TextCursor.Create("12+3*45+6+789*(6+2)*9+0"), factory);
                Result r      = parser.Run();
                //string expected = "(+ 12 (* 3 45) 6 (* 7 8 9) 0)";
                string expected = "(12 + (3 * 45) + 6 + (789 * (6 + 2) * 9) + 0)";
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
                Assert.AreEqual(expected, Convert.ToString(r.Value).Replace("\"", "").Replace("'", ""));
            }
            {
                var    parser = new Parser(expr, TextCursor.Create("120+34*((5*6+7)*8+9)-1"), factory);
                Result r      = parser.Run();
                Assert.IsNotNull(r);
                Assert.IsFalse(r.Cursor.CanPop());
                //Assert.AreEqual(120 + 34 * ((5 * 6 + 7) * 8 + 9) - 1, r.Value);
            }
        }
Esempio n. 13
0
 public void TestAndPredicateMatcher()
 {
     {//classic {a^n b^n c^n | n>=1} example
         var _a = new Placeholder();
         _a.Expression = new Sequence(new LiteralAnyCharOf("a"), Some.Optional(_a), new LiteralAnyCharOf("b"));
         var _b = new Placeholder();
         _b.Expression = new Sequence(new LiteralAnyCharOf("b"), Some.Optional(_b), new LiteralAnyCharOf("c"));
         var q = new Sequence(Predicate.And(new Sequence(_a, Predicate.Not(new LiteralAnyCharOf("b")))), Some.OneOrMore(new LiteralAnyCharOf("a")), _b, new LiteralEOI());
         {
             var parser = new Parser(q, TextCursor.Create("aaabbbccc"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("abc"));
             var r      = parser.Run();
             Assert.IsNotNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabbc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabbccc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabccc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabcc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabbbc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabbbcc"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aacbb"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("aabbac"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
         {
             var parser = new Parser(q, TextCursor.Create("bca"));
             var r      = parser.Run();
             Assert.IsNull(r);
         }
     }
 }
Esempio n. 14
0
 public void TestNested(bool useArray)
 {
     IVectorFactory factory  = useArray ? (IVectorFactory) new ArrayVectorFactory() : (IVectorFactory) new BNodeVectorFactory();
     Function       toString = delegate(object r) {
         StringBuilder sb = new StringBuilder();
         foreach (object o in factory.AsEnumerable((IVector)r))
         {
             sb.Append(o);
         }
         return(sb.ToString());
     };
     Function toList = delegate(object r) {
         List <object> list = new List <object>();
         foreach (object o in factory.AsEnumerable((IVector)r))
         {
             list.Add(o);
         }
         return(list);
     };
     {
         var _digits  = new LiteralAnyCharOf("0123456789");
         var _letters = new LiteralAnyCharOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
         var _number  = new CallbackHandler(Some.OneOrMore(_digits), (v) => { return(new Token("number", toString(v))); });
         var _ident   = new CallbackHandler(new Sequence(_letters, new CallbackHandler(Some.ZeroOrMore(new FirstOf(_digits, _letters)), toString)),
                                            (v) => { return(new Token("ident", toString(v))); });
         var _whitespace = new LiteralAnyCharOf(" \t\n\r\f\v");
         var _comment    = new Sequence(new LiteralString("/*"), Some.ZeroOrMore(new Sequence(
                                                                                     Predicate.Not(new LiteralString("*/")), new LiteralAny())), new LiteralString("*/"));
         var _atmosphere = new FirstOf(_whitespace, _comment);
         var _lexeme     = new FirstOf(
             new CallbackHandler(new FirstOf(new LiteralAnyCharOf("{"), new LiteralAnyCharOf("}"), new LiteralAnyCharOf("|")),
                                 (v) => { string s = Convert.ToString(v); return(new Token(s, s)); }),
             new ExtractOne(0, new Sequence(new FirstOf(_ident, _number),
                                            Predicate.And(new FirstOf(new LiteralAnyCharOf("{}|"), _atmosphere, new LiteralEOI())))));
         var q = new ExtractOne(1, new Sequence(Some.ZeroOrMore(_atmosphere),
                                                Some.ZeroOrMore(new ExtractOne(0, new Sequence(_lexeme, Some.ZeroOrMore(_atmosphere)))), new LiteralEOI()));
         {
             var parser = new Parser(q, TextCursor.Create("   a1 set plus 32434 Ratio| /* ffwe34kfn3k4u3f$#$df \n34f2$F@$F$F#$DF#E#44  432#@$# $%@$***/ define edit {f 2 xz /*****/ e} /*\r*/\r|\r"),
                                     factory);
             var r = parser.Run();
             Assert.IsNotNull(r);
             IVector v = r.Value as IVector;
             Assert.IsNotNull(v);
             Assert.AreEqual(15, v.Length);
             List <Token> list = new VectorFactoryHelper(factory).ToList <Token>(v);
             Assert.AreEqual("ident", list[0].ID);
             Assert.AreEqual("a1", list[0].Value);
             Assert.AreEqual("number", list[3].ID);
             Assert.AreEqual("32434", list[3].Value);
             Assert.AreEqual("{", list[8].ID);
             Assert.AreEqual("{", list[8].Value);
             Assert.AreEqual("}", list[13].ID);
             Assert.AreEqual("}", list[13].Value);
             Assert.AreEqual("|", list[14].ID);
             Assert.AreEqual("|", list[14].Value);
         }
         var _sym  = new CallbackHandler(new LiteralToken("ident"), x => ((Token)x).Value);
         var _num  = new CallbackHandler(new LiteralToken("number"), x => Convert.ToInt32(((Token)x).Value));
         var _list = new Placeholder();
         var _expr = new FirstOf(_list, _sym, _num);
         _list.Expression = new FirstOf(
             new CallbackHandler(new Sequence(new LiteralToken("{"), Some.ZeroOrMore(_expr), new LiteralToken("}")), x => toNode((IVector)((IVector)x)[1], null)),
             new CallbackHandler(new Sequence(new LiteralToken("{"), Some.OneOrMore(_expr), new LiteralToken("|"), _expr, new LiteralToken("}")), x => toNode((IVector)((IVector)x)[1], ((IVector)x)[3]))
             );
         var q2 = new ExtractOne(0, new Sequence(_list, new LiteralEOI()));
         {
             var parser = new Parser(q,
                                     new Cursor <char>("   { any 90 {set {plus 32434 Ratio}|/* ffwetrash34kfn3k4u3f$#$df \n34f2$F@$F$F#$DF#E#44  432#@$# $%@$***/ define} {edit {f 2 {}xz /*****/ e} /*\r*/|\rr}\r    \n111} \t"),
                                     factory);
             var r = parser.Run();
             Assert.IsNotNull(r);
             IVector v = r.Value as IVector;
             Assert.IsNotNull(v);
             List <Token> list    = new VectorFactoryHelper(factory).ToList <Token>(v);
             var          parser2 = new Parser(q2, new Cursor <Token>(list), factory);
             var          r2      = parser2.Run();
             Assert.IsNotNull(r2);
             BNode n1 = r2.Value as BNode;
             Assert.IsNotNull(n1);
             Assert.AreEqual(5, n1.Length);
             Assert.AreEqual("(any #System.Int32(90) (set (plus #System.Int32(32434) Ratio) . define) (edit (f #System.Int32(2) () xz e) . r) #System.Int32(111))", n1.ToString().Replace("\"", ""));
         }
     }
 }