示例#1
0
 private void ParseElement(Production node,
                           ProductionPatternElement elem)
 {
     for (int i = 0; i < elem.MaxCount; i++)
     {
         if (i < elem.MinCount || IsNext(elem))
         {
             Node child;
             if (elem.IsToken())
             {
                 child = NextToken(elem.Id);
                 EnterNode(child);
                 AddNode(node, ExitNode(child));
             }
             else
             {
                 child = ParsePattern(GetPattern(elem.Id));
                 AddNode(node, child);
             }
         }
         else
         {
             break;
         }
     }
 }
示例#2
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);
        }
示例#3
0
 private ProductionPattern ParseElement(ParseState state,
                                        ProductionPatternElement elem)
 {
     for (int i = state.tokenindex; i < elem.MaxCount; i++)
     {
         if (i < elem.MinCount || IsNext(elem))
         {
             Node child;
             if (elem.IsToken())
             {
                 child = NextToken(elem.Id);
                 EnterNode(child);
                 AddNode((Production)state.node, ExitNode(child));
             }
             else
             {
                 // continue from next token when we return
                 state.tokenindex = i + 1;
                 // return to start processing the new pattern at this state
                 return(GetPattern(elem.Id));;
             }
         }
         else
         {
             break;
         }
     }
     //
     // we completed processing this element
     state.tokenindex = 0;
     return(null);
 }
示例#4
0
 private void CheckElement(string name,
                           ProductionPatternElement elem)
 {
     if (elem.IsProduction() && GetPattern(elem.Id) == null)
     {
         throw new ParserCreationException(
                   ParserCreationException.ErrorType.INVALID_PRODUCTION,
                   name,
                   "an undefined production pattern id (" + elem.Id +
                   ") is referenced");
     }
 }
 public void AddElement(ProductionPatternElement elem,
                        int min,
                        int max)
 {
     if (elem.IsToken())
     {
         AddToken(elem.Id, min, max);
     }
     else
     {
         AddProduction(elem.Id, min, max);
     }
 }
示例#6
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)));
            }
        }
示例#7
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);
        }
示例#8
0
        private string ToString(ProductionPatternElement elem)
        {
            StringBuilder buffer = new StringBuilder();
            int           min    = elem.MinCount;
            int           max    = elem.MaxCount;

            if (min == 0 && max == 1)
            {
                buffer.Append("[");
            }
            if (elem.IsToken())
            {
                buffer.Append(GetTokenDescription(elem.Id));
            }
            else
            {
                buffer.Append(GetPattern(elem.Id).Name);
            }
            if (min == 0 && max == 1)
            {
                buffer.Append("]");
            }
            else if (min == 0 && max == Int32.MaxValue)
            {
                buffer.Append("*");
            }
            else if (min == 1 && max == Int32.MaxValue)
            {
                buffer.Append("+");
            }
            else if (min != 1 || max != 1)
            {
                buffer.Append("{");
                buffer.Append(min);
                buffer.Append(",");
                buffer.Append(max);
                buffer.Append("}");
            }
            return(buffer.ToString());
        }
 public void AddElement(ProductionPatternElement elem)
 {
     _elements.Add(elem);
 }