Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: src/prettyprinter.cc

Issue 131101: Initial implementation of printing the AST as a JSON string. This... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/prettyprinter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 Visit(node->left()); 1093 Visit(node->left());
1094 Visit(node->right()); 1094 Visit(node->right());
1095 } 1095 }
1096 1096
1097 1097
1098 void AstPrinter::VisitThisFunction(ThisFunction* node) { 1098 void AstPrinter::VisitThisFunction(ThisFunction* node) {
1099 IndentedScope indent("THIS-FUNCTION"); 1099 IndentedScope indent("THIS-FUNCTION");
1100 } 1100 }
1101 1101
1102 1102
1103 TagScope::TagScope(JsonAstBuilder* builder, const char* name)
1104 : builder_(builder), next_(builder->tag()), has_body_(false) {
1105 if (next_ != NULL) {
1106 next_->use();
1107 builder->Print(",\n");
1108 }
1109 builder->set_tag(this);
1110 builder->PrintIndented("[");
1111 builder->Print("\"%s\"", name);
1112 builder->increase_indent(JsonAstBuilder::kTagIndentSize);
1113 }
1114
1115
1116 TagScope::~TagScope() {
1117 builder_->decrease_indent(JsonAstBuilder::kTagIndentSize);
1118 if (has_body_) {
1119 builder_->Print("\n");
1120 builder_->PrintIndented("]");
1121 } else {
1122 builder_->Print("]");
1123 }
1124 builder_->set_tag(next_);
1125 }
1126
1127
1128 AttributesScope::AttributesScope(JsonAstBuilder* builder)
1129 : builder_(builder), attribute_count_(0) {
1130 builder->set_attributes(this);
1131 builder->tag()->use();
1132 builder->Print(",\n");
1133 builder->PrintIndented("{");
1134 builder->increase_indent(JsonAstBuilder::kAttributesIndentSize);
1135 }
1136
1137
1138 AttributesScope::~AttributesScope() {
1139 builder_->decrease_indent(JsonAstBuilder::kAttributesIndentSize);
1140 if (attribute_count_ > 1) {
1141 builder_->Print("\n");
1142 builder_->PrintIndented("}");
1143 } else {
1144 builder_->Print("}");
1145 }
1146 builder_->set_attributes(NULL);
1147 }
1148
1149
1150 const char* JsonAstBuilder::BuildProgram(FunctionLiteral* program) {
1151 Init();
1152 Visit(program);
1153 Print("\n");
1154 return Output();
1155 }
1156
1157
1158 void JsonAstBuilder::AddAttributePrefix(const char* name) {
1159 if (attributes()->is_used()) {
1160 Print(",\n");
1161 PrintIndented("\"");
1162 } else {
1163 Print("\"");
1164 }
1165 Print("%s\":", name);
1166 attributes()->use();
1167 }
1168
1169
1170 void JsonAstBuilder::AddAttribute(const char* name, Handle<String> value) {
1171 SmartPointer<char> value_string = value->ToCString();
1172 AddAttributePrefix(name);
1173 Print("\"%s\"", *value_string);
1174 }
1175
1176
1177 void JsonAstBuilder::AddAttribute(const char* name, const char* value) {
1178 AddAttributePrefix(name);
1179 Print("\"%s\"", value);
1180 }
1181
1182
1183 void JsonAstBuilder::AddAttribute(const char* name, int value) {
1184 AddAttributePrefix(name);
1185 Print("%d", value);
1186 }
1187
1188
1189 void JsonAstBuilder::AddAttribute(const char* name, bool value) {
1190 AddAttributePrefix(name);
1191 Print(value ? "true" : "false");
1192 }
1193
1194
1195 void JsonAstBuilder::VisitBlock(Block* stmt) {
1196 TagScope tag(this, "Block");
1197 VisitStatements(stmt->statements());
1198 }
1199
1200
1201 void JsonAstBuilder::VisitExpressionStatement(ExpressionStatement* stmt) {
1202 TagScope tag(this, "ExpressionStatement");
1203 Visit(stmt->expression());
1204 }
1205
1206
1207 void JsonAstBuilder::VisitEmptyStatement(EmptyStatement* stmt) {
1208 TagScope tag(this, "EmptyStatement");
1209 }
1210
1211
1212 void JsonAstBuilder::VisitIfStatement(IfStatement* stmt) {
1213 TagScope tag(this, "IfStatement");
1214 Visit(stmt->condition());
1215 Visit(stmt->then_statement());
1216 Visit(stmt->else_statement());
1217 }
1218
1219
1220 void JsonAstBuilder::VisitContinueStatement(ContinueStatement* stmt) {
1221 TagScope tag(this, "ContinueStatement");
1222 }
1223
1224
1225 void JsonAstBuilder::VisitBreakStatement(BreakStatement* stmt) {
1226 TagScope tag(this, "BreakStatement");
1227 }
1228
1229
1230 void JsonAstBuilder::VisitReturnStatement(ReturnStatement* stmt) {
1231 TagScope tag(this, "ReturnStatement");
1232 Visit(stmt->expression());
1233 }
1234
1235
1236 void JsonAstBuilder::VisitWithEnterStatement(WithEnterStatement* stmt) {
1237 TagScope tag(this, "WithEnterStatement");
1238 Visit(stmt->expression());
1239 }
1240
1241
1242 void JsonAstBuilder::VisitWithExitStatement(WithExitStatement* stmt) {
1243 TagScope tag(this, "WithExitStatement");
1244 }
1245
1246
1247 void JsonAstBuilder::VisitSwitchStatement(SwitchStatement* stmt) {
1248 TagScope tag(this, "SwitchStatement");
1249 }
1250
1251
1252 void JsonAstBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) {
1253 TagScope tag(this, "DoWhileStatement");
1254 Visit(stmt->body());
1255 Visit(stmt->cond());
1256 }
1257
1258
1259 void JsonAstBuilder::VisitWhileStatement(WhileStatement* stmt) {
1260 TagScope tag(this, "WhileStatement");
1261 Visit(stmt->cond());
1262 Visit(stmt->body());
1263 }
1264
1265
1266 void JsonAstBuilder::VisitForStatement(ForStatement* stmt) {
1267 TagScope tag(this, "ForStatement");
1268 if (stmt->init() != NULL) Visit(stmt->init());
1269 if (stmt->cond() != NULL) Visit(stmt->cond());
1270 Visit(stmt->body());
1271 if (stmt->next() != NULL) Visit(stmt->next());
1272 }
1273
1274
1275 void JsonAstBuilder::VisitForInStatement(ForInStatement* stmt) {
1276 TagScope tag(this, "ForInStatement");
1277 Visit(stmt->each());
1278 Visit(stmt->enumerable());
1279 Visit(stmt->body());
1280 }
1281
1282
1283 void JsonAstBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) {
1284 TagScope tag(this, "TryCatchStatement");
1285 Visit(stmt->try_block());
1286 Visit(stmt->catch_var());
1287 Visit(stmt->catch_block());
1288 }
1289
1290
1291 void JsonAstBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1292 TagScope tag(this, "TryFinallyStatement");
1293 Visit(stmt->try_block());
1294 Visit(stmt->finally_block());
1295 }
1296
1297
1298 void JsonAstBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) {
1299 TagScope tag(this, "DebuggerStatement");
1300 }
1301
1302
1303 void JsonAstBuilder::VisitFunctionLiteral(FunctionLiteral* expr) {
1304 TagScope tag(this, "FunctionLiteral");
1305 {
1306 AttributesScope attributes(this);
1307 AddAttribute("name", expr->name());
1308 }
1309 VisitDeclarations(expr->scope()->declarations());
1310 VisitStatements(expr->body());
1311 }
1312
1313
1314 void JsonAstBuilder::VisitFunctionBoilerplateLiteral(
1315 FunctionBoilerplateLiteral* expr) {
1316 TagScope tag(this, "FunctionBoilerplateLiteral");
1317 }
1318
1319
1320 void JsonAstBuilder::VisitConditional(Conditional* expr) {
1321 TagScope tag(this, "Conditional");
1322 }
1323
1324
1325 void JsonAstBuilder::VisitSlot(Slot* expr) {
1326 TagScope tag(this, "Slot");
1327 {
1328 AttributesScope attributes(this);
1329 switch (expr->type()) {
1330 case Slot::PARAMETER:
1331 AddAttribute("type", "PARAMETER");
1332 break;
1333 case Slot::LOCAL:
1334 AddAttribute("type", "LOCAL");
1335 break;
1336 case Slot::CONTEXT:
1337 AddAttribute("type", "CONTEXT");
1338 break;
1339 case Slot::LOOKUP:
1340 AddAttribute("type", "LOOKUP");
1341 break;
1342 case Slot::GLOBAL:
1343 AddAttribute("type", "GLOBAL");
1344 break;
1345 }
1346 AddAttribute("index", expr->index());
1347 }
1348 }
1349
1350
1351 void JsonAstBuilder::VisitVariableProxy(VariableProxy* expr) {
1352 if (expr->var()->rewrite() == NULL) {
1353 TagScope tag(this, "VariableProxy");
1354 {
1355 AttributesScope attributes(this);
1356 AddAttribute("name", expr->name());
1357 AddAttribute("mode", Variable::Mode2String(expr->var()->mode()));
1358 }
1359 } else {
1360 Visit(expr->var()->rewrite());
1361 }
1362 }
1363
1364
1365 void JsonAstBuilder::VisitLiteral(Literal* expr) {
1366 TagScope tag(this, "Literal");
1367 {
1368 AttributesScope attributes(this);
1369 Handle<Object> handle = expr->handle();
1370 if (handle->IsString()) {
1371 AddAttribute("handle", Handle<String>(String::cast(*handle)));
1372 } else if (handle->IsSmi()) {
1373 AddAttribute("handle", Smi::cast(*handle)->value());
1374 }
1375 }
1376 }
1377
1378
1379 void JsonAstBuilder::VisitRegExpLiteral(RegExpLiteral* expr) {
1380 TagScope tag(this, "RegExpLiteral");
1381 }
1382
1383
1384 void JsonAstBuilder::VisitObjectLiteral(ObjectLiteral* expr) {
1385 TagScope tag(this, "ObjectLiteral");
1386 }
1387
1388
1389 void JsonAstBuilder::VisitArrayLiteral(ArrayLiteral* expr) {
1390 TagScope tag(this, "ArrayLiteral");
1391 }
1392
1393
1394 void JsonAstBuilder::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1395 TagScope tag(this, "CatchExtensionObject");
1396 Visit(expr->key());
1397 Visit(expr->value());
1398 }
1399
1400
1401 void JsonAstBuilder::VisitAssignment(Assignment* expr) {
1402 TagScope tag(this, "Assignment");
1403 {
1404 AttributesScope attributes(this);
1405 AddAttribute("op", Token::Name(expr->op()));
1406 }
1407 Visit(expr->target());
1408 Visit(expr->value());
1409 }
1410
1411
1412 void JsonAstBuilder::VisitThrow(Throw* expr) {
1413 TagScope tag(this, "Throw");
1414 Visit(expr->exception());
1415 }
1416
1417
1418 void JsonAstBuilder::VisitProperty(Property* expr) {
1419 TagScope tag(this, "Property");
1420 {
1421 AttributesScope attributes(this);
1422 AddAttribute("type", expr->is_synthetic() ? "SYNTHETIC" : "NORMAL");
1423 }
1424 Visit(expr->obj());
1425 Visit(expr->key());
1426 }
1427
1428
1429 void JsonAstBuilder::VisitCall(Call* expr) {
1430 TagScope tag(this, "Call");
1431 Visit(expr->expression());
1432 VisitExpressions(expr->arguments());
1433 }
1434
1435
1436 void JsonAstBuilder::VisitCallNew(CallNew* expr) {
1437 TagScope tag(this, "CallNew");
1438 Visit(expr->expression());
1439 VisitExpressions(expr->arguments());
1440 }
1441
1442
1443 void JsonAstBuilder::VisitCallRuntime(CallRuntime* expr) {
1444 TagScope tag(this, "CallRuntime");
1445 {
1446 AttributesScope attributes(this);
1447 AddAttribute("name", expr->name());
1448 }
1449 VisitExpressions(expr->arguments());
1450 }
1451
1452
1453 void JsonAstBuilder::VisitUnaryOperation(UnaryOperation* expr) {
1454 TagScope tag(this, "UnaryOperation");
1455 {
1456 AttributesScope attributes(this);
1457 AddAttribute("op", Token::Name(expr->op()));
1458 }
1459 Visit(expr->expression());
1460 }
1461
1462
1463 void JsonAstBuilder::VisitCountOperation(CountOperation* expr) {
1464 TagScope tag(this, "CountOperation");
1465 {
1466 AttributesScope attributes(this);
1467 AddAttribute("is_prefix", expr->is_prefix());
1468 AddAttribute("op", Token::Name(expr->op()));
1469 }
1470 Visit(expr->expression());
1471 }
1472
1473
1474 void JsonAstBuilder::VisitBinaryOperation(BinaryOperation* expr) {
1475 TagScope tag(this, "BinaryOperation");
1476 {
1477 AttributesScope attributes(this);
1478 AddAttribute("op", Token::Name(expr->op()));
1479 }
1480 Visit(expr->left());
1481 Visit(expr->right());
1482 }
1483
1484
1485 void JsonAstBuilder::VisitCompareOperation(CompareOperation* expr) {
1486 TagScope tag(this, "CompareOperation");
1487 {
1488 AttributesScope attributes(this);
1489 AddAttribute("op", Token::Name(expr->op()));
1490 }
1491 Visit(expr->left());
1492 Visit(expr->right());
1493 }
1494
1495
1496 void JsonAstBuilder::VisitThisFunction(ThisFunction* expr) {
1497 TagScope tag(this, "ThisFunction");
1498 }
1499
1500
1501 void JsonAstBuilder::VisitDeclaration(Declaration* decl) {
1502 TagScope tag(this, "Declaration");
1503 {
1504 AttributesScope attributes(this);
1505 AddAttribute("mode", Variable::Mode2String(decl->mode()));
1506 }
1507 Visit(decl->proxy());
1508 if (decl->fun() != NULL) Visit(decl->fun());
1509 }
1510
1103 1511
1104 #endif // DEBUG 1512 #endif // DEBUG
1105 1513
1106 } } // namespace v8::internal 1514 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/prettyprinter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698