OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 package com.google.dart.compiler.backend.js; | |
6 | |
7 /** | |
8 * Indicates inability to parse JavaScript source. | |
9 */ | |
10 public class JsParserException extends Exception { | |
11 | |
12 /** | |
13 * Represents the location of a parser exception. | |
14 */ | |
15 public static class SourceDetail { | |
16 private final String fileName; | |
17 private final int line; | |
18 private final int lineOffset; | |
19 private final String lineSource; | |
20 | |
21 public SourceDetail(int line, String lineSource, int lineOffset, String file
Name) { | |
22 this.line = line; | |
23 this.lineSource = lineSource; | |
24 this.lineOffset = lineOffset; | |
25 this.fileName = fileName; | |
26 } | |
27 | |
28 public String getFileName() { | |
29 return fileName; | |
30 } | |
31 | |
32 public int getLine() { | |
33 return line; | |
34 } | |
35 | |
36 public int getLineOffset() { | |
37 return lineOffset; | |
38 } | |
39 | |
40 public String getLineSource() { | |
41 return lineSource; | |
42 } | |
43 } | |
44 | |
45 private static String createMessageWithDetail(String msg, SourceDetail sourceD
etail) { | |
46 if (sourceDetail == null) { | |
47 return msg; | |
48 } | |
49 StringBuffer sb = new StringBuffer(); | |
50 sb.append(sourceDetail.getFileName()); | |
51 sb.append('('); | |
52 sb.append(sourceDetail.getLine()); | |
53 sb.append(')'); | |
54 sb.append(": "); | |
55 sb.append(msg); | |
56 if (sourceDetail.getLineSource() != null) { | |
57 sb.append("\n> "); | |
58 sb.append(sourceDetail.getLineSource()); | |
59 sb.append("\n> "); | |
60 for (int i = 0, n = sourceDetail.getLineOffset(); i < n; ++i) { | |
61 sb.append('-'); | |
62 } | |
63 sb.append('^'); | |
64 } | |
65 return sb.toString(); | |
66 } | |
67 | |
68 private final SourceDetail sourceDetail; | |
69 | |
70 public JsParserException(String msg) { | |
71 this(msg, null); | |
72 } | |
73 | |
74 public JsParserException(String msg, int line, String lineSource, int lineOffs
et, String fileName) { | |
75 this(msg, new SourceDetail(line, lineSource, lineOffset, fileName)); | |
76 } | |
77 | |
78 public JsParserException(String msg, SourceDetail sourceDetail) { | |
79 super(createMessageWithDetail(msg, sourceDetail)); | |
80 this.sourceDetail = sourceDetail; | |
81 } | |
82 | |
83 /** | |
84 * Provides additional source detail in some cases. | |
85 * | |
86 * @return additional detail regarding the error, or <code>null</code> if no | |
87 * additional detail is available | |
88 */ | |
89 public SourceDetail getSourceDetail() { | |
90 return sourceDetail; | |
91 } | |
92 } | |
OLD | NEW |