public void TestMatchPrimitives() { { var q = new LiteralAnyCharOf("0123456789"); { var parser = new Parser(q, TextCursor.Create("7")); var r = parser.Run(); Assert.IsNotNull(r); Assert.AreEqual(1, r.Cursor.Position); Assert.AreEqual('7', r.Value); } { var parser = new Parser(q, TextCursor.Create("78")); var r = parser.Run(); Assert.IsNotNull(r); Assert.AreEqual(1, r.Cursor.Position); Assert.AreEqual('7', r.Value); } } { var q = new Literal('z'); { var parser = new Parser(q, TextCursor.Create("z")); var r = parser.Run(); Assert.IsNotNull(r); Assert.IsNull(parser.FailCursor); Assert.AreEqual(1, r.Cursor.Position); Assert.AreEqual('z', r.Value); } { var parser = new Parser(q, TextCursor.Create("0123")); var r = parser.Run(); Assert.IsNull(r); Assert.IsNotNull(parser.FailCursor); Assert.AreEqual(0, parser.FailCursor.Position); } } { var q = new LiteralEOI(); { var parser = new Parser(q, TextCursor.Create("")); var r = parser.Run(); Assert.IsNotNull(r); Assert.IsNull(parser.FailCursor); Assert.AreEqual(0, r.Cursor.Position); Assert.IsTrue(!r.Cursor.CanPop()); Assert.AreEqual(null, r.Value); } { var parser = new Parser(q, TextCursor.Create("0123")); var r = parser.Run(); Assert.IsNull(r); Assert.IsNotNull(parser.FailCursor); Assert.AreEqual(0, 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 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); } }
public void TestNotPredicateMatcher() { {//nested comments example var allchars = new LiteralAnyCharOf("0123456789/* "); var _open = new Sequence(new LiteralAnyCharOf("/"), new LiteralAnyCharOf("*")); var _close = new Sequence(new LiteralAnyCharOf("*"), new LiteralAnyCharOf("/")); var _nested = new Placeholder(); var _block = new Sequence(_open, Some.ZeroOrMore(_nested), _close); _nested.Expression = new FirstOf(_block, new Sequence(Predicate.Not(_open), Predicate.Not(_close), allchars)); var q = new Sequence(Some.ZeroOrMore(_nested), new LiteralEOI()); { var parser = new Parser(q, TextCursor.Create(" ** /")); var r = parser.Run(); Assert.IsNotNull(r); } { var parser = new Parser(q, TextCursor.Create(" **/")); var r = parser.Run(); Assert.IsNull(r); Assert.IsNotNull(parser.FailCursor); if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debug.WriteLine(parser.GetError()); } Assert.AreEqual(3, parser.FailCursor.Position); } { var parser = new Parser(q, TextCursor.Create(" 12 34 /* 5 6 7*8/*9 0/**/ */* * */")); var r = parser.Run(); Assert.IsNotNull(r); } { var parser = new Parser(q, TextCursor.Create(" 12 34 /* 5 6 7*8/*9 0/* */* * */")); var r = parser.Run(); Assert.IsNull(r); Assert.IsNotNull(parser.FailCursor); if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debug.WriteLine(parser.GetError()); } Assert.AreEqual(33, parser.FailCursor.Position); } } }
public void TestSomeMatcher() { { var digit = new LiteralAnyCharOf("0123456789"); var q = new Sequence( Some.Optional(new LiteralAnyCharOf("-")), Some.ZeroOrMore(digit), new LiteralAnyCharOf("."), Some.OneOrMore(digit) ); { var parser = new Parser(q, TextCursor.Create("-.52")); var r = parser.Run(); Assert.IsNotNull(r); Assert.AreEqual(4, r.Cursor.Position); Assert.IsTrue(r.Value is IVector); IVector v = (IVector)r.Value; Assert.AreEqual(4, v.Length); Assert.IsTrue(v[0] is IVector); IVector v0 = (IVector)v[0]; Assert.AreEqual(1, v0.Length); Assert.AreEqual('-', v0[0]); Assert.IsTrue(v[1] is IVector); Assert.AreEqual(0, ((IVector)v[1]).Length); Assert.AreEqual('.', v[2]); Assert.IsTrue(v[3] is IVector); IVector v3 = (IVector)v[3]; Assert.AreEqual(2, v3.Length); Assert.AreEqual('5', v3[0]); Assert.AreEqual('2', v3[1]); } } }