Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 T visitRegExpLiteral(RegExpLiteral node); | 65 T visitRegExpLiteral(RegExpLiteral node); |
| 66 T visitTemplateString(TemplateString node); | 66 T visitTemplateString(TemplateString node); |
| 67 T visitTaggedTemplate(TaggedTemplate node); | 67 T visitTaggedTemplate(TaggedTemplate node); |
| 68 | 68 |
| 69 T visitAwait(Await node); | 69 T visitAwait(Await node); |
| 70 | 70 |
| 71 T visitClassDeclaration(ClassDeclaration node); | 71 T visitClassDeclaration(ClassDeclaration node); |
| 72 T visitClassExpression(ClassExpression node); | 72 T visitClassExpression(ClassExpression node); |
| 73 T visitMethod(Method node); | 73 T visitMethod(Method node); |
| 74 | 74 |
| 75 T visitImportDeclaration(ImportDeclaration node); | |
| 76 T visitExportDeclaration(ExportDeclaration node); | |
| 77 T visitExportClause(ExportClause node); | |
| 78 T visitNameSpecifier(NameSpecifier node); | |
| 79 T visitModule(Module node); | |
| 80 | |
| 75 T visitComment(Comment node); | 81 T visitComment(Comment node); |
| 76 T visitCommentExpression(CommentExpression node); | 82 T visitCommentExpression(CommentExpression node); |
| 77 | 83 |
| 78 T visitInterpolatedExpression(InterpolatedExpression node); | 84 T visitInterpolatedExpression(InterpolatedExpression node); |
| 79 T visitInterpolatedLiteral(InterpolatedLiteral node); | 85 T visitInterpolatedLiteral(InterpolatedLiteral node); |
| 80 T visitInterpolatedParameter(InterpolatedParameter node); | 86 T visitInterpolatedParameter(InterpolatedParameter node); |
| 81 T visitInterpolatedSelector(InterpolatedSelector node); | 87 T visitInterpolatedSelector(InterpolatedSelector node); |
| 82 T visitInterpolatedStatement(InterpolatedStatement node); | 88 T visitInterpolatedStatement(InterpolatedStatement node); |
| 83 T visitInterpolatedMethod(InterpolatedMethod node); | 89 T visitInterpolatedMethod(InterpolatedMethod node); |
| 84 T visitInterpolatedIdentifier(InterpolatedIdentifier node); | 90 T visitInterpolatedIdentifier(InterpolatedIdentifier node); |
| 85 } | 91 } |
| 86 | 92 |
| 87 class BaseVisitor<T> implements NodeVisitor<T> { | 93 class BaseVisitor<T> implements NodeVisitor<T> { |
| 88 T visitNode(Node node) { | 94 T visitNode(Node node) { |
| 89 node.visitChildren(this); | 95 node.visitChildren(this); |
| 90 return null; | 96 return null; |
| 91 } | 97 } |
| 92 | 98 |
| 93 T visitProgram(Program node) => visitNode(node); | 99 T visitProgram(Program node) => visitNode(node); |
| 94 | 100 |
| 95 T visitStatement(Statement node) => visitNode(node); | 101 T visitStatement(Statement node) => visitModuleItem(node); |
| 96 T visitLoop(Loop node) => visitStatement(node); | 102 T visitLoop(Loop node) => visitStatement(node); |
| 97 T visitJump(Statement node) => visitStatement(node); | 103 T visitJump(Statement node) => visitStatement(node); |
| 98 | 104 |
| 99 T visitBlock(Block node) => visitStatement(node); | 105 T visitBlock(Block node) => visitStatement(node); |
| 100 T visitExpressionStatement(ExpressionStatement node) | 106 T visitExpressionStatement(ExpressionStatement node) |
| 101 => visitStatement(node); | 107 => visitStatement(node); |
| 102 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); | 108 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); |
| 103 T visitIf(If node) => visitStatement(node); | 109 T visitIf(If node) => visitStatement(node); |
| 104 T visitFor(For node) => visitLoop(node); | 110 T visitFor(For node) => visitLoop(node); |
| 105 T visitForIn(ForIn node) => visitLoop(node); | 111 T visitForIn(ForIn node) => visitLoop(node); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); | 173 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); |
| 168 T visitProperty(Property node) => visitNode(node); | 174 T visitProperty(Property node) => visitNode(node); |
| 169 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); | 175 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); |
| 170 T visitTemplateString(TemplateString node) => visitExpression(node); | 176 T visitTemplateString(TemplateString node) => visitExpression(node); |
| 171 T visitTaggedTemplate(TaggedTemplate node) => visitExpression(node); | 177 T visitTaggedTemplate(TaggedTemplate node) => visitExpression(node); |
| 172 | 178 |
| 173 T visitClassDeclaration(ClassDeclaration node) => visitStatement(node); | 179 T visitClassDeclaration(ClassDeclaration node) => visitStatement(node); |
| 174 T visitClassExpression(ClassExpression node) => visitExpression(node); | 180 T visitClassExpression(ClassExpression node) => visitExpression(node); |
| 175 T visitMethod(Method node) => visitProperty(node); | 181 T visitMethod(Method node) => visitProperty(node); |
| 176 | 182 |
| 183 T visitModuleItem(ModuleItem node) => visitNode(node); | |
| 184 T visitImportDeclaration(ImportDeclaration node) => visitModuleItem(node); | |
| 185 T visitExportDeclaration(ExportDeclaration node) => visitModuleItem(node); | |
| 186 T visitExportClause(ExportClause node) => visitNode(node); | |
| 187 T visitNameSpecifier(NameSpecifier node) => visitNode(node); | |
| 188 T visitModule(Module node) => visitNode(node); | |
| 189 | |
| 177 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node); | 190 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node); |
| 178 | 191 |
| 179 T visitInterpolatedExpression(InterpolatedExpression node) | 192 T visitInterpolatedExpression(InterpolatedExpression node) |
| 180 => visitInterpolatedNode(node); | 193 => visitInterpolatedNode(node); |
| 181 T visitInterpolatedLiteral(InterpolatedLiteral node) | 194 T visitInterpolatedLiteral(InterpolatedLiteral node) |
| 182 => visitInterpolatedNode(node); | 195 => visitInterpolatedNode(node); |
| 183 T visitInterpolatedParameter(InterpolatedParameter node) | 196 T visitInterpolatedParameter(InterpolatedParameter node) |
| 184 => visitInterpolatedNode(node); | 197 => visitInterpolatedNode(node); |
| 185 T visitInterpolatedSelector(InterpolatedSelector node) | 198 T visitInterpolatedSelector(InterpolatedSelector node) |
| 186 => visitInterpolatedNode(node); | 199 => visitInterpolatedNode(node); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 | 277 |
| 265 Program(this.body, {this.scriptTag}); | 278 Program(this.body, {this.scriptTag}); |
| 266 | 279 |
| 267 accept(NodeVisitor visitor) => visitor.visitProgram(this); | 280 accept(NodeVisitor visitor) => visitor.visitProgram(this); |
| 268 void visitChildren(NodeVisitor visitor) { | 281 void visitChildren(NodeVisitor visitor) { |
| 269 for (Statement statement in body) statement.accept(visitor); | 282 for (Statement statement in body) statement.accept(visitor); |
| 270 } | 283 } |
| 271 Program _clone() => new Program(body); | 284 Program _clone() => new Program(body); |
| 272 } | 285 } |
| 273 | 286 |
| 274 abstract class Statement extends Node { | 287 abstract class Statement extends ModuleItem { |
| 275 Statement toStatement() => this; | 288 Statement toStatement() => this; |
| 276 Statement toReturn() => new Block([this, new Return()]); | 289 Statement toReturn() => new Block([this, new Return()]); |
| 277 } | 290 } |
| 278 | 291 |
| 279 class Block extends Statement { | 292 class Block extends Statement { |
| 280 final List<Statement> statements; | 293 final List<Statement> statements; |
| 281 | 294 |
| 282 /// True to preserve this [Block] for scoping reasons. | 295 /// True to preserve this [Block] for scoping reasons. |
| 283 final bool isScope; | 296 final bool isScope; |
| 284 | 297 |
| (...skipping 1282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1567 final Expression expression; | 1580 final Expression expression; |
| 1568 | 1581 |
| 1569 CommentExpression(this.comment, this.expression); | 1582 CommentExpression(this.comment, this.expression); |
| 1570 | 1583 |
| 1571 int get precedenceLevel => PRIMARY; | 1584 int get precedenceLevel => PRIMARY; |
| 1572 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); | 1585 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); |
| 1573 CommentExpression _clone() => new CommentExpression(comment, expression); | 1586 CommentExpression _clone() => new CommentExpression(comment, expression); |
| 1574 | 1587 |
| 1575 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); | 1588 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); |
| 1576 } | 1589 } |
| 1590 | |
| 1591 /** | |
| 1592 * Represents allowed module items: | |
| 1593 * [Statement], [ImportDeclaration], and [ExportDeclaration]. | |
| 1594 */ | |
| 1595 abstract class ModuleItem extends Node {} | |
| 1596 | |
| 1597 class ImportDeclaration extends ModuleItem { | |
| 1598 final Identifier defaultBinding; // Can be null. | |
| 1599 | |
| 1600 // Can be null, a single specifier of `* as name`, or a list. | |
| 1601 final List<NameSpecifier> namedImports; | |
| 1602 | |
| 1603 final LiteralString from; | |
| 1604 | |
| 1605 ImportDeclaration({this.defaultBinding, this.namedImports, this.from}); | |
| 1606 | |
| 1607 /** The `import "name.js"` form of import */ | |
| 1608 ImportDeclaration.all(LiteralString module) : this(from: module); | |
| 1609 | |
| 1610 /** If this import has `* as name` returns the name, otherwise null. */ | |
| 1611 String get importStarAs { | |
|
ochafik
2015/11/20 00:06:35
nit: should this be an Identifier?
| |
| 1612 if (namedImports != null && namedImports.length == 1 && | |
| 1613 namedImports[0].name == '*') { | |
| 1614 return namedImports[0].asName; | |
| 1615 } | |
| 1616 return null; | |
| 1617 } | |
| 1618 | |
| 1619 accept(NodeVisitor visitor) => visitor.visitImportDeclaration(this); | |
| 1620 void visitChildren(NodeVisitor visitor) { | |
| 1621 if (namedImports != null) { | |
| 1622 for (NameSpecifier name in namedImports) name.accept(visitor); | |
| 1623 } | |
| 1624 from.accept(visitor); | |
| 1625 } | |
| 1626 ImportDeclaration _clone() => new ImportDeclaration( | |
| 1627 defaultBinding: defaultBinding, namedImports: namedImports, from: from); | |
| 1628 } | |
| 1629 | |
| 1630 class ExportDeclaration extends ModuleItem { | |
| 1631 /** | |
| 1632 * Exports a name from this module. | |
| 1633 * | |
| 1634 * This can be a [ClassDeclaration] or [FunctionDeclaration]. | |
| 1635 * If [isDefault] is true, it can also be an [Expression]. | |
| 1636 * Otherwise it can be a [VariableDeclarationList] or an [ExportClause]. | |
| 1637 */ | |
| 1638 final Node exported; | |
| 1639 | |
| 1640 /** True if this is an `export default`. */ | |
| 1641 final bool isDefault; | |
| 1642 | |
| 1643 ExportDeclaration(this.exported, {this.isDefault: false}) { | |
| 1644 assert(exported is ClassDeclaration || | |
| 1645 exported is FunctionDeclaration || | |
| 1646 isDefault | |
| 1647 ? exported is Expression | |
| 1648 : exported is VariableDeclarationList || | |
| 1649 exported is ExportClause); | |
| 1650 } | |
| 1651 | |
| 1652 accept(NodeVisitor visitor) => visitor.visitExportDeclaration(this); | |
| 1653 visitChildren(NodeVisitor visitor) => exported.accept(visitor); | |
| 1654 ExportDeclaration _clone() => | |
| 1655 new ExportDeclaration(exported, isDefault: isDefault); | |
| 1656 } | |
| 1657 | |
| 1658 class ExportClause extends Node { | |
| 1659 final List<NameSpecifier> exports; | |
| 1660 final LiteralString from; // Can be null. | |
| 1661 | |
| 1662 ExportClause(this.exports, {this.from}); | |
| 1663 | |
| 1664 /** The `export * from 'name.js'` form. */ | |
| 1665 ExportClause.star(LiteralString from) | |
| 1666 : this([new NameSpecifier('*')], from: from); | |
| 1667 | |
| 1668 /** True if this is an `export *`. */ | |
| 1669 bool get exportStar => exports.length == 1 && exports[0].name == '*'; | |
| 1670 | |
| 1671 accept(NodeVisitor visitor) => visitor.visitExportClause(this); | |
| 1672 void visitChildren(NodeVisitor visitor) { | |
| 1673 for (NameSpecifier name in exports) name.accept(visitor); | |
| 1674 if (from != null) from.accept(visitor); | |
| 1675 } | |
| 1676 ExportClause _clone() => new ExportClause(exports, from: from); | |
| 1677 } | |
| 1678 | |
| 1679 /** An import or export specifier. */ | |
| 1680 class NameSpecifier extends Node { | |
| 1681 // TODO(jmesserly): should we wrap this in a node of some sort? | |
|
ochafik
2015/11/20 00:06:35
(we'd need a special node for *, but otherwise Ide
Jennifer Messerly
2015/11/23 22:08:12
yup, that's the main issue. The other one is that
| |
| 1682 final String name; | |
| 1683 final String asName; // Can be null. | |
| 1684 | |
| 1685 NameSpecifier(this.name, {this.asName}); | |
| 1686 | |
| 1687 accept(NodeVisitor visitor) => visitor.visitNameSpecifier(this); | |
| 1688 void visitChildren(NodeVisitor visitor) {} | |
| 1689 NameSpecifier _clone() => new NameSpecifier(name, asName: asName); | |
| 1690 } | |
| 1691 | |
| 1692 // TODO(jmesserly): should this be related to [Program]? | |
| 1693 class Module extends Node { | |
| 1694 /// The module's name | |
| 1695 // TODO(jmesserly): this is not declared in ES6, but is known by the loader. | |
| 1696 // We use this because some ES5 desugarings require it. | |
| 1697 final String name; | |
| 1698 | |
| 1699 final List<ModuleItem> body; | |
| 1700 Module(this.body, {this.name}); | |
| 1701 | |
| 1702 accept(NodeVisitor visitor) => visitor.visitModule(this); | |
| 1703 void visitChildren(NodeVisitor visitor) { | |
| 1704 for (ModuleItem item in body) item.accept(visitor); | |
| 1705 } | |
| 1706 Module _clone() => new Module(body); | |
| 1707 } | |
| OLD | NEW |