private void AddNewDelimiter(Substring lexeme) { var canBeClosing = specification.CanBeClosing(lexeme, markdownInput); var canBeStarting = specification.CanBeStarting(lexeme, markdownInput); var delimiter = new Delimiter(lexeme.Value, lexeme.Index, canBeClosing, canBeStarting); if (delimiter.CanBeClosing) { var closed = TryCloseToken(delimiter, out var closedToken); if (closed) { ResolveToken(closedToken); CurrentToken = closedToken.ParentToken; return; } } if (delimiter.CanBeStarting) { var newToken = new Token(delimiter); CurrentToken.AddToken(newToken); CurrentToken = newToken; } else { CurrentToken.AddText(delimiter.Value); } }
private bool DelimitersArePair(Delimiter startingDelimiter, Delimiter closingDelimiter) { if (specification.MdStartingToClosingDelimiters.ContainsKey(startingDelimiter.Value)) { return(specification.MdStartingToClosingDelimiters[startingDelimiter.Value] == closingDelimiter.Value); } return(false); }
public Delimiter Escape(Delimiter delimiter, string text) { if (delimiter.Position == 0) { return(delimiter); } return(text[delimiter.Position - 1] == '\\' ? Length != 1 ? new Delimiter(fullTag.Substring(1), delimiter.Position + 1) : null : delimiter); }
public Token GetToken(Delimiter delimiter, string text) { if (delimiter.IsClosing) { return(null); } var second = delimiter.Partner; var length = second.Position - delimiter.Position + Length; return(new PairedTagToken(delimiter.Position, length, text.Substring(delimiter.Position, length), fullTag)); }
internal IEnumerable <Token> GetTokensFromDelimiters() { if (!Delimiters.Any()) { return new List <Token> { new StringToken(0, Text.Length, Text) } } ; var tokens = new LinkedList <Token>(); Token currentParentToken = null; Delimiter endOfParent = null; foreach (var delimiter in Delimiters) { var rule = GetSuitableRule(delimiter); var token = rule.GetToken(delimiter, Text); if (token != null) { if (currentParentToken == null) { currentParentToken = token; endOfParent = delimiter.Partner; tokens.AddLast(token); } if (delimiter.Partner.Position >= endOfParent.Position) { continue; } token.ParentToken = currentParentToken; if (currentParentToken.InnerTokens == null) { currentParentToken.InnerTokens = new List <Token>(); } currentParentToken.InnerTokens.Add(token); } else if (currentParentToken != null) { if (delimiter.Position < endOfParent.Position) { continue; } currentParentToken = null; endOfParent = null; } } return(InsertStringTokens(tokens)); }
public bool IsValidClosing(Delimiter delimiter, string text) { var position = delimiter.Position; if (position == 0) { return(false); } if (position + Length == text.Length) { return(!char.IsWhiteSpace(text[position - 1])); } return(!char.IsWhiteSpace(text[position - 1]) && char.IsWhiteSpace(text[position + Length])); }
public bool IsValid(Delimiter delimiter, string text) { var position = delimiter.Position; var isLast = position == text.Length - Length; var isFirst = position == 0; if (isLast || isFirst) { return(true); } var next = text[position + Length]; var previous = text[position - 1]; return(!(next.IsLetterOrDigitOrSpecifiedChar(Tag) && previous.IsLetterOrDigitOrSpecifiedChar(Tag))); }
private bool TryCloseToken(Delimiter closingDelimiter, out Token closedToken) { closedToken = null; var token = CurrentToken; if (token.Delimiter is null) { return(false); } while (token != null && !DelimitersArePair(token.Delimiter, closingDelimiter)) { if (token.ParentToken?.Delimiter is null) { return(false); } token = token.ParentToken; } token.Closed = true; token.TokenType = specification.MdToTokenTypes[token.Delimiter.Value]; closedToken = token; return(true); }
internal ITextProcessorRule GetSuitableRule(Delimiter delimiter) { return(rules.FirstOrDefault(r => r.Check(delimiter))); }
public Token(Delimiter delimiter) { Delimiter = delimiter; RootToken = this; }
public bool Check(Delimiter delimiter) => delimiter.Value == fullTag;