Exemple #1
0
		/// <summary>Searches a list of expressions/statements for one or more 
		/// patterns, and performs replacements.</summary>
		/// <param name="stmts">A list of expressions/statements in which to search.</param>
		/// <param name="patterns">Each pair consists of (A) something to search 
		/// for and (B) a replacement expression. Part A can use the substitution
		/// operator with an identifier inside (e.g. $Foo) to "capture" any 
		/// subexpression, and part B can use the same substitution (e.g. $Foo)
		/// to insert the captured subexpression(s) into the output.</param>
		/// <param name="replacementCount">Number of replacements that occurred.</param>
		/// <returns>The result of applying the replacements.</returns>
		/// <remarks><see cref="LNodeExt.MatchesPattern"/> is used for matching.</remarks>
		public static RVList<LNode> Replace(RVList<LNode> stmts, Pair<LNode, LNode>[] patterns, out int replacementCount)
		{
			// This list is used to support simple token replacement in TokenTrees
			_tokenTreeRepls = InternalList<Triplet<Symbol, LNode, int>>.Empty;
			foreach (var pair in patterns) // Look for Id => Id or Id => Literal
				if (pair.A.IsId && (pair.B.IsId || pair.B.IsLiteral))
					_tokenTreeRepls.Add(new Triplet<Symbol,LNode,int>(pair.A.Name, pair.B, 0));

			// Scan the syntax tree for things to replace...
			int count = 0;
			var temp = new MMap<Symbol, LNode>();
			var output = stmts.SmartSelect(stmt => stmt.ReplaceRecursive(n => {
				LNode r = TryReplaceHere(n, patterns, temp);
				if (r != null) count++;
				return r;
			}));
			replacementCount = count;
			return output;
		}
Exemple #2
0
 public bool Remove(ISegment item)
 {
     return(InternalList.Remove(item));
 }
Exemple #3
0
 public void CopyTo(ISegment[] array, int arrayIndex)
 {
     InternalList.CopyTo(array, arrayIndex);
 }
Exemple #4
0
 public bool Contains(ISegment item)
 {
     return(InternalList.Contains(item));
 }
Exemple #5
0
        // Used by Sort, StableSort, SortLowestK, SortLowestKStable.
        private static void SortCore <T>(this IList <T> list, int index, int count, Comparison <T> comp,
                                         int[] indexes, int quickSelectElems)
        {
            // This code duplicates the code in InternalList.Sort(), except
            // that it also supports stable sorting (indexes parameter) and
            // quickselect (sorting the first 'quickSelectElems' elements). This
            // version is slower; two versions exist so that array sorting can
            // be done faster.
            if (quickSelectElems <= 0)
            {
                return;
            }

            for (;;)
            {
                if (count < InternalList.QuickSortThreshold)
                {
                    if (count <= 2)
                    {
                        if (count == 2)
                        {
                            int c = comp(list[index], list[index + 1]);
                            if (c > 0 || (c == 0 && indexes != null && indexes[index] > indexes[index + 1]))
                            {
                                Swap(list, index, index + 1);
                            }
                        }
                        return;
                    }
                    else if (indexes == null)
                    {
                        InsertionSort(list, index, count, comp);
                        return;
                    }
                }

                // TODO: fix slug: PickPivot does not use 'indexes'. Makes stable sort slower if many duplicate items.
                int iPivot = InternalList.PickPivot(list, index, count, comp);

                int iBegin = index;
                // Swap the pivot to the beginning of the range
                T pivot = list[iPivot];
                if (iBegin != iPivot)
                {
                    Swap(list, iBegin, iPivot);
                    if (indexes != null)
                    {
                        MathEx.Swap(ref indexes[iPivot], ref indexes[iBegin]);
                    }
                }

                int i        = iBegin + 1;
                int iOut     = iBegin;
                int iStop    = index + count;
                int leftSize = 0;                 // size of left partition

                // Quick sort pass
                do
                {
                    int order = comp(list[i], pivot);
                    if (order > 0)
                    {
                        continue;
                    }
                    if (order == 0)
                    {
                        if (indexes != null)
                        {
                            if (indexes[i] > indexes[iBegin])
                            {
                                continue;
                            }
                        }
                        else if (leftSize < (count >> 1))
                        {
                            continue;
                        }
                    }

                    ++iOut;
                    ++leftSize;
                    if (i != iOut)
                    {
                        Swap(list, i, iOut);
                        if (indexes != null)
                        {
                            MathEx.Swap(ref indexes[i], ref indexes[iOut]);
                        }
                    }
                } while (++i != iStop);

                // Finally, put the pivot element in the middle (at iOut)
                Swap(list, iBegin, iOut);
                if (indexes != null)
                {
                    MathEx.Swap(ref indexes[iBegin], ref indexes[iOut]);
                }

                // Now we need to sort the left and right sub-partitions. Use a
                // recursive call only to sort the smaller partition, in order to
                // guarantee O(log N) stack space usage.
                int rightSize = count - 1 - leftSize;
                if (leftSize < rightSize)
                {
                    // Recursively sort the left partition; iteratively sort the right
                    SortCore(list, index, leftSize, comp, indexes, quickSelectElems);
                    index += leftSize + 1;
                    count  = rightSize;
                    if ((quickSelectElems -= leftSize + 1) <= 0)
                    {
                        break;
                    }
                }
                else
                {                       // Iteratively sort the left partition; recursively sort the right
                    count = leftSize;
                    SortCore(list, index + leftSize + 1, rightSize, comp, indexes, quickSelectElems - (leftSize + 1));
                }
            }
        }
Exemple #6
0
		protected override IntSet New(IntSet basis, bool inverted, InternalList<IntRange> ranges)
		{
			return new PGIntSet(basis.IsCharSet, ranges, inverted, false);
		}
Exemple #7
0
		/// <summary>Replaces Ids with Ids or Literals in token trees.</summary>
		private static bool ReplaceInTokenTree(ref TokenTree tokens, InternalList<Triplet<Symbol, LNode, int>> Replacements)
		{
			TokenTree children;
			bool modified = false;
			for (int i = 0; i < tokens.Count; i++) {
				Token token = tokens[i];
				Symbol id = token.Value as Symbol;
				if (id != null) {
					for (int r = 0; r < Replacements.Count; r++) {
						var repl = Replacements[r];
						if (id == repl.A) {
							if (repl.B.IsId) {
								ReplaceAt(ref modified, ref tokens, i, token.WithValue(repl.B.Name));
							} else if (repl.B.IsLiteral) {
								ReplaceAt(ref modified, ref tokens, i, new Token(
									(int)TokenKind.Literal,
									token.StartIndex, token.Length, token.Style,
									repl.B.Value));
							}
							if (!repl.B.IsCall)
								Replacements.InternalArray[r].C++; // prevent 'never used' warning
						}
					}
				} else if ((children = token.Children) != null) {
					if (ReplaceInTokenTree(ref children, Replacements))
						ReplaceAt(ref modified, ref tokens, i, token.WithValue(children));
				}
			}
			return modified;
		}
 /// <summary>
 /// Gets Online Responder revocation configuration by name. If named configuration is not found, the indexer returns null.
 /// </summary>
 /// <param name="name">Revocation configuration name.</param>
 public OcspResponderRevocationConfiguration this[String name] => InternalList.FirstOrDefault(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
Exemple #9
0
 public int IndexOf(ISegment item)
 {
     return(InternalList.IndexOf(item));
 }
Exemple #10
0
 public bool Remove(ISegment item) => InternalList.Remove(item);
Exemple #11
0
 public IEnumerator <ISegment> GetEnumerator() => InternalList.GetEnumerator();
Exemple #12
0
 public bool Contains(ISegment item) => InternalList.Contains(item);
Exemple #13
0
 public void Clear() => InternalList.Clear();
Exemple #14
0
 public int IndexOf(ISegment item) => InternalList.IndexOf(item);
Exemple #15
0
 public IEnumerator <ISegment> GetEnumerator()
 {
     return(InternalList.GetEnumerator());
 }
Exemple #16
0
 public void Insert(int index, ISegment item)
 {
     InternalList.Insert(index, item);
 }
Exemple #17
0
 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 {
     return(InternalList.GetEnumerator());
 }
Exemple #18
0
 public void RemoveAt(int index)
 {
     InternalList.RemoveAt(index);
 }
Exemple #19
0
			private void AutoAddBranchForAndPred(ref InternalList<PredictionBranch> children, AndPred andPred, List<KthSet> alts, Set<AndPred> matched, MSet<AndPred> falsified)
			{
				if (!falsified.Contains(andPred)) {
					var innerMatched = matched.With(andPred);
					var result = new PredictionBranch(new Set<AndPred>().With(andPred),
						ComputeAssertionTree2(alts, innerMatched));
					falsified.Add(andPred);
					RemoveFalsifiedCases(alts, falsified);
					children.Add(result);
				}
			}
Exemple #20
0
 public void Add(ISegment item)
 {
     InternalList.Add(item);
 }
Exemple #21
0
		protected PGIntSet(bool isCharSet, InternalList<IntRange> ranges, bool inverted, bool autoSimplify) : base(isCharSet, ranges, inverted, autoSimplify) { }
Exemple #22
0
 public void Clear()
 {
     InternalList.Clear();
 }
Exemple #23
0
		public PrinterState(StringBuilder s, string indent = "\t", string newline = "\n")
		{
			S = s ?? new StringBuilder();
			IndentLevel = 0;
			IndentString = indent;
			NewlineString = newline;
			_lineStartIndex = 0;
			_lineStartAfterIndent = 0;
			LineNo = 1;
			_newlines = InternalList<Revokable>.Empty;
		}
        void PrintMessages(InternalList <MacroResult> results, LNode input, int accepted, Severity maxSeverity)
        {
            if (accepted > 1)
            {
                // Multiple macros accepted the input. If AllowDuplicates is used,
                // this is fine if as long as they produced the same result.
                bool allowed, equal = AreAllOutcomesEqual(results, out allowed);
                if (!equal || !allowed)
                {
                    string list = results.Where(r => r.NewNode != null).Select(r => QualifiedName(r.Macro.Macro.Method)).Join(", ");
                    if (equal)
                    {
                        _sink.Write(Severity.Warning, input, "Ambiguous macro call. {0} macros accepted the input and produced identical results: {1}", accepted, list);
                    }
                    else
                    {
                        _sink.Write(Severity.Error, input, "Ambiguous macro call. {0} macros accepted the input: {1}", accepted, list);
                    }
                }
            }

            bool macroStyleCall = input.BaseStyle == NodeStyle.Special;

            if (accepted > 0 || macroStyleCall || maxSeverity >= Severity.Warning)
            {
                if (macroStyleCall && maxSeverity < Severity.Warning)
                {
                    maxSeverity = Severity.Warning;
                }
                var rejected = results.Where(r => r.NewNode == null && (r.Macro.Mode & MacroMode.Passive) == 0);
                if (accepted == 0 && macroStyleCall && _sink.IsEnabled(maxSeverity) && rejected.Any())
                {
                    _sink.Write(maxSeverity, input, "{0} macro(s) saw the input and declined to process it: {1}",
                                results.Count, rejected.Select(r => QualifiedName(r.Macro.Macro.Method)).Join(", "));
                }

                foreach (var result in results)
                {
                    bool printedLast = true;
                    foreach (var msg in result.Msgs)
                    {
                        // Print all messages from macros that accepted the input.
                        // For rejecting macros, print warning/error messages, and
                        // other messages when macroStyleCall.
                        if (_sink.IsEnabled(msg.Severity) && (result.NewNode != null ||
                                                              (msg.Severity == Severity.Detail && printedLast) ||
                                                              msg.Severity >= Severity.Warning ||
                                                              macroStyleCall))
                        {
                            var msg2 = new LogMessage(msg.Severity, msg.Context,
                                                      QualifiedName(result.Macro.Macro.Method) + ": " + msg.Format, msg.Args);
                            msg2.WriteTo(_sink);
                            printedLast = true;
                        }
                        else
                        {
                            printedLast = false;
                        }
                    }
                }
            }
        }