1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_PRETTYPRINTER_H_
6 #define V8_PRETTYPRINTER_H_
8 #include "src/allocation.h"
16 class PrettyPrinter: public AstVisitor {
18 explicit PrettyPrinter(Zone* zone);
19 virtual ~PrettyPrinter();
21 // The following routines print a node into a string.
22 // The result string is alive as long as the PrettyPrinter is alive.
23 const char* Print(AstNode* node);
24 const char* PrintExpression(FunctionLiteral* program);
25 const char* PrintProgram(FunctionLiteral* program);
27 void Print(const char* format, ...);
29 // Print a node to stdout.
30 static void PrintOut(Zone* zone, AstNode* node);
33 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
34 AST_NODE_LIST(DECLARE_VISIT)
38 char* output_; // output string buffer
39 int size_; // output_ size
40 int pos_; // current printing position
44 const char* Output() const { return output_; }
46 virtual void PrintStatements(ZoneList<Statement*>* statements);
47 void PrintLabels(ZoneList<const AstRawString*>* labels);
48 virtual void PrintArguments(ZoneList<Expression*>* arguments);
49 void PrintLiteral(Handle<Object> value, bool quote);
50 void PrintLiteral(const AstRawString* value, bool quote);
51 void PrintParameters(Scope* scope);
52 void PrintDeclarations(ZoneList<Declaration*>* declarations);
53 void PrintFunctionLiteral(FunctionLiteral* function);
54 void PrintCaseClause(CaseClause* clause);
55 void PrintObjectLiteralProperty(ObjectLiteralProperty* property);
57 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
61 // Prints the AST structure
62 class AstPrinter: public PrettyPrinter {
64 explicit AstPrinter(Zone* zone);
65 virtual ~AstPrinter();
67 const char* PrintProgram(FunctionLiteral* program);
70 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
71 AST_NODE_LIST(DECLARE_VISIT)
75 friend class IndentedScope;
76 void PrintIndented(const char* txt);
77 void PrintIndentedVisit(const char* s, AstNode* node);
79 void PrintStatements(ZoneList<Statement*>* statements);
80 void PrintDeclarations(ZoneList<Declaration*>* declarations);
81 void PrintParameters(Scope* scope);
82 void PrintArguments(ZoneList<Expression*>* arguments);
83 void PrintCaseClause(CaseClause* clause);
84 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
85 void PrintLiteralWithModeIndented(const char* info,
87 Handle<Object> value);
88 void PrintLabelsIndented(ZoneList<const AstRawString*>* labels);
90 void inc_indent() { indent_++; }
91 void dec_indent() { indent_--; }
98 } } // namespace v8::internal
100 #endif // V8_PRETTYPRINTER_H_