private int GetIndexOfLastOpenedAndNotClosed(Token[] tokens, ITokenType tokenType) { for (var i = tokens.Length - 1; i >= 0; i--) { if (tokens[i].Type.GetType() == tokenType.GetType()) { return(i); } } return(-1); }
public Token[] GetTokens() { var tokens = new List <Token>(); while (reader.Position < MdText.Length - 1) { currentUnderscoreType = null; var previousType = typesNesting.Any() ? typesNesting.Peek() : null; var openTypes = previousType == null ? allTokenTypes : previousType.SupportedInnerTypes(); var token = reader.ReadUntil(ch => CheckIfOpen(ch, openTypes) || CheckIfClosing(ch, typesNesting)); token.Type = previousType; if (!previousUnderscoreIsClosing) { token.Opened = true; } if (currentUnderscoreIsClosing) { token.Closed = true; } if (!currentUnderscoreIsClosing) { if (currentUnderscoreType != null) { typesNesting.Push(currentUnderscoreType); } tokens.Add(token); } else { if (currentUnderscoreType.GetType() != typesNesting.Peek().GetType()) { tokens[tokens.Count - 1].Concat(token); while (typesNesting.Peek().GetType() != currentUnderscoreType.GetType()) { typesNesting.Pop(); } } else { tokens.Add(token); } typesNesting.Pop(); } previousUnderscoreIsClosing = currentUnderscoreIsClosing; currentUnderscoreIsClosing = false; if (currentUnderscoreType != null) { reader.Skip(currentUnderscoreType.GetMarker().Length); } } if (!typesNesting.Any() || typesNesting.Peek() == null) { return(tokens.ToArray()); } while (typesNesting.Count != 0) { var indexOfLastOpenedAndNotClosed = GetIndexOfLastOpenedAndNotClosed(tokens.Where(token => token != null).ToArray(), typesNesting.Pop()); tokens[indexOfLastOpenedAndNotClosed - 1].Concat(tokens[indexOfLastOpenedAndNotClosed]); tokens[indexOfLastOpenedAndNotClosed] = null; } return(tokens.ToArray()); }