| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 package com.google.dart.compiler.ast; | |
| 6 | |
| 7 import com.google.dart.compiler.util.TextOutput; | |
| 8 | |
| 9 import java.util.Iterator; | |
| 10 import java.util.List; | |
| 11 | |
| 12 /** | |
| 13 * Used by {@link DartNode} to generate Dart source from an AST subtree. | |
| 14 */ | |
| 15 public class DartToSourceVisitor extends ASTVisitor<Void> { | |
| 16 | |
| 17 private final TextOutput out; | |
| 18 | |
| 19 public DartToSourceVisitor(TextOutput out) { | |
| 20 this.out = out; | |
| 21 } | |
| 22 | |
| 23 @Override | |
| 24 public Void visitUnit(DartUnit x) { | |
| 25 p("// unit " + x.getSourceName()); | |
| 26 nl(); | |
| 27 return super.visitUnit(x); | |
| 28 } | |
| 29 | |
| 30 @Override | |
| 31 public Void visitComment(DartComment node) { | |
| 32 return null; | |
| 33 } | |
| 34 | |
| 35 @Override | |
| 36 public Void visitNativeBlock(DartNativeBlock x) { | |
| 37 p("native;"); | |
| 38 return null; | |
| 39 } | |
| 40 | |
| 41 private void accept(DartNode x) { | |
| 42 x.accept(this); | |
| 43 } | |
| 44 | |
| 45 private void acceptList(List<? extends DartNode> xs) { | |
| 46 for (DartNode x : xs) { | |
| 47 x.accept(this); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 private void pTypeParameters(List<DartTypeParameter> typeParameters) { | |
| 52 if (typeParameters != null && !typeParameters.isEmpty()) { | |
| 53 p("<"); | |
| 54 boolean first = true; | |
| 55 for (DartNode node : typeParameters) { | |
| 56 if (!first) { | |
| 57 p(", "); | |
| 58 } | |
| 59 accept(node); | |
| 60 first = false; | |
| 61 } | |
| 62 p(">"); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 @Override | |
| 67 public Void visitLibraryDirective(DartLibraryDirective node) { | |
| 68 if (node.isObsoleteFormat()) { | |
| 69 p("#library("); | |
| 70 accept(node.getName()); | |
| 71 p(");"); | |
| 72 nl(); | |
| 73 } else { | |
| 74 p("library "); | |
| 75 accept(node.getName()); | |
| 76 p(";"); | |
| 77 nl(); | |
| 78 } | |
| 79 return null; | |
| 80 } | |
| 81 | |
| 82 @SuppressWarnings("deprecation") | |
| 83 @Override | |
| 84 public Void visitImportDirective(DartImportDirective node) { | |
| 85 if (node.isObsoleteFormat()) { | |
| 86 p("#import("); | |
| 87 accept(node.getLibraryUri()); | |
| 88 if (node.getOldPrefix() != null) { | |
| 89 p(", prefix : "); | |
| 90 accept(node.getOldPrefix()); | |
| 91 } | |
| 92 p(");"); | |
| 93 nl(); | |
| 94 } else { | |
| 95 p("import "); | |
| 96 accept(node.getLibraryUri()); | |
| 97 if (node.getPrefix() != null) { | |
| 98 p(" as "); | |
| 99 accept(node.getPrefix()); | |
| 100 } | |
| 101 for (ImportCombinator combinator : node.getCombinators()) { | |
| 102 accept(combinator); | |
| 103 } | |
| 104 if (node.isExported()) { | |
| 105 p(" & export"); | |
| 106 } | |
| 107 p(";"); | |
| 108 nl(); | |
| 109 } | |
| 110 return null; | |
| 111 } | |
| 112 | |
| 113 @Override | |
| 114 public Void visitFunctionTypeAlias(DartFunctionTypeAlias x) { | |
| 115 p("typedef "); | |
| 116 | |
| 117 if (x.getReturnTypeNode() != null) { | |
| 118 accept(x.getReturnTypeNode()); | |
| 119 } | |
| 120 | |
| 121 p(" "); | |
| 122 accept(x.getName()); | |
| 123 pTypeParameters(x.getTypeParameters()); | |
| 124 | |
| 125 p("("); | |
| 126 printSeparatedByComma(x.getParameters()); | |
| 127 p(")"); | |
| 128 | |
| 129 p(";"); | |
| 130 nl(); | |
| 131 nl(); | |
| 132 return null; | |
| 133 } | |
| 134 | |
| 135 @Override | |
| 136 public Void visitClass(DartClass x) { | |
| 137 if (x.isInterface()) { | |
| 138 p("interface "); | |
| 139 } else { | |
| 140 p("class "); | |
| 141 } | |
| 142 accept(x.getName()); | |
| 143 pTypeParameters(x.getTypeParameters()); | |
| 144 | |
| 145 if (x.getSuperclass() != null) { | |
| 146 p(" extends "); | |
| 147 accept(x.getSuperclass()); | |
| 148 } | |
| 149 | |
| 150 List<DartTypeNode> interfaces = x.getInterfaces(); | |
| 151 if (interfaces != null && !interfaces.isEmpty()) { | |
| 152 if (x.isInterface()) { | |
| 153 p(" extends "); | |
| 154 } else { | |
| 155 p(" implements "); | |
| 156 } | |
| 157 boolean first = true; | |
| 158 for (DartTypeNode cls : interfaces) { | |
| 159 if (!first) { | |
| 160 p(", "); | |
| 161 } | |
| 162 accept(cls); | |
| 163 first = false; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 if (x.getNativeName() != null) { | |
| 168 p(" native "); | |
| 169 accept(x.getNativeName()); | |
| 170 } | |
| 171 | |
| 172 if (x.getDefaultClass() != null) { | |
| 173 p(" default "); | |
| 174 accept(x.getDefaultClass()); | |
| 175 } | |
| 176 | |
| 177 p(" {"); | |
| 178 nl(); | |
| 179 indent(); | |
| 180 | |
| 181 acceptList(x.getMembers()); | |
| 182 | |
| 183 outdent(); | |
| 184 p("}"); | |
| 185 nl(); | |
| 186 nl(); | |
| 187 return null; | |
| 188 } | |
| 189 | |
| 190 @Override | |
| 191 public Void visitTypeNode(DartTypeNode x) { | |
| 192 accept(x.getIdentifier()); | |
| 193 List<DartTypeNode> arguments = x.getTypeArguments(); | |
| 194 if (arguments != null && !arguments.isEmpty()) { | |
| 195 p("<"); | |
| 196 printSeparatedByComma(arguments); | |
| 197 p(">"); | |
| 198 } | |
| 199 return null; | |
| 200 } | |
| 201 | |
| 202 @Override | |
| 203 public Void visitTypeParameter(DartTypeParameter x) { | |
| 204 accept(x.getName()); | |
| 205 DartTypeNode bound = x.getBound(); | |
| 206 if (bound != null) { | |
| 207 p(" extends "); | |
| 208 accept(bound); | |
| 209 } | |
| 210 return null; | |
| 211 } | |
| 212 | |
| 213 @Override | |
| 214 public Void visitFieldDefinition(DartFieldDefinition x) { | |
| 215 Modifiers modifiers = x.getFields().get(0).getModifiers(); | |
| 216 if (modifiers.isAbstractField()) { | |
| 217 pAbstractField(x); | |
| 218 } else { | |
| 219 pFieldModifiers(x); | |
| 220 if (x.getTypeNode() != null) { | |
| 221 accept(x.getTypeNode()); | |
| 222 p(" "); | |
| 223 } else { | |
| 224 if (!modifiers.isFinal()) { | |
| 225 p("var "); | |
| 226 } | |
| 227 } | |
| 228 printSeparatedByComma(x.getFields()); | |
| 229 p(";"); | |
| 230 } | |
| 231 | |
| 232 nl(); | |
| 233 | |
| 234 return null; | |
| 235 } | |
| 236 | |
| 237 @Override | |
| 238 public Void visitField(DartField x) { | |
| 239 accept(x.getName()); | |
| 240 if (x.getValue() != null) { | |
| 241 p(" = "); | |
| 242 accept(x.getValue()); | |
| 243 } | |
| 244 return null; | |
| 245 } | |
| 246 | |
| 247 @Override | |
| 248 public Void visitParameter(DartParameter x) { | |
| 249 if (x.getModifiers().isFinal()) { | |
| 250 p("final "); | |
| 251 } | |
| 252 if (x.getTypeNode() != null) { | |
| 253 accept(x.getTypeNode()); | |
| 254 p(" "); | |
| 255 } | |
| 256 accept(x.getName()); | |
| 257 if (x.getFunctionParameters() != null) { | |
| 258 p("("); | |
| 259 printSeparatedByComma(x.getFunctionParameters()); | |
| 260 p(")"); | |
| 261 } | |
| 262 if (x.getDefaultExpr() != null) { | |
| 263 if (x.getModifiers().isOptional()) { | |
| 264 p(" = "); | |
| 265 } | |
| 266 if (x.getModifiers().isNamed()) { | |
| 267 p(" : "); | |
| 268 } | |
| 269 accept(x.getDefaultExpr()); | |
| 270 } | |
| 271 return null; | |
| 272 } | |
| 273 | |
| 274 @Override | |
| 275 public Void visitMethodDefinition(DartMethodDefinition x) { | |
| 276 nl(); | |
| 277 pMethodModifiers(x); | |
| 278 // return type | |
| 279 DartFunction func = x.getFunction(); | |
| 280 if (func.getReturnTypeNode() != null) { | |
| 281 accept(func.getReturnTypeNode()); | |
| 282 p(" "); | |
| 283 } | |
| 284 // special methods | |
| 285 if (x.getModifiers().isOperator()) { | |
| 286 p("operator "); | |
| 287 } else if (x.getModifiers().isGetter()) { | |
| 288 p("get "); | |
| 289 } else if (x.getModifiers().isSetter()) { | |
| 290 p("set "); | |
| 291 } | |
| 292 // name | |
| 293 pFunctionDeclaration(x.getName(), func, !x.getModifiers().isGetter()); | |
| 294 p(" "); | |
| 295 // initializers | |
| 296 List<DartInitializer> inits = x.getInitializers(); | |
| 297 if (!inits.isEmpty()) { | |
| 298 p(": "); | |
| 299 for (int i = 0; i < inits.size(); ++i) { | |
| 300 accept(inits.get(i)); | |
| 301 if (i < inits.size() - 1) { | |
| 302 p(", "); | |
| 303 } | |
| 304 } | |
| 305 } | |
| 306 // body | |
| 307 if (x.getFunction().getBody() != null) { | |
| 308 accept(x.getFunction().getBody()); | |
| 309 } else if (x.getRedirectedTypeName() != null) { | |
| 310 p(" = "); | |
| 311 accept(x.getRedirectedTypeName()); | |
| 312 if (x.getRedirectedConstructorName() != null) { | |
| 313 p("."); | |
| 314 accept(x.getRedirectedConstructorName()); | |
| 315 } | |
| 316 p(";"); | |
| 317 nl(); | |
| 318 } else { | |
| 319 p(";"); | |
| 320 nl(); | |
| 321 } | |
| 322 // done | |
| 323 return null; | |
| 324 } | |
| 325 | |
| 326 @Override | |
| 327 public Void visitInitializer(DartInitializer x) { | |
| 328 if (!x.isInvocation()) { | |
| 329 p("this."); | |
| 330 p(x.getInitializerName()); | |
| 331 p(" = "); | |
| 332 } | |
| 333 accept(x.getValue()); | |
| 334 return null; | |
| 335 } | |
| 336 | |
| 337 private void pBlock(DartBlock x, boolean newline) { | |
| 338 p("{"); | |
| 339 nl(); | |
| 340 | |
| 341 indent(); | |
| 342 acceptList(x.getStatements()); | |
| 343 outdent(); | |
| 344 | |
| 345 p("}"); | |
| 346 if (newline) { | |
| 347 nl(); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 private void pFunctionDeclaration(DartNode name, DartFunction x, boolean inclu
deParameters) { | |
| 352 if (name != null) { | |
| 353 accept(name); | |
| 354 } | |
| 355 if (includeParameters) { | |
| 356 p("("); | |
| 357 pFormalParameters(x.getParameters()); | |
| 358 p(")"); | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 private void pFormalParameters(List<DartParameter> params) { | |
| 363 boolean first = true, hasPositional = false, hasNamed = false; | |
| 364 for (DartParameter param : params) { | |
| 365 if (!first) { | |
| 366 p(", "); | |
| 367 } | |
| 368 if (!hasPositional && param.getModifiers().isOptional()) { | |
| 369 hasPositional = true; | |
| 370 p("["); | |
| 371 } | |
| 372 if (!hasNamed && param.getModifiers().isNamed()) { | |
| 373 hasNamed = true; | |
| 374 p("{"); | |
| 375 } | |
| 376 accept(param); | |
| 377 first = false; | |
| 378 } | |
| 379 if (hasPositional) { | |
| 380 p("]"); | |
| 381 } | |
| 382 if (hasNamed) { | |
| 383 p("}"); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 @Override | |
| 388 public Void visitAssertStatement(DartAssertStatement x) { | |
| 389 p("assert("); | |
| 390 accept(x.getCondition()); | |
| 391 p(");"); | |
| 392 return null; | |
| 393 } | |
| 394 | |
| 395 @Override | |
| 396 public Void visitBlock(DartBlock x) { | |
| 397 pBlock(x, true); | |
| 398 return null; | |
| 399 } | |
| 400 | |
| 401 @Override | |
| 402 public Void visitIfStatement(DartIfStatement x) { | |
| 403 p("if ("); | |
| 404 accept(x.getCondition()); | |
| 405 p(") "); | |
| 406 pIfBlock(x.getThenStatement(), x.getElseStatement() == null); | |
| 407 if (x.getElseStatement() != null) { | |
| 408 p(" else "); | |
| 409 pIfBlock(x.getElseStatement(), true); | |
| 410 } | |
| 411 return null; | |
| 412 } | |
| 413 | |
| 414 @Override | |
| 415 public Void visitSwitchStatement(DartSwitchStatement x) { | |
| 416 p("switch ("); | |
| 417 accept(x.getExpression()); | |
| 418 p(") {"); | |
| 419 nl(); | |
| 420 | |
| 421 indent(); | |
| 422 acceptList(x.getMembers()); | |
| 423 outdent(); | |
| 424 | |
| 425 p("}"); | |
| 426 nl(); | |
| 427 return null; | |
| 428 } | |
| 429 | |
| 430 @Override | |
| 431 public Void visitCase(DartCase x) { | |
| 432 p("case "); | |
| 433 accept(x.getExpr()); | |
| 434 p(":"); | |
| 435 nl(); | |
| 436 indent(); | |
| 437 acceptList(x.getStatements()); | |
| 438 outdent(); | |
| 439 return null; | |
| 440 } | |
| 441 | |
| 442 @Override | |
| 443 public Void visitDefault(DartDefault x) { | |
| 444 p("default:"); | |
| 445 nl(); | |
| 446 indent(); | |
| 447 acceptList(x.getStatements()); | |
| 448 outdent(); | |
| 449 return null; | |
| 450 } | |
| 451 | |
| 452 @Override | |
| 453 public Void visitWhileStatement(DartWhileStatement x) { | |
| 454 p("while ("); | |
| 455 accept(x.getCondition()); | |
| 456 p(") "); | |
| 457 pIfBlock(x.getBody(), true); | |
| 458 return null; | |
| 459 } | |
| 460 | |
| 461 @Override | |
| 462 public Void visitDoWhileStatement(DartDoWhileStatement x) { | |
| 463 p("do "); | |
| 464 pIfBlock(x.getBody(), false); | |
| 465 p(" while ("); | |
| 466 accept(x.getCondition()); | |
| 467 p(");"); | |
| 468 nl(); | |
| 469 return null; | |
| 470 } | |
| 471 | |
| 472 @Override | |
| 473 public Void visitForStatement(DartForStatement x) { | |
| 474 p("for ("); | |
| 475 | |
| 476 // Setup | |
| 477 DartStatement setup = x.getInit(); | |
| 478 if (setup != null) { | |
| 479 if (setup instanceof DartVariableStatement) { | |
| 480 // Special case to avoid an extra semicolon & newline after the var | |
| 481 // statement. | |
| 482 p("var "); | |
| 483 printSeparatedByComma(((DartVariableStatement) setup).getVariables()); | |
| 484 } else { | |
| 485 // Plain old expression. | |
| 486 assert setup instanceof DartExprStmt; | |
| 487 accept(((DartExprStmt) setup).getExpression()); | |
| 488 } | |
| 489 } | |
| 490 p("; "); | |
| 491 | |
| 492 // Condition | |
| 493 if (x.getCondition() != null) { | |
| 494 accept(x.getCondition()); | |
| 495 } | |
| 496 p("; "); | |
| 497 | |
| 498 // Next | |
| 499 if (x.getIncrement() != null) { | |
| 500 accept(x.getIncrement()); | |
| 501 } | |
| 502 p(") "); | |
| 503 | |
| 504 // Body | |
| 505 accept(x.getBody()); | |
| 506 nl(); | |
| 507 return null; | |
| 508 } | |
| 509 | |
| 510 @Override | |
| 511 public Void visitForInStatement(DartForInStatement x) { | |
| 512 p("for ("); | |
| 513 if (x.introducesVariable()) { | |
| 514 DartTypeNode type = x.getVariableStatement().getTypeNode(); | |
| 515 if (type != null) { | |
| 516 accept(type); | |
| 517 p(" "); | |
| 518 } else { | |
| 519 p("var "); | |
| 520 } | |
| 521 printSeparatedByComma(x.getVariableStatement().getVariables()); | |
| 522 } else { | |
| 523 accept(x.getIdentifier()); | |
| 524 } | |
| 525 | |
| 526 p(" in "); | |
| 527 | |
| 528 // iterable | |
| 529 accept(x.getIterable()); | |
| 530 p(") "); | |
| 531 | |
| 532 // Body | |
| 533 accept(x.getBody()); | |
| 534 nl(); | |
| 535 return null; | |
| 536 } | |
| 537 | |
| 538 @Override | |
| 539 public Void visitContinueStatement(DartContinueStatement x) { | |
| 540 p("continue"); | |
| 541 if (x.getTargetName() != null) { | |
| 542 p(" " + x.getTargetName()); | |
| 543 } | |
| 544 p(";"); | |
| 545 nl(); | |
| 546 return null; | |
| 547 } | |
| 548 | |
| 549 @Override | |
| 550 public Void visitBreakStatement(DartBreakStatement x) { | |
| 551 p("break"); | |
| 552 if (x.getTargetName() != null) { | |
| 553 p(" " + x.getTargetName()); | |
| 554 } | |
| 555 p(";"); | |
| 556 nl(); | |
| 557 return null; | |
| 558 } | |
| 559 | |
| 560 @Override | |
| 561 public Void visitReturnStatement(DartReturnStatement x) { | |
| 562 p("return"); | |
| 563 if (x.getValue() != null) { | |
| 564 p(" "); | |
| 565 accept(x.getValue()); | |
| 566 } | |
| 567 p(";"); | |
| 568 nl(); | |
| 569 return null; | |
| 570 } | |
| 571 | |
| 572 @Override | |
| 573 public Void visitTryStatement(DartTryStatement x) { | |
| 574 p("try "); | |
| 575 accept(x.getTryBlock()); | |
| 576 acceptList(x.getCatchBlocks()); | |
| 577 if (x.getFinallyBlock() != null) { | |
| 578 p("finally "); | |
| 579 accept(x.getFinallyBlock()); | |
| 580 } | |
| 581 return null; | |
| 582 } | |
| 583 | |
| 584 @Override | |
| 585 public Void visitCatchBlock(DartCatchBlock x) { | |
| 586 DartParameter catchParameter = x.getException(); | |
| 587 DartTypeNode type = catchParameter.getTypeNode(); | |
| 588 if (type != null) { | |
| 589 p("on "); | |
| 590 accept(type); | |
| 591 p(" "); | |
| 592 } | |
| 593 p("catch ("); | |
| 594 accept(catchParameter.getName()); | |
| 595 if (x.getStackTrace() != null) { | |
| 596 p(", "); | |
| 597 accept(x.getStackTrace()); | |
| 598 } | |
| 599 p(") "); | |
| 600 accept(x.getBlock()); | |
| 601 return null; | |
| 602 } | |
| 603 | |
| 604 @Override | |
| 605 public Void visitThrowExpression(DartThrowExpression x) { | |
| 606 p("throw"); | |
| 607 if (x.getException() != null) { | |
| 608 p(" "); | |
| 609 accept(x.getException()); | |
| 610 } | |
| 611 return null; | |
| 612 } | |
| 613 | |
| 614 @Override | |
| 615 public Void visitVariableStatement(DartVariableStatement x) { | |
| 616 if (x.getTypeNode() != null) { | |
| 617 accept(x.getTypeNode()); | |
| 618 p(" "); | |
| 619 } else { | |
| 620 p("var "); | |
| 621 } | |
| 622 printSeparatedByComma(x.getVariables()); | |
| 623 p(";"); | |
| 624 nl(); | |
| 625 return null; | |
| 626 } | |
| 627 | |
| 628 @Override | |
| 629 public Void visitVariable(DartVariable x) { | |
| 630 accept(x.getName()); | |
| 631 if (x.getValue() != null) { | |
| 632 p(" = "); | |
| 633 accept(x.getValue()); | |
| 634 } | |
| 635 return null; | |
| 636 } | |
| 637 | |
| 638 @Override | |
| 639 public Void visitEmptyStatement(DartEmptyStatement x) { | |
| 640 p(";"); | |
| 641 nl(); | |
| 642 return null; | |
| 643 } | |
| 644 | |
| 645 @Override | |
| 646 public Void visitLabel(DartLabel x) { | |
| 647 p(x.getName()); | |
| 648 p(": "); | |
| 649 accept(x.getStatement()); | |
| 650 return null; | |
| 651 } | |
| 652 | |
| 653 @Override | |
| 654 public Void visitExprStmt(DartExprStmt x) { | |
| 655 accept(x.getExpression()); | |
| 656 p(";"); | |
| 657 nl(); | |
| 658 return null; | |
| 659 } | |
| 660 | |
| 661 @Override | |
| 662 public Void visitBinaryExpression(DartBinaryExpression x) { | |
| 663 accept(x.getArg1()); | |
| 664 p(" "); | |
| 665 p(x.getOperator().getSyntax()); | |
| 666 p(" "); | |
| 667 accept(x.getArg2()); | |
| 668 return null; | |
| 669 } | |
| 670 | |
| 671 @Override | |
| 672 public Void visitConditional(DartConditional x) { | |
| 673 accept(x.getCondition()); | |
| 674 p(" ? "); | |
| 675 accept(x.getThenExpression()); | |
| 676 p(" : "); | |
| 677 accept(x.getElseExpression()); | |
| 678 return null; | |
| 679 } | |
| 680 | |
| 681 @Override | |
| 682 public Void visitUnaryExpression(DartUnaryExpression x) { | |
| 683 if (x.isPrefix()) { | |
| 684 p(x.getOperator().getSyntax()); | |
| 685 } | |
| 686 accept(x.getArg()); | |
| 687 if (!x.isPrefix()) { | |
| 688 p(x.getOperator().getSyntax()); | |
| 689 } | |
| 690 return null; | |
| 691 } | |
| 692 | |
| 693 @Override | |
| 694 public Void visitPropertyAccess(DartPropertyAccess x) { | |
| 695 if (x.getQualifier() != null) { | |
| 696 accept(x.getQualifier()); | |
| 697 } | |
| 698 if (x.isCascade()) { | |
| 699 p(".."); | |
| 700 } else { | |
| 701 p("."); | |
| 702 } | |
| 703 p(x.getPropertyName()); | |
| 704 return null; | |
| 705 } | |
| 706 | |
| 707 @Override | |
| 708 public Void visitCascadeExpression(DartCascadeExpression x) { | |
| 709 accept(x.getTarget()); | |
| 710 acceptList(x.getCascadeSections()); | |
| 711 return null; | |
| 712 } | |
| 713 | |
| 714 @Override | |
| 715 public Void visitArrayAccess(DartArrayAccess x) { | |
| 716 if (x.isCascade()) { | |
| 717 p(".."); | |
| 718 } else { | |
| 719 accept(x.getTarget()); | |
| 720 } | |
| 721 p("["); | |
| 722 accept(x.getKey()); | |
| 723 p("]"); | |
| 724 return null; | |
| 725 } | |
| 726 | |
| 727 private void pArgs(List<? extends DartNode> args) { | |
| 728 p("("); | |
| 729 printSeparatedByComma(args); | |
| 730 p(")"); | |
| 731 } | |
| 732 | |
| 733 @Override | |
| 734 public Void visitUnqualifiedInvocation(DartUnqualifiedInvocation x) { | |
| 735 accept(x.getTarget()); | |
| 736 pArgs(x.getArguments()); | |
| 737 return null; | |
| 738 } | |
| 739 | |
| 740 @Override | |
| 741 public Void visitFunctionObjectInvocation(DartFunctionObjectInvocation x) { | |
| 742 accept(x.getTarget()); | |
| 743 pArgs(x.getArguments()); | |
| 744 return null; | |
| 745 } | |
| 746 | |
| 747 @Override | |
| 748 public Void visitMethodInvocation(DartMethodInvocation x) { | |
| 749 if (x.isCascade()) { | |
| 750 p(".."); | |
| 751 } else { | |
| 752 accept(x.getTarget()); | |
| 753 p("."); | |
| 754 } | |
| 755 accept(x.getFunctionName()); | |
| 756 pArgs(x.getArguments()); | |
| 757 return null; | |
| 758 } | |
| 759 | |
| 760 @Override | |
| 761 public Void visitSyntheticErrorExpression(DartSyntheticErrorExpression node) { | |
| 762 p("[error: " + node.getTokenString() + "]"); | |
| 763 return null; | |
| 764 } | |
| 765 | |
| 766 @Override | |
| 767 public Void visitSyntheticErrorStatement(DartSyntheticErrorStatement node) { | |
| 768 p("[error: " + node.getTokenString() + "]"); | |
| 769 return null; | |
| 770 } | |
| 771 | |
| 772 @Override | |
| 773 public Void visitThisExpression(DartThisExpression x) { | |
| 774 p("this"); | |
| 775 return null; | |
| 776 } | |
| 777 | |
| 778 @Override | |
| 779 public Void visitSuperExpression(DartSuperExpression x) { | |
| 780 p("super"); | |
| 781 return null; | |
| 782 } | |
| 783 | |
| 784 @Override | |
| 785 public Void visitSuperConstructorInvocation(DartSuperConstructorInvocation x)
{ | |
| 786 p("super"); | |
| 787 if (x.getName() != null) { | |
| 788 p("."); | |
| 789 accept(x.getName()); | |
| 790 } | |
| 791 pArgs(x.getArguments()); | |
| 792 return null; | |
| 793 } | |
| 794 | |
| 795 @Override | |
| 796 public Void visitNewExpression(DartNewExpression x) { | |
| 797 if (x.isConst()) { | |
| 798 p("const "); | |
| 799 } else { | |
| 800 p("new "); | |
| 801 } | |
| 802 accept(x.getConstructor()); | |
| 803 pArgs(x.getArguments()); | |
| 804 return null; | |
| 805 } | |
| 806 | |
| 807 @Override | |
| 808 public Void visitFunctionExpression(DartFunctionExpression x) { | |
| 809 DartFunction func = x.getFunction(); | |
| 810 if (func.getReturnTypeNode() != null) { | |
| 811 accept(func.getReturnTypeNode()); | |
| 812 p(" "); | |
| 813 } | |
| 814 DartIdentifier name = x.getName(); | |
| 815 pFunctionDeclaration(name, x.getFunction(), true); | |
| 816 p(" "); | |
| 817 if (x.getFunction().getBody() != null) { | |
| 818 pBlock(x.getFunction().getBody(), false); | |
| 819 } | |
| 820 return null; | |
| 821 } | |
| 822 | |
| 823 @Override | |
| 824 public Void visitIdentifier(DartIdentifier x) { | |
| 825 p(x.getName()); | |
| 826 return null; | |
| 827 } | |
| 828 | |
| 829 @Override | |
| 830 public Void visitNullLiteral(DartNullLiteral x) { | |
| 831 p("null"); | |
| 832 return null; | |
| 833 } | |
| 834 | |
| 835 @Override | |
| 836 public Void visitRedirectConstructorInvocation(DartRedirectConstructorInvocati
on x) { | |
| 837 p("this"); | |
| 838 if (x.getName() != null) { | |
| 839 p("."); | |
| 840 accept(x.getName()); | |
| 841 } | |
| 842 pArgs(x.getArguments()); | |
| 843 return null; | |
| 844 } | |
| 845 | |
| 846 @Override | |
| 847 public Void visitStringLiteral(DartStringLiteral x) { | |
| 848 if (x.getValue() == null) { | |
| 849 return null; | |
| 850 } | |
| 851 p("\""); | |
| 852 // 'replaceAll' takes regular expressions as first argument and parses the s
econd argument | |
| 853 // for captured groups. We must escape backslashes twice: once to escape the
m in the source | |
| 854 // code and once for the regular expression parser. | |
| 855 String escaped = x.getValue().replaceAll("\\\\", "\\\\\\\\"); | |
| 856 escaped = escaped.replaceAll("\"", "\\\\\""); | |
| 857 escaped = escaped.replaceAll("'", "\\\\'"); | |
| 858 escaped = escaped.replaceAll("\\n", "\\\\n"); | |
| 859 // In the replacement string '$' is used to refer to captured groups. We hav
e to escape the | |
| 860 // dollar. | |
| 861 escaped = escaped.replaceAll("\\$", "\\\\\\$"); | |
| 862 p(escaped); | |
| 863 p("\""); | |
| 864 return null; | |
| 865 } | |
| 866 | |
| 867 @Override | |
| 868 public Void visitStringInterpolation(DartStringInterpolation x) { | |
| 869 p("\""); | |
| 870 // do not use the default visitor recursion, instead alternate strings and | |
| 871 // expressions: | |
| 872 Iterator<DartExpression> eIter = x.getExpressions().iterator(); | |
| 873 boolean first = true; | |
| 874 for (DartStringLiteral lit : x.getStrings()) { | |
| 875 if (first) { | |
| 876 first = false; | |
| 877 } else { | |
| 878 p("${"); | |
| 879 assert eIter.hasNext() : "DartStringInterpolation invariant broken."; | |
| 880 accept(eIter.next()); | |
| 881 p("}"); | |
| 882 } | |
| 883 p(lit.getValue().replaceAll("\"", "\\\"")); | |
| 884 } | |
| 885 p("\""); | |
| 886 return null; | |
| 887 } | |
| 888 | |
| 889 @Override | |
| 890 public Void visitBooleanLiteral(DartBooleanLiteral x) { | |
| 891 p(Boolean.toString(x.getValue())); | |
| 892 return null; | |
| 893 } | |
| 894 | |
| 895 @Override | |
| 896 public Void visitIntegerLiteral(DartIntegerLiteral x) { | |
| 897 p(x.getValue().toString()); | |
| 898 return null; | |
| 899 } | |
| 900 | |
| 901 @Override | |
| 902 public Void visitDoubleLiteral(DartDoubleLiteral x) { | |
| 903 p(Double.toString(x.getValue())); | |
| 904 return null; | |
| 905 } | |
| 906 | |
| 907 @Override | |
| 908 public Void visitArrayLiteral(DartArrayLiteral x) { | |
| 909 List<DartTypeNode> typeArguments = x.getTypeArguments(); | |
| 910 if (typeArguments != null && typeArguments.size() > 0) { | |
| 911 p("<"); | |
| 912 printSeparatedByComma(typeArguments); | |
| 913 p(">"); | |
| 914 } | |
| 915 p("["); | |
| 916 printSeparatedByComma(x.getExpressions()); | |
| 917 p("]"); | |
| 918 return null; | |
| 919 } | |
| 920 | |
| 921 @Override | |
| 922 public Void visitMapLiteral(DartMapLiteral x) { | |
| 923 List<DartTypeNode> typeArguments = x.getTypeArguments(); | |
| 924 if (typeArguments != null && typeArguments.size() > 0) { | |
| 925 p("<"); | |
| 926 printSeparatedByComma(typeArguments); | |
| 927 p(">"); | |
| 928 } | |
| 929 p("{"); | |
| 930 List<DartMapLiteralEntry> entries = x.getEntries(); | |
| 931 for (int i = 0; i < entries.size(); ++i) { | |
| 932 DartMapLiteralEntry entry = entries.get(i); | |
| 933 accept(entry); | |
| 934 if (i < entries.size() - 1) { | |
| 935 p(", "); | |
| 936 } | |
| 937 } | |
| 938 p("}"); | |
| 939 return null; | |
| 940 } | |
| 941 | |
| 942 @Override | |
| 943 public Void visitMapLiteralEntry(DartMapLiteralEntry x) { | |
| 944 // Always quote keys just to be safe. This could be optimized to only quote | |
| 945 // unsafe identifiers. | |
| 946 accept(x.getKey()); | |
| 947 p(" : "); | |
| 948 accept(x.getValue()); | |
| 949 return null; | |
| 950 } | |
| 951 | |
| 952 @Override | |
| 953 public Void visitParameterizedTypeNode(DartParameterizedTypeNode x) { | |
| 954 accept(x.getExpression()); | |
| 955 if (!x.getTypeParameters().isEmpty()) { | |
| 956 p("<"); | |
| 957 printSeparatedByComma(x.getTypeParameters()); | |
| 958 p(">"); | |
| 959 } | |
| 960 return null; | |
| 961 } | |
| 962 | |
| 963 @Override | |
| 964 public Void visitParenthesizedExpression(DartParenthesizedExpression x) { | |
| 965 p("("); | |
| 966 accept(x.getExpression()); | |
| 967 p(")"); | |
| 968 return null; | |
| 969 } | |
| 970 | |
| 971 @Override | |
| 972 public Void visitNamedExpression(DartNamedExpression x) { | |
| 973 accept(x.getName()); | |
| 974 p(":"); | |
| 975 accept(x.getExpression()); | |
| 976 return null; | |
| 977 } | |
| 978 | |
| 979 private void pAbstractField(DartFieldDefinition x) { | |
| 980 accept(x.getFields().get(0).getAccessor()); | |
| 981 } | |
| 982 | |
| 983 private void pIfBlock(DartStatement stmt, boolean newline) { | |
| 984 if (stmt instanceof DartBlock) { | |
| 985 pBlock((DartBlock) stmt, newline); | |
| 986 } else { | |
| 987 p("{"); | |
| 988 nl(); | |
| 989 indent(); | |
| 990 accept(stmt); | |
| 991 outdent(); | |
| 992 p("}"); | |
| 993 if (newline) { | |
| 994 nl(); | |
| 995 } | |
| 996 } | |
| 997 } | |
| 998 | |
| 999 private void printSeparatedByComma(List<? extends DartNode> nodes) { | |
| 1000 boolean first = true; | |
| 1001 for (DartNode node : nodes) { | |
| 1002 if (!first) { | |
| 1003 p(", "); | |
| 1004 } | |
| 1005 accept(node); | |
| 1006 first = false; | |
| 1007 } | |
| 1008 } | |
| 1009 | |
| 1010 private void pFieldModifiers(DartFieldDefinition field) { | |
| 1011 Modifiers modifiers = field.getFields().get(0).getModifiers(); | |
| 1012 if (modifiers.isStatic()) { | |
| 1013 p("static "); | |
| 1014 } | |
| 1015 if (modifiers.isFinal()) { | |
| 1016 p("final "); | |
| 1017 } | |
| 1018 } | |
| 1019 | |
| 1020 private void pMethodModifiers(DartMethodDefinition method) { | |
| 1021 if (method.getModifiers().isConstant()) { | |
| 1022 p("const "); | |
| 1023 } | |
| 1024 if (method.getModifiers().isStatic()) { | |
| 1025 p("static "); | |
| 1026 } | |
| 1027 if (method.getModifiers().isAbstract()) { | |
| 1028 p("abstract "); | |
| 1029 } | |
| 1030 if (method.getModifiers().isFactory()) { | |
| 1031 p("factory "); | |
| 1032 } | |
| 1033 } | |
| 1034 | |
| 1035 private void p(String x) { | |
| 1036 out.print(x); | |
| 1037 } | |
| 1038 | |
| 1039 private void nl() { | |
| 1040 out.newline(); | |
| 1041 } | |
| 1042 | |
| 1043 private void indent() { | |
| 1044 out.indentIn(); | |
| 1045 } | |
| 1046 | |
| 1047 private void outdent() { | |
| 1048 out.indentOut(); | |
| 1049 } | |
| 1050 } | |
| OLD | NEW |