public virtual void ImportWhitespaceOrComment(Token token) { if (importReplaceTabs != null) token.Value = token.Value.Replace("\t", importReplaceTabs); if (importReplaceSpaces != null) token.Value = token.Value.Replace(importReplaceSpaces, "\t"); }
public virtual void ImportTokenValue(Token t) { }
public virtual void ImportTrailingWhitespace(Token token) { var t2 = token.Next as Token; while (t2 != null && t2.IsWhitespaceOrComment) t2 = t2.Next as Token; if (t2 == null) { token = token.Next as Token; if (token != null && token.IsWhitespaceOrComment) { ImportWhitespaceOrComment(token); ImportTrailingWhitespace(token); } } }
public virtual void WriteTrailingWhitespace(Token token) { var nextNonWhitespace = token.Next as Token; while (nextNonWhitespace != null && nextNonWhitespace.IsWhitespaceOrComment) nextNonWhitespace = nextNonWhitespace.Next as Token; if (nextNonWhitespace == null) { // only write trailing whitespace if there is no more next token that will write leading whitespace. var next = token.Next as Token; if (next != null && next.IsWhitespaceOrComment) { WriteWhitespaceOrComment(next); WriteTrailingWhitespace(next); } } }
public Reader() { EndOfDocument = new Token(TokenClass.EndOfDocument); WhitespaceTokens = new Queue<Token>(); Text = string.Empty; }
public virtual void WriteLeadingWhitespace(Token token) { var prev = token.Previous as Token; if (prev != null && prev.IsWhitespaceOrComment) { WriteLeadingWhitespace(prev); WriteWhitespaceOrComment(prev); } }
public virtual void WriteToken(Token token) { WriteLeadingWhitespace(token); if (token.IsWhitespaceOrComment) WriteWhitespaceOrComment(token); else { if (token.Class == TokenClass.String) { if (token.StringClass == StringClass.DoubleQuote) { TextWriter.Write('"'); TextWriter.Write(token.Value); TextWriter.Write('"'); } else { TextWriter.Write("'"); TextWriter.Write(token.Value); TextWriter.Write("'"); } } else if (token.Class != TokenClass.EndOfDocument) TextWriter.Write(token.Value); } WriteTrailingWhitespace(token); }
public virtual void ReadToReserved(Token t) { ReadTo(t, Reserved); }
public virtual void Skip(Token t) { if (t.Length < 0) t.Length = 0; while (Position < Text.Length && Position < t.End) { if (Text[Position] == '\n') { Line++; LastNewline = Position+1; } Position++; } }
public virtual void ReadTo(Token t, params char[] characters) { Skip(t); int w; if (characters.Length > 1) { w = Text.IndexOfAny(characters, Position); } else { w = Text.IndexOf(characters[0], Position); } if (w > 0) { t.End = w; Skip(t); } else { //Start = End = Html.Length; t.End = Text.Length; Skip(t); } }
public virtual void ReadTo(Token t, string s) { while(true) { ReadTo(t, s[0]); if (Position == Text.Length || Peek(s, null)) return; t.Length++; } }
public virtual void ReadOver(Token t, string s) { ReadTo(t, s); if (Position < Text.Length) t.Length += s.Length; else t.End = Text.Length; Skip(t); }
public void Read(Token t) { Contract.Ensures(t.Start <= t.End); Skip(t); if (t.Class == TokenClass.EndOfDocument) return; if (t.Class == TokenClass.String) t.Value = Text.Substring(t.Start + 1, t.Length - 2); else t.Value = Text.Substring(t.Start, t.Length); if (t.Class != TokenClass.Whitespace) { t.Identation = CurrentIdentation; CurrentIdentation = string.Empty; } }
public virtual bool Peek(string s, Token t) { return Peek(0, s); }
public void CopyFrom(Token t) { Start = t.Start; End = t.End; Line = t.Line; Column = t.Column; Identation = t.Identation; tokenClass = t.tokenClass; stringClass = t.stringClass; serverTagClass = t.serverTagClass; Value = t.Value; }
public virtual void SkipWhitespace(ref Token t) { var start = Position; while (Position < Text.Length && Whitespace.Contains(Text[Position])) { if (Text[Position] == '\n') { Line++; LastNewline = Position+1; } Position++; } if (start != Position) { t.Class = TokenClass.Whitespace; t.End = Position; WhitespaceTokens.Enqueue(t); Read(t); if (LastNewline >= t.Start) CurrentIdentation = Text.Substring(LastNewline, t.End - LastNewline); t = NewToken(); CurrentToken = t; } }
public void ExportServerControlToken(Token t) { t.Value = t.Value.Replace("<", "[<").Replace(">", ">]"); }
public virtual void ImportLeadingWhitespace(Token token) { token = token.Previous as Token; if (token != null && token.IsWhitespaceOrComment) { ImportLeadingWhitespace(token); ImportWhitespaceOrComment(token); } }
public virtual void WriteNewLineAfter(Token token) { if (!token.IsWhitespaceOrComment) WriteToken(token); }
public virtual void ImportNewLineAfter(Token token) { if (!token.IsWhitespaceOrComment) ImportToken(token); }
public virtual void WriteTokenValue(Token t) { switch (t.Class) { case TokenClass.EndTagStart: t.Value = "</"; break; case TokenClass.Equals: t.Value = "="; break; case TokenClass.ServerTagEnd: if (t.ServerTagClass == ServerTagClass.Comment) t.Value = "--%>"; else if (t.ServerTagClass == ServerTagClass.PersistentObjectName) t.Value = "]"; else t.Value = "%>"; break; case TokenClass.ServerTagStart: switch (t.ServerTagClass) { case ServerTagClass.AspNetExpression: t.Value = "<%$"; break; case ServerTagClass.HtmlEncodedExpression: t.Value = "<%:"; break; case ServerTagClass.Binding: t.Value = "<%#"; break; case ServerTagClass.Expression: t.Value = "<%="; break; case ServerTagClass.Declaration: t.Value = "<%@"; break; case ServerTagClass.Comment: t.Value = "<%--"; break; case ServerTagClass.Code: t.Value = "<%"; break; case ServerTagClass.PersistentObject: t.Value = "<%--[Silversite.PersistentObject:"; break; } break; case TokenClass.SingleTagEnd: t.Value = "/>"; break; case TokenClass.TagEnd: t.Value = ">"; break; case TokenClass.TagStart: t.Value = "<"; break; case TokenClass.DoctypeStart: t.Value = "<!DOCTYPE"; break; case TokenClass.HtmlCommentStart: t.Value = "<!--"; break; case TokenClass.HtmlCommentEnd: t.Value = "-->"; break; case TokenClass.CDataEnd: t.Value = "]]>"; break; case TokenClass.CDataStart: t.Value = "<![CDATA["; break; case TokenClass.Literal: case TokenClass.Script: case TokenClass.Identifier: case TokenClass.String: t.Value = string.Empty; break; case TokenClass.Whitespace: t.Value = " "; break; case TokenClass.XmlDocTagStart: t.Value = "<?"; break; case TokenClass.XmlDocTagEnd: t.Value = "?>"; break; default: break; } }
public virtual void ImportToken(Token token) { if (token.IsWhitespaceOrComment) ImportWhitespaceOrComment(token); else { ImportLeadingWhitespace(token); ImportTokenValue(token); ImportTrailingWhitespace(token); } }
public virtual void WriteWhitespaceOrComment(Token token) { switch (token.Class) { case TokenClass.ServerTagStart: case TokenClass.ServerTagEnd: case TokenClass.Literal: if (token.ServerTagClass == ServerTagClass.Comment) TextWriter.Write(token.Value); break; case TokenClass.Whitespace: string value = token.Value; if (replaceTabs != null) value = value.Replace("\t", replaceTabs); if (replaceSpaces != null) value = value.Replace(replaceSpaces, "\t"); TextWriter.Write(value); break; case TokenClass.HtmlComment: case TokenClass.HtmlCommentEnd: case TokenClass.HtmlCommentStart: TextWriter.Write(token.Value); break; default: break; } }
public Token NewToken() { var t = new Token(); t.Start = Position; t.Length = 0; t.Line = Line; t.Column = t.Start - LastNewline + 1; t.Parser = Parser; return t; }