Example #1
0
 private static bool ParseNum(ArraySegment <char> str, out Expr result)
 {
     if (!int.TryParse(str, out var value))
     {
         throw ParserException.Build("Could not recognize number", str, 0);
     }
     result = new ExprNum(value);
     return(true);
 }
Example #2
0
        public static Expr Parse(string str)
        {
            var arraySegment = str.ToCharArray();

            if (!ParseExpr(arraySegment, out var result))
            {
                throw ParserException.Build("Could not find an arithmetic expression", arraySegment, 0);
            }

            return(result ?? throw new Exception("Result cannot be null"));
        }
Example #3
0
        private static void LexCheck(ArraySegment <char> str)
        {
            for (int i = 0; i < str.Count; i++)
            {
                var ch = str[i];

                bool valid = char.IsDigit(ch) || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')';

                if (!valid)
                {
                    throw ParserException.Build("Incorrect char", str, i);
                }
            }
        }
Example #4
0
        private static bool ParseSubExpr(ArraySegment <char> str, out Expr?result)
        {
            result = null;
            if (str.Count > 0 && str[0] == '(')
            {
                int sum = 1;

                for (int i = 1; i < str.Count; i++)
                {
                    if (str[i] == '(')
                    {
                        sum++;
                    }
                    if (str[i] == ')')
                    {
                        sum--;
                    }

                    if (sum == 0)
                    {
                        if (i + 1 != str.Count)// (....)....
                        {
                            return(false);
                        }

                        if (!ParseExprAll(str.Slice(1, i - 1), out var sub))
                        {
                            throw ParserException.Build("Could not recognize any sub expression", str, i);
                        }

                        Debug.Assert(sub != null, nameof(sub) + " != null");
                        result = new ExprSubExpr(sub);
                        return(true);
                    }
                }
                throw ParserException.Build("Could not find closing ')'", str, 0);
            }
            return(false);
        }