/// <summary> /// Constructs a version using the given string as the /// version. This breaks up the version into version parts (broken /// down by periods (".") and slashes ("-"). A version part /// consists of a number, followed optionally by a string. If the /// version cannot be parsed, it throws an MfGamesException. /// </summary> public ExtendedVersion(string version) { // Check for null and blank if (version == null || version.Trim() == "") { throw new Exception("Cannot parse a null or blank version"); } // Save the string version and remove the spaces this.version = version = version.Trim(); // Check for spaces if (RegexSpace.IsMatch(version)) { throw new Exception("Versions cannot have whitespace"); } // Split the version into parts. We also allocate the space for // everything before parsing. string[] parts = version.Split('.', '-'); numerics = new int[parts.Length]; strings = new string[parts.Length]; for (int i = 0; i < parts.Length; i++) { // Check for match and sanity checking if (!RegexPart.IsMatch(parts[i])) { throw new Exception( "Cannot parse part '" + parts[i] + "' of '" + version + "'"); } // Pull out the parts Match match = RegexPart.Match(parts[i]); string strNumber = match.Groups[1].Value; strings[i] = match.Groups[2].Value; try { // Try to parse the integer numerics[i] = Int32.Parse(strNumber); } catch { throw new Exception("Cannot numerically parse '" + parts[i] + "'"); } } }
/// <summary> /// Alternates this part with the given value. Equivalent to just /// using the "|" operator on two values: QuotedPart | NonWhitespacePart; /// </summary> public RegexPart Alternate(RegexPart alternate) { return new RegexPart(this.pattern + "|" + alternate); }
private void ResetBackFormat(RegexPart p) { try { this.SelectionStart = p.Index + p.ToString().Length + 1; this.SelectionLength = this.Text.Length; this.SelectionBackColor = Color.White; } catch (Exception) { } }
private void Highlight() { this.groupCount = 0; int start = this.SelectionStart; try { _parts = parser.Parse(this.Text); Stack<RegexPart> parts = new Stack<RegexPart>(); Stack<RegexPart> lastChildren = new Stack<RegexPart>(); parts.Push(_parts); HasRegexError = false; while (parts.Count > 0) { RegexPart p = parts.Pop(); //Debug.WriteLine(p.Type.ToString() + " " + p.ToString()); if (p.Error != ErrorType.None || p.Type == PartType.Invalid || p.Type == PartType.InvalidQuantifier) { HasRegexError = true; } if (lastChildren.Count > 0 && lastChildren.Peek() == p) { lastChildren.Pop(); if (!p.Parent.Parent.IsGroup()) { ResetBackFormat(p); } } if (p.IsGroup() && p.Parts.Count > 0) { lastChildren.Push(p.Parts[p.Parts.Count - 1]); } for (int i = p.Parts.Count - 1; i >= 0; i--) { parts.Push(p.Parts[i]); } if (p.ValueStart != null) { FormatPart(p, groupColours); } } } catch (Exception) { throw; } this.SelectionStart = start; this.SelectionLength = 0; }
private void FormatPart(RegexPart part, List<Color> groupColors) { this.SelectionStart = part.Index; this.SelectionLength = part.ToString().Length; if (part.Error != ErrorType.None) { SyntaxHighlighting.Error.Apply(this); } else { switch (part.Type) { case PartType.Undefined: SyntaxHighlighting.Undefined.Apply(this); break; case PartType.Root: SyntaxHighlighting.Root.Apply(this); break; case PartType.Text: SyntaxHighlighting.Text.Apply(this); break; case PartType.DefinedEscaped: SyntaxHighlighting.DefinedEscaped.Apply(this); break; case PartType.OctalCharacter: SyntaxHighlighting.OctalCharacter.Apply(this); break; case PartType.HexCharacter: SyntaxHighlighting.HexCharacter.Apply(this); break; case PartType.ControlCharacter: SyntaxHighlighting.ControlCharacter.Apply(this); break; case PartType.UnicodeCharacter: SyntaxHighlighting.UnicodeCharacter.Apply(this); break; case PartType.EscapedCharacter: SyntaxHighlighting.EscapedCharacter.Apply(this); break; case PartType.Backreference: SyntaxHighlighting.Backreference.Apply(this); break; case PartType.CharacterGroup: SyntaxHighlighting.CharacterGroup.Apply(this); break; case PartType.NegatedCharacterGroup: SyntaxHighlighting.NegatedCharacterGroup.Apply(this); break; case PartType.NegateCharacterGroup: SyntaxHighlighting.NegateCharacterGroup.Apply(this); break; case PartType.ExactQuantifier: SyntaxHighlighting.ExactQuantifier.Apply(this); break; case PartType.AtLeastQuantifier: SyntaxHighlighting.AtLeastQuantifier.Apply(this); break; case PartType.RangeQuantifier: SyntaxHighlighting.RangeQuantifier.Apply(this); break; case PartType.AtLeastMinimumQuantifier: SyntaxHighlighting.AtLeastMinimumQuantifier.Apply(this); break; case PartType.Operator: SyntaxHighlighting.Operator.Apply(this); break; case PartType.MinimumRangeQuantifier: SyntaxHighlighting.MinimumRangeQuantifier.Apply(this); break; case PartType.BackspaceEscaped: SyntaxHighlighting.BackspaceEscaped.Apply(this); break; case PartType.NamedBackreference: SyntaxHighlighting.NamedBackreference.Apply(this); break; case PartType.Group: SyntaxHighlighting.Group.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.NegativeLookahead: SyntaxHighlighting.NegativeLookahead.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.PositiveLookbehind: SyntaxHighlighting.PositiveLookbehind.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.NegativeLookbehind: SyntaxHighlighting.NegativeLookbehind.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.Greedy: SyntaxHighlighting.Greedy.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.NamedCapture: SyntaxHighlighting.NamedCapture.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.PositiveLookahead: SyntaxHighlighting.PositiveLookahead.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.NonCapturing: SyntaxHighlighting.NonCapturing.Apply(this, GetColor(groupCount++, groupColors)); break; case PartType.Comment: SyntaxHighlighting.Comment.Apply(this); break; case PartType.Invalid: SyntaxHighlighting.Invalid.Apply(this); break; case PartType.InvalidQuantifier: SyntaxHighlighting.InvalidQuantifier.Apply(this); break; default: throw new NotImplementedException(); } } }
/// <summary> /// Alternates this part with the given value. Equivalent to just /// using the "|" operator on two values: QuotedPart | NonWhitespacePart; /// </summary> public RegexPart Alternate(RegexPart alternate) { return(new RegexPart(this.pattern + "|" + alternate)); }