/// <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="Les2Lexer"/>. /// </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(Les2Printer.PrintLiteral(t.Value, t.Style)); case TT.BQOperator: return(Les2Printer.PrintString((t.Value ?? "").ToString(), '`', false)); case TT.Id: return(Les2Printer.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.At: case TT.Comma: case TT.Semicolon: case TT.Not: var name = (t.Value ?? "(punc missing value)").ToString(); return(name); default: return("@unknown_token"); } }
/// <summary>Expresses a token as a string, using LES printers for identifiers and literals.</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. /// </remarks> public static string ToString(Token t) { StringBuilder sb = new StringBuilder(); switch (t.Kind) { case TokenKind.Spaces: return((t.Value ?? " ").ToString()); case TokenKind.Comment: if (t.Type() == TokenType.SLComment) { return("// (comment)"); } else { return("/* (comment) */"); } case TokenKind.Id: return(Les2Printer.PrintId(t.Value as Symbol ?? GSymbol.Empty)); case TokenKind.Literal: return(Les2Printer.PrintLiteral(t.Value, t.Style)); } if (t.Value != null) { if (t.Type() == TokenType.BQOperator) { return(Les2Printer.PrintString((t.Value ?? "").ToString(), '`', false)); } else if (t.Type() == TokenType.Shebang) { return("#!" + t.Value.ToString() + "\n"); } return(t.Value.ToString()); } switch (t.Kind) { case TokenKind.LParen: return("("); case TokenKind.RParen: return(")"); case TokenKind.LBrack: return("["); case TokenKind.RBrack: return("]"); case TokenKind.LBrace: return("{"); case TokenKind.RBrace: return("}"); case TokenKind.Indent: return("(Indent)"); case TokenKind.Dedent: return("(Dedent)"); case TokenKind.Dot: return("(Dot)"); case TokenKind.Assignment: return("(Assignment)"); case TokenKind.Operator: return("(Operator)"); case TokenKind.Separator: return("(Separator)"); case TokenKind.AttrKeyword: return("(AttrKeyword)"); case TokenKind.TypeKeyword: return("(TypeKeyword)"); case TokenKind.OtherKeyword: return("(OtherKeyword)"); } return("(Type " + t.TypeInt + ")"); }