コード例 #1
0
ファイル: Parser.cs プロジェクト: chubbyerror/BpmNet
 private void CheckPattern(ProductionPattern pattern)
 {
     for (int i = 0; i < pattern.Count; i++)
     {
         CheckAlternative(pattern.Name, pattern[i]);
     }
 }
コード例 #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);
        }
コード例 #3
0
        private bool IsNext(ProductionPattern pattern)
        {
            LookAheadSet set = pattern.LookAhead;

            if (set == null)
            {
                return(false);
            }
            else
            {
                return(set.IsNext(this));
            }
        }
コード例 #4
0
        private Node ParsePattern(ProductionPattern pattern)
        {
            var defaultAlt = pattern.DefaultAlternative;

            for (int i = 0; i < pattern.Count; i++)
            {
                var alt = pattern[i];
                if (defaultAlt != alt && IsNext(alt))
                {
                    return(ParseAlternative(alt));
                }
            }
            if (defaultAlt == null || !IsNext(defaultAlt))
            {
                ThrowParseException(FindUnion(pattern));
            }
            return(ParseAlternative(defaultAlt));
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: Parser.cs プロジェクト: chubbyerror/BpmNet
        private string ToString(ProductionPattern prod)
        {
            StringBuilder buffer = new StringBuilder();
            StringBuilder indent = new StringBuilder();
            int           i;

            buffer.Append(prod.Name);
            buffer.Append(" (");
            buffer.Append(prod.Id);
            buffer.Append(") ");
            for (i = 0; i < buffer.Length; i++)
            {
                indent.Append(" ");
            }
            buffer.Append("= ");
            indent.Append("| ");
            for (i = 0; i < prod.Count; i++)
            {
                if (i > 0)
                {
                    buffer.Append(indent);
                }
                buffer.Append(ToString(prod[i]));
                buffer.Append("\n");
            }
            for (i = 0; i < prod.Count; i++)
            {
                var set = prod[i].LookAhead;
                if (set.GetMaxLength() > 1)
                {
                    buffer.Append("Using ");
                    buffer.Append(set.GetMaxLength());
                    buffer.Append(" token look-ahead for alternative ");
                    buffer.Append(i + 1);
                    buffer.Append(": ");
                    buffer.Append(set.ToString(_tokenizer));
                    buffer.Append("\n");
                }
            }
            return(buffer.ToString());
        }
コード例 #7
0
ファイル: Parser.cs プロジェクト: chubbyerror/BpmNet
 public virtual void AddPattern(ProductionPattern pattern)
 {
     if (pattern.Count <= 0)
     {
         throw new ParserCreationException(
                   ParserCreationException.ErrorType.INVALID_PRODUCTION,
                   pattern.Name,
                   "no production alternatives are present (must have at " +
                   "least one)");
     }
     if (_patternIds.ContainsKey(pattern.Id))
     {
         throw new ParserCreationException(
                   ParserCreationException.ErrorType.INVALID_PRODUCTION,
                   pattern.Name,
                   "another pattern with the same id (" + pattern.Id +
                   ") has already been added");
     }
     _patterns.Add(pattern);
     _patternIds.Add(pattern.Id, pattern);
     SetInitialized(false);
 }
コード例 #8
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);
        }
コード例 #9
0
        public override void AddPattern(ProductionPattern pattern)
        {
            // Check for empty matches
            if (pattern.IsMatchingEmpty())
            {
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INVALID_PRODUCTION,
                          pattern.Name,
                          "zero elements can be matched (minimum is one)");
            }

            // Check for left-recusive patterns
            if (pattern.IsLeftRecursive())
            {
                throw new ParserCreationException(
                          ParserCreationException.ErrorType.INVALID_PRODUCTION,
                          pattern.Name,
                          "left recursive patterns are not allowed");
            }

            // Add pattern
            base.AddPattern(pattern);
        }
コード例 #10
0
 public virtual Production NewProduction(ProductionPattern pattern)
 {
     return(new Production(pattern));
 }
コード例 #11
0
ファイル: Parser.cs プロジェクト: chubbyerror/BpmNet
 protected virtual Production NewProduction(ProductionPattern pattern)
 {
     return(_analyzer.NewProduction(pattern));
 }
コード例 #12
0
 public Production(ProductionPattern pattern)
 {
     this._pattern  = pattern;
     this._children = new ArrayList();
 }
コード例 #13
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);
            }
        }
コード例 #14
0
 internal void SetPattern(ProductionPattern pattern)
 {
     this._pattern = pattern;
 }