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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js/nodes.dart

Issue 237583014: JS templates (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cleanup Created 6 years, 8 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
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; 5 part of js;
6 6
7 abstract class NodeVisitor<T> { 7 abstract class NodeVisitor<T> {
8 T visitProgram(Program node); 8 T visitProgram(Program node);
9 9
10 T visitBlock(Block node); 10 T visitBlock(Block node);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 57
58 T visitArrayInitializer(ArrayInitializer node); 58 T visitArrayInitializer(ArrayInitializer node);
59 T visitArrayElement(ArrayElement node); 59 T visitArrayElement(ArrayElement node);
60 T visitObjectInitializer(ObjectInitializer node); 60 T visitObjectInitializer(ObjectInitializer node);
61 T visitProperty(Property node); 61 T visitProperty(Property node);
62 T visitRegExpLiteral(RegExpLiteral node); 62 T visitRegExpLiteral(RegExpLiteral node);
63 63
64 T visitComment(Comment node); 64 T visitComment(Comment node);
65 65
66 T visitInterpolatedExpression(InterpolatedExpression node); 66 T visitInterpolatedExpression(InterpolatedExpression node);
67 T visitInterpolatedLiteral(InterpolatedLiteral node);
68 T visitInterpolatedParameter(InterpolatedParameter node);
69 T visitInterpolatedSelector(InterpolatedSelector node);
67 T visitInterpolatedStatement(InterpolatedStatement node); 70 T visitInterpolatedStatement(InterpolatedStatement node);
68 T visitJSExpression(JSExpression node);
69 } 71 }
70 72
71 class BaseVisitor<T> implements NodeVisitor<T> { 73 class BaseVisitor<T> implements NodeVisitor<T> {
72 T visitNode(Node node) { 74 T visitNode(Node node) {
73 node.visitChildren(this); 75 node.visitChildren(this);
74 return null; 76 return null;
75 } 77 }
76 78
77 T visitProgram(Program node) => visitNode(node); 79 T visitProgram(Program node) => visitNode(node);
78 80
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 T visitLiteralString(LiteralString node) => visitLiteral(node); 145 T visitLiteralString(LiteralString node) => visitLiteral(node);
144 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); 146 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node);
145 T visitLiteralNull(LiteralNull node) => visitLiteral(node); 147 T visitLiteralNull(LiteralNull node) => visitLiteral(node);
146 148
147 T visitArrayInitializer(ArrayInitializer node) => visitExpression(node); 149 T visitArrayInitializer(ArrayInitializer node) => visitExpression(node);
148 T visitArrayElement(ArrayElement node) => visitNode(node); 150 T visitArrayElement(ArrayElement node) => visitNode(node);
149 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); 151 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node);
150 T visitProperty(Property node) => visitNode(node); 152 T visitProperty(Property node) => visitNode(node);
151 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); 153 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node);
152 154
155 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node);
156
153 T visitInterpolatedExpression(InterpolatedExpression node) 157 T visitInterpolatedExpression(InterpolatedExpression node)
154 => visitExpression(node); 158 => visitInterpolatedNode(node);
159 T visitInterpolatedLiteral(InterpolatedLiteral node)
160 => visitInterpolatedNode(node);
161 T visitInterpolatedParameter(InterpolatedParameter node)
162 => visitInterpolatedNode(node);
163 T visitInterpolatedSelector(InterpolatedSelector node)
164 => visitInterpolatedNode(node);
155 T visitInterpolatedStatement(InterpolatedStatement node) 165 T visitInterpolatedStatement(InterpolatedStatement node)
156 => visitStatement(node); 166 => visitInterpolatedNode(node);
157 T visitJSExpression(JSExpression node) => visitExpression(node);
158 167
159 // Ignore comments by default. 168 // Ignore comments by default.
160 T visitComment(Comment node) => null; 169 T visitComment(Comment node) => null;
161 } 170 }
162 171
163 abstract class Node { 172 abstract class Node {
164 var sourcePosition; 173 var sourcePosition;
165 var endSourcePosition; 174 var endSourcePosition;
166 175
167 accept(NodeVisitor visitor); 176 accept(NodeVisitor visitor);
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 456
448 LiteralStatement(this.code); 457 LiteralStatement(this.code);
449 458
450 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this); 459 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this);
451 void visitChildren(NodeVisitor visitor) { } 460 void visitChildren(NodeVisitor visitor) { }
452 } 461 }
453 462
454 abstract class Expression extends Node { 463 abstract class Expression extends Node {
455 int get precedenceLevel; 464 int get precedenceLevel;
456 465
457 Call callWith(List<Expression> arguments) => new Call(this, arguments); 466 //PropertyAccess operator [](expression) {
458 467 // // DEPRECATED.
floitsch 2014/04/22 16:11:18 dead?
sra1 2014/04/23 02:33:50 Done.
459 PropertyAccess operator [](expression) { 468 // if (expression is Expression) {
460 if (expression is Expression) { 469 // return new PropertyAccess(this, expression);
461 return new PropertyAccess(this, expression); 470 // } else if (expression is int) {
462 } else if (expression is int) { 471 // return new PropertyAccess.indexed(this, expression);
463 return new PropertyAccess.indexed(this, expression); 472 // } else if (expression is String) {
464 } else if (expression is String) { 473 // return new PropertyAccess.field(this, expression);
465 return new PropertyAccess.field(this, expression); 474 // } else {
466 } else { 475 // throw new ArgumentError('Expected an int, String, or Expression');
467 throw new ArgumentError('Expected an int, String, or Expression'); 476 // }
468 } 477 //}
469 }
470 478
471 Statement toStatement() => new ExpressionStatement(this); 479 Statement toStatement() => new ExpressionStatement(this);
472 480
473 Call call([expression]) { 481 //Call call([expression]) {
floitsch 2014/04/22 16:11:18 dead?
sra1 2014/04/23 02:33:50 Done.
474 List<Expression> arguments; 482 // // DEPRECATED.
475 if (expression == null) { 483 // List<Expression> arguments;
476 arguments = <Expression>[]; 484 // if (expression == null) {
477 } else if (expression is List) { 485 // arguments = <Expression>[];
478 arguments = expression.map(js.toExpression).toList(); 486 // } else if (expression is List) {
479 } else { 487 // arguments = expression.map(js.toExpression).toList();
480 arguments = <Expression>[js.toExpression(expression)]; 488 // } else {
481 } 489 // arguments = <Expression>[js.toExpression(expression)];
482 return callWith(arguments); 490 // }
483 } 491 // return new Call(this, arguments);
484 492 //}
485 Expression operator +(expression) => binary('+', expression);
486
487 Expression operator -(expression) => binary('-', expression);
488
489 Expression operator &(expression) => binary('&', expression);
490
491 Expression operator <(expression) => binary('<', expression);
492
493 Expression operator >(expression) => binary('>', expression);
494
495 Expression operator >=(expression) => binary('>=', expression);
496
497 Expression binary(String operator, expression) {
498 return new Binary(operator, this, js.toExpression(expression));
499 }
500
501 Expression update(String operator, expression) {
502 return new Assignment.compound(this, operator, js.toExpression(expression));
503 }
504
505 Expression get plusPlus => new Postfix('++', this);
506
507 Prefix get typeof => new Prefix('typeof', this);
508
509 Prefix get not => new Prefix('!', this);
510 } 493 }
511 494
512 /// Wrap a CodeBuffer as an expression. 495 /// Wrap a CodeBuffer as an expression.
513 class Blob extends Expression { 496 class Blob extends Expression {
514 // TODO(ahe): This class is an aid to convert everything to ASTs, remove when 497 // TODO(ahe): This class is an aid to convert everything to ASTs, remove when
515 // not needed anymore. 498 // not needed anymore.
516 499
517 final leg.CodeBuffer buffer; 500 final leg.CodeBuffer buffer;
518 501
519 Blob(this.buffer); 502 Blob(this.buffer);
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 929
947 accept(NodeVisitor visitor) => visitor.visitProperty(this); 930 accept(NodeVisitor visitor) => visitor.visitProperty(this);
948 931
949 void visitChildren(NodeVisitor visitor) { 932 void visitChildren(NodeVisitor visitor) {
950 name.accept(visitor); 933 name.accept(visitor);
951 value.accept(visitor); 934 value.accept(visitor);
952 } 935 }
953 } 936 }
954 937
955 /// Tag class for all interpolated positions. 938 /// Tag class for all interpolated positions.
956 abstract class InterpolatedNode implements Node {} 939 abstract class InterpolatedNode implements Node {
940 get name; // 'int' for positional interpolated nodes, 'String' for named.
941 }
957 942
958 class InterpolatedExpression extends Expression implements InterpolatedNode { 943 class InterpolatedExpression extends Expression implements InterpolatedNode {
959 Expression value; 944 final name;
960 945
961 InterpolatedExpression(this.value); 946 InterpolatedExpression(this.name);
962
963 void assign(Expression newValue) {
964 value = newValue;
965 }
966 947
967 accept(NodeVisitor visitor) => visitor.visitInterpolatedExpression(this); 948 accept(NodeVisitor visitor) => visitor.visitInterpolatedExpression(this);
949 void visitChildren(NodeVisitor visitor) {}
968 950
969 void visitChildren(NodeVisitor visitor) { 951 int get precedenceLevel => PRIMARY;
970 if (value != null) value.accept(visitor); 952 }
971 }
972 953
973 int get precedenceLevel => value.precedenceLevel; 954 class InterpolatedLiteral extends Literal implements InterpolatedNode {
955 final name;
956
957 InterpolatedLiteral(this.name);
958
959 accept(NodeVisitor visitor) => visitor.visitInterpolatedLiteral(this);
960 void visitChildren(NodeVisitor visitor) {}
961 }
962
963 class InterpolatedParameter extends Expression
964 implements Parameter, InterpolatedNode {
965 final name;
966
967 InterpolatedParameter(this.name);
968
969 accept(NodeVisitor visitor) => visitor.visitInterpolatedParameter(this);
970 void visitChildren(NodeVisitor visitor) {}
971
972 int get precedenceLevel => PRIMARY;
973 }
974
975 class InterpolatedSelector extends Expression implements InterpolatedNode {
976 final name;
977
978 InterpolatedSelector(this.name);
979
980 accept(NodeVisitor visitor) => visitor.visitInterpolatedSelector(this);
981 void visitChildren(NodeVisitor visitor) {}
982
983 int get precedenceLevel => PRIMARY;
974 } 984 }
975 985
976 class InterpolatedStatement extends Statement implements InterpolatedNode { 986 class InterpolatedStatement extends Statement implements InterpolatedNode {
977 Statement value; 987 final name;
978 988
979 InterpolatedStatement(this.value); 989 InterpolatedStatement(this.name);
980
981 void assign(Node newValue) {
982 if (newValue is Expression)
983 value = new ExpressionStatement(newValue);
984 else
985 value = newValue;
986 }
987 990
988 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this); 991 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this);
989 992 void visitChildren(NodeVisitor visitor) {}
990 void visitChildren(NodeVisitor visitor) {
991 if (value != null) value.accept(visitor);
992 }
993 } 993 }
994 994
995 class JSExpression extends Expression {
996 Expression value;
997 List<InterpolatedNode> interpolatedNodes;
998
999 JSExpression(this.value, this.interpolatedNodes);
1000
1001 accept(NodeVisitor visitor) => visitor.visitJSExpression(this);
1002
1003 void visitChildren(NodeVisitor visitor) {
1004 value.accept(visitor);
1005 for (InterpolatedNode node in interpolatedNodes) {
1006 node.accept(visitor);
1007 }
1008 }
1009
1010 int get precedenceLevel => value.precedenceLevel;
1011 }
1012
1013 // TODO(sra): JSStatement like JSExpression.
1014
1015 /** 995 /**
1016 * [RegExpLiteral]s, despite being called "Literal", do not inherit from 996 * [RegExpLiteral]s, despite being called "Literal", do not inherit from
1017 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and 997 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and
1018 * are thus not in the same category as numbers or strings. 998 * are thus not in the same category as numbers or strings.
1019 */ 999 */
1020 class RegExpLiteral extends Expression { 1000 class RegExpLiteral extends Expression {
1021 /** Contains the pattern and the flags.*/ 1001 /** Contains the pattern and the flags.*/
1022 String pattern; 1002 String pattern;
1023 1003
1024 RegExpLiteral(this.pattern); 1004 RegExpLiteral(this.pattern);
(...skipping 12 matching lines...) Expand all
1037 */ 1017 */
1038 class Comment extends Statement { 1018 class Comment extends Statement {
1039 final String comment; 1019 final String comment;
1040 1020
1041 Comment(this.comment); 1021 Comment(this.comment);
1042 1022
1043 accept(NodeVisitor visitor) => visitor.visitComment(this); 1023 accept(NodeVisitor visitor) => visitor.visitComment(this);
1044 1024
1045 void visitChildren(NodeVisitor visitor) {} 1025 void visitChildren(NodeVisitor visitor) {}
1046 } 1026 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698