Debian Codegraph @

src/com/google/javascript/jscomp/deps/DepsFileParser.java

raw symbols SCIP
1 /*
2 * Copyright 2008 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
17 package com.google.javascript.jscomp.deps;
18
19 import com.google.common.base.CharMatcher;
20 import com.google.common.base.Function;
21 import com.google.common.base.Functions;
22 import com.google.common.collect.Lists;
23 import com.google.javascript.jscomp.ErrorManager;
24
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.util.List;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
35 /**
36 * A parser that can extract dependency information from existing deps.js files.
37 *
38 * <p>See //javascript/closure/deps.js for an example file.</p>
39 *
40 * @author [email protected] (Andrew Grieve)
41 */
42 public class DepsFileParser extends JsFileLineParser {
43
44 private static Logger logger = Logger.getLogger(DepsFileParser.class.getName());
45
46 /**
47 * Pattern for matching JavaScript string literals. The group is:
48 * goog.addDependency({1});
49 */
50 private final Matcher depMatcher =
51 Pattern.compile("\\s*goog.addDependency\\((.*)\\);?\\s*").matcher("");
52
53 /**
54 * Pattern for matching the args of a goog.addDependency(). The group is:
55 * goog.addDependency({1}, {2}, {3});
56 */
57 private final Matcher depArgsMatch =
58 Pattern.compile("\\s*([^,]*), (\\[[^\\]]*\\]), (\\[[^\\]]*\\])\\s*").matcher("");
59
60 /**
61 * The dependency information extracted from the current file.
62 */
63 private List<DependencyInfo> depInfos;
64
65 /** Translates paths in different build systems. */
66 private final Function<String, String> pathTranslator;
67
68 /**
69 * Constructor
70 *
71 * @param errorManager Handles parse errors.
72 */
73 public DepsFileParser(ErrorManager errorManager) {
74 this(Functions.<String>identity(), errorManager);
75 }
76
77 /**
78 * @param pathTranslator Translates paths in different build systems.
79 * @param errorManager Handles parse errors.
80 */
81 public DepsFileParser(Function<String, String> pathTranslator,
82 ErrorManager errorManager) {
83 super(errorManager);
84 this.pathTranslator = pathTranslator;
85 }
86
87 /**
88 * Parses the given file and returns a list of dependency information that it
89 * contained.
90 *
91 * @param filePath Path to the file to parse.
92 * @return A list of DependencyInfo objects.
93 * @throws IOException Thrown if the file could not be read.
94 */
95 public List<DependencyInfo> parseFile(String filePath) throws IOException {
96 return parseFileReader(filePath, new FileReader(filePath));
97 }
98
99 /**
100 * Parses the given file and returns a list of dependency information that it
101 * contained.
102 * It uses the passed in fileContents instead of reading the file.
103 *
104 * @param filePath Path to the file to parse.
105 * @param fileContents The contents to parse.
106 * @return A list of DependencyInfo objects.
107 */
108 public List<DependencyInfo> parseFile(String filePath, String fileContents) {
109 return parseFileReader(filePath, new StringReader(fileContents));
110 }
111
112
113 /**
114 * Parses the file from the given reader and returns a list of
115 * dependency information that it contained.
116 *
117 * @param filePath Path to the file to parse.
118 * @param reader A reader for the file.
119 * @return A list of DependencyInfo objects.
120 */
121 public List<DependencyInfo> parseFileReader(String filePath, Reader reader) {
122 depInfos = Lists.newArrayList();
123 logger.fine("Parsing Dep: " + filePath);
124 doParse(filePath, reader);
125 return depInfos;
126 }
127
128 /**
129 * Extracts dependency information from lines that look like
130 * goog.addDependency('pathRelativeToClosure', ['provides'], ['requires']);
131 * Adds the dependencies to depInfos.
132 *
133 * @throws ParseException Thrown if the given line has a malformed
134 * goog.addDependency().
135 */
136 @Override
137 protected boolean parseLine(String line) throws ParseException {
138 boolean hasDependencies = false;
139
140 // Quick sanity check that will catch most cases. This is a performance
141 // win for people with a lot of JS.
142 if (line.indexOf("addDependency") != -1) {
143 depMatcher.reset(line);
144 // See if the line looks like: goog.addDependency(...)
145 if (depMatcher.matches()) {
146 hasDependencies = true;
147 String addDependencyParams = depMatcher.group(1);
148 depArgsMatch.reset(addDependencyParams);
149 // Extract the three parameters.
150 if (!depArgsMatch.matches()) {
151 // Although we could recover, we mark this as fatal since there should
152 // not be problems with generated deps.js files.
153 throw new ParseException("Invalid arguments to goog.addDependency(). Found: "
154 + addDependencyParams, true);
155 }
156 // Parse the file path.
157 String path = pathTranslator.apply(parseJsString(depArgsMatch.group(1)));
158 DependencyInfo depInfo = new SimpleDependencyInfo(path, filePath,
159 // Parse the provides.
160 parseJsStringArray(depArgsMatch.group(2)),
161 // Parse the requires.
162 parseJsStringArray(depArgsMatch.group(3)));
163
164 if (logger.isLoggable(Level.FINE)) {
165 logger.fine("Found dep: " + depInfo);
166 }
167 depInfos.add(depInfo);
168 }
169 }
170
171 return !shortcutMode || hasDependencies ||
172 CharMatcher.WHITESPACE.matchesAllOf(line);
173 }
174 }