| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 package com.google.dart.compiler; | 5 package com.google.dart.compiler; |
| 6 | 6 |
| 7 import com.google.common.base.Objects; | 7 import com.google.common.base.Objects; |
| 8 import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; | 8 import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; |
| 9 | 9 |
| 10 import java.io.PrintStream; | 10 import java.io.PrintStream; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * An error formatter that simply prints the file name with the line and column | 13 * An error formatter that simply prints the file name with the line and column |
| 14 * location. | 14 * location. |
| 15 */ | 15 */ |
| 16 public class DefaultErrorFormatter implements ErrorFormatter { | 16 public class DefaultErrorFormatter implements ErrorFormatter { |
| 17 protected final PrintStream outputStream; | 17 protected final PrintStream outputStream; |
| 18 protected final ErrorFormat errorFormat; | 18 protected final ErrorFormat errorFormat; |
| 19 | 19 |
| 20 public DefaultErrorFormatter(PrintStream outputStream, ErrorFormat errorFormat
) { | 20 public DefaultErrorFormatter(PrintStream outputStream, ErrorFormat errorFormat
) { |
| 21 this.outputStream = outputStream; | 21 this.outputStream = outputStream; |
| 22 this.errorFormat = errorFormat; | 22 this.errorFormat = errorFormat; |
| 23 } | 23 } |
| 24 | 24 |
| 25 @Override | 25 @Override |
| 26 public void format(DartCompilationError event) { | 26 public void format(DartCompilationError event) { |
| 27 String sourceName = "<unknown-source-file>"; | 27 StringBuilder buf = new StringBuilder(); |
| 28 Source sourceFile = event.getSource(); | 28 appendError(buf, event); |
| 29 String includeFrom = getImportString(sourceFile); | 29 outputStream.print(buf); |
| 30 | 30 outputStream.print("\n"); |
| 31 if (sourceFile != null) { | |
| 32 sourceName = sourceFile.getUri().toString(); | |
| 33 } | |
| 34 outputStream.printf("%s:%d:%d: %s%s\n", | |
| 35 sourceName, | |
| 36 event.getLineNumber(), | |
| 37 event.getColumnNumber(), | |
| 38 event.getMessage(), | |
| 39 includeFrom); | |
| 40 } | 31 } |
| 41 | 32 |
| 42 public String getImportString(Source sourceFile) { | 33 protected void appendError(StringBuilder buf, DartCompilationError error) { |
| 34 Source source = error.getSource(); |
| 35 String sourceName = getSourceName(source); |
| 36 int line = error.getLineNumber(); |
| 37 int col = error.getColumnNumber(); |
| 38 int length = error.getLength(); |
| 39 if (errorFormat == ErrorFormat.MACHINE) { |
| 40 buf.append(String.format( |
| 41 "%s|%s|%s|%s|%d|%d|%d|%s", |
| 42 escapePipe(error.getErrorCode().getErrorSeverity().toString()), |
| 43 escapePipe(error.getErrorCode().getSubSystem().toString()), |
| 44 escapePipe(error.getErrorCode().toString()), |
| 45 escapePipe(sourceName), |
| 46 line, |
| 47 col, |
| 48 length, |
| 49 escapePipe(error.getMessage()))); |
| 50 } else { |
| 51 String includeFrom = getImportString(source); |
| 52 buf.append(String.format( |
| 53 "%s:%d:%d: %s%s", |
| 54 sourceName, |
| 55 line, |
| 56 col, |
| 57 error.getMessage(), |
| 58 includeFrom)); |
| 59 } |
| 60 } |
| 61 |
| 62 protected static String getImportString(Source sourceFile) { |
| 43 String includeFrom = ""; | 63 String includeFrom = ""; |
| 44 if (sourceFile instanceof DartSource) { | 64 if (sourceFile instanceof DartSource) { |
| 45 LibrarySource lib = ((DartSource) sourceFile).getLibrary(); | 65 LibrarySource lib = ((DartSource) sourceFile).getLibrary(); |
| 46 if (lib != null && !Objects.equal(sourceFile.getUri(), lib.getUri())) { | 66 if (lib != null && !Objects.equal(sourceFile.getUri(), lib.getUri())) { |
| 47 includeFrom = " (sourced from " + lib.getUri() + ")"; | 67 includeFrom = " (sourced from " + lib.getUri() + ")"; |
| 48 } | 68 } |
| 49 } | 69 } |
| 50 return includeFrom; | 70 return includeFrom; |
| 51 } | 71 } |
| 72 |
| 73 protected static String getSourceName(Source source) { |
| 74 if (source instanceof UrlDartSource) { |
| 75 return source.getUri().toString(); |
| 76 } |
| 77 if (source != null) { |
| 78 return source.getName(); |
| 79 } |
| 80 return "<unknown-source-file>"; |
| 81 } |
| 82 |
| 83 protected static String escapePipe(String input) { |
| 84 StringBuilder result = new StringBuilder(); |
| 85 for (char c : input.toCharArray()) { |
| 86 if (c == '\\' || c == '|') { |
| 87 result.append('\\'); |
| 88 } |
| 89 result.append(c); |
| 90 } |
| 91 return result.toString(); |
| 92 } |
| 52 } | 93 } |
| OLD | NEW |