private bool Lookahead(string expected, bool takeIfMatch, bool caseSensitive) { Func <char, char> filter = c => c; if (!caseSensitive) { filter = Char.ToLowerInvariant; } if (expected.Length == 0 || filter(CurrentCharacter) != filter(expected[0])) { return(false); } // Capture the current buffer content in case we have to backtrack string oldBuffer = null; if (takeIfMatch) { Buffer.ToString(); } using (LookaheadToken lookahead = Source.BeginLookahead()) { for (int i = 0; i < expected.Length; i++) { if (filter(CurrentCharacter) != filter(expected[i])) { if (takeIfMatch) { // Clear the buffer and put the old buffer text back Buffer.Clear(); Buffer.Append(oldBuffer); } // Return without accepting lookahead (thus rejecting it) return(false); } if (takeIfMatch) { TakeCurrent(); } else { MoveNext(); } } if (takeIfMatch) { lookahead.Accept(); } } return(true); }
public void After_Accepting_Lookahead_Tokenizer_Returns_Next_Token() { var tokenizer = new HtmlTokenizer(new SeekableTextReader(new StringReader("<foo>"))); using (LookaheadToken lookahead = tokenizer.Source.BeginLookahead()) { Assert.Equal(new HtmlSymbol(0, 0, 0, "<", HtmlSymbolType.OpenAngle), tokenizer.NextSymbol()); Assert.Equal(new HtmlSymbol(1, 0, 1, "foo", HtmlSymbolType.Text), tokenizer.NextSymbol()); lookahead.Accept(); } Assert.Equal(new HtmlSymbol(4, 0, 4, ">", HtmlSymbolType.CloseAngle), tokenizer.NextSymbol()); }