Ejemplo n.º 1
0
        public static void Parse2()
        {
            {
                // best way to get result
                var x = Many1(Digit).AndR(Upper).Run("123a");
                //// throws exception
                //var rx = x.GetResult();
            }

            {
                // get as string
                var y = Many1(Digit).AndR(Many1(Upper)).Run("123A");
                Microsoft.FSharp.Collections.FSharpList <char> ry1 = y.GetResult();
                var ry2 = new string(ry1.ToArray());
            }

            {
                var x = Many1(Digit).AndR(Upper).Run("123a");
                // does not throw
                var rx = x.UnwrapResult();
            }

            // arithmetic expressions
            {
                var basicExprParser = new OPPBuilder <Unit, int, Unit>()
                                      .WithOperators(ops => ops
                                                     .AddInfix("+", 1, (x, y) => x + y)
                                                     .AddInfix("*", 2, (x, y) => x * y))
                                      .WithTerms(Natural)
                                      .Build()
                                      .ExpressionParser;

                var recursiveExprParser = new OPPBuilder <Unit, int, Unit>()
                                          .WithOperators(ops => ops
                                                         .AddInfix("+", 1, (x, y) => x + y)
                                                         .AddInfix("*", 2, (x, y) => x * y))
                                          .WithTerms(term => Choice(Natural, Between('(', term, ')')))
                                          .Build()
                                          .ExpressionParser;

                var calculated = recursiveExprParser.Run("4*(2+3*2)").GetResult();
            }

            // save expr tree as graph
            {
                var naturalTermToBasicElt = Natural.Map(i => new BasicValue {
                    Val = i
                } as IBasicElt);

                var basicExprParser = new OPPBuilder <Unit, IBasicElt, Unit>()
                                      .WithOperators(ops => ops
                                                     .AddInfix("+", 1, (x, y) => new BasicExprTree("PLUS", x, y))
                                                     .AddInfix("*", 2, (x, y) => new BasicExprTree("TIMES", x, y)))
                                      //.WithTerms(Natural.Map(i => new BasicValue { Val = i } as IBasicElt))
                                      .WithTerms(naturalTermToBasicElt)
                                      .Build()
                                      .ExpressionParser
                ;

                var recursiveExprParser = new OPPBuilder <Unit, IBasicElt, Unit>()
                                          .WithOperators(ops => ops
                                                         .AddInfix("+", 1, (x, y) => new BasicExprTree("PLUS", x, y))
                                                         .AddInfix("*", 2, (x, y) => new BasicExprTree("TIMES", x, y)))
                                          .WithTerms(term => Choice(naturalTermToBasicElt, Between('(', term, ')')))
                                          .Build()
                                          .ExpressionParser;

                var calculated    = recursiveExprParser.Run("4*(2+3*2)").GetResult();
                var calculatedVal = calculated.Calculate();
            }
        }