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

Side by Side Diff: pkg/dartdoc/lib/src/mirrors/dart2js_mirror.dart

Issue 11275097: Merge SourceLocation and Source (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: RegExp used for line numbers Created 8 years, 1 month 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 library mirrors_dart2js; 5 library mirrors_dart2js;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:uri'; 8 import 'dart:uri';
9 9
10 import '../../../../../lib/compiler/compiler.dart' as diagnostics; 10 import '../../../../../lib/compiler/compiler.dart' as diagnostics;
11 import '../../../../../lib/compiler/implementation/elements/elements.dart'; 11 import '../../../../../lib/compiler/implementation/elements/elements.dart';
12 import '../../../../../lib/compiler/implementation/resolution/resolution.dart' 12 import '../../../../../lib/compiler/implementation/resolution/resolution.dart'
13 show ResolverTask, ResolverVisitor; 13 show ResolverTask, ResolverVisitor;
14 import '../../../../../lib/compiler/implementation/apiimpl.dart' as api; 14 import '../../../../../lib/compiler/implementation/apiimpl.dart' as api;
15 import '../../../../../lib/compiler/implementation/scanner/scannerlib.dart'; 15 import '../../../../../lib/compiler/implementation/scanner/scannerlib.dart';
16 import '../../../../../lib/compiler/implementation/ssa/ssa.dart'; 16 import '../../../../../lib/compiler/implementation/ssa/ssa.dart';
17 import '../../../../../lib/compiler/implementation/dart2jslib.dart'; 17 import '../../../../../lib/compiler/implementation/dart2jslib.dart';
18 import '../../../../../lib/compiler/implementation/filenames.dart'; 18 import '../../../../../lib/compiler/implementation/filenames.dart';
19 import '../../../../../lib/compiler/implementation/source_file.dart'; 19 import '../../../../../lib/compiler/implementation/source_file.dart';
20 import '../../../../../lib/compiler/implementation/tree/tree.dart'; 20 import '../../../../../lib/compiler/implementation/tree/tree.dart';
21 import '../../../../../lib/compiler/implementation/util/util.dart'; 21 import '../../../../../lib/compiler/implementation/util/util.dart';
22 import '../../../../../lib/compiler/implementation/util/uri_extras.dart'; 22 import '../../../../../lib/compiler/implementation/util/uri_extras.dart';
23 import '../../../../../lib/compiler/implementation/dart2js.dart'; 23 import '../../../../../lib/compiler/implementation/dart2js.dart';
24 import '../../../../../lib/compiler/implementation/util/characters.dart';
24 25
25 // TODO(rnystrom): Use "package:" URL (#4968). 26 // TODO(rnystrom): Use "package:" URL (#4968).
26 import '../../mirrors.dart'; 27 import '../../mirrors.dart';
27 import 'util.dart'; 28 import 'util.dart';
28 29
29 //------------------------------------------------------------------------------ 30 //------------------------------------------------------------------------------
30 // Utility types and functions for the dart2js mirror system 31 // Utility types and functions for the dart2js mirror system
31 //------------------------------------------------------------------------------ 32 //------------------------------------------------------------------------------
32 33
33 bool _isPrivate(String name) { 34 bool _isPrivate(String name) {
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 422
422 Dart2JsElementMirror(this.mirrors, this._element) { 423 Dart2JsElementMirror(this.mirrors, this._element) {
423 assert (mirrors !== null); 424 assert (mirrors !== null);
424 assert (_element !== null); 425 assert (_element !== null);
425 } 426 }
426 427
427 String get simpleName => _element.name.slowToString(); 428 String get simpleName => _element.name.slowToString();
428 429
429 String get displayName => simpleName; 430 String get displayName => simpleName;
430 431
431 SourceLocation get location => new Dart2JsLocation( 432 SourceLocation get location => new Dart2JsSourceLocation(
432 _element.getCompilationUnit().script, 433 _element.getCompilationUnit().script,
433 mirrors.compiler.spanFromElement(_element)); 434 mirrors.compiler.spanFromElement(_element));
434 435
435 String toString() => _element.toString(); 436 String toString() => _element.toString();
436 437
437 int get hashCode => qualifiedName.hashCode; 438 int get hashCode => qualifiedName.hashCode;
438 } 439 }
439 440
440 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror { 441 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror {
441 final Dart2JsMirrorSystem mirrors; 442 final Dart2JsMirrorSystem mirrors;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 } 611 }
611 } 612 }
612 613
613 Map<String, ClassMirror> get classes { 614 Map<String, ClassMirror> get classes {
614 _ensureClasses(); 615 _ensureClasses();
615 return new ImmutableMapWrapper<String, ClassMirror>(_classes); 616 return new ImmutableMapWrapper<String, ClassMirror>(_classes);
616 } 617 }
617 618
618 SourceLocation get location { 619 SourceLocation get location {
619 var script = _library.getCompilationUnit().script; 620 var script = _library.getCompilationUnit().script;
620 return new Dart2JsLocation( 621 SourceSpan span;
621 script, 622 if (_library.libraryTag != null) {
622 new SourceSpan(script.uri, 0, script.text.length)); 623 span = mirrors.compiler.spanFromNode(_library.libraryTag, script.uri);
624 } else {
625 span = new SourceSpan(script.uri, 0, 0);
626 }
627 return new Dart2JsSourceLocation(script, span);
623 } 628 }
624 } 629 }
625 630
626 class Dart2JsLocation implements SourceLocation { 631 class Dart2JsSourceLocation implements SourceLocation {
627 Script _script; 632 final Script _script;
628 SourceSpan _span; 633 final SourceSpan _span;
634 int _line;
635 int _column;
629 636
630 Dart2JsLocation(this._script, this._span); 637 Dart2JsSourceLocation(this._script, this._span);
631 638
632 int get start => _span.begin; 639 int _computeLine() {
640 var sourceFile = _script.file as SourceFile;
641 if (sourceFile != null) {
642 return sourceFile.getLine(offset) + 1;
643 }
644 var lineNumber = 0;
645 for (Match match in const RegExp("\r\n?|\n\r?").allMatches(sourceText)) {
646 lineNumber++;
647 if (match.start >= offset) {
648 break;
649 }
650 }
651 return lineNumber;
652 }
633 653
634 int get end => _span.end; 654 int get line {
655 if (_line == null) {
656 _line = _computeLine();
657 }
658 return _line;
659 }
635 660
636 Source get source => new Dart2JsSource(_script); 661 int _computeColumn() {
662 if (length == 0) return 0;
637 663
638 String get text => _script.text.substring(start, end); 664 var sourceFile = _script.file as SourceFile;
639 } 665 if (sourceFile != null) {
666 return sourceFile.getColumn(sourceFile.getLine(offset), offset) + 1;
667 }
668 int index = offset - 1;
669 var columnNumber = 0;
670 while (0 <= index && index < sourceText.length) {
671 columnNumber++;
672 var charCode = sourceText.charCodeAt(index);
673 if (charCode == $CR || charCode == $LF) {
674 break;
675 }
676 index--;
677 }
678 return columnNumber;
679 }
640 680
641 class Dart2JsSource implements Source { 681 int get column {
642 Script _script; 682 if (_column == null) {
683 _column = _computeColumn();
684 }
685 return _column;
686 }
643 687
644 Dart2JsSource(this._script); 688 int get offset => _span.begin;
645 689
646 Uri get uri => _script.uri; 690 int get length => _span.end - _span.begin;
647 691
648 String get text => _script.text; 692 String get text => _script.text.substring(_span.begin, _span.end);
693
694 Uri get sourceUri => _script.uri;
695
696 String get sourceText => _script.text;
649 } 697 }
650 698
651 class Dart2JsParameterMirror extends Dart2JsMemberMirror 699 class Dart2JsParameterMirror extends Dart2JsMemberMirror
652 implements ParameterMirror { 700 implements ParameterMirror {
653 final MethodMirror _method; 701 final MethodMirror _method;
654 final bool isOptional; 702 final bool isOptional;
655 final bool isNamed; 703 final bool isNamed;
656 704
657 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system, 705 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system,
658 MethodMirror method, 706 MethodMirror method,
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 DeclarationMirror get owner => library; 805 DeclarationMirror get owner => library;
758 806
759 String get qualifiedName => '${library.qualifiedName}.${simpleName}'; 807 String get qualifiedName => '${library.qualifiedName}.${simpleName}';
760 808
761 SourceLocation get location { 809 SourceLocation get location {
762 if (_class is PartialClassElement) { 810 if (_class is PartialClassElement) {
763 var node = _class.parseNode(mirrors.compiler); 811 var node = _class.parseNode(mirrors.compiler);
764 if (node !== null) { 812 if (node !== null) {
765 var script = _class.getCompilationUnit().script; 813 var script = _class.getCompilationUnit().script;
766 var span = mirrors.compiler.spanFromNode(node, script.uri); 814 var span = mirrors.compiler.spanFromNode(node, script.uri);
767 return new Dart2JsLocation(script, span); 815 return new Dart2JsSourceLocation(script, span);
768 } 816 }
769 } 817 }
770 return super.location; 818 return super.location;
771 } 819 }
772 820
773 void _ensureMembers() { 821 void _ensureMembers() {
774 if (_members == null) { 822 if (_members == null) {
775 _members = <String, Dart2JsMemberMirror>{}; 823 _members = <String, Dart2JsMemberMirror>{};
776 _class.forEachMember((_, e) { 824 _class.forEachMember((_, e) {
777 for (var member in _convertElementMemberToMemberMirrors(this, e)) { 825 for (var member in _convertElementMemberToMemberMirrors(this, e)) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 940
893 TypedefType get _typedef => _type; 941 TypedefType get _typedef => _type;
894 942
895 String get qualifiedName => '${library.qualifiedName}.${simpleName}'; 943 String get qualifiedName => '${library.qualifiedName}.${simpleName}';
896 944
897 SourceLocation get location { 945 SourceLocation get location {
898 var node = _typedef.element.parseNode(_diagnosticListener); 946 var node = _typedef.element.parseNode(_diagnosticListener);
899 if (node !== null) { 947 if (node !== null) {
900 var script = _typedef.element.getCompilationUnit().script; 948 var script = _typedef.element.getCompilationUnit().script;
901 var span = mirrors.compiler.spanFromNode(node, script.uri); 949 var span = mirrors.compiler.spanFromNode(node, script.uri);
902 return new Dart2JsLocation(script, span); 950 return new Dart2JsSourceLocation(script, span);
903 } 951 }
904 return super.location; 952 return super.location;
905 } 953 }
906 954
907 LibraryMirror get library => _library; 955 LibraryMirror get library => _library;
908 956
909 bool get isTypedef => true; 957 bool get isTypedef => true;
910 958
911 List<TypeMirror> get typeArguments { 959 List<TypeMirror> get typeArguments {
912 throw new UnsupportedError( 960 throw new UnsupportedError(
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 implements Dart2JsTypeMirror { 1064 implements Dart2JsTypeMirror {
1017 final DartType _type; 1065 final DartType _type;
1018 1066
1019 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type) 1067 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type)
1020 : super(system); 1068 : super(system);
1021 1069
1022 String get simpleName => _type.name.slowToString(); 1070 String get simpleName => _type.name.slowToString();
1023 1071
1024 SourceLocation get location { 1072 SourceLocation get location {
1025 var script = _type.element.getCompilationUnit().script; 1073 var script = _type.element.getCompilationUnit().script;
1026 return new Dart2JsLocation(script, 1074 return new Dart2JsSourceLocation(script,
1027 mirrors.compiler.spanFromElement(_type.element)); 1075 mirrors.compiler.spanFromElement(_type.element));
1028 } 1076 }
1029 1077
1030 DeclarationMirror get owner => library; 1078 DeclarationMirror get owner => library;
1031 1079
1032 LibraryMirror get library { 1080 LibraryMirror get library {
1033 return mirrors._getLibrary(_type.element.getLibrary()); 1081 return mirrors._getLibrary(_type.element.getLibrary());
1034 } 1082 }
1035 1083
1036 bool get isObject => false; 1084 bool get isObject => false;
1037 1085
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 1475
1428 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR; 1476 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR;
1429 1477
1430 String get operatorName => _operatorName; 1478 String get operatorName => _operatorName;
1431 1479
1432 SourceLocation get location { 1480 SourceLocation get location {
1433 var node = _function.parseNode(_diagnosticListener); 1481 var node = _function.parseNode(_diagnosticListener);
1434 if (node !== null) { 1482 if (node !== null) {
1435 var script = _function.getCompilationUnit().script; 1483 var script = _function.getCompilationUnit().script;
1436 var span = mirrors.compiler.spanFromNode(node, script.uri); 1484 var span = mirrors.compiler.spanFromNode(node, script.uri);
1437 return new Dart2JsLocation(script, span); 1485 return new Dart2JsSourceLocation(script, span);
1438 } 1486 }
1439 return super.location; 1487 return super.location;
1440 } 1488 }
1441 1489
1442 } 1490 }
1443 1491
1444 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { 1492 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror {
1445 Dart2JsContainerMirror _objectMirror; 1493 Dart2JsContainerMirror _objectMirror;
1446 VariableElement _variable; 1494 VariableElement _variable;
1447 1495
(...skipping 20 matching lines...) Expand all
1468 1516
1469 TypeMirror get type => _convertTypeToTypeMirror(mirrors, 1517 TypeMirror get type => _convertTypeToTypeMirror(mirrors,
1470 _variable.computeType(mirrors.compiler), 1518 _variable.computeType(mirrors.compiler),
1471 mirrors.compiler.types.dynamicType); 1519 mirrors.compiler.types.dynamicType);
1472 1520
1473 SourceLocation get location { 1521 SourceLocation get location {
1474 var script = _variable.getCompilationUnit().script; 1522 var script = _variable.getCompilationUnit().script;
1475 var node = _variable.variables.parseNode(_diagnosticListener); 1523 var node = _variable.variables.parseNode(_diagnosticListener);
1476 if (node !== null) { 1524 if (node !== null) {
1477 var span = mirrors.compiler.spanFromNode(node, script.uri); 1525 var span = mirrors.compiler.spanFromNode(node, script.uri);
1478 return new Dart2JsLocation(script, span); 1526 return new Dart2JsSourceLocation(script, span);
1479 } else { 1527 } else {
1480 var span = mirrors.compiler.spanFromElement(_variable); 1528 var span = mirrors.compiler.spanFromElement(_variable);
1481 return new Dart2JsLocation(script, span); 1529 return new Dart2JsSourceLocation(script, span);
1482 } 1530 }
1483 } 1531 }
1484 } 1532 }
1485 1533
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698