Пример #1
0
        public void TestInteger()
        {
            var p = Prim.Integer();

            Assert.True(!p.Parse("123").IsFaulted&& p.Parse("123").Value.Single().Item1 == 123);
            Assert.True(!p.Parse("-123").IsFaulted&& p.Parse("-123").Value.Single().Item1 == -123);
            Assert.True(!p.Parse(int.MaxValue.ToString()).IsFaulted&& p.Parse(int.MaxValue.ToString()).Value.Single().Item1 == int.MaxValue);

            // Bug here in both .NET and Mono, neither can parse an Int32.MinValue, overflow exception is thrown.
            //Assert.True(!p.Parse(int.MinValue.ToString()).IsFaulted && p.Parse(int.MinValue.ToString()).Value.Single().Item1 == int.MinValue);
        }
Пример #2
0
        public void BuildLangParser()
        {
            var opChars = ";.,<>?/\\|\"':=+-_*&^%$£@!".AsEnumerable();

            Id = from w in Prim.WhiteSpace()
                 from c in Prim.Letter()
                 from cs in Prim.Many(Prim.LetterOrDigit())
                 select c.Cons(cs);

            Op = (from w in Prim.WhiteSpace()
                  from o in Prim.Satisfy(c => opChars.Contains(c), "an operator")
                  from os in Prim.Many(Prim.Satisfy(c => opChars.Contains(c), "an operator"))
                  select o.Cons(os))
                 .Fail("an operator");

            Ident = (from s in Id
                     where
                     s.IsNotEqualTo("let") &&
                     s.IsNotEqualTo("in")
                     select s)
                    .Fail("identifier");

            LetId = (from s in Id
                     where s.IsEqualTo("let")
                     select s)
                    .Fail("let");

            InId = (from s in Id
                    where s.IsEqualTo("in")
                    select s)
                   .Fail("'in'");

            Semi = (from s in Op
                    where s.IsEqualTo(";")
                    select s)
                   .Fail("';'");

            LambdaArrow = (from s in Op
                           where s.IsEqualTo("=>")
                           select s)
                          .Fail("a lambda arrow '=>'");

            Integer = (from w in Prim.WhiteSpace()
                       from d in Prim.Integer()
                       select new IntegerTerm(d) as Term)
                      .Fail("an integer");

            String = (from w in Prim.WhiteSpace()
                      from o in Prim.Character('"')
                      from cs in Prim.Many(Prim.Satisfy(c => c != '"'))
                      from c in Prim.Character('"')
                      select new StringTerm(cs) as Term)
                     .Fail("a string literal");

            Term1 = Integer
                    | String
                    | (from x in Ident
                       select new VarTerm(x) as Term)
                    | (from u1 in Lang.WsChr('(')
                       from t in Term
                       from u2 in Lang.WsChr(')')
                       select t)
                    .Fail("a term");

            Term = (from x in Ident
                    from arrow in LambdaArrow
                    from t in Term
                    select new LambdaTerm(x, t) as Term)
                   | (from lid in LetId
                      from x in Ident
                      from u1 in Lang.WsChr('=')
                      from t in Term
                      from s in Semi
                      from c in Term
                      select new LetTerm(x, t, c) as Term)
                   | (from t in Term1
                      from ts in Prim.Many(Term1)
                      select new AppTerm(t, ts) as Term)
                   .Fail("a term");

            Parser = from t in Term
                     from u in Lang.WsChr(';')
                     from w in Prim.WhiteSpace()
                     select t;
        }