| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart2js.resolution.enum_creator; | 5 library dart2js.resolution.enum_creator; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common_elements.dart' show CommonElements; | 8 import '../common_elements.dart' show CommonElements; |
| 9 import '../elements/resolution_types.dart'; | 9 import '../elements/resolution_types.dart'; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| 11 import '../elements/modelx.dart'; | 11 import '../elements/modelx.dart'; |
| 12 import 'package:front_end/src/fasta/scanner.dart'; | 12 import 'package:front_end/src/fasta/scanner.dart'; |
| 13 import 'package:front_end/src/fasta/scanner/precedence.dart' as Precedence; | |
| 14 import 'package:front_end/src/scanner/token.dart' show TokenType; | 13 import 'package:front_end/src/scanner/token.dart' show TokenType; |
| 15 import '../tree/tree.dart'; | 14 import '../tree/tree.dart'; |
| 16 import '../util/util.dart'; | 15 import '../util/util.dart'; |
| 17 | 16 |
| 18 // TODO(johnniwinther): Merge functionality with the `TreePrinter`. | 17 // TODO(johnniwinther): Merge functionality with the `TreePrinter`. |
| 19 class AstBuilder { | 18 class AstBuilder { |
| 20 final int charOffset; | 19 final int charOffset; |
| 21 | 20 |
| 22 AstBuilder(this.charOffset); | 21 AstBuilder(this.charOffset); |
| 23 | 22 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 39 } | 38 } |
| 40 return new Modifiers.withFlags( | 39 return new Modifiers.withFlags( |
| 41 new NodeList(null, linkedList(identifiers), null, ''), flags); | 40 new NodeList(null, linkedList(identifiers), null, ''), flags); |
| 42 } | 41 } |
| 43 | 42 |
| 44 Token keywordToken(String text) { | 43 Token keywordToken(String text) { |
| 45 return new KeywordToken(Keyword.keywords[text], charOffset); | 44 return new KeywordToken(Keyword.keywords[text], charOffset); |
| 46 } | 45 } |
| 47 | 46 |
| 48 Token stringToken(String text) { | 47 Token stringToken(String text) { |
| 49 return new StringToken.fromString( | 48 return new StringToken.fromString(TokenType.IDENTIFIER, text, charOffset); |
| 50 Precedence.IDENTIFIER_INFO, text, charOffset); | |
| 51 } | 49 } |
| 52 | 50 |
| 53 Token symbolToken(TokenType info) { | 51 Token symbolToken(TokenType info) { |
| 54 return new SymbolToken(info, charOffset); | 52 return new SymbolToken(info, charOffset); |
| 55 } | 53 } |
| 56 | 54 |
| 57 Identifier identifier(String text) { | 55 Identifier identifier(String text) { |
| 58 Keyword keyword = Keyword.keywords[text]; | 56 Keyword keyword = Keyword.keywords[text]; |
| 59 Token token; | 57 Token token; |
| 60 if (keyword != null) { | 58 if (keyword != null) { |
| 61 token = new KeywordToken(keyword, charOffset); | 59 token = new KeywordToken(keyword, charOffset); |
| 62 } else { | 60 } else { |
| 63 token = stringToken(text); | 61 token = stringToken(text); |
| 64 } | 62 } |
| 65 return new Identifier(token); | 63 return new Identifier(token); |
| 66 } | 64 } |
| 67 | 65 |
| 68 Link linkedList(List elements) { | 66 Link linkedList(List elements) { |
| 69 LinkBuilder builder = new LinkBuilder(); | 67 LinkBuilder builder = new LinkBuilder(); |
| 70 elements.forEach((e) => builder.addLast(e)); | 68 elements.forEach((e) => builder.addLast(e)); |
| 71 return builder.toLink(); | 69 return builder.toLink(); |
| 72 } | 70 } |
| 73 | 71 |
| 74 NodeList argumentList(List<Node> nodes) { | 72 NodeList argumentList(List<Node> nodes) { |
| 75 return new NodeList(symbolToken(Precedence.OPEN_PAREN_INFO), | 73 return new NodeList(symbolToken(TokenType.OPEN_PAREN), linkedList(nodes), |
| 76 linkedList(nodes), symbolToken(Precedence.CLOSE_PAREN_INFO), ','); | 74 symbolToken(TokenType.CLOSE_PAREN), ','); |
| 77 } | 75 } |
| 78 | 76 |
| 79 Return returnStatement(Expression expression) { | 77 Return returnStatement(Expression expression) { |
| 80 return new Return(keywordToken('return'), | 78 return new Return( |
| 81 symbolToken(Precedence.SEMICOLON_INFO), expression); | 79 keywordToken('return'), symbolToken(TokenType.SEMICOLON), expression); |
| 82 } | 80 } |
| 83 | 81 |
| 84 FunctionExpression functionExpression(Modifiers modifiers, String name, | 82 FunctionExpression functionExpression(Modifiers modifiers, String name, |
| 85 NodeList typeVariables, NodeList argumentList, Statement body, | 83 NodeList typeVariables, NodeList argumentList, Statement body, |
| 86 [TypeAnnotation returnType]) { | 84 [TypeAnnotation returnType]) { |
| 87 return new FunctionExpression( | 85 return new FunctionExpression( |
| 88 identifier(name), | 86 identifier(name), |
| 89 typeVariables, | 87 typeVariables, |
| 90 argumentList, | 88 argumentList, |
| 91 body, | 89 body, |
| 92 returnType, | 90 returnType, |
| 93 modifiers, | 91 modifiers, |
| 94 null, // Initializer. | 92 null, // Initializer. |
| 95 null, // get/set. | 93 null, // get/set. |
| 96 null // Async modifier. | 94 null // Async modifier. |
| 97 ); | 95 ); |
| 98 } | 96 } |
| 99 | 97 |
| 100 EmptyStatement emptyStatement() { | 98 EmptyStatement emptyStatement() { |
| 101 return new EmptyStatement(symbolToken(Precedence.COMMA_INFO)); | 99 return new EmptyStatement(symbolToken(TokenType.COMMA)); |
| 102 } | 100 } |
| 103 | 101 |
| 104 LiteralInt literalInt(int value) { | 102 LiteralInt literalInt(int value) { |
| 105 return new LiteralInt(stringToken('$value'), null); | 103 return new LiteralInt(stringToken('$value'), null); |
| 106 } | 104 } |
| 107 | 105 |
| 108 LiteralString literalString(String text, | 106 LiteralString literalString(String text, |
| 109 {String prefix: '"', String suffix: '"'}) { | 107 {String prefix: '"', String suffix: '"'}) { |
| 110 return new LiteralString( | 108 return new LiteralString( |
| 111 stringToken('$prefix$text$suffix'), new DartString.literal(text)); | 109 stringToken('$prefix$text$suffix'), new DartString.literal(text)); |
| 112 } | 110 } |
| 113 | 111 |
| 114 LiteralList listLiteral(List<Node> elements, {bool isConst: false}) { | 112 LiteralList listLiteral(List<Node> elements, {bool isConst: false}) { |
| 115 return new LiteralList( | 113 return new LiteralList( |
| 116 null, | 114 null, |
| 117 new NodeList( | 115 new NodeList( |
| 118 symbolToken(Precedence.OPEN_SQUARE_BRACKET_INFO), | 116 symbolToken(TokenType.OPEN_SQUARE_BRACKET), |
| 119 linkedList(elements), | 117 linkedList(elements), |
| 120 symbolToken(Precedence.CLOSE_SQUARE_BRACKET_INFO), | 118 symbolToken(TokenType.CLOSE_SQUARE_BRACKET), |
| 121 ','), | 119 ','), |
| 122 isConst ? keywordToken('const') : null); | 120 isConst ? keywordToken('const') : null); |
| 123 } | 121 } |
| 124 | 122 |
| 125 Node createDefinition(Identifier name, Expression initializer) { | 123 Node createDefinition(Identifier name, Expression initializer) { |
| 126 if (initializer == null) return name; | 124 if (initializer == null) return name; |
| 127 return new SendSet( | 125 return new SendSet(null, name, new Operator(symbolToken(TokenType.EQ)), |
| 128 null, | |
| 129 name, | |
| 130 new Operator(symbolToken(Precedence.EQ_INFO)), | |
| 131 new NodeList.singleton(initializer)); | 126 new NodeList.singleton(initializer)); |
| 132 } | 127 } |
| 133 | 128 |
| 134 VariableDefinitions initializingFormal(String fieldName) { | 129 VariableDefinitions initializingFormal(String fieldName) { |
| 135 return new VariableDefinitions.forParameter( | 130 return new VariableDefinitions.forParameter( |
| 136 new NodeList.empty(), | 131 new NodeList.empty(), |
| 137 null, | 132 null, |
| 138 Modifiers.EMPTY, | 133 Modifiers.EMPTY, |
| 139 new NodeList.singleton( | 134 new NodeList.singleton( |
| 140 new Send(identifier('this'), identifier(fieldName)))); | 135 new Send(identifier('this'), identifier(fieldName)))); |
| 141 } | 136 } |
| 142 | 137 |
| 143 NewExpression newExpression(String typeName, NodeList arguments, | 138 NewExpression newExpression(String typeName, NodeList arguments, |
| 144 {bool isConst: false}) { | 139 {bool isConst: false}) { |
| 145 return new NewExpression(keywordToken(isConst ? 'const' : 'new'), | 140 return new NewExpression(keywordToken(isConst ? 'const' : 'new'), |
| 146 new Send(null, identifier(typeName), arguments)); | 141 new Send(null, identifier(typeName), arguments)); |
| 147 } | 142 } |
| 148 | 143 |
| 149 Send reference(Identifier identifier) { | 144 Send reference(Identifier identifier) { |
| 150 return new Send(null, identifier); | 145 return new Send(null, identifier); |
| 151 } | 146 } |
| 152 | 147 |
| 153 Send indexGet(Expression receiver, Expression index) { | 148 Send indexGet(Expression receiver, Expression index) { |
| 154 return new Send(receiver, new Operator(symbolToken(Precedence.INDEX_INFO)), | 149 return new Send(receiver, new Operator(symbolToken(TokenType.INDEX)), |
| 155 new NodeList.singleton(index)); | 150 new NodeList.singleton(index)); |
| 156 } | 151 } |
| 157 | 152 |
| 158 LiteralMapEntry mapLiteralEntry(Expression key, Expression value) { | 153 LiteralMapEntry mapLiteralEntry(Expression key, Expression value) { |
| 159 return new LiteralMapEntry(key, symbolToken(Precedence.COLON_INFO), value); | 154 return new LiteralMapEntry(key, symbolToken(TokenType.COLON), value); |
| 160 } | 155 } |
| 161 | 156 |
| 162 LiteralMap mapLiteral(List<LiteralMapEntry> entries, {bool isConst: false}) { | 157 LiteralMap mapLiteral(List<LiteralMapEntry> entries, {bool isConst: false}) { |
| 163 return new LiteralMap( | 158 return new LiteralMap( |
| 164 null, // Type arguments. | 159 null, // Type arguments. |
| 165 new NodeList( | 160 new NodeList( |
| 166 symbolToken(Precedence.OPEN_CURLY_BRACKET_INFO), | 161 symbolToken(TokenType.OPEN_CURLY_BRACKET), |
| 167 linkedList(entries), | 162 linkedList(entries), |
| 168 symbolToken(Precedence.CLOSE_CURLY_BRACKET_INFO), | 163 symbolToken(TokenType.CLOSE_CURLY_BRACKET), |
| 169 ','), | 164 ','), |
| 170 isConst ? keywordToken('const') : null); | 165 isConst ? keywordToken('const') : null); |
| 171 } | 166 } |
| 172 } | 167 } |
| 173 | 168 |
| 174 /// This class generates the model for an enum class. | 169 /// This class generates the model for an enum class. |
| 175 /// | 170 /// |
| 176 /// For instance | 171 /// For instance |
| 177 /// | 172 /// |
| 178 /// enum A { b, c, } | 173 /// enum A { b, c, } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 EnumMethodElementX toString = new EnumMethodElementX( | 308 EnumMethodElementX toString = new EnumMethodElementX( |
| 314 'toString', enumClass, Modifiers.EMPTY, toStringNode); | 309 'toString', enumClass, Modifiers.EMPTY, toStringNode); |
| 315 FunctionSignatureX toStringSignature = new FunctionSignatureX( | 310 FunctionSignatureX toStringSignature = new FunctionSignatureX( |
| 316 type: new ResolutionFunctionType(toString, stringType)); | 311 type: new ResolutionFunctionType(toString, stringType)); |
| 317 toString.functionSignature = toStringSignature; | 312 toString.functionSignature = toStringSignature; |
| 318 enumClass.addMember(toString, reporter); | 313 enumClass.addMember(toString, reporter); |
| 319 | 314 |
| 320 enumClass.enumValues = enumValues; | 315 enumClass.enumValues = enumValues; |
| 321 } | 316 } |
| 322 } | 317 } |
| OLD | NEW |