karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_PRETTYPRINTER_H_ |
| 29 | #define V8_PRETTYPRINTER_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "ast.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef DEBUG |
| 38 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 39 | class PrettyPrinter: public AstVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | public: |
| 41 | PrettyPrinter(); |
| 42 | virtual ~PrettyPrinter(); |
| 43 | |
| 44 | // The following routines print a node into a string. |
| 45 | // The result string is alive as long as the PrettyPrinter is alive. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 46 | const char* Print(AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | const char* PrintExpression(FunctionLiteral* program); |
| 48 | const char* PrintProgram(FunctionLiteral* program); |
| 49 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 50 | void Print(const char* format, ...); |
| 51 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | // Print a node to stdout. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 53 | static void PrintOut(AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 55 | virtual void VisitSlot(Slot* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | // Individual nodes |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 57 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 58 | AST_NODE_LIST(DECLARE_VISIT) |
| 59 | #undef DECLARE_VISIT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | char* output_; // output string buffer |
| 63 | int size_; // output_ size |
| 64 | int pos_; // current printing position |
| 65 | |
| 66 | protected: |
| 67 | void Init(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | const char* Output() const { return output_; } |
| 69 | |
| 70 | virtual void PrintStatements(ZoneList<Statement*>* statements); |
| 71 | void PrintLabels(ZoneStringList* labels); |
| 72 | virtual void PrintArguments(ZoneList<Expression*>* arguments); |
| 73 | void PrintLiteral(Handle<Object> value, bool quote); |
| 74 | void PrintParameters(Scope* scope); |
| 75 | void PrintDeclarations(ZoneList<Declaration*>* declarations); |
| 76 | void PrintFunctionLiteral(FunctionLiteral* function); |
| 77 | void PrintCaseClause(CaseClause* clause); |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | // Prints the AST structure |
| 82 | class AstPrinter: public PrettyPrinter { |
| 83 | public: |
| 84 | AstPrinter(); |
| 85 | virtual ~AstPrinter(); |
| 86 | |
| 87 | const char* PrintProgram(FunctionLiteral* program); |
| 88 | |
| 89 | // Individual nodes |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 90 | virtual void VisitSlot(Slot* node); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 91 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 92 | AST_NODE_LIST(DECLARE_VISIT) |
| 93 | #undef DECLARE_VISIT |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 94 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | private: |
| 96 | friend class IndentedScope; |
| 97 | void PrintIndented(const char* txt); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 98 | void PrintIndentedVisit(const char* s, AstNode* node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | |
| 100 | void PrintStatements(ZoneList<Statement*>* statements); |
| 101 | void PrintDeclarations(ZoneList<Declaration*>* declarations); |
| 102 | void PrintParameters(Scope* scope); |
| 103 | void PrintArguments(ZoneList<Expression*>* arguments); |
| 104 | void PrintCaseClause(CaseClause* clause); |
| 105 | void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); |
| 106 | void PrintLiteralWithModeIndented(const char* info, |
| 107 | Variable* var, |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 108 | Handle<Object> value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | void PrintLabelsIndented(const char* info, ZoneStringList* labels); |
| 110 | |
| 111 | void inc_indent() { indent_++; } |
| 112 | void dec_indent() { indent_--; } |
| 113 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 114 | int indent_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 117 | |
| 118 | // Forward declaration of helper classes. |
| 119 | class TagScope; |
| 120 | class AttributesScope; |
| 121 | |
| 122 | // Build a C string containing a JSON representation of a function's |
| 123 | // AST. The representation is based on JsonML (www.jsonml.org). |
| 124 | class JsonAstBuilder: public PrettyPrinter { |
| 125 | public: |
| 126 | JsonAstBuilder() |
| 127 | : indent_(0), top_tag_scope_(NULL), attributes_scope_(NULL) { |
| 128 | } |
| 129 | virtual ~JsonAstBuilder() {} |
| 130 | |
| 131 | // Controls the indentation of subsequent lines of a tag body after |
| 132 | // the first line. |
| 133 | static const int kTagIndentSize = 2; |
| 134 | |
| 135 | // Controls the indentation of subsequent lines of an attributes |
| 136 | // blocks's body after the first line. |
| 137 | static const int kAttributesIndentSize = 1; |
| 138 | |
| 139 | // Construct a JSON representation of a function literal. |
| 140 | const char* BuildProgram(FunctionLiteral* program); |
| 141 | |
| 142 | // Print text indented by the current indentation level. |
| 143 | void PrintIndented(const char* text) { Print("%*s%s", indent_, "", text); } |
| 144 | |
| 145 | // Change the indentation level. |
| 146 | void increase_indent(int amount) { indent_ += amount; } |
| 147 | void decrease_indent(int amount) { indent_ -= amount; } |
| 148 | |
| 149 | // The builder maintains a stack of opened AST node constructors. |
| 150 | // Each node constructor corresponds to a JsonML tag. |
| 151 | TagScope* tag() { return top_tag_scope_; } |
| 152 | void set_tag(TagScope* scope) { top_tag_scope_ = scope; } |
| 153 | |
| 154 | // The builder maintains a pointer to the currently opened attributes |
| 155 | // of current AST node or NULL if the attributes are not opened. |
| 156 | AttributesScope* attributes() { return attributes_scope_; } |
| 157 | void set_attributes(AttributesScope* scope) { attributes_scope_ = scope; } |
| 158 | |
| 159 | // Add an attribute to the currently opened attributes. |
| 160 | void AddAttribute(const char* name, Handle<String> value); |
| 161 | void AddAttribute(const char* name, const char* value); |
| 162 | void AddAttribute(const char* name, int value); |
| 163 | void AddAttribute(const char* name, bool value); |
| 164 | |
| 165 | // AST node visit functions. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 166 | virtual void VisitSlot(Slot* node); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 167 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 168 | AST_NODE_LIST(DECLARE_VISIT) |
| 169 | #undef DECLARE_VISIT |
| 170 | |
| 171 | private: |
| 172 | int indent_; |
| 173 | TagScope* top_tag_scope_; |
| 174 | AttributesScope* attributes_scope_; |
| 175 | |
| 176 | // Utility function used by AddAttribute implementations. |
| 177 | void AddAttributePrefix(const char* name); |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | // The JSON AST builder keeps a stack of open element tags (AST node |
| 182 | // constructors from the current iteration point to the root of the |
| 183 | // AST). TagScope is a helper class to manage the opening and closing |
| 184 | // of tags, the indentation of their bodies, and comma separating their |
| 185 | // contents. |
| 186 | class TagScope BASE_EMBEDDED { |
| 187 | public: |
| 188 | TagScope(JsonAstBuilder* builder, const char* name); |
| 189 | ~TagScope(); |
| 190 | |
| 191 | void use() { has_body_ = true; } |
| 192 | |
| 193 | private: |
| 194 | JsonAstBuilder* builder_; |
| 195 | TagScope* next_; |
| 196 | bool has_body_; |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | // AttributesScope is a helper class to manage the opening and closing |
| 201 | // of attribute blocks, the indentation of their bodies, and comma |
| 202 | // separating their contents. JsonAstBuilder::AddAttribute adds an |
| 203 | // attribute to the currently open AttributesScope. They cannot be |
| 204 | // nested so the builder keeps an optional single scope rather than a |
| 205 | // stack. |
| 206 | class AttributesScope BASE_EMBEDDED { |
| 207 | public: |
| 208 | explicit AttributesScope(JsonAstBuilder* builder); |
| 209 | ~AttributesScope(); |
| 210 | |
| 211 | bool is_used() { return attribute_count_ > 0; } |
| 212 | void use() { ++attribute_count_; } |
| 213 | |
| 214 | private: |
| 215 | JsonAstBuilder* builder_; |
| 216 | int attribute_count_; |
| 217 | }; |
| 218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | #endif // DEBUG |
| 220 | |
| 221 | } } // namespace v8::internal |
| 222 | |
| 223 | #endif // V8_PRETTYPRINTER_H_ |