prettyprinter.h 2.88 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_PRETTYPRINTER_H_
#define V8_PRETTYPRINTER_H_

8 9
#include "src/allocation.h"
#include "src/ast.h"
10

11 12
namespace v8 {
namespace internal {
13 14 15

#ifdef DEBUG

16
class PrettyPrinter: public AstVisitor {
17
 public:
18
  explicit PrettyPrinter(Zone* zone);
19 20 21 22
  virtual ~PrettyPrinter();

  // The following routines print a node into a string.
  // The result string is alive as long as the PrettyPrinter is alive.
23
  const char* Print(AstNode* node);
24 25 26
  const char* PrintExpression(FunctionLiteral* program);
  const char* PrintProgram(FunctionLiteral* program);

27 28
  void Print(const char* format, ...);

29
  // Print a node to stdout.
30
  static void PrintOut(Zone* zone, AstNode* node);
31 32

  // Individual nodes
33 34 35
#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT
36 37 38 39 40 41 42 43 44 45 46

 private:
  char* output_;  // output string buffer
  int size_;  // output_ size
  int pos_;  // current printing position

 protected:
  void Init();
  const char* Output() const { return output_; }

  virtual void PrintStatements(ZoneList<Statement*>* statements);
47
  void PrintLabels(ZoneList<const AstRawString*>* labels);
48 49
  virtual void PrintArguments(ZoneList<Expression*>* arguments);
  void PrintLiteral(Handle<Object> value, bool quote);
50
  void PrintLiteral(const AstRawString* value, bool quote);
51 52 53 54
  void PrintParameters(Scope* scope);
  void PrintDeclarations(ZoneList<Declaration*>* declarations);
  void PrintFunctionLiteral(FunctionLiteral* function);
  void PrintCaseClause(CaseClause* clause);
55 56

  DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
57 58 59 60 61 62
};


// Prints the AST structure
class AstPrinter: public PrettyPrinter {
 public:
63
  explicit AstPrinter(Zone* zone);
64 65 66 67 68
  virtual ~AstPrinter();

  const char* PrintProgram(FunctionLiteral* program);

  // Individual nodes
69 70 71
#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT
72

73 74 75
 private:
  friend class IndentedScope;
  void PrintIndented(const char* txt);
76
  void PrintIndentedVisit(const char* s, AstNode* node);
77 78 79 80 81 82 83 84 85

  void PrintStatements(ZoneList<Statement*>* statements);
  void PrintDeclarations(ZoneList<Declaration*>* declarations);
  void PrintParameters(Scope* scope);
  void PrintArguments(ZoneList<Expression*>* arguments);
  void PrintCaseClause(CaseClause* clause);
  void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote);
  void PrintLiteralWithModeIndented(const char* info,
                                    Variable* var,
86
                                    Handle<Object> value);
87
  void PrintLabelsIndented(ZoneList<const AstRawString*>* labels);
88 89 90 91

  void inc_indent() { indent_++; }
  void dec_indent() { indent_--; }

92
  int indent_;
93 94 95 96 97 98 99
};

#endif  // DEBUG

} }  // namespace v8::internal

#endif  // V8_PRETTYPRINTER_H_