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

Side by Side Diff: lib/src/js/printer.dart

Issue 1122283002: fixes #158, precendence of call and access in new (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: redesgin Created 5 years, 7 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
« no previous file with comments | « lib/runtime/dart/isolate.js ('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 (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of js_ast; 5 part of js_ast;
6 6
7 7
8 class JavaScriptPrintingOptions { 8 class JavaScriptPrintingOptions {
9 final bool shouldCompressOutput; 9 final bool shouldCompressOutput;
10 final bool minifyLocalVariables; 10 final bool minifyLocalVariables;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 class Printer implements NodeVisitor { 54 class Printer implements NodeVisitor {
55 final JavaScriptPrintingOptions options; 55 final JavaScriptPrintingOptions options;
56 final JavaScriptPrintingContext context; 56 final JavaScriptPrintingContext context;
57 final bool shouldCompressOutput; 57 final bool shouldCompressOutput;
58 final DanglingElseVisitor danglingElseVisitor; 58 final DanglingElseVisitor danglingElseVisitor;
59 final LocalNamer localNamer; 59 final LocalNamer localNamer;
60 60
61 bool inForInit = false; 61 bool inForInit = false;
62 bool atStatementBegin = false; 62 bool atStatementBegin = false;
63 bool inNewTarget = false;
63 bool pendingSemicolon = false; 64 bool pendingSemicolon = false;
64 bool pendingSpace = false; 65 bool pendingSpace = false;
65 66
66 // The current indentation level. 67 // The current indentation level.
67 int _indentLevel = 0; 68 int _indentLevel = 0;
68 // A cache of all indentation strings used so far. 69 // A cache of all indentation strings used so far.
69 List<String> _indentList = <String>[""]; 70 List<String> _indentList = <String>[""];
70 71
71 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); 72 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]');
72 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); 73 static final expressionContinuationRegExp = new RegExp(r'^[-+([]');
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 // for (a = (x in o); ... ; ... ) { ... } 554 // for (a = (x in o); ... ; ... ) { ... }
554 (newInForInit && node is Binary && node.op == "in") || 555 (newInForInit && node is Binary && node.op == "in") ||
555 // (function() { ... })(). 556 // (function() { ... })().
556 // ({a: 2, b: 3}.toString()). 557 // ({a: 2, b: 3}.toString()).
557 (newAtStatementBegin && (node is NamedFunction || 558 (newAtStatementBegin && (node is NamedFunction ||
558 node is FunctionExpression || 559 node is FunctionExpression ||
559 node is ObjectInitializer)); 560 node is ObjectInitializer));
560 if (needsParentheses) { 561 if (needsParentheses) {
561 inForInit = false; 562 inForInit = false;
562 atStatementBegin = false; 563 atStatementBegin = false;
564 inNewTarget = false;
563 out("("); 565 out("(");
564 visit(node); 566 visit(node);
565 out(")"); 567 out(")");
566 } else { 568 } else {
567 inForInit = newInForInit; 569 inForInit = newInForInit;
568 atStatementBegin = newAtStatementBegin; 570 atStatementBegin = newAtStatementBegin;
569 visit(node); 571 visit(node);
570 } 572 }
571 } 573 }
572 574
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 newInForInit: false, newAtStatementBegin: false); 611 newInForInit: false, newAtStatementBegin: false);
610 spaceOut(); 612 spaceOut();
611 out(":"); 613 out(":");
612 spaceOut(); 614 spaceOut();
613 visitNestedExpression(cond.otherwise, ASSIGNMENT, 615 visitNestedExpression(cond.otherwise, ASSIGNMENT,
614 newInForInit: inForInit, newAtStatementBegin: false); 616 newInForInit: inForInit, newAtStatementBegin: false);
615 } 617 }
616 618
617 visitNew(New node) { 619 visitNew(New node) {
618 out("new "); 620 out("new ");
621 inNewTarget = true;
619 visitNestedExpression(node.target, ACCESS, 622 visitNestedExpression(node.target, ACCESS,
620 newInForInit: inForInit, newAtStatementBegin: false); 623 newInForInit: inForInit, newAtStatementBegin: false);
624 inNewTarget = false;
621 out("("); 625 out("(");
622 visitCommaSeparated(node.arguments, ASSIGNMENT, 626 visitCommaSeparated(node.arguments, ASSIGNMENT,
623 newInForInit: false, newAtStatementBegin: false); 627 newInForInit: false, newAtStatementBegin: false);
624 out(")"); 628 out(")");
625 } 629 }
626 630
627 visitCall(Call call) { 631 visitCall(Call call) {
628 visitNestedExpression(call.target, LEFT_HAND_SIDE, 632 visitNestedExpression(call.target, LEFT_HAND_SIDE,
629 newInForInit: inForInit, 633 newInForInit: inForInit,
630 newAtStatementBegin: atStatementBegin); 634 newAtStatementBegin: atStatementBegin);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 } 806 }
803 } 807 }
804 808
805 // TODO(floitsch): normally we should also check that the field is not a 809 // TODO(floitsch): normally we should also check that the field is not a
806 // reserved word. We don't generate fields with reserved word names except 810 // reserved word. We don't generate fields with reserved word names except
807 // for 'super'. 811 // for 'super'.
808 return options.allowKeywordsInProperties || field != '"super"'; 812 return options.allowKeywordsInProperties || field != '"super"';
809 } 813 }
810 814
811 visitAccess(PropertyAccess access) { 815 visitAccess(PropertyAccess access) {
812 visitNestedExpression(access.receiver, CALL, 816 // Normally we can omit parens on the receiver if it is a Call, even though
817 // Call expressions have lower precedence. However this optimization doesn't
818 // work inside New expressions:
819 //
820 // new obj.foo().bar()
821 //
822 // This will be parsed as:
823 //
824 // (new obj.foo()).bar()
825 //
826 // Which is incorrect. So we must have parenthesis in this case:
827 //
828 // new (obj.foo()).bar()
829 //
830 int precedence = inNewTarget ? ACCESS : CALL;
831
832 visitNestedExpression(access.receiver, precedence,
813 newInForInit: inForInit, 833 newInForInit: inForInit,
814 newAtStatementBegin: atStatementBegin); 834 newAtStatementBegin: atStatementBegin);
815 propertyNameOut(access.selector, inAccess: true); 835 propertyNameOut(access.selector, inAccess: true);
816 } 836 }
817 837
818 visitNamedFunction(NamedFunction namedFunction) { 838 visitNamedFunction(NamedFunction namedFunction) {
819 functionOut(namedFunction.function, namedFunction.name); 839 functionOut(namedFunction.function, namedFunction.name);
820 } 840 }
821 841
822 visitFun(Fun fun) { 842 visitFun(Fun fun) {
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
1415 declare(node.name); 1435 declare(node.name);
1416 node.function.accept(this); 1436 node.function.accept(this);
1417 } 1437 }
1418 1438
1419 visitClassExpression(ClassExpression node) { 1439 visitClassExpression(ClassExpression node) {
1420 declare(node.name); 1440 declare(node.name);
1421 if (node.heritage != null) node.heritage.accept(this); 1441 if (node.heritage != null) node.heritage.accept(this);
1422 for (Method element in node.methods) element.accept(this); 1442 for (Method element in node.methods) element.accept(this);
1423 } 1443 }
1424 } 1444 }
OLDNEW
« no previous file with comments | « lib/runtime/dart/isolate.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698