Esempio n. 1
0
        private void ThrowParseException(LookAheadSet set)
        {
            ArrayList list = new ArrayList();

            // Read tokens until mismatch
            while (set.IsNext(this, 1))
            {
                set = set.CreateNextSet(NextToken().Id);
            }

            // Find next token descriptions
            var initials = set.GetInitialTokens();

            for (int i = 0; i < initials.Length; i++)
            {
                list.Add(GetTokenDescription(initials[i]));
            }

            // Create exception
            var token = NextToken();

            throw new ParseException(ParseException.ErrorType.UNEXPECTED_TOKEN,
                                     token.ToShortString(),
                                     list,
                                     token.StartLine,
                                     token.StartColumn);
        }
Esempio n. 2
0
        private LookAheadSet FindLookAhead(ProductionPattern pattern,
                                           int length,
                                           CallStack stack,
                                           LookAheadSet filter)
        {
            // Check for infinite loop
            if (stack.Contains(pattern.Name, length))
            {
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INFINITE_LOOP,
                          pattern.Name,
                          (String)null);
            }

            // Find pattern look-ahead
            stack.Push(pattern.Name, length);
            var result = new LookAheadSet(length);

            for (int i = 0; i < pattern.Count; i++)
            {
                var temp = FindLookAhead(pattern[i],
                                         length,
                                         0,
                                         stack,
                                         filter);
                result.AddAll(temp);
            }
            stack.Pop();

            return(result);
        }
Esempio n. 3
0
        private LookAheadSet FindLookAhead(ProductionPatternElement elem,
                                           int length,
                                           int dummy,
                                           CallStack stack,
                                           LookAheadSet filter)
        {
            LookAheadSet result;

            if (elem.IsToken())
            {
                result = new LookAheadSet(length);
                result.Add(elem.Id);
            }
            else
            {
                var pattern = GetPattern(elem.Id);
                result = FindLookAhead(pattern, length, stack, filter);
                if (stack.Contains(pattern.Name))
                {
                    result = result.CreateRepetitive();
                }
            }

            return(result);
        }
Esempio n. 4
0
 public void AddAll(LookAheadSet set)
 {
     for (int i = 0; i < set._elements.Count; i++)
     {
         Add((Sequence)set._elements[i]);
     }
 }
Esempio n. 5
0
 public void RemoveAll(LookAheadSet set)
 {
     for (int i = 0; i < set._elements.Count; i++)
     {
         Remove((Sequence)set._elements[i]);
     }
 }
Esempio n. 6
0
        public LookAheadSet CreateFilter(LookAheadSet set)
        {
            LookAheadSet result = new LookAheadSet(_maxLength);

            // Handle special cases
            if (this.Size() <= 0 || set.Size() <= 0)
            {
                return(this);
            }

            // Create combinations
            for (int i = 0; i < _elements.Count; i++)
            {
                var first = (Sequence)_elements[i];
                for (int j = 0; j < set._elements.Count; j++)
                {
                    var second = (Sequence)set._elements[j];
                    if (first.StartsWith(second))
                    {
                        result.Add(first.Subsequence(second.Length()));
                    }
                }
            }
            return(result);
        }
Esempio n. 7
0
 public ProductionPattern(int id, string name)
 {
     this._id           = id;
     this._name         = name;
     this._synthetic    = false;
     this._alternatives = new ArrayList();
     this._defaultAlt   = -1;
     this._lookAhead    = null;
 }
Esempio n. 8
0
 public bool IsOverlap(LookAheadSet set)
 {
     for (int i = 0; i < _elements.Count; i++)
     {
         if (set.IsOverlap((Sequence)_elements[i]))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 9
0
 public bool Intersects(LookAheadSet set)
 {
     for (int i = 0; i < _elements.Count; i++)
     {
         if (set.Contains((Sequence)_elements[i]))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 10
0
        private LookAheadSet FindConflicts(string pattern,
                                           string location,
                                           LookAheadSet set1,
                                           LookAheadSet set2)
        {
            var result = set1.CreateIntersection(set2);

            if (result.IsRepetitive())
            {
                ThrowAmbiguityException(pattern, location, result);
            }
            return(result);
        }
Esempio n. 11
0
        private bool IsNext(ProductionPattern pattern)
        {
            LookAheadSet set = pattern.LookAhead;

            if (set == null)
            {
                return(false);
            }
            else
            {
                return(set.IsNext(this));
            }
        }
Esempio n. 12
0
        private bool IsNext(ProductionPatternAlternative alt)
        {
            LookAheadSet set = alt.LookAhead;

            if (set == null)
            {
                return(false);
            }
            else
            {
                return(set.IsNext(this));
            }
        }
Esempio n. 13
0
        public LookAheadSet CreateOverlaps(LookAheadSet set)
        {
            LookAheadSet result = new LookAheadSet(_maxLength);

            for (int i = 0; i < _elements.Count; i++)
            {
                var seq = (Sequence)_elements[i];
                if (set.IsOverlap(seq))
                {
                    result.Add(seq);
                }
            }
            return(result);
        }
Esempio n. 14
0
        public LookAheadSet CreateNextSet(int token)
        {
            LookAheadSet result = new LookAheadSet(_maxLength - 1);

            for (int i = 0; i < _elements.Count; i++)
            {
                var seq   = (Sequence)_elements[i];
                var value = seq.GetToken(0);
                if (value != null && token == (int)value)
                {
                    result.Add(seq.Subsequence(1));
                }
            }
            return(result);
        }
Esempio n. 15
0
        private bool IsNext(ProductionPatternElement elem)
        {
            LookAheadSet set = elem.LookAhead;

            if (set != null)
            {
                return(set.IsNext(this));
            }
            else if (elem.IsToken())
            {
                return(elem.IsMatch(PeekToken(0)));
            }
            else
            {
                return(IsNext(GetPattern(elem.Id)));
            }
        }
Esempio n. 16
0
        public LookAheadSet CreateRepetitive()
        {
            LookAheadSet result = new LookAheadSet(_maxLength);

            for (int i = 0; i < _elements.Count; i++)
            {
                var seq = (Sequence)_elements[i];
                if (seq.IsRepetitive())
                {
                    result.Add(seq);
                }
                else
                {
                    result.Add(new Sequence(true, seq));
                }
            }
            return(result);
        }
Esempio n. 17
0
        private LookAheadSet FindLookAhead(ProductionPatternAlternative alt,
                                           int length,
                                           int pos,
                                           CallStack stack,
                                           LookAheadSet filter)
        {
            LookAheadSet follow;

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

            // Find look-ahead for this element
            var 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))
            {
                var 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);
        }
Esempio n. 18
0
        private LookAheadSet FindLookAhead(ProductionPatternElement elem,
                                           int length,
                                           CallStack stack,
                                           LookAheadSet filter)
        {
            // Find initial element look-ahead
            var first  = FindLookAhead(elem, length, 0, stack, filter);
            var 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();
            }
            var max = elem.MaxCount;

            if (length < max)
            {
                max = length;
            }
            for (int i = 1; i < max; i++)
            {
                first = first.CreateOverlaps(filter);
                if (first.Size() <= 0 || first.GetMinLength() >= length)
                {
                    break;
                }
                var follow = FindLookAhead(elem,
                                           length,
                                           0,
                                           stack,
                                           filter.CreateFilter(first));
                first = first.CreateCombination(follow);
                result.AddAll(first);
            }

            return(result);
        }
Esempio n. 19
0
        public LookAheadSet CreateIntersection(LookAheadSet set)
        {
            LookAheadSet result = new LookAheadSet(_maxLength);

            for (int i = 0; i < _elements.Count; i++)
            {
                var seq1 = (Sequence)_elements[i];
                var seq2 = set.FindSequence(seq1);
                if (seq2 != null && seq1.IsRepetitive())
                {
                    result.Add(seq2);
                }
                else if (seq2 != null)
                {
                    result.Add(seq1);
                }
            }
            return(result);
        }
Esempio n. 20
0
        private LookAheadSet FindConflicts(ProductionPattern pattern,
                                           int maxLength)
        {
            LookAheadSet result = new LookAheadSet(maxLength);

            for (int i = 0; i < pattern.Count; i++)
            {
                var set1 = pattern[i].LookAhead;
                for (int j = 0; j < i; j++)
                {
                    var set2 = pattern[j].LookAhead;
                    result.AddAll(set1.CreateIntersection(set2));
                }
            }
            if (result.IsRepetitive())
            {
                ThrowAmbiguityException(pattern.Name, null, result);
            }
            return(result);
        }
Esempio n. 21
0
        private void ThrowAmbiguityException(string pattern,
                                             string location,
                                             LookAheadSet set)
        {
            ArrayList list = new ArrayList();

            // Find next token descriptions
            var initials = set.GetInitialTokens();

            for (int i = 0; i < initials.Length; i++)
            {
                list.Add(GetTokenDescription(initials[i]));
            }

            // Create exception
            throw new ParserCreationException(
                      ParserCreationException.ErrorType.INHERENT_AMBIGUITY,
                      pattern,
                      location,
                      list);
        }
Esempio n. 22
0
        private LookAheadSet FindUnion(ProductionPattern pattern)
        {
            LookAheadSet result;
            int          length = 0;
            int          i;

            for (i = 0; i < pattern.Count; i++)
            {
                result = pattern[i].LookAhead;
                if (result.GetMaxLength() > length)
                {
                    length = result.GetMaxLength();
                }
            }
            result = new LookAheadSet(length);
            for (i = 0; i < pattern.Count; i++)
            {
                result.AddAll(pattern[i].LookAhead);
            }

            return(result);
        }
 public ProductionPatternElement(bool isToken,
                                 int id,
                                 int min,
                                 int max)
 {
     this._token = isToken;
     this._id    = id;
     if (min < 0)
     {
         min = 0;
     }
     this._min = min;
     if (max <= 0)
     {
         max = Int32.MaxValue;
     }
     else if (max < min)
     {
         max = min;
     }
     this._max       = max;
     this._lookAhead = null;
 }
Esempio n. 24
0
        public LookAheadSet CreateCombination(LookAheadSet set)
        {
            LookAheadSet result = new LookAheadSet(_maxLength);

            // Handle special cases
            if (this.Size() <= 0)
            {
                return(set);
            }
            else if (set.Size() <= 0)
            {
                return(this);
            }

            // Create combinations
            for (int i = 0; i < _elements.Count; i++)
            {
                var first = (Sequence)_elements[i];
                if (first.Length() >= _maxLength)
                {
                    result.Add(first);
                }
                else if (first.Length() <= 0)
                {
                    result.AddAll(set);
                }
                else
                {
                    for (int j = 0; j < set._elements.Count; j++)
                    {
                        var second = (Sequence)set._elements[j];
                        result.Add(first.Concat(_maxLength, second));
                    }
                }
            }
            return(result);
        }
Esempio n. 25
0
        private void CalculateLookAhead(ProductionPattern pattern)
        {
            ProductionPatternAlternative alt;
            LookAheadSet previous = new LookAheadSet(0);
            int          length   = 1;
            int          i;
            CallStack    stack = new CallStack();

            // Calculate simple look-ahead
            stack.Push(pattern.Name, 1);
            var result       = new LookAheadSet(1);
            var alternatives = new LookAheadSet[pattern.Count];

            for (i = 0; i < pattern.Count; i++)
            {
                alt             = pattern[i];
                alternatives[i] = FindLookAhead(alt, 1, 0, stack, null);
                alt.LookAhead   = alternatives[i];
                result.AddAll(alternatives[i]);
            }
            if (pattern.LookAhead == null)
            {
                pattern.LookAhead = result;
            }
            var conflicts = FindConflicts(pattern, 1);

            // Resolve conflicts
            while (conflicts.Size() > 0)
            {
                length++;
                stack.Clear();
                stack.Push(pattern.Name, length);
                conflicts.AddAll(previous);
                for (i = 0; i < pattern.Count; i++)
                {
                    alt = pattern[i];
                    if (alternatives[i].Intersects(conflicts))
                    {
                        alternatives[i] = FindLookAhead(alt,
                                                        length,
                                                        0,
                                                        stack,
                                                        conflicts);
                        alt.LookAhead = alternatives[i];
                    }
                    if (alternatives[i].Intersects(conflicts))
                    {
                        if (pattern.DefaultAlternative == null)
                        {
                            pattern.DefaultAlternative = alt;
                        }
                        else if (pattern.DefaultAlternative != alt)
                        {
                            result = alternatives[i].CreateIntersection(conflicts);
                            ThrowAmbiguityException(pattern.Name,
                                                    null,
                                                    result);
                        }
                    }
                }
                previous  = conflicts;
                conflicts = FindConflicts(pattern, length);
            }

            // Resolve conflicts inside rules
            for (i = 0; i < pattern.Count; i++)
            {
                CalculateLookAhead(pattern[i], 0);
            }
        }
Esempio n. 26
0
        private void CalculateLookAhead(ProductionPatternAlternative alt,
                                        int pos)
        {
            LookAheadSet previous = new LookAheadSet(0);
            int          length   = 1;

            // Check trivial cases
            if (pos >= alt.Count)
            {
                return;
            }

            // Check for non-optional element
            var pattern = alt.Pattern;
            var elem    = alt[pos];

            if (elem.MinCount == elem.MaxCount)
            {
                CalculateLookAhead(alt, pos + 1);
                return;
            }

            // Calculate simple look-aheads
            var first  = FindLookAhead(elem, 1, new CallStack(), null);
            var follow = FindLookAhead(alt, 1, pos + 1, new CallStack(), null);

            // Resolve conflicts
            var location  = "at position " + (pos + 1);
            var conflicts = FindConflicts(pattern.Name,
                                          location,
                                          first,
                                          follow);

            while (conflicts.Size() > 0)
            {
                length++;
                conflicts.AddAll(previous);
                first = FindLookAhead(elem,
                                      length,
                                      new CallStack(),
                                      conflicts);
                follow = FindLookAhead(alt,
                                       length,
                                       pos + 1,
                                       new CallStack(),
                                       conflicts);
                first          = first.CreateCombination(follow);
                elem.LookAhead = first;
                if (first.Intersects(conflicts))
                {
                    first = first.CreateIntersection(conflicts);
                    ThrowAmbiguityException(pattern.Name, location, first);
                }
                previous  = conflicts;
                conflicts = FindConflicts(pattern.Name,
                                          location,
                                          first,
                                          follow);
            }

            // Check remaining elements
            CalculateLookAhead(alt, pos + 1);
        }
Esempio n. 27
0
 public LookAheadSet(int maxLength, LookAheadSet set)
     : this(maxLength)
 {
     AddAll(set);
 }