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); } }
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); } }
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); } }
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); } }