Exemple #1
0
        static ReElement ParseRepitition(ReElement element, ReParseContext context)
        {
            var min = ParseInt(context) ?? 0;

            if (context.Current == ',')
            {
                if (!context.MoveNext())
                {
                    ThrowUnexpectedEnd();
                }
                var max = ParseInt(context);
                return(ReFactory.NewRepetition(element, min, max));
            }
            else
            {
                return(ReFactory.NewRepetition(element, min, min));
            }
        }
Exemple #2
0
        static ReElement ParseSequence(ReParseContext context)
        {
            var choice   = new List <ReElement>();
            var sequence = new List <ReElement>();

            var abort = false;

            while (!context.End && !abort)
            {
                switch (context.Current)
                {
                case '[':
                    if (!context.MoveNext())
                    {
                        ThrowUnexpectedEnd();
                    }
                    sequence.Add(ParseCharSet(context));
                    if (context.End)
                    {
                        ThrowUnexpectedEnd();
                    }
                    if (context.Current != ']')
                    {
                        ThrowUnexpectedChar(context);
                    }
                    context.MoveNext();
                    break;

                case '(':
                    if (!context.MoveNext())
                    {
                        ThrowUnexpectedEnd();
                    }
                    sequence.Add(ParseSequence(context));
                    if (context.End)
                    {
                        ThrowUnexpectedEnd();
                    }
                    if (context.Current != ')')
                    {
                        ThrowUnexpectedChar(context);
                    }
                    context.MoveNext();
                    break;

                case ')':
                    abort = true;
                    break;

                case '|':
                    choice.Add(ReFactory.NewConcatenation(sequence));
                    sequence.Clear();
                    context.MoveNext();
                    break;

                case '.':
                    sequence.Add(ReFactory.NewSingleton(CharSet.LineBody));
                    context.MoveNext();
                    break;

                case '?':
                    if (sequence.Count == 0)
                    {
                        ThrowUnexpectedChar(context);
                    }
                    sequence[sequence.Count - 1] = ReFactory.NewRepetition(sequence[sequence.Count - 1], 0, 1);
                    context.MoveNext();
                    break;

                case '+':
                    if (sequence.Count == 0)
                    {
                        ThrowUnexpectedChar(context);
                    }
                    sequence[sequence.Count - 1] = ReFactory.NewRepetition(sequence[sequence.Count - 1], 1, null);
                    context.MoveNext();
                    break;

                case '*':
                    if (sequence.Count == 0)
                    {
                        ThrowUnexpectedChar(context);
                    }
                    sequence[sequence.Count - 1] = ReFactory.NewRepetition(sequence[sequence.Count - 1], 0, null);
                    context.MoveNext();
                    break;

                case '{':
                    if (sequence.Count == 0)
                    {
                        ThrowUnexpectedChar(context);
                    }
                    if (!context.MoveNext())
                    {
                        ThrowUnexpectedEnd();
                    }
                    sequence[sequence.Count - 1] = ParseRepitition(sequence[sequence.Count - 1], context);
                    if (context.Current != '}')
                    {
                        ThrowUnexpectedChar(context);
                    }
                    context.MoveNext();
                    break;

                case '\\':
                    if (!context.MoveNext())
                    {
                        ThrowUnexpectedEnd();
                    }
                    sequence.Add(ReFactory.NewSingleton(CharSet.New(ParseEscapeSequence(context))));
                    break;

                default:
                    sequence.Add(ReFactory.NewSingleton(CharSet.New(context.Current)));
                    context.MoveNext();
                    break;
                }
            }

            choice.Add(ReFactory.NewConcatenation(sequence));
            return(ReFactory.NewUnion(choice));
        }