Chromium Code Reviews| 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.common; | 5 package com.google.dart.compiler.common; |
| 6 | 6 |
| 7 import com.google.common.base.Preconditions; | 7 import com.google.common.base.Preconditions; |
| 8 import com.google.common.collect.ImmutableList; | 8 import com.google.common.collect.ImmutableList; |
| 9 import com.google.common.collect.Lists; | 9 import com.google.common.collect.Lists; |
| 10 import com.google.common.collect.MapMaker; | 10 import com.google.common.collect.MapMaker; |
| 11 import com.google.dart.compiler.Source; | 11 import com.google.dart.compiler.Source; |
| 12 | 12 |
| 13 import java.io.BufferedReader; | 13 import java.io.BufferedReader; |
| 14 import java.io.IOException; | |
| 14 import java.io.Serializable; | 15 import java.io.Serializable; |
| 15 import java.util.Collections; | 16 import java.util.Collections; |
| 16 import java.util.List; | 17 import java.util.List; |
| 17 import java.util.Map; | 18 import java.util.Map; |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * Contains {@link Source} and location information for AST nodes. | 21 * Contains {@link Source} and location information for AST nodes. |
| 21 * <p> | 22 * <p> |
| 22 * Each node in the subtree (other than the contrived nodes) carries source rang e(s) information | 23 * Each node in the subtree (other than the contrived nodes) carries source rang e(s) information |
| 23 * relating back to positions in the given source (the given source itself is no t remembered with | 24 * relating back to positions in the given source (the given source itself is no t remembered with |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 lines.put(source, linesInfo); | 60 lines.put(source, linesInfo); |
| 60 } | 61 } |
| 61 return linesInfo; | 62 return linesInfo; |
| 62 } | 63 } |
| 63 | 64 |
| 64 /** | 65 /** |
| 65 * @return the new {@link LinesInfo}, may be empty if some {@link Exception} h appens, but not | 66 * @return the new {@link LinesInfo}, may be empty if some {@link Exception} h appens, but not |
| 66 * <code>null</code>. | 67 * <code>null</code>. |
| 67 */ | 68 */ |
| 68 private static LinesInfo createLinesInfo(Source source) { | 69 private static LinesInfo createLinesInfo(Source source) { |
| 70 BufferedReader reader = null; | |
| 69 try { | 71 try { |
| 70 BufferedReader reader = new BufferedReader(source.getSourceReader()); | 72 reader = new BufferedReader(source.getSourceReader()); |
| 71 int offset = 0; | 73 int offset = 0; |
| 72 boolean ignoreLF = false; | 74 boolean ignoreLF = false; |
| 73 List<Integer> lineOffsets = Lists.newArrayList(0); | 75 List<Integer> lineOffsets = Lists.newArrayList(0); |
| 74 while (true) { | 76 while (true) { |
| 75 int charValue = reader.read(); | 77 int charValue = reader.read(); |
| 76 if (charValue == -1) { | 78 if (charValue == -1) { |
| 77 break; | 79 break; |
| 78 } | 80 } |
| 79 offset++; | 81 offset++; |
| 80 char c = (char) charValue; | 82 char c = (char) charValue; |
| 81 ignoreLF = c == '\n'; | 83 ignoreLF = c == '\n'; |
| 82 if (c == '\n' || c == '\r' && !ignoreLF) { | 84 if (c == '\n' || c == '\r' && !ignoreLF) { |
| 83 lineOffsets.add(offset); | 85 lineOffsets.add(offset); |
| 84 } | 86 } |
| 85 } | 87 } |
| 86 return new LinesInfo(lineOffsets); | 88 return new LinesInfo(lineOffsets); |
| 87 } catch (Throwable e) { | 89 } catch (Throwable e) { |
| 88 return new LinesInfo(ImmutableList.of(0)); | 90 return new LinesInfo(ImmutableList.of(0)); |
| 91 } finally { | |
| 92 if (reader != null) { | |
|
scheglov
2012/03/16 21:33:22
com.google.common.io.Closeables.closeQuietly(Close
Brian Wilkerson
2012/03/16 21:45:07
Sure, but in a later CL.
| |
| 93 try { | |
| 94 reader.close(); | |
| 95 } catch (IOException e) { | |
| 96 // Ignored | |
| 97 } | |
| 98 } | |
| 89 } | 99 } |
| 90 } | 100 } |
| 91 | 101 |
| 92 /** | 102 /** |
| 93 * @return the {@link Source}. | 103 * @return the {@link Source}. |
| 94 */ | 104 */ |
| 95 public Source getSource() { | 105 public Source getSource() { |
| 96 return source; | 106 return source; |
| 97 } | 107 } |
| 98 | 108 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 165 } |
| 156 return -(2 + index); | 166 return -(2 + index); |
| 157 } | 167 } |
| 158 | 168 |
| 159 int getColumnOfOffset(int offset) { | 169 int getColumnOfOffset(int offset) { |
| 160 int line = getLineOfOffset(offset); | 170 int line = getLineOfOffset(offset); |
| 161 return offset - getLineOffset(line); | 171 return offset - getLineOffset(line); |
| 162 } | 172 } |
| 163 } | 173 } |
| 164 } | 174 } |
| OLD | NEW |