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

Side by Side Diff: sdk/lib/core/exceptions.dart

Issue 396603003: Change "FormatException.position" to be named "offset". (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reapply change, with necessary fixes in span_execption.dart Created 6 years, 5 months 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
« no previous file with comments | « pkg/source_span/lib/src/span_exception.dart ('k') | tests/corelib/format_exception_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 part of dart.core; 5 part of dart.core;
6 6
7 // Exceptions are thrown either by the VM or from Dart code. 7 // Exceptions are thrown either by the VM or from Dart code.
8 8
9 /** 9 /**
10 * A marker interface implemented by all core library exceptions. 10 * A marker interface implemented by all core library exceptions.
(...skipping 26 matching lines...) Expand all
37 37
38 /** 38 /**
39 * Exception thrown when a string or some other data does not have an expected 39 * Exception thrown when a string or some other data does not have an expected
40 * format and cannot be parsed or processed. 40 * format and cannot be parsed or processed.
41 */ 41 */
42 class FormatException implements Exception { 42 class FormatException implements Exception {
43 /** 43 /**
44 * A message describing the format error. 44 * A message describing the format error.
45 */ 45 */
46 final String message; 46 final String message;
47
47 /** 48 /**
48 * The source that caused the error. 49 * The actual source input that caused the error.
49 * 50 *
50 * This is usually a [String], but can be other types too. If it is a string, 51 * This is usually a [String], but can be other types too.
51 * parts of it may be included in the [toString] message. 52 * If it is a string, parts of it may be included in the [toString] message.
52 * 53 *
53 * May also be `null` if omitted. 54 * The source is `null` if omitted or unknown.
54 */ 55 */
55 final source; 56 final source;
57
56 /** 58 /**
57 * The position in source where the error was detected. 59 * The offset in [source] where the error was detected.
58 * 60 *
59 * May be omitted. If present, [source] should also be present. 61 * A zero-based offset into the source that marks the format error causing
62 * this exception to be created. If `source` is a string, this should be a
63 * string index in the range `0 <= offset <= source.length`.
64 *
65 * If input is a string, the [toString] method may represent this offset as
66 * a line and character position. The offset should be inside the string,
67 * or at the end of the string.
68 *
69 * May be omitted. If present, [source] should also be present if possible.
60 */ 70 */
61 final int position; 71 final int offset;
62 72
63 /** 73 /**
64 * Creates a new FormatException with an optional error [message]. 74 * Creates a new FormatException with an optional error [message].
65 * 75 *
66 * Optionally also supply the [source] that had the incorrect format, and 76 * Optionally also supply the actual [source] that had the incorrect format,
67 * even the [position] in the format where this was detected. 77 * and an [offset] in the format where a problem was detected.
68 */ 78 */
69 const FormatException([this.message = "", this.source, this.position]); 79 const FormatException([this.message = "", this.source, this.offset]);
70 80
71 /** 81 /**
72 * Returns a description of the format exception. 82 * Returns a description of the format exception.
73 * 83 *
74 * The description always contains the [message]. 84 * The description always contains the [message].
75 * If [source] was provided, the description will contain (at least a part of)
76 * the source.
77 * If [position] is also provided, the part of the source included will
78 * contain that position, and the position will be marked.
79 * 85 *
80 * If the source contains a line break before position, only the line 86 * If [source] is present and is a string, the description will contain
81 * containing position will be included, and its line number will also be 87 * (at least a part of) the source.
82 * part of the description. Line and character offsets are 1-based. 88 * If [offset] is also provided, the part of the source included will
89 * contain that offset, and the offset will be marked.
90 *
91 * If the source is a string and it contains a line break before offset,
92 * only the line containing offset will be included, and its line number
93 * will also be part of the description. Line and character offsets are
94 * 1-based.
83 */ 95 */
84 String toString() { 96 String toString() {
85 String report = "FormatException"; 97 String report = "FormatException";
86 if (message != null && "" != message) { 98 if (message != null && "" != message) {
87 report = "$report: $message"; 99 report = "$report: $message";
88 } 100 }
89 int position = this.position; 101 int offset = this.offset;
90 if (source is! String) { 102 if (source is! String) {
91 if (position != null) { 103 if (offset != null) {
92 report += " (at position $position)"; 104 report += " (at offset $offset)";
93 } 105 }
94 return report; 106 return report;
95 } 107 }
96 if (position != null && (position < 0 || position > source.length)) { 108 if (offset != null && (offset < 0 || offset > source.length)) {
97 position = null; 109 offset = null;
98 } 110 }
99 // Source is string and position is null or valid. 111 // Source is string and offset is null or valid.
100 if (position == null) { 112 if (offset == null) {
101 String source = this.source; 113 String source = this.source;
102 if (source.length > 78) { 114 if (source.length > 78) {
103 source = source.substring(0, 75) + "..."; 115 source = source.substring(0, 75) + "...";
104 } 116 }
105 return "$report\n$source"; 117 return "$report\n$source";
106 } 118 }
107 int lineNum = 1; 119 int lineNum = 1;
108 int lineStart = 0; 120 int lineStart = 0;
109 bool lastWasCR; 121 bool lastWasCR;
110 for (int i = 0; i < position; i++) { 122 for (int i = 0; i < offset; i++) {
111 int char = source.codeUnitAt(i); 123 int char = source.codeUnitAt(i);
112 if (char == 0x0a) { 124 if (char == 0x0a) {
113 if (lineStart != i || !lastWasCR) { 125 if (lineStart != i || !lastWasCR) {
114 lineNum++; 126 lineNum++;
115 } 127 }
116 lineStart = i + 1; 128 lineStart = i + 1;
117 lastWasCR = false; 129 lastWasCR = false;
118 } else if (char == 0x0d) { 130 } else if (char == 0x0d) {
119 lineNum++; 131 lineNum++;
120 lineStart = i + 1; 132 lineStart = i + 1;
121 lastWasCR = true; 133 lastWasCR = true;
122 } 134 }
123 } 135 }
124 if (lineNum > 1) { 136 if (lineNum > 1) {
125 report += " (at line $lineNum, character ${position - lineStart + 1})\n"; 137 report += " (at line $lineNum, character ${offset - lineStart + 1})\n";
126 } else { 138 } else {
127 report += " (at character ${position + 1})\n"; 139 report += " (at character ${offset + 1})\n";
128 } 140 }
129 int lineEnd = source.length; 141 int lineEnd = source.length;
130 for (int i = position; i < source.length; i++) { 142 for (int i = offset; i < source.length; i++) {
131 int char = source.codeUnitAt(i); 143 int char = source.codeUnitAt(i);
132 if (char == 0x0a || char == 0x0d) { 144 if (char == 0x0a || char == 0x0d) {
133 lineEnd = i; 145 lineEnd = i;
134 break; 146 break;
135 } 147 }
136 } 148 }
137 int length = lineEnd - lineStart; 149 int length = lineEnd - lineStart;
138 int start = lineStart; 150 int start = lineStart;
139 int end = lineEnd; 151 int end = lineEnd;
140 String prefix = ""; 152 String prefix = "";
141 String postfix = ""; 153 String postfix = "";
142 if (length > 78) { 154 if (length > 78) {
143 // Can't show entire line. Try to anchor at the nearest end, if 155 // Can't show entire line. Try to anchor at the nearest end, if
144 // one is within reach. 156 // one is within reach.
145 int index = position - lineStart; 157 int index = offset - lineStart;
146 if (index < 75) { 158 if (index < 75) {
147 end = start + 75; 159 end = start + 75;
148 postfix = "..."; 160 postfix = "...";
149 } else if (end - position < 75) { 161 } else if (end - offset < 75) {
150 start = end - 75; 162 start = end - 75;
151 prefix = "..."; 163 prefix = "...";
152 } else { 164 } else {
153 // Neither end is near, just pick an area around the position. 165 // Neither end is near, just pick an area around the offset.
154 start = position - 36; 166 start = offset - 36;
155 end = position + 36; 167 end = offset + 36;
156 prefix = postfix = "..."; 168 prefix = postfix = "...";
157 } 169 }
158 } 170 }
159 String slice = source.substring(start, end); 171 String slice = source.substring(start, end);
160 int markOffset = position - start + prefix.length; 172 int markOffset = offset - start + prefix.length;
161 return "$report$prefix$slice$postfix\n${" " * markOffset}^\n"; 173 return "$report$prefix$slice$postfix\n${" " * markOffset}^\n";
162 } 174 }
163 } 175 }
164 176
165 class IntegerDivisionByZeroException implements Exception { 177 class IntegerDivisionByZeroException implements Exception {
166 const IntegerDivisionByZeroException(); 178 const IntegerDivisionByZeroException();
167 String toString() => "IntegerDivisionByZeroException"; 179 String toString() => "IntegerDivisionByZeroException";
168 } 180 }
OLDNEW
« no previous file with comments | « pkg/source_span/lib/src/span_exception.dart ('k') | tests/corelib/format_exception_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698