Пример #1
0
        //*
        // * Checks if another look-ahead set has an overlapping token
        // * sequence. An overlapping token sequence is a token sequence
        // * that is identical to another sequence, but for the length.
        // * I.e. one of the two sequences may be longer than the other.
        // *
        // * @param set the look-ahead set to check
        // *
        // * @return true if there is some token sequence that overlaps, or
        // * false otherwise
        //

        public bool IsOverlap(LookAheadSet @set)
        {
            for (int i = 0; i <= elements.Count - 1; i++)
            {
                if (@set.IsOverlap((Sequence)elements[i]))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #2
0
        //*
        // * Creates a new look-ahead set with overlaps from another. All
        // * token sequences in this set that overlaps with the other set
        // * will be added to the new look-ahead set.
        // *
        // * @param set the look-ahead set to check with
        // *
        // * @return a new look-ahead set containing the overlaps
        //

        public LookAheadSet CreateOverlaps(LookAheadSet @set)
        {
            LookAheadSet result = new LookAheadSet(maxLength);
            Sequence     seq    = default(Sequence);

            for (int i = 0; i <= elements.Count - 1; i++)
            {
                seq = (Sequence)elements[i];
                if (@set.IsOverlap(seq))
                {
                    result.Add(seq);
                }
            }
            return(result);
        }
        //*
        // * Finds the look-ahead set for a production pattern alternative.
        // * The pattern position and maximum look-ahead length must be
        // * specified. It is also possible to specify a look-ahead set
        // * filter, which will make sure that unnecessary token sequences
        // * will be avoided.
        // *
        // * @param alt the production pattern alternative
        // * @param length the maximum look-ahead length
        // * @param pos the pattern element position
        // * @param stack the call stack used for loop detection
        // * @param filter the look-ahead set filter
        // *
        // * @return the look-ahead set for the pattern alternative
        // *
        // * @throws ParserCreationException if an infinite loop was found
        // * in the grammar
        //

        private LookAheadSet FindLookAhead(ProductionPatternAlternative alt, int length, int pos, CallStack stack, LookAheadSet filter)
        {
            LookAheadSet first    = default(LookAheadSet);
            LookAheadSet follow   = default(LookAheadSet);
            LookAheadSet overlaps = default(LookAheadSet);

            // Check trivial cases
            if (length <= 0 || pos >= alt.Count)
            {
                return(new LookAheadSet(0));
            }

            // Find look-ahead for this element
            first = FindLookAhead(alt[pos], length, stack, filter);
            if (alt[pos].MinCount == 0)
            {
                first.AddEmpty();
            }

            // Find remaining look-ahead
            if (filter == null)
            {
                length -= first.GetMinLength();
                if (length > 0)
                {
                    follow = FindLookAhead(alt, length, pos + 1, stack, null);
                    first  = first.CreateCombination(follow);
                }
            }
            else if (filter.IsOverlap(first))
            {
                overlaps = first.CreateOverlaps(filter);
                length  -= overlaps.GetMinLength();
                filter   = filter.CreateFilter(overlaps);
                follow   = FindLookAhead(alt, length, pos + 1, stack, filter);
                first.RemoveAll(overlaps);
                first.AddAll(overlaps.CreateCombination(follow));
            }

            return(first);
        }
        //*
        // * Finds the look-ahead set for a production pattern element. The
        // * maximum look-ahead length must be specified. This method takes
        // * the element repeats into consideration when creating the
        // * look-ahead set, but does NOT include an empty sequence even if
        // * the minimum count is zero (0). It is also possible to specify a
        // * look-ahead set filter, which will make sure that unnecessary
        // * token sequences will be avoided.
        // *
        // * @param elem the production pattern element
        // * @param length the maximum look-ahead length
        // * @param stack the call stack used for loop detection
        // * @param filter the look-ahead set filter
        // *
        // * @return the look-ahead set for the pattern element
        // *
        // * @throws ParserCreationException if an infinite loop was found
        // * in the grammar
        //

        private LookAheadSet FindLookAhead(ProductionPatternElement elem, int length, CallStack stack, LookAheadSet filter)
        {
            LookAheadSet result = default(LookAheadSet);
            LookAheadSet first  = default(LookAheadSet);
            LookAheadSet follow = default(LookAheadSet);
            int          max    = 0;

            // Find initial element look-ahead
            first  = FindLookAhead(elem, length, 0, stack, filter);
            result = new LookAheadSet(length);
            result.AddAll(first);
            if (filter == null || !filter.IsOverlap(result))
            {
                return(result);
            }

            // Handle element repetitions
            if (elem.MaxCount == Int32.MaxValue)
            {
                first = first.CreateRepetitive();
            }
            max = elem.MaxCount;
            if (length < max)
            {
                max = length;
            }
            for (int i = 1; i <= max - 1; i++)
            {
                first = first.CreateOverlaps(filter);
                if (first.Size() <= 0 || first.GetMinLength() >= length)
                {
                    break;                     // TODO: might not be correct. Was : Exit For
                }
                follow = FindLookAhead(elem, length, 0, stack, filter.CreateFilter(first));
                first  = first.CreateCombination(follow);
                result.AddAll(first);
            }

            return(result);
        }