| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library error; | 4 library error; |
| 5 | 5 |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
| 8 | 8 |
| 9 import 'generated/error.dart'; | 9 import 'generated/error.dart'; |
| 10 import 'generated/source.dart'; | 10 import 'generated/source.dart'; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 /// representation. | 37 /// representation. |
| 38 class AnalyzerError implements Exception { | 38 class AnalyzerError implements Exception { |
| 39 final AnalysisError error; | 39 final AnalysisError error; |
| 40 | 40 |
| 41 AnalyzerError(this.error); | 41 AnalyzerError(this.error); |
| 42 | 42 |
| 43 String get message => toString(); | 43 String get message => toString(); |
| 44 | 44 |
| 45 String toString() { | 45 String toString() { |
| 46 var builder = new StringBuffer(); | 46 var builder = new StringBuffer(); |
| 47 var content = error.source.contents.data; | |
| 48 var beforeError = content.substring(0, error.offset); | |
| 49 var lineNumber = "\n".allMatches(beforeError).length + 1; | |
| 50 builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " | |
| 51 "${error.message}"); | |
| 52 | 47 |
| 53 var errorLineIndex = beforeError.lastIndexOf("\n") + 1; | 48 // Print a less friendly string representation to ensure that |
| 54 var errorEndOfLineIndex = content.indexOf("\n", error.offset); | 49 // error.source.contents is not executed, as .contents it isn't async |
| 55 if (errorEndOfLineIndex == -1) errorEndOfLineIndex = content.length; | 50 builder.writeln("Error in ${error.source.fullName}: ${error.message}"); |
| 56 var errorLine = content.substring( | |
| 57 errorLineIndex, errorEndOfLineIndex); | |
| 58 var errorColumn = error.offset - errorLineIndex; | |
| 59 var errorLength = error.length; | |
| 60 | 51 |
| 61 // Ensure that the error line we display isn't too long. | 52 // var content = error.source.contents.data; |
| 62 if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { | 53 // var beforeError = content.substring(0, error.offset); |
| 63 var leftLength = errorColumn; | 54 // var lineNumber = "\n".allMatches(beforeError).length + 1; |
| 64 var rightLength = errorLine.length - leftLength; | 55 // builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " |
| 65 if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && | 56 // "${error.message}"); |
| 66 rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { | |
| 67 errorLine = "..." + errorLine.substring( | |
| 68 errorColumn - _MAX_ERROR_LINE_LENGTH ~/ 2 + 3, | |
| 69 errorColumn + _MAX_ERROR_LINE_LENGTH ~/ 2 - 3) | |
| 70 + "..."; | |
| 71 errorColumn = _MAX_ERROR_LINE_LENGTH ~/ 2; | |
| 72 } else if (rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { | |
| 73 errorLine = errorLine.substring(0, _MAX_ERROR_LINE_LENGTH - 3) + "..."; | |
| 74 } else { | |
| 75 assert(leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2); | |
| 76 errorColumn -= errorLine.length - _MAX_ERROR_LINE_LENGTH; | |
| 77 errorLine = "..." + errorLine.substring( | |
| 78 errorLine.length - _MAX_ERROR_LINE_LENGTH + 3, errorLine.length); | |
| 79 } | |
| 80 errorLength = math.min(errorLength, _MAX_ERROR_LINE_LENGTH - errorColumn); | |
| 81 } | |
| 82 builder.writeln(errorLine); | |
| 83 | 57 |
| 84 for (var i = 0; i < errorColumn; i++) builder.write(" "); | 58 // var errorLineIndex = beforeError.lastIndexOf("\n") + 1; |
| 85 for (var i = 0; i < errorLength; i++) builder.write("^"); | 59 // var errorEndOfLineIndex = content.indexOf("\n", error.offset); |
| 60 // if (errorEndOfLineIndex == -1) errorEndOfLineIndex = content.length; |
| 61 // var errorLine = content.substring( |
| 62 // errorLineIndex, errorEndOfLineIndex); |
| 63 // var errorColumn = error.offset - errorLineIndex; |
| 64 // var errorLength = error.length; |
| 65 // |
| 66 // // Ensure that the error line we display isn't too long. |
| 67 // if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { |
| 68 // var leftLength = errorColumn; |
| 69 // var rightLength = errorLine.length - leftLength; |
| 70 // if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && |
| 71 // rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { |
| 72 // errorLine = "..." + errorLine.substring( |
| 73 // errorColumn - _MAX_ERROR_LINE_LENGTH ~/ 2 + 3, |
| 74 // errorColumn + _MAX_ERROR_LINE_LENGTH ~/ 2 - 3) |
| 75 // + "..."; |
| 76 // errorColumn = _MAX_ERROR_LINE_LENGTH ~/ 2; |
| 77 // } else if (rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { |
| 78 // errorLine = errorLine.substring(0, _MAX_ERROR_LINE_LENGTH - 3) + "..."
; |
| 79 // } else { |
| 80 // assert(leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2); |
| 81 // errorColumn -= errorLine.length - _MAX_ERROR_LINE_LENGTH; |
| 82 // errorLine = "..." + errorLine.substring( |
| 83 // errorLine.length - _MAX_ERROR_LINE_LENGTH + 3, errorLine.length); |
| 84 // } |
| 85 // errorLength = math.min(errorLength, _MAX_ERROR_LINE_LENGTH - errorColumn
); |
| 86 // } |
| 87 // builder.writeln(errorLine); |
| 88 // |
| 89 // for (var i = 0; i < errorColumn; i++) builder.write(" "); |
| 90 // for (var i = 0; i < errorLength; i++) builder.write("^"); |
| 86 builder.writeln(); | 91 builder.writeln(); |
| 87 | 92 |
| 88 return builder.toString(); | 93 return builder.toString(); |
| 89 } | 94 } |
| 90 } | 95 } |
| 91 | 96 |
| 92 // A content receiver that collects all the content into a string. | 97 // A content receiver that collects all the content into a string. |
| 93 class _ContentReceiver implements Source_ContentReceiver { | 98 class _ContentReceiver implements Source_ContentReceiver { |
| 94 final _buffer = new StringBuffer(); | 99 final _buffer = new StringBuffer(); |
| 95 | 100 |
| 96 String get result => _buffer.toString(); | 101 String get result => _buffer.toString(); |
| 97 | 102 |
| 98 void accept(String contents, _) => | 103 void accept(String contents, _) => |
| 99 _buffer.write(contents.substring(0, contents.length)); | 104 _buffer.write(contents.substring(0, contents.length)); |
| 100 } | 105 } |
| OLD | NEW |