OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 public: | 39 public: |
40 PrettyPrinter(); | 40 PrettyPrinter(); |
41 virtual ~PrettyPrinter(); | 41 virtual ~PrettyPrinter(); |
42 | 42 |
43 // The following routines print a node into a string. | 43 // The following routines print a node into a string. |
44 // The result string is alive as long as the PrettyPrinter is alive. | 44 // The result string is alive as long as the PrettyPrinter is alive. |
45 const char* Print(AstNode* node); | 45 const char* Print(AstNode* node); |
46 const char* PrintExpression(FunctionLiteral* program); | 46 const char* PrintExpression(FunctionLiteral* program); |
47 const char* PrintProgram(FunctionLiteral* program); | 47 const char* PrintProgram(FunctionLiteral* program); |
48 | 48 |
| 49 void Print(const char* format, ...); |
| 50 |
49 // Print a node to stdout. | 51 // Print a node to stdout. |
50 static void PrintOut(AstNode* node); | 52 static void PrintOut(AstNode* node); |
51 | 53 |
52 // Individual nodes | 54 // Individual nodes |
53 #define DEF_VISIT(type) \ | 55 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
54 virtual void Visit##type(type* node); | 56 AST_NODE_LIST(DECLARE_VISIT) |
55 AST_NODE_LIST(DEF_VISIT) | 57 #undef DECLARE_VISIT |
56 #undef DEF_VISIT | |
57 | 58 |
58 private: | 59 private: |
59 char* output_; // output string buffer | 60 char* output_; // output string buffer |
60 int size_; // output_ size | 61 int size_; // output_ size |
61 int pos_; // current printing position | 62 int pos_; // current printing position |
62 | 63 |
63 protected: | 64 protected: |
64 void Init(); | 65 void Init(); |
65 void Print(const char* format, ...); | |
66 const char* Output() const { return output_; } | 66 const char* Output() const { return output_; } |
67 | 67 |
68 virtual void PrintStatements(ZoneList<Statement*>* statements); | 68 virtual void PrintStatements(ZoneList<Statement*>* statements); |
69 void PrintLabels(ZoneStringList* labels); | 69 void PrintLabels(ZoneStringList* labels); |
70 virtual void PrintArguments(ZoneList<Expression*>* arguments); | 70 virtual void PrintArguments(ZoneList<Expression*>* arguments); |
71 void PrintLiteral(Handle<Object> value, bool quote); | 71 void PrintLiteral(Handle<Object> value, bool quote); |
72 void PrintParameters(Scope* scope); | 72 void PrintParameters(Scope* scope); |
73 void PrintDeclarations(ZoneList<Declaration*>* declarations); | 73 void PrintDeclarations(ZoneList<Declaration*>* declarations); |
74 void PrintFunctionLiteral(FunctionLiteral* function); | 74 void PrintFunctionLiteral(FunctionLiteral* function); |
75 void PrintCaseClause(CaseClause* clause); | 75 void PrintCaseClause(CaseClause* clause); |
76 }; | 76 }; |
77 | 77 |
78 | 78 |
79 // Prints the AST structure | 79 // Prints the AST structure |
80 class AstPrinter: public PrettyPrinter { | 80 class AstPrinter: public PrettyPrinter { |
81 public: | 81 public: |
82 AstPrinter(); | 82 AstPrinter(); |
83 virtual ~AstPrinter(); | 83 virtual ~AstPrinter(); |
84 | 84 |
85 const char* PrintProgram(FunctionLiteral* program); | 85 const char* PrintProgram(FunctionLiteral* program); |
86 | 86 |
87 // Individual nodes | 87 // Individual nodes |
88 #define DEF_VISIT(type) \ | 88 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
89 virtual void Visit##type(type* node); | 89 AST_NODE_LIST(DECLARE_VISIT) |
90 AST_NODE_LIST(DEF_VISIT) | 90 #undef DECLARE_VISIT |
91 #undef DEF_VISIT | |
92 private: | 91 private: |
93 friend class IndentedScope; | 92 friend class IndentedScope; |
94 void PrintIndented(const char* txt); | 93 void PrintIndented(const char* txt); |
95 void PrintIndentedVisit(const char* s, AstNode* node); | 94 void PrintIndentedVisit(const char* s, AstNode* node); |
96 | 95 |
97 void PrintStatements(ZoneList<Statement*>* statements); | 96 void PrintStatements(ZoneList<Statement*>* statements); |
98 void PrintDeclarations(ZoneList<Declaration*>* declarations); | 97 void PrintDeclarations(ZoneList<Declaration*>* declarations); |
99 void PrintParameters(Scope* scope); | 98 void PrintParameters(Scope* scope); |
100 void PrintArguments(ZoneList<Expression*>* arguments); | 99 void PrintArguments(ZoneList<Expression*>* arguments); |
101 void PrintCaseClause(CaseClause* clause); | 100 void PrintCaseClause(CaseClause* clause); |
102 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); | 101 void PrintLiteralIndented(const char* info, Handle<Object> value, bool quote); |
103 void PrintLiteralWithModeIndented(const char* info, | 102 void PrintLiteralWithModeIndented(const char* info, |
104 Variable* var, | 103 Variable* var, |
105 Handle<Object> value, | 104 Handle<Object> value, |
106 SmiAnalysis* type); | 105 SmiAnalysis* type); |
107 void PrintLabelsIndented(const char* info, ZoneStringList* labels); | 106 void PrintLabelsIndented(const char* info, ZoneStringList* labels); |
108 | 107 |
109 void inc_indent() { indent_++; } | 108 void inc_indent() { indent_++; } |
110 void dec_indent() { indent_--; } | 109 void dec_indent() { indent_--; } |
111 | 110 |
112 static int indent_; | 111 static int indent_; |
113 }; | 112 }; |
114 | 113 |
| 114 |
| 115 // Forward declaration of helper classes. |
| 116 class TagScope; |
| 117 class AttributesScope; |
| 118 |
| 119 // Build a C string containing a JSON representation of a function's |
| 120 // AST. The representation is based on JsonML (www.jsonml.org). |
| 121 class JsonAstBuilder: public PrettyPrinter { |
| 122 public: |
| 123 JsonAstBuilder() |
| 124 : indent_(0), top_tag_scope_(NULL), attributes_scope_(NULL) { |
| 125 } |
| 126 virtual ~JsonAstBuilder() {} |
| 127 |
| 128 // Controls the indentation of subsequent lines of a tag body after |
| 129 // the first line. |
| 130 static const int kTagIndentSize = 2; |
| 131 |
| 132 // Controls the indentation of subsequent lines of an attributes |
| 133 // blocks's body after the first line. |
| 134 static const int kAttributesIndentSize = 1; |
| 135 |
| 136 // Construct a JSON representation of a function literal. |
| 137 const char* BuildProgram(FunctionLiteral* program); |
| 138 |
| 139 // Print text indented by the current indentation level. |
| 140 void PrintIndented(const char* text) { Print("%*s%s", indent_, "", text); } |
| 141 |
| 142 // Change the indentation level. |
| 143 void increase_indent(int amount) { indent_ += amount; } |
| 144 void decrease_indent(int amount) { indent_ -= amount; } |
| 145 |
| 146 // The builder maintains a stack of opened AST node constructors. |
| 147 // Each node constructor corresponds to a JsonML tag. |
| 148 TagScope* tag() { return top_tag_scope_; } |
| 149 void set_tag(TagScope* scope) { top_tag_scope_ = scope; } |
| 150 |
| 151 // The builder maintains a pointer to the currently opened attributes |
| 152 // of current AST node or NULL if the attributes are not opened. |
| 153 AttributesScope* attributes() { return attributes_scope_; } |
| 154 void set_attributes(AttributesScope* scope) { attributes_scope_ = scope; } |
| 155 |
| 156 // Add an attribute to the currently opened attributes. |
| 157 void AddAttribute(const char* name, Handle<String> value); |
| 158 void AddAttribute(const char* name, const char* value); |
| 159 void AddAttribute(const char* name, int value); |
| 160 void AddAttribute(const char* name, bool value); |
| 161 |
| 162 // AST node visit functions. |
| 163 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 164 AST_NODE_LIST(DECLARE_VISIT) |
| 165 #undef DECLARE_VISIT |
| 166 |
| 167 private: |
| 168 int indent_; |
| 169 TagScope* top_tag_scope_; |
| 170 AttributesScope* attributes_scope_; |
| 171 |
| 172 // Utility function used by AddAttribute implementations. |
| 173 void AddAttributePrefix(const char* name); |
| 174 }; |
| 175 |
| 176 |
| 177 // The JSON AST builder keeps a stack of open element tags (AST node |
| 178 // constructors from the current iteration point to the root of the |
| 179 // AST). TagScope is a helper class to manage the opening and closing |
| 180 // of tags, the indentation of their bodies, and comma separating their |
| 181 // contents. |
| 182 class TagScope BASE_EMBEDDED { |
| 183 public: |
| 184 TagScope(JsonAstBuilder* builder, const char* name); |
| 185 ~TagScope(); |
| 186 |
| 187 void use() { has_body_ = true; } |
| 188 |
| 189 private: |
| 190 JsonAstBuilder* builder_; |
| 191 TagScope* next_; |
| 192 bool has_body_; |
| 193 }; |
| 194 |
| 195 |
| 196 // AttributesScope is a helper class to manage the opening and closing |
| 197 // of attribute blocks, the indentation of their bodies, and comma |
| 198 // separating their contents. JsonAstBuilder::AddAttribute adds an |
| 199 // attribute to the currently open AttributesScope. They cannot be |
| 200 // nested so the builder keeps an optional single scope rather than a |
| 201 // stack. |
| 202 class AttributesScope BASE_EMBEDDED { |
| 203 public: |
| 204 explicit AttributesScope(JsonAstBuilder* builder); |
| 205 ~AttributesScope(); |
| 206 |
| 207 bool is_used() { return attribute_count_ > 0; } |
| 208 void use() { ++attribute_count_; } |
| 209 |
| 210 private: |
| 211 JsonAstBuilder* builder_; |
| 212 int attribute_count_; |
| 213 }; |
| 214 |
115 #endif // DEBUG | 215 #endif // DEBUG |
116 | 216 |
117 } } // namespace v8::internal | 217 } } // namespace v8::internal |
118 | 218 |
119 #endif // V8_PRETTYPRINTER_H_ | 219 #endif // V8_PRETTYPRINTER_H_ |
OLD | NEW |