Exemplo n.º 1
0
 public static bool TryParseMany(string s, out Bracket[] result)
 {
     result = null;
     try
     {
         result = ParseMany(s);
         return true;
     }
     catch
     {
         return false;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// not the best, but it will parse brackets when they are right
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static IEnumerable<Bracket> ParseAsIE(string s)
        {
            Regex bracketRegex = new Regex(@"^\(+[0-9\-\(\)]+\)+$");
            Regex invalidBracket = new Regex(@"[0-9]+\(");
            if (!bracketRegex.IsMatch(s))
                throw new InvalidOperationException();
            if (s.Where(c => c == '(').Count(c => true) != s.Where(c => c == ')').Count(c => true))
                throw new InvalidOperationException();
            if (invalidBracket.IsMatch(s))
                throw new InvalidOperationException("brackets with inner brackets cannot have values");


            int bracketCount = 0;
            int loc = 0;
            string parseString = "";
            int startingpos = 0;
            foreach (var c in s)
            {
                if (c == '(')
                    bracketCount++;
                else if (c == ')')
                    bracketCount--;
                if (loc != startingpos && bracketCount != 0)
                    parseString += c;
                loc++;
                if (bracketCount == 0)
                {
                    if (parseString == "")
                        throw new Exception("brackets must not be empty");

                    var b = new Bracket();
                    int v = 0;
                    bool suc = int.TryParse(parseString, out v); //TO DO TO DO
                    if (suc)
                    {
                        if (b.value == null)
                            b.value = v;
                        else
                            throw new Exception("cannot value twice");
                        //b.inside.Add(new Bracket(v));
                    }
                    else
                    {
                        b.inside = ParseAsIE(parseString).ToList();
                    }
                    yield return b;
                    startingpos = ++loc;
                    parseString = "";
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// not the best, but it will parse brackets when they are right
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Bracket Parse(string s)
        {
            Regex bracketRegex = new Regex(@"^\(+[0-9\(\)]+\)+$");
            Regex invalidBracket = new Regex(@"[0-9]+\(");
            if (!bracketRegex.IsMatch(s))
                throw new InvalidOperationException();
            if (s.Where(c => c == '(').Count(c => true) != s.Where(c => c == ')').Count(c => true))
                throw new InvalidOperationException();
            if (invalidBracket.IsMatch(s))
                throw new InvalidOperationException("brackets with inner brackets cannot have values");

            Bracket b = new Bracket();
            int bracketCount = 0;
            int loc = 0;
            string parseString = "";
            int startingpos = 0;
            foreach (var c in s)
            {
                if (c == '(')
                    bracketCount++;
                else if (c == ')')
                    bracketCount--;
                if (loc != startingpos && bracketCount != 0)
                    parseString += c;
                loc++;
                if (bracketCount == 0)
                {
                    if (parseString == "")
                        throw new Exception("brackets must not be empty");


                    int v = 0;
                    bool suc = int.TryParse(parseString, out v); //TO DO TO DO
                    if (suc)
                    {
                        if (b.value == null)
                            b.value = v;
                        else 
                            throw new Exception("A bracket cannot have more then one value");
                        //b.inside.Add(new Bracket(v));
                    }
                    else
                        b.inside.InsertRange(b.inside.Count, ParseAsIE(parseString));
                    startingpos = ++loc;
                    parseString = "";
                }
            }
            return b;
        }