예제 #1
0
        private void CheckIfTagSequenceValid(int index, params InputSymbolTag[] tags)
        {
            switch (tags.Length)
            {
            case 2:
            {
                InputSymbolTag previous = tags[0];
                InputSymbolTag current  = tags[1];

                if ((previous == InputSymbolTag.Union || previous == InputSymbolTag.OpeningParenthesis) &&
                    (
                        current == InputSymbolTag.Union ||
                        current == InputSymbolTag.KleeneStar ||
                        current == InputSymbolTag.KleenePlus
                    ))
                {
                    throw new ArgumentException(index == 0 ? errorAtStart + Rules[0] : String.Format(
                                                    errorAtChar + Rules[0], index, GetTagsSample()));
                }

                if ((previous == InputSymbolTag.KleeneStar || previous == InputSymbolTag.KleenePlus) &&
                    (current == InputSymbolTag.KleeneStar || current == InputSymbolTag.KleenePlus))
                {
                    throw new ArgumentException(index == 0 ? errorAtStart + Rules[1] : String.Format(
                                                    errorAtChar + Rules[1], index, GetTagsSample()));
                }

                if (previous == InputSymbolTag.OpeningParenthesis &&
                    current == InputSymbolTag.ClosingParenthesis)
                {
                    throw new ArgumentException(index == 0 ? errorAtStart + Rules[2] : String.Format(
                                                    errorAtChar + Rules[2], index, GetTagsSample()));
                }
            } break;

            case 3:
            {
                InputSymbolTag previous2 = tags[0];
                InputSymbolTag previous  = tags[1];
                InputSymbolTag current   = tags[2];

                if ((previous2 == InputSymbolTag.OpeningParenthesis && previous == InputSymbolTag.ClosingParenthesis) &&
                    (
                        current == InputSymbolTag.Union ||
                        current == InputSymbolTag.KleeneStar ||
                        current == InputSymbolTag.KleenePlus
                    ))
                {
                    throw new ArgumentException(index == 0 ? errorAtStart + Rules[3] : String.Format(
                                                    errorAtChar + Rules[3], index));
                }
            } break;
            }
        }
예제 #2
0
        private int CheckReservedSymbolsAt(int i)
        {
            int  count   = taggedInput.Count;
            bool success = false;

            InputSymbolTag previous = default(InputSymbolTag);

            if (count > 0)
            {
                previous = taggedInput[count - 1].Value;
            }
            InputSymbolTag previous2 = default(InputSymbolTag);

            if (count > 1)
            {
                previous2 = taggedInput[count - 2].Value;
            }

            // at most one of parallel threads reaches the last lines of action body
            try
            {
                Parallel.For(0, ReservedSymbols.Length, (int n) =>
                {
                    if (input.IndexOf(ReservedSymbols[n].Key, i, Math.Min(ReservedSymbolMaxLength, input.Length - i)) != i)
                    {
                        return;
                    }

                    var current = ReservedSymbols[n].Value;
                    if (count > 0)
                    {
                        CheckIfTagSequenceValid(i, previous, current);

                        if (count > 1)
                        {
                            CheckIfTagSequenceValid(i, previous2, previous, current);
                        }
                    }
                    else if (current == InputSymbolTag.Union || current == InputSymbolTag.KleeneStar ||
                             current == InputSymbolTag.KleenePlus || current == InputSymbolTag.ClosingParenthesis)
                    {
                        throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                                  errorAtStart + Rules[4]));
                    }

                    taggedInput.Add(ReservedSymbols[n]);
                    if (ReservedSymbols[n].Key.Length > 1)
                    {
                        i += ReservedSymbols[n].Key.Length - 1;
                    }
                    success = true;
                });
            }
            catch (AggregateException e)
            {
                int exceptionsCount = e.InnerExceptions.Count;
                if (exceptionsCount > 1)
                {
                    throw new ArgumentException(String.Format("there are {0} errors, including an {1}",
                                                              exceptionsCount, e.InnerException.Message));
                }
                throw e.InnerException;
            }

            if (success)
            {
                return(i);
            }
            return(-1);
        }