Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="scanner"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public override ReadResult <Alternative> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var context = scanner.GetContext();
            IList <SyntaxError> errors       = new List <SyntaxError>(lexers.Length);
            SyntaxError         partialMatch = null;

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var i = 0; i < lexers.Length; i++)
            {
                var result = lexers[i].ReadElement(scanner);
                if (result.Success)
                {
                    return(ReadResult <Alternative> .FromResult(new Alternative(result.Text, result.Element, context, i + 1)));
                }

                errors.Add(result.Error);
                if (partialMatch == null || result.Text.Length > partialMatch.Text.Length)
                {
                    partialMatch = result.Error;
                }
            }

            Debug.Assert(partialMatch != null, "partialMatch != null");
            return(ReadResult <Alternative> .FromSyntaxError(partialMatch));
        }
Пример #2
0
        public override ReadResult <Terminal> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var         context = scanner.GetContext();
            MatchResult result  = null;

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var i = 0; i < valueRange.Length; i++)
            {
                var c = valueRange[i];
                result = scanner.TryMatch(c);
                if (result.EndOfInput)
                {
                    return(ReadResult <Terminal> .FromSyntaxError(SyntaxError.FromMatchResult(result, context)));
                }
                if (result.Success)
                {
                    return(ReadResult <Terminal> .FromResult(new Terminal(result.Text, context)));
                }
            }
            Debug.Assert(result != null, "result != null");
            return(ReadResult <Terminal> .FromSyntaxError(SyntaxError.FromMatchResult(result, context)));
        }
Пример #3
0
        public override ReadResult <Alternative> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var    context                   = scanner.GetContext();
            ILexer bestCandidate             = null;
            var    bestCandidateLength       = -1;
            var    ordinal                   = 0;
            IList <SyntaxError> errors       = new List <SyntaxError>(lexers.Length);
            SyntaxError         partialMatch = null;

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var i = 0; i < lexers.Length; i++)
            {
                var lexer     = lexers[i];
                var candidate = lexer.ReadElement(scanner);
                if (candidate.Success)
                {
                    var alternative = candidate.Element;
                    var length      = alternative.Text.Length;
                    if (length > bestCandidateLength)
                    {
                        bestCandidate       = lexer;
                        bestCandidateLength = length;
                        ordinal             = i + 1;
                    }

                    if (length != 0)
                    {
                        scanner.Unread(alternative.Text);
                    }
                }
                else
                {
                    errors.Add(candidate.Error);
                    if (partialMatch == null || candidate.Text.Length > partialMatch.Text.Length)
                    {
                        partialMatch = candidate.Error;
                    }
                }
            }
            if (bestCandidate == null)
            {
                Debug.Assert(partialMatch != null, "partialMatch != null");
                return(ReadResult <Alternative> .FromSyntaxError(partialMatch));
            }
            var readResult = bestCandidate.ReadElement(scanner);

            Debug.Assert(readResult.Success, "readResult.Success");
            return(ReadResult <Alternative> .FromResult(new Alternative(readResult.Text, readResult.Element, context, ordinal)));
        }
Пример #4
0
        public override ReadResult <Sign> Read(ITextScanner scanner)
        {
            var result = innerLexer.Read(scanner);

            if (result.Success)
            {
                var sign = new Sign(result.Element);
                return(ReadResult <Sign> .FromResult(sign));
            }
            var syntaxError = SyntaxError.FromReadResult(result, scanner.GetContext());

            return(ReadResult <Sign> .FromSyntaxError(syntaxError));
        }
Пример #5
0
        public override ReadResult <Integer> Read(ITextScanner scanner)
        {
            var result = innerLexer.Read(scanner);

            if (result.Success)
            {
                var integer = new Integer(result.Element);
                return(ReadResult <Integer> .FromResult(integer));
            }
            var syntaxError = SyntaxError.FromReadResult(result, scanner.GetContext());

            return(ReadResult <Integer> .FromSyntaxError(syntaxError));
        }
Пример #6
0
        public override ReadResult <Alpha> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var context = scanner.GetContext();
            var result  = innerLexer.Read(scanner);

            if (result.Success)
            {
                return(ReadResult <Alpha> .FromResult(new Alpha(result.Element)));
            }
            return(ReadResult <Alpha> .FromSyntaxError(SyntaxError.FromReadResult(result, context)));
        }
Пример #7
0
        public override ReadResult <Terminal> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var context = scanner.GetContext();
            var result  = scanner.TryMatch(terminal, comparer);

            if (result.Success)
            {
                return(ReadResult <Terminal> .FromResult(new Terminal(result.Text, context)));
            }
            return(ReadResult <Terminal> .FromSyntaxError(SyntaxError.FromMatchResult(result, context)));
        }
Пример #8
0
        public override ReadResult <Repetition> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var             context       = scanner.GetContext();
            var             stringBuilder = new StringBuilder();
            IList <Element> elements      = new List <Element>(lowerBound);

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var i = 0; i < upperBound; i++)
            {
                var readResult = lexer.ReadElement(scanner);
                if (readResult.Success)
                {
                    elements.Add(readResult.Element);
                    stringBuilder.Append(readResult.Text);
                }
                else
                {
                    if (elements.Count >= lowerBound)
                    {
                        return
                            (ReadResult <Repetition> .FromResult(
                                 new Repetition(stringBuilder.ToString(), elements, context)));
                    }
                    var partialMatch = stringBuilder.ToString();
                    if (partialMatch.Length != 0)
                    {
                        scanner.Unread(partialMatch);
                    }
                    return
                        (ReadResult <Repetition> .FromSyntaxError(
                             new SyntaxError(
                                 readResult.EndOfInput,
                                 partialMatch,
                                 readResult.ErrorText,
                                 context,
                                 readResult.Error)));
                }
            }
            return(ReadResult <Repetition> .FromResult(new Repetition(stringBuilder.ToString(), elements, context)));
        }
Пример #9
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);
        }
Пример #10
0
        public override ReadResult <HexadecimalDigit> Read(ITextScanner scanner)
        {
            if (scanner == null)
            {
                throw new ArgumentNullException(nameof(scanner));
            }
            var result = innerLexer.Read(scanner);

            if (result.Success)
            {
                return(ReadResult <HexadecimalDigit> .FromResult(new HexadecimalDigit(result.Element)));
            }
            return(ReadResult <HexadecimalDigit> .FromSyntaxError(SyntaxError.FromReadResult(result, scanner.GetContext())));
        }