// * // * Copyright (C) 2008 Roger Alsing : http://www.RogerAlsing.com // * // * This library is free software; you can redistribute it and/or modify it // * under the terms of the GNU Lesser General Public License 2.1 or later, as // * published by the Free Software Foundation. See the included license.txt // * or http://www.gnu.org/copyleft/lesser.html for details. // * // * using System; using System.Collections; namespace Alsing.SourceCode { /// /// A List containing patterns. /// this could be for example a list of keywords or operators /// public sealed class PatternList : IEnumerable { private readonly PatternCollection patterns = new PatternCollection(); /// /// Gets or Sets if this list contains case seinsitive patterns /// public bool CaseSensitive; /// /// For public use only /// public PatternCollection ComplexPatterns = new PatternCollection(); /// /// The name of the pattern list /// public string Name = ""; /// /// Gets or Sets if the patterns in this list should be case normalized /// public bool NormalizeCase; /// /// /// public PatternListList Parent; /// /// The parent spanDefinition of this list /// public SpanDefinition parentSpanDefinition; /// /// for public use only /// public Hashtable SimplePatterns = new Hashtable(); /// /// /// public Hashtable SimplePatterns1Char = new Hashtable(); /// /// For public use only /// public Hashtable SimplePatterns2Char = new Hashtable(); /// /// Gets or Sets the TextStyle that should be assigned to patterns in this list /// public TextStyle Style = new TextStyle(); /// /// /// public PatternList() { SimplePatterns = new Hashtable(StringComparer.CurrentCultureIgnoreCase); } #region IEnumerable Members /// /// /// /// public IEnumerator GetEnumerator() { return patterns.GetEnumerator(); } #endregion /// /// /// /// /// public Pattern Add(Pattern Pattern) { if (Parent != null && Parent.Parent != null && Parent.Parent.Parent != null) { Pattern.Separators = Parent.Parent.Parent.Separators; Parent.Parent.Parent.ChangeVersion(); } if (!Pattern.IsComplex && !Pattern.ContainsSeparator) { //store pattern in lookuptable if it is a simple pattern string s; if (Pattern.StringPattern.Length >= 2) s = Pattern.StringPattern.Substring(0, 2); else s = Pattern.StringPattern.Substring(0, 1) + " "; s = s.ToLowerInvariant(); if (Pattern.StringPattern.Length == 1) { SimplePatterns1Char[Pattern.StringPattern] = Pattern; } else { if (SimplePatterns2Char[s] == null) SimplePatterns2Char[s] = new PatternCollection(); var ar = (PatternCollection) SimplePatterns2Char[s]; ar.Add(Pattern); } if (CaseSensitive) SimplePatterns[Pattern.LowerStringPattern] = Pattern; else SimplePatterns[Pattern.StringPattern] = Pattern; } else { ComplexPatterns.Add(Pattern); } patterns.Add(Pattern); if (Pattern.Parent == null) Pattern.Parent = this; else { throw (new Exception("Pattern already assigned to another PatternList")); } return Pattern; } /// /// /// public void Clear() { patterns.Clear(); } } }