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

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: merge 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);
458
459 PropertyAccess operator [](expression) {
460 if (expression is Expression) {
461 return new PropertyAccess(this, expression);
462 } else if (expression is int) {
463 return new PropertyAccess.indexed(this, expression);
464 } else if (expression is String) {
465 return new PropertyAccess.field(this, expression);
466 } else {
467 throw new ArgumentError('Expected an int, String, or Expression');
468 }
469 }
470
471 Statement toStatement() => new ExpressionStatement(this); 466 Statement toStatement() => new ExpressionStatement(this);
472
473 Call call([expression]) {
474 List<Expression> arguments;
475 if (expression == null) {
476 arguments = <Expression>[];
477 } else if (expression is List) {
478 arguments = expression.map(js.toExpression).toList();
479 } else {
480 arguments = <Expression>[js.toExpression(expression)];
481 }
482 return callWith(arguments);
483 }
484
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 } 467 }
511 468
512 /// Wrap a CodeBuffer as an expression. 469 /// Wrap a CodeBuffer as an expression.
513 class Blob extends Expression { 470 class Blob extends Expression {
514 // TODO(ahe): This class is an aid to convert everything to ASTs, remove when 471 // TODO(ahe): This class is an aid to convert everything to ASTs, remove when
515 // not needed anymore. 472 // not needed anymore.
516 473
517 final leg.CodeBuffer buffer; 474 final leg.CodeBuffer buffer;
518 475
519 Blob(this.buffer); 476 Blob(this.buffer);
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 903
947 accept(NodeVisitor visitor) => visitor.visitProperty(this); 904 accept(NodeVisitor visitor) => visitor.visitProperty(this);
948 905
949 void visitChildren(NodeVisitor visitor) { 906 void visitChildren(NodeVisitor visitor) {
950 name.accept(visitor); 907 name.accept(visitor);
951 value.accept(visitor); 908 value.accept(visitor);
952 } 909 }
953 } 910 }
954 911
955 /// Tag class for all interpolated positions. 912 /// Tag class for all interpolated positions.
956 abstract class InterpolatedNode implements Node {} 913 abstract class InterpolatedNode implements Node {
914 get name; // 'int' for positional interpolated nodes, 'String' for named.
915 }
957 916
958 class InterpolatedExpression extends Expression implements InterpolatedNode { 917 class InterpolatedExpression extends Expression implements InterpolatedNode {
959 Expression value; 918 final name;
960 919
961 InterpolatedExpression(this.value); 920 InterpolatedExpression(this.name);
962
963 void assign(Expression newValue) {
964 value = newValue;
965 }
966 921
967 accept(NodeVisitor visitor) => visitor.visitInterpolatedExpression(this); 922 accept(NodeVisitor visitor) => visitor.visitInterpolatedExpression(this);
923 void visitChildren(NodeVisitor visitor) {}
968 924
969 void visitChildren(NodeVisitor visitor) { 925 int get precedenceLevel => PRIMARY;
970 if (value != null) value.accept(visitor); 926 }
971 }
972 927
973 int get precedenceLevel => value.precedenceLevel; 928 class InterpolatedLiteral extends Literal implements InterpolatedNode {
929 final name;
930
931 InterpolatedLiteral(this.name);
932
933 accept(NodeVisitor visitor) => visitor.visitInterpolatedLiteral(this);
934 void visitChildren(NodeVisitor visitor) {}
935 }
936
937 class InterpolatedParameter extends Expression
938 implements Parameter, InterpolatedNode {
939 final name;
940
941 InterpolatedParameter(this.name);
942
943 accept(NodeVisitor visitor) => visitor.visitInterpolatedParameter(this);
944 void visitChildren(NodeVisitor visitor) {}
945
946 int get precedenceLevel => PRIMARY;
947 }
948
949 class InterpolatedSelector extends Expression implements InterpolatedNode {
950 final name;
951
952 InterpolatedSelector(this.name);
953
954 accept(NodeVisitor visitor) => visitor.visitInterpolatedSelector(this);
955 void visitChildren(NodeVisitor visitor) {}
956
957 int get precedenceLevel => PRIMARY;
974 } 958 }
975 959
976 class InterpolatedStatement extends Statement implements InterpolatedNode { 960 class InterpolatedStatement extends Statement implements InterpolatedNode {
977 Statement value; 961 final name;
978 962
979 InterpolatedStatement(this.value); 963 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 964
988 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this); 965 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this);
989 966 void visitChildren(NodeVisitor visitor) {}
990 void visitChildren(NodeVisitor visitor) {
991 if (value != null) value.accept(visitor);
992 }
993 } 967 }
994 968
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 /** 969 /**
1016 * [RegExpLiteral]s, despite being called "Literal", do not inherit from 970 * [RegExpLiteral]s, despite being called "Literal", do not inherit from
1017 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and 971 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and
1018 * are thus not in the same category as numbers or strings. 972 * are thus not in the same category as numbers or strings.
1019 */ 973 */
1020 class RegExpLiteral extends Expression { 974 class RegExpLiteral extends Expression {
1021 /** Contains the pattern and the flags.*/ 975 /** Contains the pattern and the flags.*/
1022 String pattern; 976 String pattern;
1023 977
1024 RegExpLiteral(this.pattern); 978 RegExpLiteral(this.pattern);
(...skipping 12 matching lines...) Expand all
1037 */ 991 */
1038 class Comment extends Statement { 992 class Comment extends Statement {
1039 final String comment; 993 final String comment;
1040 994
1041 Comment(this.comment); 995 Comment(this.comment);
1042 996
1043 accept(NodeVisitor visitor) => visitor.visitComment(this); 997 accept(NodeVisitor visitor) => visitor.visitComment(this);
1044 998
1045 void visitChildren(NodeVisitor visitor) {} 999 void visitChildren(NodeVisitor visitor) {}
1046 } 1000 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/js/js.dart ('k') | sdk/lib/_internal/compiler/implementation/js/printer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698