| 1 | /* |
| 2 | * Copyright 2007 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.google.javascript.jscomp; |
| 17 | |
| 18 | /** |
| 19 | * A source excerpt provider is responsible for building source code excerpt |
| 20 | * of specific locations, such as a specific line or a region around a |
| 21 | * given line number. |
| 22 | * |
| 23 | */ |
| 24 | public interface SourceExcerptProvider { |
| 25 | /** |
| 26 | * Source excerpt variety. |
| 27 | */ |
| 28 | enum SourceExcerpt { |
| 29 | /** |
| 30 | * Line excerpt. |
| 31 | */ |
| 32 | LINE { |
| 33 | @Override |
| 34 | public String get(SourceExcerptProvider source, String sourceName, |
| 35 | int lineNumber, ExcerptFormatter formatter) { |
| 36 | return formatter.formatLine( |
| 37 | source.getSourceLine(sourceName, lineNumber), lineNumber); |
| 38 | } |
| 39 | }, |
| 40 | /** |
| 41 | * Region excerpt. |
| 42 | */ |
| 43 | REGION { |
| 44 | @Override |
| 45 | public String get(SourceExcerptProvider source, String sourceName, |
| 46 | int lineNumber, ExcerptFormatter formatter) { |
| 47 | return formatter.formatRegion( |
| 48 | source.getSourceRegion(sourceName, lineNumber)); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Get a source excerpt string based on the type of the source excerpt. |
| 54 | */ |
| 55 | public abstract String get(SourceExcerptProvider source, String sourceName, |
| 56 | int lineNumber, ExcerptFormatter formatter); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the line indicated by the line number. This call will return only the |
| 61 | * specific line. |
| 62 | * |
| 63 | * @param lineNumber the line number, 1 being the first line of the file |
| 64 | * @return the line indicated, or {@code null} if it does not exist |
| 65 | */ |
| 66 | String getSourceLine(String sourceName, int lineNumber); |
| 67 | |
| 68 | /** |
| 69 | * Get a region around the indicated line number. The exact definition of a |
| 70 | * region is implementation specific, but it must contain the line indicated |
| 71 | * by the line number. A region must not start or end by a carriage return. |
| 72 | * |
| 73 | * @param lineNumber the line number, 1 being the first line of the file |
| 74 | * @return the region around the line number indicated, or <code>null</null> |
| 75 | * if it does not exist |
| 76 | */ |
| 77 | Region getSourceRegion(String sourceName, int lineNumber); |
| 78 | |
| 79 | /** |
| 80 | * A excerpt formatter is responsible of formatting source excerpts. |
| 81 | */ |
| 82 | interface ExcerptFormatter { |
| 83 | /** |
| 84 | * Format a line excerpt. |
| 85 | */ |
| 86 | String formatLine(String line, int lineNumber); |
| 87 | |
| 88 | /** |
| 89 | * Format a region excerpt. |
| 90 | */ |
| 91 | String formatRegion(Region region); |
| 92 | } |
| 93 | } |