OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_AST_PRETTYPRINTER_H_ | 5 #ifndef V8_AST_PRETTYPRINTER_H_ |
6 #define V8_AST_PRETTYPRINTER_H_ | 6 #define V8_AST_PRETTYPRINTER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast/ast.h" | 9 #include "src/ast/ast.h" |
10 #include "src/base/compiler-specific.h" | 10 #include "src/base/compiler-specific.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 protected: | 46 protected: |
47 void PrintLiteral(Object* value, bool quote); | 47 void PrintLiteral(Object* value, bool quote); |
48 void PrintLiteral(const AstRawString* value, bool quote); | 48 void PrintLiteral(const AstRawString* value, bool quote); |
49 void FindStatements(ZoneList<Statement*>* statements); | 49 void FindStatements(ZoneList<Statement*>* statements); |
50 void FindArguments(ZoneList<Expression*>* arguments); | 50 void FindArguments(ZoneList<Expression*>* arguments); |
51 }; | 51 }; |
52 | 52 |
53 | 53 |
54 #ifdef DEBUG | 54 #ifdef DEBUG |
55 | 55 |
56 class PrettyPrinter: public AstVisitor { | 56 // Prints the AST structure |
| 57 class AstPrinter : public AstVisitor { |
57 public: | 58 public: |
58 explicit PrettyPrinter(Isolate* isolate); | 59 explicit AstPrinter(Isolate* isolate); |
59 virtual ~PrettyPrinter(); | 60 virtual ~AstPrinter(); |
60 | 61 |
61 // The following routines print a node into a string. | 62 // The following routines print a node into a string. |
62 // The result string is alive as long as the PrettyPrinter is alive. | 63 // The result string is alive as long as the PrettyPrinter is alive. |
63 const char* Print(AstNode* node); | 64 const char* Print(AstNode* node); |
64 const char* PrintExpression(FunctionLiteral* program); | |
65 const char* PrintProgram(FunctionLiteral* program); | 65 const char* PrintProgram(FunctionLiteral* program); |
66 | 66 |
67 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); | 67 void PRINTF_FORMAT(2, 3) Print(const char* format, ...); |
68 | 68 |
69 // Print a node to stdout. | 69 // Print a node to stdout. |
70 static void PrintOut(Isolate* isolate, AstNode* node); | 70 static void PrintOut(Isolate* isolate, AstNode* node); |
71 | 71 |
72 // Individual nodes | 72 // Individual nodes |
73 #define DECLARE_VISIT(type) void Visit##type(type* node) override; | 73 #define DECLARE_VISIT(type) void Visit##type(type* node) override; |
74 AST_NODE_LIST(DECLARE_VISIT) | 74 AST_NODE_LIST(DECLARE_VISIT) |
75 #undef DECLARE_VISIT | 75 #undef DECLARE_VISIT |
76 | 76 |
77 private: | 77 private: |
78 Isolate* isolate_; | |
79 char* output_; // output string buffer | |
80 int size_; // output_ size | |
81 int pos_; // current printing position | |
82 | |
83 protected: | |
84 void Init(); | |
85 const char* Output() const { return output_; } | |
86 | |
87 virtual void PrintStatements(ZoneList<Statement*>* statements); | |
88 void PrintLabels(ZoneList<const AstRawString*>* labels); | |
89 virtual void PrintArguments(ZoneList<Expression*>* arguments); | |
90 void PrintLiteral(Handle<Object> value, bool quote); | |
91 void PrintLiteral(const AstRawString* value, bool quote); | |
92 void PrintParameters(Scope* scope); | |
93 void PrintDeclarations(ZoneList<Declaration*>* declarations); | |
94 void PrintFunctionLiteral(FunctionLiteral* function); | |
95 void PrintCaseClause(CaseClause* clause); | |
96 void PrintObjectLiteralProperty(ObjectLiteralProperty* property); | |
97 | |
98 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | |
99 }; | |
100 | |
101 | |
102 // Prints the AST structure | |
103 class AstPrinter: public PrettyPrinter { | |
104 public: | |
105 explicit AstPrinter(Isolate* isolate); | |
106 virtual ~AstPrinter(); | |
107 | |
108 const char* PrintProgram(FunctionLiteral* program); | |
109 | |
110 // Print a node to stdout. | |
111 static void PrintOut(Isolate* isolate, AstNode* node); | |
112 | |
113 // Individual nodes | |
114 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); | |
115 AST_NODE_LIST(DECLARE_VISIT) | |
116 #undef DECLARE_VISIT | |
117 | |
118 private: | |
119 friend class IndentedScope; | 78 friend class IndentedScope; |
| 79 |
| 80 void Init(); |
| 81 |
| 82 void PrintLabels(ZoneList<const AstRawString*>* labels); |
| 83 void PrintLiteral(const AstRawString* value, bool quote); |
| 84 void PrintLiteral(Handle<Object> value, bool quote); |
120 void PrintIndented(const char* txt); | 85 void PrintIndented(const char* txt); |
121 void PrintIndentedVisit(const char* s, AstNode* node); | 86 void PrintIndentedVisit(const char* s, AstNode* node); |
122 | 87 |
123 void PrintStatements(ZoneList<Statement*>* statements); | 88 void PrintStatements(ZoneList<Statement*>* statements); |
124 void PrintDeclarations(ZoneList<Declaration*>* declarations); | 89 void PrintDeclarations(ZoneList<Declaration*>* declarations); |
125 void PrintParameters(Scope* scope); | 90 void PrintParameters(Scope* scope); |
126 void PrintArguments(ZoneList<Expression*>* arguments); | 91 void PrintArguments(ZoneList<Expression*>* arguments); |
127 void PrintCaseClause(CaseClause* clause); | 92 void PrintCaseClause(CaseClause* clause); |
128 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); | 93 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); |
129 void PrintLiteralWithModeIndented(const char* info, | 94 void PrintLiteralWithModeIndented(const char* info, |
130 Variable* var, | 95 Variable* var, |
131 Handle<Object> value); | 96 Handle<Object> value); |
132 void PrintLabelsIndented(ZoneList<const AstRawString*>* labels); | 97 void PrintLabelsIndented(ZoneList<const AstRawString*>* labels); |
133 void PrintProperties(ZoneList<ObjectLiteral::Property*>* properties); | 98 void PrintProperties(ZoneList<ObjectLiteral::Property*>* properties); |
134 | 99 |
135 void inc_indent() { indent_++; } | 100 void inc_indent() { indent_++; } |
136 void dec_indent() { indent_--; } | 101 void dec_indent() { indent_--; } |
137 | 102 |
| 103 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
| 104 |
| 105 Isolate* isolate_; |
| 106 char* output_; // output string buffer |
| 107 int size_; // output_ size |
| 108 int pos_; // current printing position |
138 int indent_; | 109 int indent_; |
139 }; | 110 }; |
140 | 111 |
141 #endif // DEBUG | 112 #endif // DEBUG |
142 | 113 |
143 } // namespace internal | 114 } // namespace internal |
144 } // namespace v8 | 115 } // namespace v8 |
145 | 116 |
146 #endif // V8_AST_PRETTYPRINTER_H_ | 117 #endif // V8_AST_PRETTYPRINTER_H_ |
OLD | NEW |