public void CharEscapePattern_Empty() { BasePattern actual = BasePattern.CreatePattern(""); BasePattern expected = new GroupPattern(true, new BasePattern[] { }); Assert.AreEqual(expected, actual); }
public void TwoChars() { string patternText = "cd"; BasePattern expected = new GroupPattern(true, new StringPattern("cd")); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void ManyChars() { string patternText = @"A longer string\."; BasePattern expected = new GroupPattern(true, new StringPattern("A longer string.")); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void OneChar() { string patternText = "x"; BasePattern expected = new GroupPattern(true, new CharEscapePattern('x')); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void Quantifier_0() { string patternText = "a(bc){0}"; BasePattern expected = new GroupPattern( true, new CharEscapePattern('a')); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void CharEscapePattern_Short() { BasePattern actual = BasePattern.CreatePattern("abc"); BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharEscapePattern('a'), new CharEscapePattern('b'), new CharEscapePattern('c') }); Assert.AreEqual(expected, actual); }
public void NegativeRanges() { BasePattern actual = BasePattern.CreatePattern("[A-Z][^x][^a-z][0-9]"); BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }), new CharGroupPattern(false, "x"), new CharGroupPattern(false, new[] { lowercase }), new CharGroupPattern(true, new[] { digits }) }); Assert.AreEqual(expected, actual); }
public void Singleline_True() { string patternText = ".+"; BasePattern expected = new GroupPattern( true, new QuantifierPattern( new AnyCharPattern(true), 1, null, true)); RegexAssert.IsASTTransformCorrect(expected, patternText, new RegexOptionsASTTransform(RegexOptions.Singleline)); }
public void ManyCharsWithClasses() { string patternText = @"ab[cd][efg]hijk\dm"; BasePattern expected = new GroupPattern(true, new StringPattern("ab"), new CharGroupPattern(true, "cd"), new CharGroupPattern(true, "efg"), new StringPattern("hijk"), CharGroupPattern.DigitChar, new CharEscapePattern('m')); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void Doubled() { BasePattern actual = BasePattern.CreatePattern(@"((ab)+)+"); BasePattern expected = new GroupPattern(true, new QuantifierPattern( new GroupPattern(true, new QuantifierPattern( new GroupPattern(true, new CharEscapePattern('a'), new CharEscapePattern('b')), 1, null, true)), 1, null, true)); Assert.AreEqual(expected, actual, "With parentheses"); }
public void Quantifier_N() { string patternText = "a(bc){4}"; BasePattern innerGroup = new GroupPattern( true, new CharEscapePattern('b'), new CharEscapePattern('c')); // Unchanged BasePattern expected = new GroupPattern( true, new CharEscapePattern('a'), new QuantifierPattern(innerGroup, 4, 4, true)); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public List<IGroupPattern> Convert() { List<IGroupPattern> groupPatternList = new List<IGroupPattern>(); foreach (var group in groupPatterns) { List<Coord[]> patternCoordsList = new List<Coord[]>(); foreach (var pattern in group.Cast<Transform>().OrderBy(t => t.name)) { patternCoordsList.Add(CreateGroupPattern(pattern)); } IGroupPattern groupPattern = new GroupPattern(patternCoordsList); groupPatternList.Add(groupPattern); } return groupPatternList; }
public List <IGroupPattern> Convert() { List <IGroupPattern> groupPatternList = new List <IGroupPattern>(); foreach (var group in groupPatterns) { List <Coord[]> patternCoordsList = new List <Coord[]>(); foreach (var pattern in group.Cast <Transform>().OrderBy(t => t.name)) { patternCoordsList.Add(CreateGroupPattern(pattern)); } IGroupPattern groupPattern = new GroupPattern(patternCoordsList); groupPatternList.Add(groupPattern); } return(groupPatternList); }
public void CharSetsAndRanges() { BasePattern actual = BasePattern.CreatePattern("[A-Z]"); BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }) }); Assert.AreEqual(expected, actual, "One Range"); actual = BasePattern.CreatePattern("[.,;:?!A-Za-z][xya-zm-wA-M][msabcdm ;xyz]"); expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, ".,;:?!", new[] { uppercase, lowercase }), new CharGroupPattern(true, "xy", new[] { lowercase, new CharRangePattern('m', 'w'), new CharRangePattern('A', 'M') }), new CharGroupPattern(true, "msabcd ;xyz") }); Assert.AreEqual(expected, actual); }
public void Multiline_True() { string patternText = @"^\b\B\z\Z\A\G$"; BasePattern expected = new GroupPattern( true, new AnchorPattern(AnchorType.StartOfLine), new AnchorPattern(AnchorType.WordBoundary), new AnchorPattern(AnchorType.NonWordBoundary), new AnchorPattern(AnchorType.EndOfString), new AnchorPattern(AnchorType.EndOfStringOrBeforeEndingNewline), new AnchorPattern(AnchorType.StartOfString), new AnchorPattern(AnchorType.ContiguousMatch), new AnchorPattern(AnchorType.EndOfLine)); RegexAssert.IsASTTransformCorrect(expected, patternText, new RegexOptionsASTTransform(RegexOptions.Multiline)); }
public RootNode(string regex) { var result = PatternParsers.Regex(new ArrayConsList <char>(regex)); if (result.Rest.IsEmpty) { InputPattern = result.Tree; } else { throw new ArgumentException(string.Format("RegexParse could not understand part of the regex pattern: {0}.", result.Rest), "regex."); } Type = TreeNodeType.Root; IsModified = false; OriginalRegex = regex; RootGroup = new GroupNode(); RootGroup.FromPattern(InputPattern); }
public void Quantifier_Plus() { string patternText = "a(bc)+"; BasePattern innerGroup = new GroupPattern( true, new CharEscapePattern('b'), new CharEscapePattern('c')); BasePattern expected = new GroupPattern( true, new CharEscapePattern('a'), new GroupPattern( false, innerGroup, new QuantifierPattern(innerGroup, 0, null, true))); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }
public void Quantifiers() { BasePattern actual = BasePattern.CreatePattern(@"\d*\s?\W+x*?\n??y+?"); BasePattern expected = new GroupPattern( true, new BasePattern[] { new QuantifierPattern(CharGroupPattern.DigitChar, 0, null, true), new QuantifierPattern(CharGroupPattern.WhitespaceChar, 0, 1, true), new QuantifierPattern(CharGroupPattern.WordChar.Negated, 1, null, true), new QuantifierPattern(new CharEscapePattern('x'), 0, null, false), new QuantifierPattern(new CharEscapePattern('\n'), 0, 1, false), new QuantifierPattern(new CharEscapePattern('y'), 1, null, false) }); Assert.AreEqual(expected, actual); }
public void GroupPattern() { string pattern = "t(h(in))g"; BasePattern actual = BasePattern.CreatePattern(pattern); BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharEscapePattern('t'), new GroupPattern(true, new BasePattern[] { new CharEscapePattern('h'), new GroupPattern(true, new BasePattern[] { new CharEscapePattern('i'), new CharEscapePattern('n') }), }), new CharEscapePattern('g') }); Assert.AreEqual(expected, actual, pattern); }
public void Ranges() { BasePattern actual = BasePattern.CreatePattern("[A-Z]"); BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }) }); Assert.AreEqual(expected, actual, "One Range"); actual = BasePattern.CreatePattern("[A-Z][a-z][0-9][a-zA-Z][A-Za-z0-9][a-zm-wA-M]"); expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }), new CharGroupPattern(true, new[] { lowercase }), new CharGroupPattern(true, new[] { digits }), new CharGroupPattern(true, new[] { lowercase, uppercase }), new CharGroupPattern(true, new[] { uppercase, lowercase, digits }), new CharGroupPattern(true, new[] { lowercase, new CharRangePattern('m', 'w'), new CharRangePattern('A', 'M') }) }); Assert.AreEqual(expected, actual, "Many Ranges"); }
public virtual BasePattern Transform(BasePattern pattern) { // The Identity transform switch (pattern.Type) { case PatternType.Group: GroupPattern group = (GroupPattern)pattern; BasePattern[] newChildren = group.Patterns .Select(p => Transform(p)) .Where(IsNotEmpty) .ToArray(); return(CreateGroupOrSingleton(group.IsCapturing, newChildren)); case PatternType.Quantifier: QuantifierPattern quant = (QuantifierPattern)pattern; BasePattern newChild = Transform(quant.ChildPattern); if (IsNotEmpty(newChild)) { return(new QuantifierPattern(newChild, quant.MinOccurrences, quant.MaxOccurrences, quant.IsGreedy)); } else { return(GroupPattern.Empty); } case PatternType.Alternation: return(new AlternationPattern(((AlternationPattern)pattern).Alternatives .Select(a => Transform(a)))); default: return(pattern); } }
public override BasePattern Transform(BasePattern pattern) { if (pattern.Type == PatternType.Group) { // TODO: use groupBy (Haskell-style) GroupPattern group = (GroupPattern)pattern; List <BasePattern> newChildPatterns = new List <BasePattern>(); StringBuilder currentString = new StringBuilder(); foreach (BasePattern oldChildPattern in group.Patterns) { if (oldChildPattern is CharEscapePattern) { currentString.Append(((CharEscapePattern)oldChildPattern).Value); } else { if (currentString.Length > 0) { addStringPattern(newChildPatterns, currentString.ToString()); currentString = new StringBuilder(); } newChildPatterns.Add(Transform(oldChildPattern)); } } if (currentString.Length > 0) { addStringPattern(newChildPatterns, currentString.ToString()); } return(CreateGroupOrSingleton(group.IsCapturing, newChildPatterns.ToArray())); } else { return(base.Transform(pattern)); } }
public void IgnoreCase() { string patternText = @"aA7 :.[a-m]\w\D\s.+"; BasePattern expected = new GroupPattern( true, new CaseInsensitiveCharPattern(new CharEscapePattern('a')), new CaseInsensitiveCharPattern(new CharEscapePattern('A')), new CharEscapePattern('7'), new CharEscapePattern(' '), new CharEscapePattern(':'), new AnyCharPattern(false), new CaseInsensitiveCharPattern( new CharGroupPattern(true, new [] { new CharRangePattern('a', 'm') })), CharGroupPattern.WordChar, CharGroupPattern.DigitChar.Negated, CharGroupPattern.WhitespaceChar, new QuantifierPattern( new AnyCharPattern(false), 1, null, true)); RegexAssert.IsASTTransformCorrect(expected, patternText, new RegexOptionsASTTransform(RegexOptions.IgnoreCase)); }
public override void FromPattern(GroupPattern gp) { throw new NotImplementedException(); }
internal static bool TryParse(GparamRoot gparam, out System.Collections.ObjectModel.ObservableCollection<CategoryViewModel> destCollection, string spaceReplace) { ObservableCollection<CategoryViewModel> tempCollection = new ObservableCollection<CategoryViewModel>(); try { using (var reader = new StreamReader(s_groupDefinitionFile)) { var json = DynamicJson.Parse(reader.ReadToEnd()); Console.WriteLine(json.ToString()); var GroupPatterns = new Dictionary<string, List<GroupPattern>>(); int n = 0; // #TODO: IDのName用 foreach (var paramset in gparam.ParamSet) { string categoryDispName = paramset.DispName; string categoryName = paramset.Name; string categoryBindableName = categoryDispName.Trim(); int tabIndex = 0; // TextBox.TabIndex用カウンタ categoryBindableName = categoryBindableName.Replace(" ", spaceReplace); // グループ情報の格納 #region グループ情報の格納 List<GroupPattern> groupPatterns; foreach(var jsonCategoryObject in json.Categories) { // グループのグループ化するパラメータとグループ後の名前のコレクション groupPatterns = new List<GroupPattern>(); //groupPatterns.Add foreach(var jsonGroupObject in jsonCategoryObject.Group) { GroupPattern pattern = new GroupPattern() { Name = jsonGroupObject.Name, DispName = jsonGroupObject.DispName, IsExpanded = (bool)(jsonGroupObject.IsExpanded), }; foreach(var jsonArgs in jsonGroupObject.Args) { pattern.Args.Add(jsonArgs); } groupPatterns.Add(pattern); } GroupPatterns[jsonCategoryObject.Name] = groupPatterns; } groupPatterns = GroupPatterns[categoryName]; //if (json.IsDefined(categoryDispName) && json[categoryDispName].IsObject) //{ // var jsonCategory = json[categoryDispName]; // // カテゴリ(Object)内にグループがあり、Arrayの場合 // if (jsonCategory.IsDefined(s_groupLabel) && jsonCategory[s_groupLabel].IsArray) // { // var jsonGroups = jsonCategory[s_groupLabel]; // // グループの配列分組み合わせと名前を収拾 // foreach (var groupObject in jsonGroups) // { // if (groupObject.IsDefined(s_argumentsLabel) && groupObject.IsDefined(s_namesLabel)) // { // GroupPattern groupPattern = new GroupPattern(); // foreach (string arg in groupObject[s_argumentsLabel]) // { // groupPattern.Args.Add(arg); // } // foreach (string arg in groupObject[s_namesLabel]) // { // groupPattern.Name.Add(arg); // } // foreach(bool arg in groupObject[s_isExpandedLabel]) // { // groupPattern.IsExpanded.Add(arg); // } // groupPatterns.Add(groupPattern); // } // } // } //} #endregion var parameterCollection = new CategoryViewModel() { DispName = categoryDispName, Name = categoryName, BindableName = categoryBindableName }; var parameters = parameterCollection.Parameters; paramset.Edited.Id.Sort(); // 昇順ソート foreach (var id in paramset.Edited.Id) { var commentValue = paramset.Comment.Value.FirstOrDefault(v => v.Id == id); if (null == commentValue) { System.Windows.MessageBox.Show(string.Format("id:{0} not found", id), ""); continue; } var comment = commentValue.Text; var parameter = parameterCollection.CreateId(id, "name" + (++n).ToString(), comment); var slots = parameter.Slots; // 全追加用グループコレクション var groupCollection = new ObservableCollection<IEditableValue>(); // グループ配列用リスト var groupDic = new Dictionary<GroupPattern, List<ObservableCollection<IEditableValue>>>(); //groupDic[id] = new List<ObservableCollection<IEditableValue>>(); List<ObservableCollection<IEditableValue>> groupList = null; // 追加済みグループカウンタ var addedGroupCountDictionary = new Dictionary<GroupPattern, int>(); foreach (var gparamSlot in paramset.Slot) { // パラメータ名 string parameterDispName = gparamSlot.DispName; // バインディング用 string parameterBindableName = parameterDispName.Trim(); parameterBindableName = parameterBindableName.Replace(" ", spaceReplace); // 固有名 string uniqueName = gparamSlot.Name; // IDが一致している値部 string gparamTextById = gparamSlot.Value.FirstOrDefault(v => v.Id == id).Text; // パラメータコレクションに追加させる値 IEditableValue slotValue = null; switch (gparamSlot.Type) { case (int)GX_META_INFO_TYPE.INT8: case (int)GX_META_INFO_TYPE.INT16: case (int)GX_META_INFO_TYPE.INT32: case (int)GX_META_INFO_TYPE.INT64: case (int)GX_META_INFO_TYPE.UINT8: case (int)GX_META_INFO_TYPE.UINT16: case (int)GX_META_INFO_TYPE.UINT32: case (int)GX_META_INFO_TYPE.UINT64: case (int)GX_META_INFO_TYPE.FLOAT32: case (int)GX_META_INFO_TYPE.FLOAT64: { var unitValue = new TUnitValue<double>(); unitValue.Value = double.Parse(gparamTextById); slotValue = unitValue; } break; case (int)GX_META_INFO_TYPE.STRINGW: { var unitValue = new TUnitValue<string>(); unitValue.Value = gparamTextById; slotValue = unitValue; } break; case (int)GX_META_INFO_TYPE.VECTOR2AL: case (int)GX_META_INFO_TYPE.VECTOR3AL: case (int)GX_META_INFO_TYPE.VECTOR4AL: { var values = gparamTextById.Split(','); var unitValue = new TUnitValue<TArrayValue<float>>(); var arrayValue = new TArrayValue<float>(Array.ConvertAll<string, float>(values, (s => float.Parse(s)))); unitValue.Value = arrayValue; slotValue = unitValue; } break; case (int)GX_META_INFO_TYPE.COLOR32: { var values = gparamTextById.Split(','); var unitValue = new TUnitValue<TArrayValue<byte>>(); var arrayValue = new TArrayValue<byte>(Array.ConvertAll<string, byte>(values, (s => byte.Parse(s)))); unitValue.Value = arrayValue; slotValue = unitValue; } break; case (int)GX_META_INFO_TYPE.BOOL: { var unitValue = new TUnitValue<bool>(); unitValue.Value = bool.Parse(gparamTextById); slotValue = unitValue; } break; } slotValue.Type = gparamSlot.Type; slotValue.DispName = parameterDispName; slotValue.Name = uniqueName; slotValue.BindableName = parameterBindableName; // patternのargsを蓄積したものを一気に追加させる bool isAdded = false; bool isGroupChanged = false; do { isAdded = false; isGroupChanged = false; foreach (var pattern in groupPatterns) { if(!groupDic.ContainsKey(pattern)) { groupDic[pattern] = new List<ObservableCollection<IEditableValue>>(); } groupList = groupDic[pattern]; // 配列が空の場合、全ての要素を追加する if (pattern.Args.Count == 0) { groupCollection.Add(slotValue); // 全ての要素を集めきったら if (groupCollection.Count == paramset.Slot.Count) { // 名前をグループ名に変更し、値にグループコレクションを入れてスロットに追加させる EditableValueGroup value = new EditableValueGroup(); value.Name = pattern.Name; value.DispName = pattern.DispName; value.BindableName = pattern.DispName.Trim().Replace(" ", spaceReplace); value.Value = groupCollection; value.TabIndex = tabIndex++; value.IsExpanded = pattern.IsExpanded; slots.Add(value); } isAdded = true; } // patternのArgsに同じparamNameが存在する場合 else if (pattern.Args.Contains(slotValue.Name)) { // group用のコレクションに追加する // その際、既にコレクション内にparamNameの値が追加されていたら // 別のコレクションを探す // 追加先コレクション ObservableCollection<IEditableValue> targetCollection = null; // グループコレクションの確認 foreach (var group in groupList) { // コレクション内に追加されたパラメータを1つずつ見る foreach (var groupCollectionValue in group) { // 既にあるコレクションの中で自身と同じ名前のパラメータが登録されていたら if (slotValue.Name == groupCollectionValue.Name) { // このグループには追加させないようにしてパラメータ検索終了 targetCollection = null; break; } // 同じ名前のパラメータでない場合 else if (null == targetCollection) { // このコレクションにパラメータを追加させるように登録 targetCollection = group; } } // 追加先が決まった場合はループ終了 if (null != targetCollection) { break; } } // 全てのコレクションに存在していたら新しいコレクションを作成し、追加を行う if (null == targetCollection) { targetCollection = new ObservableCollection<IEditableValue>(); groupList.Add(targetCollection); } targetCollection.Add(slotValue); isAdded = true; // グループのパラメータ数分集まったら if (targetCollection.Count == pattern.Args.Count) { // 名前をグループ名に変更し、値にグループコレクションを入れてスロットに追加させる EditableValueGroup value = new EditableValueGroup(); value.Name = pattern.Name; value.DispName = pattern.DispName; value.BindableName = value.DispName.Trim().Replace(" ", spaceReplace); value.Value = targetCollection; value.TabIndex = tabIndex++; value.IsExpanded = pattern.IsExpanded; //slots.Add(value); //targetCollection.Add(slotValue); slotValue = value; // スロットの値をグループにしてからもう一度グループパターンのチェックをする isGroupChanged = true; break; } } } if(!isGroupChanged) { //isAdded = false; } } while (isGroupChanged /*&& !isAdded*/); if (!isAdded) { slotValue.TabIndex = tabIndex++; slots.Add(slotValue); } } parameters.Add(parameter); } // 自身のコレクションに追加 tempCollection.Add(parameterCollection); } } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, ""); System.Diagnostics.Debug.Assert(false, e.StackTrace); destCollection = new ObservableCollection<CategoryViewModel>(); return false; } destCollection = tempCollection; return true; }
public override void FromPattern(GroupPattern gp) { throw new InvalidOperationException("Root nodes can only be initialized from a string pattern"); }
public abstract void FromPattern(GroupPattern gp);
public void Quantifier_N_Infinity() { string patternText = "a(bc){3,}"; BasePattern innerGroup = new GroupPattern( true, new CharEscapePattern('b'), new CharEscapePattern('c')); BasePattern expected = new GroupPattern( true, new CharEscapePattern('a'), new GroupPattern( false, new QuantifierPattern(innerGroup, 3, 3, true), new QuantifierPattern(innerGroup, 0, null, true))); RegexAssert.IsASTTransformCorrect(expected, patternText, transform); }