| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 Variable* var, | 105 Variable* var, |
| 106 Handle<Object> value); | 106 Handle<Object> value); |
| 107 void PrintLabelsIndented(const char* info, ZoneStringList* labels); | 107 void PrintLabelsIndented(const char* info, ZoneStringList* labels); |
| 108 | 108 |
| 109 void inc_indent() { indent_++; } | 109 void inc_indent() { indent_++; } |
| 110 void dec_indent() { indent_--; } | 110 void dec_indent() { indent_--; } |
| 111 | 111 |
| 112 int indent_; | 112 int indent_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 | |
| 116 // Forward declaration of helper classes. | |
| 117 class TagScope; | |
| 118 class AttributesScope; | |
| 119 | |
| 120 // Build a C string containing a JSON representation of a function's | |
| 121 // AST. The representation is based on JsonML (www.jsonml.org). | |
| 122 class JsonAstBuilder: public PrettyPrinter { | |
| 123 public: | |
| 124 JsonAstBuilder() | |
| 125 : indent_(0), top_tag_scope_(NULL), attributes_scope_(NULL) { | |
| 126 } | |
| 127 virtual ~JsonAstBuilder() {} | |
| 128 | |
| 129 // Controls the indentation of subsequent lines of a tag body after | |
| 130 // the first line. | |
| 131 static const int kTagIndentSize = 2; | |
| 132 | |
| 133 // Controls the indentation of subsequent lines of an attributes | |
| 134 // blocks's body after the first line. | |
| 135 static const int kAttributesIndentSize = 1; | |
| 136 | |
| 137 // Construct a JSON representation of a function literal. | |
| 138 const char* BuildProgram(FunctionLiteral* program); | |
| 139 | |
| 140 // Print text indented by the current indentation level. | |
| 141 void PrintIndented(const char* text) { Print("%*s%s", indent_, "", text); } | |
| 142 | |
| 143 // Change the indentation level. | |
| 144 void increase_indent(int amount) { indent_ += amount; } | |
| 145 void decrease_indent(int amount) { indent_ -= amount; } | |
| 146 | |
| 147 // The builder maintains a stack of opened AST node constructors. | |
| 148 // Each node constructor corresponds to a JsonML tag. | |
| 149 TagScope* tag() { return top_tag_scope_; } | |
| 150 void set_tag(TagScope* scope) { top_tag_scope_ = scope; } | |
| 151 | |
| 152 // The builder maintains a pointer to the currently opened attributes | |
| 153 // of current AST node or NULL if the attributes are not opened. | |
| 154 AttributesScope* attributes() { return attributes_scope_; } | |
| 155 void set_attributes(AttributesScope* scope) { attributes_scope_ = scope; } | |
| 156 | |
| 157 // Add an attribute to the currently opened attributes. | |
| 158 void AddAttribute(const char* name, Handle<String> value); | |
| 159 void AddAttribute(const char* name, const char* value); | |
| 160 void AddAttribute(const char* name, int value); | |
| 161 void AddAttribute(const char* name, bool value); | |
| 162 | |
| 163 // AST node visit functions. | |
| 164 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); | |
| 165 AST_NODE_LIST(DECLARE_VISIT) | |
| 166 #undef DECLARE_VISIT | |
| 167 | |
| 168 private: | |
| 169 int indent_; | |
| 170 TagScope* top_tag_scope_; | |
| 171 AttributesScope* attributes_scope_; | |
| 172 | |
| 173 // Utility function used by AddAttribute implementations. | |
| 174 void AddAttributePrefix(const char* name); | |
| 175 }; | |
| 176 | |
| 177 | |
| 178 // The JSON AST builder keeps a stack of open element tags (AST node | |
| 179 // constructors from the current iteration point to the root of the | |
| 180 // AST). TagScope is a helper class to manage the opening and closing | |
| 181 // of tags, the indentation of their bodies, and comma separating their | |
| 182 // contents. | |
| 183 class TagScope BASE_EMBEDDED { | |
| 184 public: | |
| 185 TagScope(JsonAstBuilder* builder, const char* name); | |
| 186 ~TagScope(); | |
| 187 | |
| 188 void use() { has_body_ = true; } | |
| 189 | |
| 190 private: | |
| 191 JsonAstBuilder* builder_; | |
| 192 TagScope* next_; | |
| 193 bool has_body_; | |
| 194 }; | |
| 195 | |
| 196 | |
| 197 // AttributesScope is a helper class to manage the opening and closing | |
| 198 // of attribute blocks, the indentation of their bodies, and comma | |
| 199 // separating their contents. JsonAstBuilder::AddAttribute adds an | |
| 200 // attribute to the currently open AttributesScope. They cannot be | |
| 201 // nested so the builder keeps an optional single scope rather than a | |
| 202 // stack. | |
| 203 class AttributesScope BASE_EMBEDDED { | |
| 204 public: | |
| 205 explicit AttributesScope(JsonAstBuilder* builder); | |
| 206 ~AttributesScope(); | |
| 207 | |
| 208 bool is_used() { return attribute_count_ > 0; } | |
| 209 void use() { ++attribute_count_; } | |
| 210 | |
| 211 private: | |
| 212 JsonAstBuilder* builder_; | |
| 213 int attribute_count_; | |
| 214 }; | |
| 215 | |
| 216 #endif // DEBUG | 115 #endif // DEBUG |
| 217 | 116 |
| 218 } } // namespace v8::internal | 117 } } // namespace v8::internal |
| 219 | 118 |
| 220 #endif // V8_PRETTYPRINTER_H_ | 119 #endif // V8_PRETTYPRINTER_H_ |
| OLD | NEW |