| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.deps; |
| 18 |
|
| 19 |
import com.google.common.base.CharMatcher; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.javascript.jscomp.ErrorManager; |
| 22 |
|
| 23 |
import java.io.FileReader; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.Reader; |
| 26 |
import java.io.StringReader; |
| 27 |
import java.util.List; |
| 28 |
import java.util.logging.Logger; |
| 29 |
import java.util.regex.Matcher; |
| 30 |
import java.util.regex.Pattern; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class JsFileParser extends JsFileLineParser { |
| 39 |
|
| 40 |
private static Logger logger = Logger.getLogger(JsFileParser.class.getName()); |
| 41 |
|
| 42 |
|
| 43 |
private static final Pattern GOOG_PROVIDE_REQUIRE_PATTERN = Pattern.compile( |
| 44 |
"(?:^|;)\\s*goog\\.(provide|require|addDependency)\\s*\\((.*?)\\)"); |
| 45 |
|
| 46 |
|
| 47 |
private static final String BASE_JS_START = "var COMPILED = false;"; |
| 48 |
|
| 49 |
|
| 50 |
private Matcher googMatcher = GOOG_PROVIDE_REQUIRE_PATTERN.matcher(""); |
| 51 |
|
| 52 |
|
| 53 |
private List<String> provides; |
| 54 |
private List<String> requires; |
| 55 |
private boolean fileHasProvidesOrRequires; |
| 56 |
|
| 57 |
|
| 58 |
private boolean includeGoogBase = false; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public JsFileParser(ErrorManager errorManager) { |
| 66 |
super(errorManager); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public JsFileParser setIncludeGoogBase(boolean include) { |
| 83 |
includeGoogBase = include; |
| 84 |
return this; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public DependencyInfo parseFile(String filePath, String closureRelativePath) |
| 98 |
throws IOException { |
| 99 |
return parseReader(filePath, closureRelativePath, new FileReader(filePath)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public DependencyInfo parseFile(String filePath, String closureRelativePath, |
| 113 |
String fileContents) { |
| 114 |
return parseReader(filePath, closureRelativePath, |
| 115 |
new StringReader(fileContents)); |
| 116 |
} |
| 117 |
|
| 118 |
private DependencyInfo parseReader(String filePath, |
| 119 |
String closureRelativePath, Reader fileContents) { |
| 120 |
provides = Lists.newArrayList(); |
| 121 |
requires = Lists.newArrayList(); |
| 122 |
fileHasProvidesOrRequires = false; |
| 123 |
|
| 124 |
logger.fine("Parsing Source: " + filePath); |
| 125 |
doParse(filePath, fileContents); |
| 126 |
|
| 127 |
DependencyInfo dependencyInfo = new SimpleDependencyInfo( |
| 128 |
closureRelativePath, filePath, provides, requires); |
| 129 |
logger.fine("DepInfo: " + dependencyInfo); |
| 130 |
return dependencyInfo; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
protected boolean parseLine(String line) throws ParseException { |
| 139 |
boolean lineHasProvidesOrRequires = false; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (line.indexOf("provide") != -1 || |
| 144 |
line.indexOf("require") != -1 || |
| 145 |
line.indexOf("addDependency") != -1) { |
| 146 |
|
| 147 |
googMatcher.reset(line); |
| 148 |
while (googMatcher.find()) { |
| 149 |
lineHasProvidesOrRequires = true; |
| 150 |
|
| 151 |
if (includeGoogBase && !fileHasProvidesOrRequires) { |
| 152 |
fileHasProvidesOrRequires = true; |
| 153 |
requires.add("goog"); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
char firstChar = googMatcher.group(1).charAt(0); |
| 158 |
boolean isProvide = firstChar == 'p'; |
| 159 |
boolean isRequire = firstChar == 'r'; |
| 160 |
|
| 161 |
if (isProvide || isRequire) { |
| 162 |
|
| 163 |
String arg = parseJsString(googMatcher.group(2)); |
| 164 |
|
| 165 |
|
| 166 |
if (isRequire) { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if (!"goog".equals(arg)) { |
| 171 |
requires.add(arg); |
| 172 |
} |
| 173 |
} else { |
| 174 |
provides.add(arg); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} else if (includeGoogBase && line.startsWith(BASE_JS_START) && |
| 179 |
provides.isEmpty() && requires.isEmpty()) { |
| 180 |
provides.add("goog"); |
| 181 |
|
| 182 |
|
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
return !shortcutMode || lineHasProvidesOrRequires || |
| 187 |
CharMatcher.WHITESPACE.matchesAllOf(line); |
| 188 |
} |
| 189 |
} |