/// <summary>Expresses an LES token as a string.</summary> /// <remarks>Note that some Tokens do not contain enough information to /// reconstruct a useful token string, e.g. comment tokens do not store the /// comment but merely contain the location of the comment in the source code. /// For performance reasons, a <see cref="Token"/> does not have a reference /// to its source file, so this method cannot return the original string. /// <para/> /// The results are undefined if the token was not produced by <see cref="LesLexer"/>. /// </remarks> public static string ToString(Token t) { StringBuilder sb = new StringBuilder(); switch (t.Type()) { case TT.Newline: return("\n"); case TT.SLComment: return("//\n"); case TT.MLComment: return("/**/"); case TT.Literal: return(LesNodePrinter.PrintLiteral(t.Value, t.Style)); case TT.BQString: return(LesNodePrinter.PrintString((t.Value ?? "").ToString(), '`', false)); case TT.Id: return(LesNodePrinter.PrintId(t.Value as Symbol ?? GSymbol.Empty)); case TT.LParen: return("("); case TT.RParen: return(")"); case TT.LBrack: return("["); case TT.RBrack: return("]"); case TT.LBrace: return("{"); case TT.RBrace: return("}"); //case TT.OpenOf: return ".["; case TT.Shebang: return("#!" + t.Value + "\n"); case TT.Dot: case TT.Assignment: case TT.NormalOp: case TT.PreOrSufOp: case TT.PrefixOp: //case TT.SuffixOp: case TT.Colon: case TT.At: case TT.Comma: case TT.Semicolon: case TT.Not: var name = (t.Value ?? "(punc missing value)").ToString(); return(name); case TT.Indent: return("@indent"); case TT.Dedent: return("@dedent"); default: return("@unknown_token"); } }
private void DoPrinterTest(LNode node) { var sb = new StringBuilder(); messages.List.Clear(); LesNodePrinter.Printer(node, sb, messages, null, " "); Assert.AreEqual(0, messages.List.Count); var reparsed = LesLanguageService.Value.Parse(sb.ToString(), messages); Assert.AreEqual(0, messages.List.Count); Assert.AreEqual(1, reparsed.Count); Assert.AreEqual(node, reparsed[0]); }
private void DoPrinterTest(LNode node, StringBuilder sb) { sb.Length = 0; _messages.List.Clear(); var p = LesNodePrinter.New(sb, "\t", "\n", _messages); p.Print(node); Assert.AreEqual(0, _messages.List.Count); var reparsed = LesLanguageService.Value.Parse(sb.ToString(), _messages); Assert.AreEqual(0, _messages.List.Count); Assert.AreEqual(1, reparsed.Count); Assert.AreEqual(node, reparsed[0]); }
public static void Print(LNode node, StringBuilder target, IMessageSink errors, object mode, string indentString, string lineSeparator) { var w = new LesNodePrinterWriter(target, indentString, lineSeparator); var p = _printer = _printer ?? new LesNodePrinter(w); p.Writer = w; p.Errors = errors; if (object.Equals(mode, NodeStyle.Expression) || mode == ParsingService.Exprs) { p.Print(node, 0, StartExpr); } else { p.Print(node, 0, StartStmt); } p._out = null; p.Errors = null; }
protected override MessageHolder Test(Mode mode, int parseErrors, string str, params LNode[] nodes) { var messages = new MessageHolder(); if (parseErrors == 0) { if (mode == Mode.Exact) { var sb = new StringBuilder(); var printer = LesNodePrinter.New(sb, " ", "\n", messages); var sep = ""; foreach (LNode node in nodes) { sb.Append(sep); sep = "\n"; printer.Print(node); } Assert.AreEqual(str, sb.ToString()); } else { // Start by parsing. If parsing fails, just stop; such errors are // already reported by LesParserTests so we need not report them here. var results = LesLanguageService.Value.Parse(str, messages); if (messages.List.Count == 0) { var sb = new StringBuilder(); foreach (LNode node in nodes) { DoPrinterTest(node, sb); } } } } return(messages); }