Exemple #1
0
        /*
         * S -> [S] | ParamList
         * ParamList -> [S] OptParams | NUM | NUM OptParams
         * OptParams -> , ParamList | eps
         * NUM -> int
         */
        private static ArrayItem FromString(string str, ref int pos)
        {
            ArrayItem arrayItem = new ArrayItem();

            if (str[pos] == '[')
            {
                ++pos;
                while (pos < str.Length && str[pos] != ']')
                {
                    if (str[pos] == '[')
                    {
                        arrayItem.Items.Add(FromString(str, ref pos));
                        if (pos >= str.Length)
                        {
                            throw new ArgumentException($"Missing closing parenthesis at pos {str.Length - 1}");
                        }
                        if (str[pos] != ']')
                        {
                            throw new ArgumentException($"Missing closing parenthesis at pos {pos}");
                        }
                        ++pos;
                        if (pos < str.Length && str[pos] == ',')
                        {
                            ++pos;
                        }
                    }
                    else if (char.IsDigit(str[pos]))
                    {
                        Console.WriteLine("Processing number");
                        NumItem tmp = new NumItem();
                        while (pos < str.Length && char.IsDigit(str[pos]))
                        {
                            tmp.Value *= 10;
                            tmp.Value += int.Parse(str[pos].ToString());
                            ++pos;
                        }
                        arrayItem.Items.Add(tmp);

                        if (pos < str.Length && str[pos] == ',')
                        {
                            ++pos;
                        }
                    }
                    else
                    {
                        throw new ArgumentException($"Unexpected `{str[pos]}` got at pos {pos}");
                    }
                }
                if (pos >= str.Length && str[str.Length - 1] != ']')
                {
                    throw new ArgumentException($"Missing closing parenthesis at pos {str.Length - 1}");
                }
            }
            else
            {
                throw new ArgumentException($"Unexpected `{str[pos]}` got at pos {pos}");
            }

            return(arrayItem);
        }
Exemple #2
0
        public static Item ListElementNTerm(string s, ref int pos)
        {
            Item item = new Item();

            Console.WriteLine("ListElementNTerm");

            if (s[pos] == '[')
            {
                item = ListNTerm(s, ref pos);
            }
            else if (char.IsDigit(s[pos]))
            {
                Console.WriteLine("Number encountered");
                item = new NumItem();
                while (pos < s.Length && char.IsDigit(s[pos]))
                {
                    ((NumItem)item).Value *= 10;
                    ((NumItem)item).Value += int.Parse(s[pos].ToString());
                    ++pos;
                }
            }

            return(item);
        }