private static void AddNodes(TreeNodeCollection treeNodes, RegexNode regexNode) { var treeNode = treeNodes.Add( regexNode.Id.ToString(CultureInfo.InvariantCulture), string.Format("{0} - {1}", regexNode.Pattern, regexNode.NodeType)); if (regexNode is LeafNode) { return; } var wrapperNode = regexNode as WrapperNode; if (wrapperNode != null) { AddNodes(treeNode.Nodes, (wrapperNode).Child); } var containerNode = regexNode as ContainerNode; if (containerNode != null) { foreach (var child in (containerNode).Children) { AddNodes(treeNode.Nodes, child); } } var alternation = regexNode as Alternation; if (alternation != null) { foreach (var choice in (alternation).Choices) { AddNodes(treeNode.Nodes, choice); } } }
internal static ParseStep Fail(RegexNode node, State initialState, State currentState, string additionalMessage = null) { return new ParseStep { Type = ParseStepType.Fail, Node = node, InitialState = initialState, CurrentState = currentState }.WithMessage(step => (additionalMessage != null ? additionalMessage + ": " : "") + node.GetFailMessage(step.InitialState)); }
internal static ParseStep EndOfString(RegexNode node, State currentState) { return new ParseStep { Type = ParseStepType.EndOfString, Node = node, CurrentState = currentState }.WithMessage(step => "End of string"); }
internal static ParseStep CaptureDiscarded(RegexNode node, string capturedText, int captureNumber) { return new ParseStep { Type = ParseStepType.CaptureDiscarded, Node = node, MatchedText = capturedText, CaptureNumber = captureNumber }.WithMessage(step => string.Format( "Discarded captured text, '{0}' (capture number: {1})", step.MatchedText, step.CaptureNumber)); }
internal static ParseStep Capture(RegexNode node, string capturedText, int captureNumber, State initialState, State currentState) { return new ParseStep { Type = ParseStepType.Capture, Node = node, MatchedText = capturedText, CaptureNumber = captureNumber, InitialState = initialState, CurrentState = currentState }.WithMessage(step => string.Format( "Captured '{0}' (capture number: {1}) starting at index {2}", step.MatchedText, step.CaptureNumber, step.InitialState.Index)); }
internal static ParseStep Break(RegexNode node) { return new ParseStep { Type = ParseStepType.Break, Node = node }; }
internal static ParseStep Backtrack(RegexNode node, State initialState, State backtrackState) { return new ParseStep { Type = ParseStepType.Backtrack, Node = node, InitialState = initialState, CurrentState = backtrackState }.WithMessage(step => string.Format("Backtracking to {0}", step.CurrentState.Index)); }
private static RegexNode CreateQuantifier(QuantifierParams quantifierParams, RegexNode child, int index, string pattern) { if (quantifierParams.Shorthand != null) { switch (quantifierParams.Shorthand) { case '?': if (quantifierParams.Optional != null) { if (quantifierParams.Optional == '?') { return new LazyQuestionMark(child, index, pattern); } return new PossessiveQuestionMark(child, index, pattern); } return new GreedyQuestionMark(child, index, pattern); case '*': if (quantifierParams.Optional != null) { if (quantifierParams.Optional == '?') { return new LazyStar(child, index, pattern); } return new PossessiveStar(child, index, pattern); } return new GreedyStar(child, index, pattern); case '+': if (quantifierParams.Optional != null) { if (quantifierParams.Optional == '?') { return new LazyPlus(child, index, pattern); } return new PossessivePlus(child, index, pattern); } return new GreedyPlus(child, index, pattern); } } Debug.Assert(quantifierParams.Min != null); if (quantifierParams.Optional != null) { if (quantifierParams.Optional == '?') { return new LazyQuantifier(quantifierParams.Min.Value, quantifierParams.Max, child, index, pattern); } return new PossessiveQuantifier(quantifierParams.Min.Value, quantifierParams.Max, child, index, pattern); } return new GreedyQuantifier(quantifierParams.Min.Value, quantifierParams.Max, child, index, pattern); }
public ParseStep ConvertToOuterContext(string input, int indexModifier, RegexNode regexNode, Func<RegexNode, bool> changeNodePredicate, Func<string, string> modifyMessageFunction) { if (changeNodePredicate(Node)) { Node = regexNode; var originalGetMessage = _getMessage; _getMessage = step => modifyMessageFunction(originalGetMessage(step)); } InitialState = InitialState == null ? null : new State(input, InitialState.Index + indexModifier); CurrentState = CurrentState == null ? null : new State(input, CurrentState.Index + indexModifier); return this; }
public static ParseStep StartLookaround(RegexNode node, State currentState) { return new ParseStep { Type = ParseStepType.LookaroundStart, Node = node, CurrentState = currentState }.WithMessage(step => "Look-around started"); }
public static ParseStep ResetIndex(RegexNode node, State initialState, State currentState) { return new ParseStep { Type = ParseStepType.ResetIndex, Node = node, InitialState = initialState, CurrentState = currentState }.WithMessage(step => string.Format("Resetting index to {0}", step.InitialState.Index)); }
public static ParseStep EndLookaround(RegexNode node) { return new ParseStep { Type = ParseStepType.LookaroundEnd, Node = node }.WithMessage(step => "Look-around ended"); }
public static ParseStep BeginParse(RegexNode node, State initialState) { return new ParseStep { Type = ParseStepType.BeginParse, Node = node, InitialState = initialState }.WithMessage(step => "Parse started"); }
internal static ParseStep Pass(RegexNode node, string matchedText, State initialState, State currentState) { return new ParseStep { Type = ParseStepType.Pass, Node = node, MatchedText = matchedText, InitialState = initialState, CurrentState = currentState }.WithMessage(step => node.GetPassMessage(step.MatchedText, step.InitialState)); }
internal static ParseStep AdvanceIndex(RegexNode node, State state) { return new ParseStep { Type = ParseStepType.AdvanceIndex, Node = node, CurrentState = state, }.WithMessage(step => string.Format("Advanced index to {0}", step.CurrentState.Index)); }
internal static ParseStep StateSaved(RegexNode node, State currentState, string message) { return new ParseStep { Type = ParseStepType.StateSaved, Node = node, CurrentState = currentState }.WithMessage(step => message); }
private static void AssignNodeIdsAndCapturingNumbers(RegexNode node, ref int nextId, ref int nextCapturingNumber) { node.Id = nextId++; var containerNode = node as ContainerNode; if (containerNode != null) { var capturingParens = node as CapturingParens; if (capturingParens != null) { capturingParens.Number = nextCapturingNumber++; } foreach (var child in containerNode.Children) { AssignNodeIdsAndCapturingNumbers(child, ref nextId, ref nextCapturingNumber); } } else { var wrapperNode = node as WrapperNode; if (wrapperNode != null) { AssignNodeIdsAndCapturingNumbers(wrapperNode.Child, ref nextId, ref nextCapturingNumber); } else { var alternation = node as Alternation; if (alternation != null) { foreach (var choice in alternation.Choices) { AssignNodeIdsAndCapturingNumbers(choice, ref nextId, ref nextCapturingNumber); } } } } }