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

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

Issue 1183033004: js_ast: implement rest/spread parsing (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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/src/js/precedence.dart ('k') | lib/src/js/template.dart » ('j') | 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 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 newInForInit: inForInit, newAtStatementBegin: false); 616 newInForInit: inForInit, newAtStatementBegin: false);
617 } 617 }
618 618
619 visitNew(New node) { 619 visitNew(New node) {
620 out("new "); 620 out("new ");
621 inNewTarget = true; 621 inNewTarget = true;
622 visitNestedExpression(node.target, ACCESS, 622 visitNestedExpression(node.target, ACCESS,
623 newInForInit: inForInit, newAtStatementBegin: false); 623 newInForInit: inForInit, newAtStatementBegin: false);
624 inNewTarget = false; 624 inNewTarget = false;
625 out("("); 625 out("(");
626 visitCommaSeparated(node.arguments, ASSIGNMENT, 626 visitCommaSeparated(node.arguments, SPREAD,
627 newInForInit: false, newAtStatementBegin: false); 627 newInForInit: false, newAtStatementBegin: false);
628 out(")"); 628 out(")");
629 } 629 }
630 630
631 visitCall(Call call) { 631 visitCall(Call call) {
632 visitNestedExpression(call.target, LEFT_HAND_SIDE, 632 visitNestedExpression(call.target, LEFT_HAND_SIDE,
633 newInForInit: inForInit, 633 newInForInit: inForInit,
634 newAtStatementBegin: atStatementBegin); 634 newAtStatementBegin: atStatementBegin);
635 out("("); 635 out("(");
636 visitCommaSeparated(call.arguments, ASSIGNMENT, 636 visitCommaSeparated(call.arguments, SPREAD,
637 newInForInit: false, newAtStatementBegin: false); 637 newInForInit: false, newAtStatementBegin: false);
638 out(")"); 638 out(")");
639 } 639 }
640 640
641 visitBinary(Binary binary) { 641 visitBinary(Binary binary) {
642 Expression left = binary.left; 642 Expression left = binary.left;
643 Expression right = binary.right; 643 Expression right = binary.right;
644 String op = binary.op; 644 String op = binary.op;
645 int leftPrecedenceRequirement; 645 int leftPrecedenceRequirement;
646 int rightPrecedenceRequirement; 646 int rightPrecedenceRequirement;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 out(op); 757 out(op);
758 break; 758 break;
759 case "-": 759 case "-":
760 case "--": 760 case "--":
761 if (lastCharCode == charCodes.$MINUS) out(" "); 761 if (lastCharCode == charCodes.$MINUS) out(" ");
762 out(op); 762 out(op);
763 break; 763 break;
764 default: 764 default:
765 out(op); 765 out(op);
766 } 766 }
767 visitNestedExpression(unary.argument, UNARY, 767 visitNestedExpression(unary.argument, unary.precedenceLevel,
768 newInForInit: inForInit, newAtStatementBegin: false); 768 newInForInit: inForInit, newAtStatementBegin: false);
769 } 769 }
770 770
771 visitSpread(Spread unary) => visitPrefix(unary);
772
771 visitPostfix(Postfix postfix) { 773 visitPostfix(Postfix postfix) {
772 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE, 774 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE,
773 newInForInit: inForInit, 775 newInForInit: inForInit,
774 newAtStatementBegin: atStatementBegin); 776 newAtStatementBegin: atStatementBegin);
775 out(postfix.op); 777 out(postfix.op);
776 } 778 }
777 779
778 visitThis(This node) { 780 visitThis(This node) {
779 out("this"); 781 out("this");
780 } 782 }
781 783
782 visitSuper(Super node) { 784 visitSuper(Super node) {
783 out("super"); 785 out("super");
784 } 786 }
785 787
786 visitIdentifier(Identifier node) { 788 visitIdentifier(Identifier node) {
787 out(localNamer.getName(node)); 789 out(localNamer.getName(node));
788 } 790 }
789 791
792 visitRestParameter(RestParameter node) {
793 out('...');
794 visitIdentifier(node.parameter);
795 }
796
790 bool isDigit(int charCode) { 797 bool isDigit(int charCode) {
791 return charCodes.$0 <= charCode && charCode <= charCodes.$9; 798 return charCodes.$0 <= charCode && charCode <= charCodes.$9;
792 } 799 }
793 800
794 bool isValidJavaScriptId(String field) { 801 bool isValidJavaScriptId(String field) {
795 if (field.length < 3) return false; 802 if (field.length < 3) return false;
796 // Ignore the leading and trailing string-delimiter. 803 // Ignore the leading and trailing string-delimiter.
797 for (int i = 1; i < field.length - 1; i++) { 804 for (int i = 1; i < field.length - 1; i++) {
798 // TODO(floitsch): allow more characters. 805 // TODO(floitsch): allow more characters.
799 int charCode = field.codeUnitAt(i); 806 int charCode = field.codeUnitAt(i);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 functionOut(namedFunction.function, namedFunction.name); 846 functionOut(namedFunction.function, namedFunction.name);
840 } 847 }
841 848
842 visitFun(Fun fun) { 849 visitFun(Fun fun) {
843 functionOut(fun, null); 850 functionOut(fun, null);
844 } 851 }
845 852
846 visitArrowFun(ArrowFun fun) { 853 visitArrowFun(ArrowFun fun) {
847 localNamer.enterScope(fun); 854 localNamer.enterScope(fun);
848 if (fun.params.length == 1) { 855 if (fun.params.length == 1) {
849 visitNestedExpression(fun.params.single, PRIMARY, 856 visitNestedExpression(fun.params.single, SPREAD,
850 newInForInit: false, newAtStatementBegin: false); 857 newInForInit: false, newAtStatementBegin: false);
851 } else { 858 } else {
852 out("("); 859 out("(");
853 visitCommaSeparated(fun.params, PRIMARY, 860 visitCommaSeparated(fun.params, SPREAD,
854 newInForInit: false, newAtStatementBegin: false); 861 newInForInit: false, newAtStatementBegin: false);
855 out(")"); 862 out(")");
856 } 863 }
857 spaceOut(); 864 spaceOut();
858 out("=>"); 865 out("=>");
859 if (fun.body is Expression) { 866 if (fun.body is Expression) {
860 spaceOut(); 867 spaceOut();
861 // Object initializers require parenthesis to disambiguate 868 // Object initializers require parenthesis to disambiguate
862 // AssignmentExpression from FunctionBody. See: 869 // AssignmentExpression from FunctionBody. See:
863 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-arrow-functio n-definitions 870 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-arrow-functio n-definitions
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 out('get '); 1032 out('get ');
1026 } else if (node.isSetter) { 1033 } else if (node.isSetter) {
1027 out('set '); 1034 out('set ');
1028 } 1035 }
1029 propertyNameOut(node.name, inMethod: true); 1036 propertyNameOut(node.name, inMethod: true);
1030 1037
1031 var fun = node.function; 1038 var fun = node.function;
1032 localNamer.enterScope(fun); 1039 localNamer.enterScope(fun);
1033 out("("); 1040 out("(");
1034 if (fun.params != null) { 1041 if (fun.params != null) {
1035 visitCommaSeparated(fun.params, PRIMARY, 1042 visitCommaSeparated(fun.params, SPREAD,
1036 newInForInit: false, newAtStatementBegin: false); 1043 newInForInit: false, newAtStatementBegin: false);
1037 } 1044 }
1038 out(")"); 1045 out(")");
1039 // TODO(jmesserly): async modifiers 1046 // TODO(jmesserly): async modifiers
1040 if (fun.body.statements.isEmpty) { 1047 if (fun.body.statements.isEmpty) {
1041 spaceOut(); 1048 spaceOut();
1042 out("{}"); 1049 out("{}");
1043 } else { 1050 } else {
1044 blockBody(fun.body, needsSeparation: false, needsNewline: false); 1051 blockBody(fun.body, needsSeparation: false, needsNewline: false);
1045 } 1052 }
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 return newName; 1419 return newName;
1413 } 1420 }
1414 } 1421 }
1415 1422
1416 /// Like [BaseVisitor], but calls [declare] for [Identifier] declarations, and 1423 /// Like [BaseVisitor], but calls [declare] for [Identifier] declarations, and
1417 /// [visitIdentifier] otherwise. 1424 /// [visitIdentifier] otherwise.
1418 abstract class VariableDeclarationVisitor<T> extends BaseVisitor<T> { 1425 abstract class VariableDeclarationVisitor<T> extends BaseVisitor<T> {
1419 declare(Identifier node); 1426 declare(Identifier node);
1420 1427
1421 visitFunctionExpression(FunctionExpression node) { 1428 visitFunctionExpression(FunctionExpression node) {
1422 for (Identifier param in node.params) declare(param); 1429 for (var p in node.params) {
1430 declare(p is RestParameter ? p.parameter : p);
1431 }
1423 node.body.accept(this); 1432 node.body.accept(this);
1424 } 1433 }
1425 1434
1426 visitVariableInitialization(VariableInitialization node) { 1435 visitVariableInitialization(VariableInitialization node) {
1427 declare(node.declaration); 1436 declare(node.declaration);
1428 if (node.value != null) node.value.accept(this); 1437 if (node.value != null) node.value.accept(this);
1429 } 1438 }
1430 1439
1431 visitCatch(Catch node) { 1440 visitCatch(Catch node) {
1432 declare(node.declaration); 1441 declare(node.declaration);
1433 node.body.accept(this); 1442 node.body.accept(this);
1434 } 1443 }
1435 1444
1436 visitFunctionDeclaration(FunctionDeclaration node) { 1445 visitFunctionDeclaration(FunctionDeclaration node) {
1437 declare(node.name); 1446 declare(node.name);
1438 node.function.accept(this); 1447 node.function.accept(this);
1439 } 1448 }
1440 1449
1441 visitNamedFunction(NamedFunction node) { 1450 visitNamedFunction(NamedFunction node) {
1442 declare(node.name); 1451 declare(node.name);
1443 node.function.accept(this); 1452 node.function.accept(this);
1444 } 1453 }
1445 1454
1446 visitClassExpression(ClassExpression node) { 1455 visitClassExpression(ClassExpression node) {
1447 declare(node.name); 1456 declare(node.name);
1448 if (node.heritage != null) node.heritage.accept(this); 1457 if (node.heritage != null) node.heritage.accept(this);
1449 for (Method element in node.methods) element.accept(this); 1458 for (Method element in node.methods) element.accept(this);
1450 } 1459 }
1451 } 1460 }
OLDNEW
« no previous file with comments | « lib/src/js/precedence.dart ('k') | lib/src/js/template.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698