Beispiel #1
0
 public void SetMethodTwice()
 {
     Reduction r = new Reduction("return 42;");
     MethodInfo m = typeof(TestReduction).GetMethod("SetMethodTwice");
     r.SetCompiledMethod(m);
     r.SetCompiledMethod(m);
 }
 public void AddAssembly()
 {
     ReductionCompiler comp = new ReductionCompiler();
     Reduction r = new Reduction("return new Earley.ReductionCompiler();");
     comp.Add(r);
     comp.AddReference(typeof(ReductionCompiler).Assembly);
     comp.Compile();
     Assert.AreEqual(typeof(ReductionCompiler), r.Apply(new object[] {}).GetType());
 }
Beispiel #3
0
        public Parser(Reduction startProduction)
        {
            if (startProduction == null) throw new ArgumentNullException();

            if (startProduction.Symbols.Count == 0 ||
                startProduction.Symbols[startProduction.Symbols.Count - 1] != Terminal.Eof)
            {
                throw new ArgumentException();
            }

            this.startProduction = startProduction;
        }
 public void Trival()
 {
     ReductionCompiler comp = new ReductionCompiler();
     Reduction r = new Reduction("return 42;");
     comp.Add(r);
     comp.Compile();
     Assert.AreEqual(42, r.Apply(null));
 }
Beispiel #5
0
        public void TrivalReduce()
        {
            Reduction reduction = new Reduction("return 42;");

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(reduction);
            comp.Compile();

            Item item = new Item(reduction, new State());
            IList<object> results = item.Reduce();
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual(42, results[0]);
        }
Beispiel #6
0
        public void ReduceTerminals()
        {
            Reduction r = new Reduction(
                "return string.Format(\"{0}{1}{2}\", (char)(int)$0, (char)(int)$1, (char)(int)$2);",
                new Terminal('f', 'b'),
                new Terminal('o'),
                new Terminal('o'));

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(r);
            comp.Compile();

            Item item = new Item(r, new State());
            item = item.NextItem;
            item.Add('f');
            item.Add('b');

            item = item.NextItem;
            item.Add('o');

            item = item.NextItem;
            item.Add('o');

            IList<object> results = item.Reduce();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("foo", results[0]);
            Assert.AreEqual("boo", results[1]);
        }
Beispiel #7
0
        public void ReduceBeforeEnd()
        {
            Reduction r = new Reduction("return \"hello\";", Terminal.Eof);

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(r);
            comp.Compile();

            new Item(r, new State()).Reduce();
        }
Beispiel #8
0
        public void Reduce()
        {
            Reduction n1 = new Reduction("return 1;");
            Reduction n2 = new Reduction("return 2;");
            Reduction n10 = new Reduction("return 10;");
            Nonterminal exp = new Nonterminal();
            Reduction plus = new Reduction("return (int)$0 + (int)$1;", exp, exp);

            exp.Add(n1);
            exp.Add(n2);
            exp.Add(n10);
            exp.Add(plus);

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(n1);
            comp.Add(n2);
            comp.Add(n10);
            comp.Add(plus);
            comp.Compile();

            //     +
            //    / \
            //   +   \
            //  / \   \
            //  ?  1   ?
            // / \    / \
            // 1 2   +  10
            //      / \
            //      ?  1
            //	   / \
            //	   1 2

            Item inner = new Item(plus, new State());
            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));
            inner.Add(new Item(n2, new State()));

            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));

            Item item = new Item(plus, new State());
            item = item.NextItem;
            item.Add(inner);

            item = item.NextItem;
            item.Add(inner);
            item.Add(new Item(n10, new State()));

            IList<object> result = item.Reduce();

            int[] precomputed = new int[] {
                4, 5, 12, 5, 6, 13
            };

            Assert.AreEqual(precomputed.Length, result.Count);

            for (int i = 0; i < precomputed.Length; i++)
            {
                Assert.AreEqual(precomputed[i], result[i]);
            }
        }
Beispiel #9
0
 public void SetNullCompiledMethod()
 {
     Reduction r = new Reduction("return \"hi\";");
     r.SetCompiledMethod(null);
 }
Beispiel #10
0
 public void SetCompiledMethod()
 {
     Reduction r = new Reduction("return \"hi\";");
     r.SetCompiledMethod(typeof(TestReduction).GetMethod("MyMethod", BindingFlags.Static | BindingFlags.Public));
     Assert.AreEqual("there", r.Apply(null));
 }
Beispiel #11
0
 public void CreateNullCode()
 {
     Production p = new Reduction(
         null,
         new Terminal('x'));
 }
Beispiel #12
0
 public void Create()
 {
     Production p = new Reduction(
         "return 42;",
         new Terminal('x'));
 }
Beispiel #13
0
 public void ApplyNotCompiled()
 {
     Reduction r = new Reduction("return 42;");
     r.Apply(null);
 }