| 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 codegen.protocol; | 5 library codegen.protocol; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'api.dart'; | 9 import 'api.dart'; |
| 10 import 'codegen_tools.dart'; | 10 import 'codegen_tools.dart'; |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 writeln(); | 271 writeln(); |
| 272 } | 272 } |
| 273 if (emitSpecialGetters(className)) { | 273 if (emitSpecialGetters(className)) { |
| 274 writeln(); | 274 writeln(); |
| 275 } | 275 } |
| 276 emitToJsonMember(type); | 276 emitToJsonMember(type); |
| 277 writeln(); | 277 writeln(); |
| 278 if (emitToRequestMember(impliedType)) { | 278 if (emitToRequestMember(impliedType)) { |
| 279 writeln(); | 279 writeln(); |
| 280 } | 280 } |
| 281 if (emitToResponseMember(impliedType)) { |
| 282 writeln(); |
| 283 } |
| 281 if (emitSpecialMembers(className)) { | 284 if (emitSpecialMembers(className)) { |
| 282 writeln(); | 285 writeln(); |
| 283 } | 286 } |
| 284 writeln('@override'); | 287 writeln('@override'); |
| 285 writeln('String toString() => JSON.encode(toJson());'); | 288 writeln('String toString() => JSON.encode(toJson());'); |
| 286 writeln(); | 289 writeln(); |
| 287 emitObjectEqualsMember(type, className); | 290 emitObjectEqualsMember(type, className); |
| 288 writeln(); | 291 writeln(); |
| 289 emitObjectHashCode(type); | 292 emitObjectHashCode(type); |
| 290 }); | 293 }); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 bool emitSpecialConstructors(String className) { | 341 bool emitSpecialConstructors(String className) { |
| 339 switch (className) { | 342 switch (className) { |
| 340 case 'AnalysisError': | 343 case 'AnalysisError': |
| 341 docComment([new dom.Text( | 344 docComment([new dom.Text( |
| 342 'Construct based on error information from the analyzer engine.')]); | 345 'Construct based on error information from the analyzer engine.')]); |
| 343 writeln( | 346 writeln( |
| 344 'factory AnalysisError.fromEngine(engine.LineInfo lineInfo, engine.A
nalysisError error) =>' | 347 'factory AnalysisError.fromEngine(engine.LineInfo lineInfo, engine.A
nalysisError error) =>' |
| 345 ); | 348 ); |
| 346 writeln(' _analysisErrorFromEngine(lineInfo, error);'); | 349 writeln(' _analysisErrorFromEngine(lineInfo, error);'); |
| 347 return true; | 350 return true; |
| 351 case 'Element': |
| 352 docComment([new dom.Text('Construct based on a value from the analyzer e
ngine.')]); |
| 353 writeln('factory Element.fromEngine(engine.Element element) =>'); |
| 354 writeln(' elementFromEngine(element);'); |
| 355 return true; |
| 348 case 'ElementKind': | 356 case 'ElementKind': |
| 349 docComment([new dom.Text( | 357 docComment([new dom.Text( |
| 350 'Construct based on a value from the analyzer engine.')]); | 358 'Construct based on a value from the analyzer engine.')]); |
| 351 writeln('factory ElementKind.fromEngine(engine.ElementKind kind) =>'); | 359 writeln('factory ElementKind.fromEngine(engine.ElementKind kind) =>'); |
| 352 writeln(' _elementKindFromEngine(kind);'); | 360 writeln(' _elementKindFromEngine(kind);'); |
| 353 return true; | 361 return true; |
| 362 case 'Location': |
| 363 docComment([new dom.Text( |
| 364 'Construct based on an element from the analyzer engine.')]); |
| 365 writeln('factory Location.fromElement(engine.Element element) =>'); |
| 366 writeln(' _locationFromElement(element);'); |
| 367 writeln(); |
| 368 docComment([new dom.Text('Create a Location based on an element and offs
et from the analyzer engine.')]); |
| 369 writeln('factory Location.fromOffset(engine.Element element, int offset,
int length) =>'); |
| 370 writeln(' _locationFromOffset(element, offset, length);'); |
| 371 return true; |
| 372 case 'OverriddenMember': |
| 373 docComment([new dom.Text('Construct based on an element from the analyze
r engine.')]); |
| 374 writeln('factory OverriddenMember.fromEngine(engine.Element member) =>')
; |
| 375 writeln(' _overriddenMemberFromEngine(member);'); |
| 376 return true; |
| 377 case 'SearchResult': |
| 378 docComment([new dom.Text('Construct based on a value from the search eng
ine.')]); |
| 379 writeln('factory SearchResult.fromMatch(engine.SearchMatch match) =>'); |
| 380 writeln(' searchResultFromMatch(match);'); |
| 381 return true; |
| 382 case 'SearchResultKind': |
| 383 docComment([new dom.Text('Construct based on a value from the search eng
ine.')]); |
| 384 writeln('factory SearchResultKind.fromEngine(engine.MatchKind kind) =>')
; |
| 385 writeln(' _searchResultKindFromEngine(kind);'); |
| 386 return true; |
| 354 case 'SourceEdit': | 387 case 'SourceEdit': |
| 355 docComment([new dom.Text('Construct based on a SourceRange.')]); | 388 docComment([new dom.Text('Construct based on a SourceRange.')]); |
| 356 writeln( | 389 writeln( |
| 357 'SourceEdit.range(engine.SourceRange range, String replacement, {Str
ing id})'); | 390 'SourceEdit.range(engine.SourceRange range, String replacement, {Str
ing id})'); |
| 358 writeln(' : this(range.offset, range.length, replacement, id: id);'); | 391 writeln(' : this(range.offset, range.length, replacement, id: id);'); |
| 359 return true; | 392 return true; |
| 360 default: | 393 default: |
| 361 return false; | 394 return false; |
| 362 } | 395 } |
| 363 } | 396 } |
| 364 | 397 |
| 365 /** | 398 /** |
| 366 * If the class named [className] requires special getters, emit them and | 399 * If the class named [className] requires special getters, emit them and |
| 367 * return true. | 400 * return true. |
| 368 */ | 401 */ |
| 369 bool emitSpecialGetters(String className) { | 402 bool emitSpecialGetters(String className) { |
| 370 switch (className) { | 403 switch (className) { |
| 371 case 'Element': | 404 case 'Element': |
| 372 for (String name in specialElementFlags.keys) { | 405 for (String name in specialElementFlags.keys) { |
| 373 String flag = 'FLAG_${name.toUpperCase()}'; | 406 String flag = 'FLAG_${name.toUpperCase()}'; |
| 374 writeln('bool get ${camelJoin(['is', name])} => (flags & $flag) != 0;' | 407 writeln('bool get ${camelJoin(['is', name])} => (flags & $flag) != 0;' |
| 375 ); | 408 ); |
| 376 } | 409 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 388 * If the class named [className] requires special members, emit them and | 421 * If the class named [className] requires special members, emit them and |
| 389 * return true. | 422 * return true. |
| 390 */ | 423 */ |
| 391 bool emitSpecialMembers(String className) { | 424 bool emitSpecialMembers(String className) { |
| 392 switch (className) { | 425 switch (className) { |
| 393 case 'SourceEdit': | 426 case 'SourceEdit': |
| 394 docComment([new dom.Text( | 427 docComment([new dom.Text( |
| 395 'Get the result of applying the edit to the given [code].')]); | 428 'Get the result of applying the edit to the given [code].')]); |
| 396 writeln('String apply(String code) => _applyEdit(code, this);'); | 429 writeln('String apply(String code) => _applyEdit(code, this);'); |
| 397 return true; | 430 return true; |
| 431 case 'SourceFileEdit': |
| 432 docComment([new dom.Text('Adds the given [Edit] to the list.')]); |
| 433 writeln('void add(SourceEdit edit) => _addEditForSource(this, edit);'); |
| 434 writeln(); |
| 435 docComment([new dom.Text('Adds the given [Edit]s.')]); |
| 436 writeln('void addAll(Iterable<SourceEdit> edits) =>'); |
| 437 writeln(' _addAllEditsForSource(this, edits);'); |
| 438 return true; |
| 398 default: | 439 default: |
| 399 return false; | 440 return false; |
| 400 } | 441 } |
| 401 } | 442 } |
| 402 | 443 |
| 403 /** | 444 /** |
| 404 * Emit the constructor for an object class. | 445 * Emit the constructor for an object class. |
| 405 */ | 446 */ |
| 406 void emitObjectConstructor(TypeObject type, String className) { | 447 void emitObjectConstructor(TypeObject type, String className) { |
| 407 List<String> args = <String>[]; | 448 List<String> args = <String>[]; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 Request).longMethod); | 506 Request).longMethod); |
| 466 writeln('return new Request(id, $methodString, toJson());'); | 507 writeln('return new Request(id, $methodString, toJson());'); |
| 467 }); | 508 }); |
| 468 writeln('}'); | 509 writeln('}'); |
| 469 return true; | 510 return true; |
| 470 } | 511 } |
| 471 return false; | 512 return false; |
| 472 } | 513 } |
| 473 | 514 |
| 474 /** | 515 /** |
| 516 * Emit the toResponse() code for a class, if appropriate. Returns true if |
| 517 * code was emitted. |
| 518 */ |
| 519 bool emitToResponseMember(ImpliedType impliedType) { |
| 520 if (impliedType.kind == 'requestResult') { |
| 521 writeln('Response toResponse(String id) {'); |
| 522 indent(() { |
| 523 writeln('return new Response(id, result: toJson());'); |
| 524 }); |
| 525 writeln('}'); |
| 526 return true; |
| 527 } |
| 528 return false; |
| 529 } |
| 530 |
| 531 /** |
| 475 * Emit the operator== code for an object class. | 532 * Emit the operator== code for an object class. |
| 476 */ | 533 */ |
| 477 void emitObjectEqualsMember(TypeObject type, String className) { | 534 void emitObjectEqualsMember(TypeObject type, String className) { |
| 478 writeln('@override'); | 535 writeln('@override'); |
| 479 writeln('bool operator==(other) {'); | 536 writeln('bool operator==(other) {'); |
| 480 indent(() { | 537 indent(() { |
| 481 writeln('if (other is $className) {'); | 538 writeln('if (other is $className) {'); |
| 482 indent(() { | 539 indent(() { |
| 483 var comparisons = <String>[]; | 540 var comparisons = <String>[]; |
| 484 for (TypeObjectField field in type.fields) { | 541 for (TypeObjectField field in type.fields) { |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); | 1033 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); |
| 977 return visitor.collectCode(visitor.visitApi); | 1034 return visitor.collectCode(visitor.visitApi); |
| 978 }); | 1035 }); |
| 979 | 1036 |
| 980 /** | 1037 /** |
| 981 * Translate spec_input.html into protocol_matchers.dart. | 1038 * Translate spec_input.html into protocol_matchers.dart. |
| 982 */ | 1039 */ |
| 983 main() { | 1040 main() { |
| 984 target.generate(); | 1041 target.generate(); |
| 985 } | 1042 } |
| OLD | NEW |