コード例 #1
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);
         }
     }
 }
コード例 #2
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);
         }
     }
 }
コード例 #3
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]);
         }
     }
 }
コード例 #4
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);
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
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("\"", ""));
         }
     }
 }