Esempio n. 1
0
        public static bool OverlapsAt(this ILexResult result, int position, int length)
        {
            int end = position + length;

            // has some length
            if (length > 0)
            {
                return((position >= result.Start && position < result.GetEnd()) ||
                       (end > result.Start && end <= result.GetEnd()));
            }
            // zero-length nodes
            else if (length == 0)
            {
                return(position > result.Start && position < result.GetEnd());
            }
            // negative lengths canceled
            else
            {
                throw new InvalidOperationException("Negative lengths not allowed.");
            }
        }
Esempio n. 2
0
        public static bool IsInside(this ILexResultGroup group, ILexResult target)
        {
            int targetEnd = target.GetEnd();

            if ((target.Start >= group.Start && target.Start <= group.GetEnd()) &&
                (targetEnd >= group.Start && targetEnd <= group.GetEnd()))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
        public ILexResult Match(string target)
        {
            // if no rules, no match
            if (_rules.Count == 0)
            {
                return(LexNode.NoMatch);
            }

            LexResultGroup group = new LexResultGroup(MatchType);

            group.RequiresFilledSpace = true;

            // get first match, then offset
            ILexResult firstResult = _rules[0].Match(target);

            if (!firstResult.IsSuccessful())
            {
                return(LexNode.NoMatch);
            }

            string eaten = target.Clone() as string;

            group.Start = firstResult.Start;
            group.Add(firstResult);

            // eat the string until after the match
            eaten = eaten.Remove(0, firstResult.GetEnd());
            int offset = firstResult.GetEnd();

            // go through each match after the first
            for (int i = 1; i < Count; i++)
            {
                ILexResult result = _rules[i].Match(eaten);

                // ensure all pieces in the sequence succeed
                if (result.IsSuccessful())
                {
                    // matches must be "squared up"
                    if (result.Start != 0)
                    {
                        // wrap group as a partial result
                        return(LayeredLexNode.CreatePartial(group));
                    }
                    else
                    {
                        // eat the string
                        eaten = eaten.Remove(0, result.GetEnd());

                        // store an offset copy to the group
                        group.Add(result.Offset(offset));

                        // offset the offset
                        offset += result.GetEnd();
                    }
                }
                else if (result.MatchType == LexMatchType.Invalid)
                {
                    return(result.Offset(offset));
                }
                else
                {
                    return(LexNode.NoMatch);
                }
            }

            return(group);
        }