Пример #1
0
        protected override IEnumerable <Concatenation> ReadImpl(ITextScanner scanner, ITextContext context)
        {
            var success = false;

            foreach (var concatenation in Branch(scanner, context, new List <Element>(lexers.Count)))
            {
                success = true;
                yield return(concatenation);
            }
            if (!success)
            {
                scanner.Seek(context.Offset);
            }
        }
Пример #2
0
 protected override IEnumerable <Alternation> ReadImpl(ITextScanner scanner, ITextContext context)
 {
     for (var i = 0; i < lexers.Length; i++)
     {
         scanner.Seek(context.Offset);
         var lexer = lexers[i];
         Debug.Assert(lexer != null, "lexer != null");
         var element = lexer.Read(scanner);
         if (element != null)
         {
             yield return(new Alternation(element.Text, element, context, i + 1));
         }
     }
 }
Пример #3
0
        protected override IEnumerable <Repetition> ReadImpl(ITextScanner scanner, ITextContext context)
        {
            var success = false;

            foreach (var repetition in Branch(scanner, context, new List <Element>(LowerBound)))
            {
                success = true;
                yield return(repetition);
            }
            if (!success)
            {
                scanner.Seek(context.Offset);
            }
        }
Пример #4
0
 public bool MoveNext()
 {
     if (!initialized)
     {
         start = scanner.Offset;
         scanner.StartRecording();
         inner       = lexer.ReadImpl(scanner, context).GetEnumerator();
         initialized = true;
     }
     if (scanner.Offset != context.Offset)
     {
         scanner.Seek(context.Offset);
     }
     if (!inner.MoveNext())
     {
         if (scanner.Offset != context.Offset)
         {
             scanner.Seek(context.Offset);
         }
         return(false);
     }
     Current = inner.Current;
     return(true);
 }
Пример #5
0
        public static TElement Read <TElement>([NotNull] this ILexer <TElement> instance, [NotNull] ITextScanner scanner) where TElement : Element
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var offset = scanner.Offset;

            scanner.StartRecording();
            TElement candidate = null;

            try
            {
                var context = scanner.GetContext();
                foreach (var element in instance.Read(scanner, context))
                {
                    if (candidate == null)
                    {
                        candidate = element;
                    }
                    else if (element.Text.Length > candidate.Text.Length)
                    {
                        candidate = element;
                    }
                }
                if (candidate == null)
                {
                    return(null);
                }
                scanner.Seek(offset + candidate.Text.Length);
            }
            finally
            {
                scanner.StopRecording();
            }
            return(candidate);
        }