Пример #1
0
            public override Parser <TInput> VisitFails(FailsParser <TInput> parser)
            {
                if (state.State == 0)
                {
                    if (source.IsEnd(state.InputStart))
                    {
                        state.InputLength = 0;
                        return(null);
                    }
                    else
                    {
                        state.State++;
                        return(parser.Pattern);
                    }
                }
                else
                {
                    if (state.LastResult >= 0)
                    {
                        state.InputLength = -1;
                    }
                    else
                    {
                        state.InputLength = 0;
                    }

                    return(null);
                }
            }
            public override Parser <TInput> VisitNot(NotParser <TInput> parser)
            {
                // for finding, allow not appear to succeed so that things that condition on it will continue to find.
                if (source.IsEnd(state.InputStart))
                {
                    state.InputLength   = 0;
                    this.prevWasMissing = false;
                    return(null);
                }

                var len = parser.Pattern.Scan(source, state.InputStart);

                if (len >= 0)
                {
                    state.InputLength   = -1;
                    this.prevWasMissing = false;
                    return(null);
                }
                else
                {
                    state.InputLength   = 1;
                    this.prevWasMissing = false;
                    return(null);
                }
            }
Пример #3
0
        public override int VisitNot(NotParser <TInput> parser, int start)
        {
            _prevWasMissing = false;

            // for finding, allow not appear to succeed so that things that condition on it will continue to find.
            if (_source.IsEnd(start))
            {
                return(0);
            }

            var len = parser.Pattern.Scan(_source, start);

            if (len >= 0)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
Пример #4
0
        /// <summary>
        /// Matches one or more lexical tokens to the corresponding text.
        /// </summary>
        private static int MatchesText(Source <LexicalToken> source, int start, string text)
        {
            // consume all lexical tokens with combined text that matches the specified text
            int textOffset = 0;
            int i          = 0;

            for (; !source.IsEnd(start + i); i++)
            {
                var token = source.Peek(start + i);

                // only first token can have trivia
                if (i > 0 && token.Trivia.Length > 0)
                {
                    break;
                }

                // token has more text than remaining
                if (token.Text.Length > text.Length - textOffset)
                {
                    break;
                }

                // text parts must match exactly
                if (string.Compare(token.Text, 0, text, textOffset, token.Text.Length) != 0)
                {
                    break;
                }

                textOffset += token.Text.Length;

                if (textOffset == text.Length)
                {
                    return(i + 1);
                }
            }

            return(-(i + 1));
        }