OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // A very simple parser for a subset of DartTypes for use in testing type | 4 // A very simple parser for a subset of DartTypes for use in testing type |
5 // algebra. | 5 // algebra. |
6 library kernel.test.type_parser; | 6 library kernel.test.type_parser; |
7 | 7 |
8 import 'package:kernel/kernel.dart'; | 8 import 'package:kernel/kernel.dart'; |
9 import 'package:kernel/text/ast_to_text.dart'; | 9 import 'package:kernel/text/ast_to_text.dart'; |
10 | 10 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 var returnType = parseType(); | 167 var returnType = parseType(); |
168 popTypeParameters(typeParameters); | 168 popTypeParameters(typeParameters); |
169 return new FunctionType(parameters, returnType, | 169 return new FunctionType(parameters, returnType, |
170 typeParameters: typeParameters, namedParameters: namedParameters); | 170 typeParameters: typeParameters, namedParameters: namedParameters); |
171 | 171 |
172 default: | 172 default: |
173 return fail('Unexpected token: $tokenText'); | 173 return fail('Unexpected token: $tokenText'); |
174 } | 174 } |
175 } | 175 } |
176 | 176 |
177 void parseParameterList( | 177 void parseParameterList(List<DartType> positional, List<NamedType> named) { |
178 List<DartType> positional, List<NamedType> named) { | |
179 int token = scanToken(); | 178 int token = scanToken(); |
180 assert(token == Token.LeftParen); | 179 assert(token == Token.LeftParen); |
181 token = peekToken(); | 180 token = peekToken(); |
182 while (token != Token.RightParen) { | 181 while (token != Token.RightParen) { |
183 var type = parseType(); // Could be a named parameter name. | 182 var type = parseType(); // Could be a named parameter name. |
184 token = scanToken(); | 183 token = scanToken(); |
185 if (token == Token.Colon) { | 184 if (token == Token.Colon) { |
186 String name = convertTypeToParameterName(type); | 185 String name = convertTypeToParameterName(type); |
187 named.add(new NamedType(name, parseType())); | 186 named.add(new NamedType(name, parseType())); |
188 token = scanToken(); | 187 token = scanToken(); |
(...skipping 19 matching lines...) Expand all Loading... |
208 | 207 |
209 List<DartType> parseTypeList(int open, int close) { | 208 List<DartType> parseTypeList(int open, int close) { |
210 int token = scanToken(); | 209 int token = scanToken(); |
211 assert(token == open); | 210 assert(token == open); |
212 List<DartType> types = <DartType>[]; | 211 List<DartType> types = <DartType>[]; |
213 token = peekToken(); | 212 token = peekToken(); |
214 while (token != close) { | 213 while (token != close) { |
215 types.add(parseType()); | 214 types.add(parseType()); |
216 token = scanToken(); | 215 token = scanToken(); |
217 if (token != Token.Comma && token != close) { | 216 if (token != Token.Comma && token != close) { |
218 return fail('Unterminated list'); | 217 throw fail('Unterminated list'); |
219 } | 218 } |
220 } | 219 } |
221 return types; | 220 return types; |
222 } | 221 } |
223 | 222 |
224 List<DartType> parseOptionalList(int open, int close) { | 223 List<DartType> parseOptionalList(int open, int close) { |
225 if (peekToken() != open) return null; | 224 if (peekToken() != open) return null; |
226 return parseTypeList(open, close); | 225 return parseTypeList(open, close); |
227 } | 226 } |
228 | 227 |
229 List<DartType> parseOptionalTypeArgumentList() { | 228 List<DartType> parseOptionalTypeArgumentList() { |
230 return parseOptionalList(Token.LeftAngle, Token.RightAngle); | 229 return parseOptionalList(Token.LeftAngle, Token.RightAngle); |
231 } | 230 } |
232 | 231 |
233 void popTypeParameters(List<TypeParameter> typeParameters) { | 232 void popTypeParameters(List<TypeParameter> typeParameters) { |
234 typeParameters.forEach(localTypeParameters.remove); | 233 typeParameters.forEach(localTypeParameters.remove); |
235 } | 234 } |
236 | 235 |
237 List<TypeParameter> parseAndPushTypeParameterList() { | 236 List<TypeParameter> parseAndPushTypeParameterList() { |
238 int token = scanToken(); | 237 int token = scanToken(); |
239 assert(token == Token.LeftAngle); | 238 assert(token == Token.LeftAngle); |
240 List<TypeParameter> typeParameters = <TypeParameter>[]; | 239 List<TypeParameter> typeParameters = <TypeParameter>[]; |
241 token = peekToken(); | 240 token = peekToken(); |
242 while (token != Token.RightAngle) { | 241 while (token != Token.RightAngle) { |
243 typeParameters.add(parseAndPushTypeParameter()); | 242 typeParameters.add(parseAndPushTypeParameter()); |
244 token = scanToken(); | 243 token = scanToken(); |
245 if (token != Token.Comma && token != Token.RightAngle) { | 244 if (token != Token.Comma && token != Token.RightAngle) { |
246 return fail('Unterminated type parameter list'); | 245 throw fail('Unterminated type parameter list'); |
247 } | 246 } |
248 } | 247 } |
249 return typeParameters; | 248 return typeParameters; |
250 } | 249 } |
251 | 250 |
252 TypeParameter parseAndPushTypeParameter() { | 251 TypeParameter parseAndPushTypeParameter() { |
253 var nameTok = scanToken(); | 252 var nameTok = scanToken(); |
254 if (nameTok != Token.Name) return fail('Expected a name'); | 253 if (nameTok != Token.Name) return fail('Expected a name'); |
255 var typeParameter = new TypeParameter(tokenText); | 254 var typeParameter = new TypeParameter(tokenText); |
256 if (localTypeParameters.containsKey(typeParameter.name)) { | 255 if (localTypeParameters.containsKey(typeParameter.name)) { |
(...skipping 11 matching lines...) Expand all Loading... |
268 } | 267 } |
269 | 268 |
270 dynamic fail(String message) { | 269 dynamic fail(String message) { |
271 throw '$message at index $index'; | 270 throw '$message at index $index'; |
272 } | 271 } |
273 } | 272 } |
274 | 273 |
275 class LazyTypeEnvironment { | 274 class LazyTypeEnvironment { |
276 final Map<String, Class> classes = <String, Class>{}; | 275 final Map<String, Class> classes = <String, Class>{}; |
277 final Map<String, TypeParameter> typeParameters = <String, TypeParameter>{}; | 276 final Map<String, TypeParameter> typeParameters = <String, TypeParameter>{}; |
278 final Library dummyLibrary = new Library(new Uri(path: 'dummy.dart'), | 277 final Library dummyLibrary = |
279 name: 'lib'); | 278 new Library(new Uri(path: 'dummy.dart'), name: 'lib'); |
280 | 279 |
281 TreeNode lookup(String name) { | 280 TreeNode lookup(String name) { |
282 return name.length == 1 | 281 return name.length == 1 |
283 ? typeParameters.putIfAbsent(name, () => new TypeParameter(name)) | 282 ? typeParameters.putIfAbsent(name, () => new TypeParameter(name)) |
284 : classes.putIfAbsent(name, () => makeClass(name)); | 283 : classes.putIfAbsent(name, () => makeClass(name)); |
285 } | 284 } |
286 | 285 |
287 Class makeClass(String name) { | 286 Class makeClass(String name) { |
288 var class_ = new Class(name: name); | 287 var class_ = new Class(name: name); |
289 dummyLibrary.addClass(class_); | 288 dummyLibrary.addClass(class_); |
(...skipping 25 matching lines...) Expand all Loading... |
315 void main(List<String> args) { | 314 void main(List<String> args) { |
316 if (args.length != 1) { | 315 if (args.length != 1) { |
317 print('Usage: type_parser TYPE'); | 316 print('Usage: type_parser TYPE'); |
318 } | 317 } |
319 var environment = new LazyTypeEnvironment(); | 318 var environment = new LazyTypeEnvironment(); |
320 var type = parseDartType(args[0], environment.lookup); | 319 var type = parseDartType(args[0], environment.lookup); |
321 var buffer = new StringBuffer(); | 320 var buffer = new StringBuffer(); |
322 new Printer(buffer).writeType(type); | 321 new Printer(buffer).writeType(type); |
323 print(buffer); | 322 print(buffer); |
324 } | 323 } |
OLD | NEW |