Beispiel #1
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > UnaryParens(string s)
        {
            if (s.StartsWith("("))
            {
                s = Parser.Space(s.Substring(1));

                Either <Error, Tuple <string, ExpressionBuilder> > res = Expr(s);
                if (res.IsLeft)
                {
                    return(res.Left);
                }

                Tuple <string, ExpressionBuilder> t = res.Right;

                s = Parser.Space(t.Item1);
                if (!s.StartsWith(")"))
                {
                    return(new Error(s, "missing )"));
                }

                s = Parser.Space(s.Substring(1));
                return(new Tuple <string, ExpressionBuilder>(s, new ExpressionBuilder.Unary(ExpressionBuilder.Op.Parens, t.Item2)));
            }
            else
            {
                return(new Error(s, "missing ("));
            }
        }
Beispiel #2
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Expr4(string s)
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res1 = ExprTerm(s);

            if (res1.IsLeft)
            {
                return(res1.Left);
            }
            Tuple <string, ExpressionBuilder> t1 = res1.Right;

            s = Parser.Space(t1.Item1);
            ExpressionBuilder e1 = t1.Item2;

            if (!s.StartsWith("."))
            {
                return(new Tuple <string, ExpressionBuilder>(s, e1));
            }
            s = s.Substring(1);

            Either <Error, Tuple <string, ExpressionBuilder.Op> > res2 = BinaryOp4(s);

            if (res2.IsLeft)
            {
                return(res2.Left);
            }
            Tuple <string, ExpressionBuilder.Op> t2 = res2.Right;

            s = Parser.Space(t2.Item1);
            ExpressionBuilder.Op op = t2.Item2;

            if (!s.StartsWith("("))
            {
                return(new Error(s, "missing ("));
            }

            s = Parser.Space(s.Substring(1));

            Either <Error, Tuple <string, ExpressionBuilder> > res3 = Expr(s);

            if (res3.IsLeft)
            {
                return(res3.Left);
            }

            Tuple <string, ExpressionBuilder> t3 = res3.Right;

            s = Parser.Space(t3.Item1);
            if (!s.StartsWith(")"))
            {
                return(new Error(s, "missing )"));
            }
            s = Parser.Space(s.Substring(1));
            ExpressionBuilder e2 = t3.Item2;

            return(new Tuple <string, ExpressionBuilder>(s, new ExpressionBuilder.Binary(op, e1, e2)));
        }
Beispiel #3
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Expr(string str)
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res1 = Expr1(str);

            if (res1.IsLeft)
            {
                return(res1.Left);
            }

            var t1 = res1.Right;

            str = t1.Item1;
            ExpressionBuilder builder = t1.Item2;
            bool errorOrEmpty         = false;

            while (!errorOrEmpty)
            {
                str = Parser.Space(str);
                if (!str.IsEmpty())
                {
                    Either <Error, Tuple <string, ExpressionBuilder.Op> > res2 = BinaryOp(str);
                    if (!res2.IsLeft)
                    {
                        Tuple <string, ExpressionBuilder.Op> t2 = res2.Right;
                        str = t2.Item1;
                        ExpressionBuilder.Op op = t2.Item2;

                        str = Parser.Space(str);

                        Either <Error, Tuple <string, ExpressionBuilder> > res3 = Expr1(str);
                        if (res3.IsLeft)
                        {
                            return(res3.Left);
                        }
                        Tuple <string, ExpressionBuilder> t3 = res3.Right;

                        str = t3.Item1;
                        ExpressionBuilder e2 = t3.Item2;

                        builder = new ExpressionBuilder.Binary(op, builder, e2);
                    }
                    else
                    {
                        errorOrEmpty = true;
                    }
                }
                else
                {
                    errorOrEmpty = true;
                }
            }

            return(new Tuple <string, ExpressionBuilder>(str, builder));
        }
Beispiel #4
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Expr1(string s)
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res1 = Expr2(s);

            if (res1.IsLeft)
            {
                return(res1.Left);
            }
            Tuple <string, ExpressionBuilder> t1 = res1.Right;

            s = t1.Item1;
            ExpressionBuilder e = t1.Item2;
            bool errorOrEmpty   = false;

            while (!errorOrEmpty)
            {
                s = Parser.Space(s);
                if (s.Length != 0)
                {
                    Either <Error, Tuple <string, ExpressionBuilder.Op> > res2 = BinaryOp1(s);
                    if (res2.IsRight)
                    {
                        Tuple <string, ExpressionBuilder.Op> t2 = res2.Right;
                        s = t2.Item1;
                        ExpressionBuilder.Op op = t2.Item2;

                        s = Parser.Space(s);

                        Either <Error, Tuple <string, ExpressionBuilder> > res3 = Expr2(s);
                        if (res3.IsLeft)
                        {
                            return(res3.Left);
                        }
                        Tuple <string, ExpressionBuilder> t3 = res3.Right;

                        s = t3.Item1;
                        ExpressionBuilder e2 = t3.Item2;

                        e = new ExpressionBuilder.Binary(op, e, e2);
                    }
                    else
                    {
                        errorOrEmpty = true;
                    }
                }
                else
                {
                    errorOrEmpty = true;
                }
            }

            return(new Tuple <string, ExpressionBuilder>(s, e));
        }
Beispiel #5
0
 public static Either <Error, Tuple <string, ExpressionBuilder> > Parse(string s)
 {
     return(Expr(Parser.Space(s)));
 }
Beispiel #6
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Unary(string str)
        {
            str = Parser.Space(str);

            if (str.StartsWith("!"))
            {
                str = Parser.Space(str.Substring(1));

                Either <Error, Tuple <string, ExpressionBuilder> > resultExpression = Expr(str);
                if (resultExpression.IsLeft)
                {
                    return(resultExpression.Left);
                }

                Tuple <string, ExpressionBuilder> t = resultExpression.Right;
                return(new Tuple <string, ExpressionBuilder>(t.Item1, new ExpressionBuilder.Unary(ExpressionBuilder.Op.Negate, t.Item2)));
            }


            if (str.StartsWith("("))
            {
                Either <Error, Tuple <string, ExpressionBuilder> > unaryParens = UnaryParens(str);
                if (unaryParens.IsLeft)
                {
                    return(unaryParens.Left);
                }

                Tuple <string, ExpressionBuilder> t = unaryParens.Right;

                str = Parser.Space(str.Substring(1));
                return(new Tuple <string, ExpressionBuilder>(t.Item1, t.Item2));
            }

            ExpressionBuilder e;
            Either <Error, Tuple <string, Term> > res = Parser.Term(str);

            if (res.IsRight)
            {
                Tuple <string, Term> t = res.Right;
                str = Parser.Space(t.Item1);
                e   = new ExpressionBuilder.Value(t.Item2);
            }
            else
            {
                Either <Error, Tuple <string, ExpressionBuilder> > res2 = UnaryParens(str);
                if (res2.IsLeft)
                {
                    return(res2.Left);
                }

                Tuple <string, ExpressionBuilder> t = res2.Right;
                str = Parser.Space(t.Item1);
                e   = t.Item2;
            }

            if (str.StartsWith(".Length"))
            {
                str = Parser.Space(str.Substring(9));
                return(new Tuple <string, ExpressionBuilder>(str, new ExpressionBuilder.Unary(ExpressionBuilder.Op.Length, e)));
            }
            else
            {
                return(new Error(str, "unexpected token"));
            }
        }