/// <summary> /// Tokenizes the simple. /// </summary> /// <param name="text">The text.</param> /// <param name="kind">The kind.</param> /// <param name="nextMode">The next mode.</param> /// <returns></returns> private XmlToken TokenizeSimple(string text, XmlTokenKind kind, XmlTokenizerMode nextMode) { XmlToken token = new XmlToken(kind, text.Length); position += text.Length; mode = nextMode; return(token); }
/// <summary> /// Tokenizes the entity. /// </summary> /// <returns></returns> private XmlToken TokenizeEntity() { Debug.Assert(mode == XmlTokenizerMode.OutsideElement); Debug.Assert(input[position] == '&'); XmlToken token = new XmlToken(XmlTokenKind.Entity, input.IndexOf(';', position) - position); position += token.Length; return(token); }
private XmlToken TokenizeAttributeValue() { Debug.Assert(mode == XmlTokenizerMode.AfterAttributeEquals); int closePosition = input.IndexOf(input[position], position + 1); XmlToken token = new XmlToken(XmlTokenKind.AttributeValue, closePosition + 1 - position); position = closePosition + 1; mode = XmlTokenizerMode.InsideElement; return(token); }
/// <summary> /// Nexts the token. /// </summary> /// <returns></returns> private XmlToken NextToken() { if (position >= input.Length) { return(new XmlToken(XmlTokenKind.EOF, 0)); } XmlToken token; switch (mode) { case XmlTokenizerMode.AfterAttributeEquals: token = TokenizeAttributeValue(); break; case XmlTokenizerMode.AfterAttributeName: token = TokenizeSimple("=", XmlTokenKind.Equals, XmlTokenizerMode.AfterAttributeEquals); break; case XmlTokenizerMode.AfterOpen: token = TokenizeName(XmlTokenKind.ElementName, XmlTokenizerMode.InsideElement); break; case XmlTokenizerMode.InsideCData: token = TokenizeInsideCData(); break; case XmlTokenizerMode.InsideComment: token = TokenizeInsideComment(); break; case XmlTokenizerMode.InsideElement: token = TokenizeInsideElement(); break; case XmlTokenizerMode.InsideProcessingInstruction: token = TokenizeInsideProcessingInstruction(); break; case XmlTokenizerMode.OutsideElement: token = TokenizeOutsideElement(); break; default: token = new XmlToken(XmlTokenKind.EOF, 0); Debug.Fail("missing case"); break; } return(token); }
/// <summary> /// Tokenizes the element whitespace. /// </summary> /// <returns></returns> private XmlToken TokenizeElementWhitespace() { int i; for (i = position; i < input.Length; i++) { if (!char.IsWhiteSpace(input[i])) { break; } } XmlToken token = new XmlToken(XmlTokenKind.ElementWhitespace, i - position); position = i; return(token); }
/// <summary> /// Tokenizes the inside comment. /// </summary> /// <returns></returns> private XmlToken TokenizeInsideComment() { Debug.Assert(mode == XmlTokenizerMode.InsideComment); int tokenend = input.IndexOf("-->", position); if (position == tokenend) { position += "-->".Length; mode = XmlTokenizerMode.OutsideElement; return(new XmlToken(XmlTokenKind.CommentEnd, "-->".Length)); } else { XmlToken token = new XmlToken(XmlTokenKind.CommentText, tokenend - position); position = tokenend; return(token); } }
/// <summary> /// Tokenizes the inside C data. /// </summary> /// <returns></returns> private XmlToken TokenizeInsideCData() { Debug.Assert(mode == XmlTokenizerMode.InsideCData); int tokenend = input.IndexOf("]]>", position); if (position == tokenend) { position += "]]>".Length; mode = XmlTokenizerMode.OutsideElement; return(new XmlToken(XmlTokenKind.CDataEnd, "]]>".Length)); } else { XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position); position = tokenend; return(token); } }
/// <summary> /// Tokenizes the inside processing instruction. /// </summary> /// <returns></returns> private XmlToken TokenizeInsideProcessingInstruction() { Debug.Assert(mode == XmlTokenizerMode.InsideProcessingInstruction); int tokenend = input.IndexOf("?>", position); if (position == tokenend) { position += "?>".Length; mode = XmlTokenizerMode.OutsideElement; return(new XmlToken(XmlTokenKind.CloseProcessingInstruction, "?>".Length)); } else { XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position); position = tokenend; return(token); } }
/// <summary> /// Tokenizes the name. /// </summary> /// <param name="kind">The kind.</param> /// <param name="nextMode">The next mode.</param> /// <returns></returns> private XmlToken TokenizeName(XmlTokenKind kind, XmlTokenizerMode nextMode) { Debug.Assert(mode == XmlTokenizerMode.AfterOpen || mode == XmlTokenizerMode.InsideElement); int i; for (i = position; i < input.Length; i++) { if (!IsNameCharacter(input[i])) { break; } } XmlToken token = new XmlToken(kind, i - position); mode = nextMode; position = i; return(token); }
/// <summary> /// Colors for token. /// </summary> /// <param name="token">The token.</param> /// <param name="tokenText">The token text.</param> /// <returns></returns> public static Color ColorForToken(XmlToken token, string tokenText) { Color color = Color.FromRgb(0, 0, 0); switch (token.Kind) { case XmlTokenKind.Open: case XmlTokenKind.OpenClose: case XmlTokenKind.Close: case XmlTokenKind.SelfClose: case XmlTokenKind.CommentBegin: case XmlTokenKind.CommentEnd: case XmlTokenKind.CDataBegin: case XmlTokenKind.CDataEnd: case XmlTokenKind.Equals: case XmlTokenKind.OpenProcessingInstruction: case XmlTokenKind.CloseProcessingInstruction: case XmlTokenKind.AttributeValue: color = Color.FromRgb(0, 0, 255); break; case XmlTokenKind.ElementName: color = Color.FromRgb(163, 21, 21); break; case XmlTokenKind.TextContent: color = Color.FromRgb(0, 0, 0); break; case XmlTokenKind.AttributeName: case XmlTokenKind.Entity: color = Color.FromRgb(255, 0, 0); break; case XmlTokenKind.CommentText: color = Color.FromRgb(0, 128, 0); break; } if (token.Kind == XmlTokenKind.ElementWhitespace || (token.Kind == XmlTokenKind.TextContent && tokenText.Trim() == "")) { } return(color); }
/// <summary> /// Tokenizes the text. /// </summary> /// <returns></returns> private XmlToken TokenizeText() { Debug.Assert(input[position] != '<'); Debug.Assert(input[position] != '&'); Debug.Assert(mode == XmlTokenizerMode.OutsideElement); int i; for (i = position; i < input.Length; i++) { if (input[i] == '<' || input[i] == '&') { break; } } XmlToken token = new XmlToken(XmlTokenKind.TextContent, i - position); position = i; return(token); }