internal override IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state) { var nest = 0; var last = state.Tokens.Count - 1; for (var i = state.Index; i <= last; i++) { var t = state.Tokens[i]; if (t.IsOperatorOf("(")) { nest++; } if (nest == 0) { if (t.IsOperatorOf(")") || t.IsOperatorOf(",") || t.IsOperatorOf(":") || t.IsKeywordOf("Then") || t.IsKeywordOf("To")) { last = i - 1; break; } } else { if (t.IsOperatorOf(")")) { nest--; } } } if (nest == 0 && state.Index <= last) { if (CaptureName != null) { if (!TryCreateExpression(state.Tokens, state.Index, last - state.Index + 1, out var e)) { return(Enumerable.Empty <TokenMatcherState>()); } state.Captures[CaptureName] = e; } state.Index = last + 1; return(new[] { state }); } return(Enumerable.Empty <TokenMatcherState>()); }
internal override IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state) { var i = state.Index; foreach (var s in _InternalItem.EnumerateMatches(state)) { yield return(s); } state.Index = i; if (_InternalItem.CaptureName != null) { state.Captures.Remove(_InternalItem.CaptureName); } yield return(state); }
internal override IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state) { var t = state.Tokens.ElementAtOrDefault(state.Index); if (t != null && (_TypeMask == TokenType.Default || (_TypeMask & t.Type) != TokenType.Default) && _TextPattern?.IsMatch(t.Text) != false) { state.Index++; if (CaptureName != null) { state.Captures[CaptureName] = _CaptureConversion == null ? t.Text : _CaptureConversion.DynamicInvoke(t); } yield return(state); } }
internal override IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state) { var s = new TokenMatcherState(state.Tokens); s.Index = state.Index; var ens = new Stack <IEnumerator <TokenMatcherState> >(); var indexes = new Stack <int>(); indexes.Push(state.Index); ens.Push(_Items[0].EnumerateMatches(s).GetEnumerator()); while (ens.Any()) { var p = ens.Peek(); if (p.MoveNext()) { if (ens.Count == _Items.Count) { state.Index = p.Current.Index; foreach (var kv in p.Current.Captures) { state.Captures[kv.Key] = kv.Value; } if (CaptureName != null) { state.Captures[CaptureName] = _CaptureConversion == null ? p.Current : _CaptureConversion.DynamicInvoke(p.Current); } yield return(state); } else { indexes.Push(p.Current.Index); ens.Push(_Items[ens.Count].EnumerateMatches(p.Current).GetEnumerator()); } } else { s.Index = indexes.Pop(); ens.Pop(); } } }
internal IEnumerable <TokenMatcherState> EnumerateMatches(IReadOnlyList <Token> tokens, int index) { var s = new TokenMatcherState(tokens) { Index = index }; var ens = new Stack <IEnumerator <TokenMatcherState> >(); var indexes = new Stack <int>(); indexes.Push(s.Index); ens.Push(_Items[0].EnumerateMatches(s).GetEnumerator()); while (ens.Any()) { var p = ens.Peek(); if (p.MoveNext()) { if (ens.Count == _Items.Count) { s.Index = p.Current.Index; yield return(s); } else { indexes.Push(p.Current.Index); ens.Push(_Items[ens.Count].EnumerateMatches(p.Current).GetEnumerator()); } } else { s.Index = indexes.Pop(); ens.Pop(); } } }
internal abstract IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state);
internal override IEnumerable <TokenMatcherState> EnumerateMatches(TokenMatcherState state) { var ens = new Stack <IEnumerator <TokenMatcherState> >(); var states = new Stack <TokenMatcherState>(); var captures = new List <object>(); states.Push(state); ens.Push(_InternalItem.EnumerateMatches(new TokenMatcherState(state.Tokens) { Index = state.Index }).GetEnumerator()); while (ens.Any()) { var p = ens.Peek(); if (p.MoveNext()) { var cs = p.Current; var cap = _InternalItem.CaptureName != null && cs.Captures.TryGetValue(_InternalItem.CaptureName, out var obj) ? obj : null; captures.Add(cap); if (captures.Count >= _Maximum) { state.Index = cs.Index; if (CaptureName != null) { state.Captures[CaptureName] = captures; } yield return(state); captures.RemoveAt(captures.Count - 1); } else { states.Push(cs); ens.Push(_InternalItem.EnumerateMatches(new TokenMatcherState(state.Tokens) { Index = cs.Index }).GetEnumerator()); } } else { var cs = states.Pop(); ens.Pop(); if (_Minimum <= captures.Count && captures.Count <= _Maximum) { state.Index = cs.Index; if (CaptureName != null) { state.Captures[CaptureName] = captures; } yield return(state); if (captures.Any()) { captures.RemoveAt(captures.Count - 1); } } } } }