Debian Codegraph @

src/com/google/javascript/jscomp/deps/JsFunctionParser.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.collect.Lists;
21 import com.google.javascript.jscomp.ErrorManager;
22
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.util.Collection;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 /**
31 * A parser that can extract dependency information from a .js file.
32 *
33 * @author [email protected] (Andrew Grieve)
34 * @author [email protected] (Islam El-Ashi)
35 */
36 public class JsFunctionParser extends JsFileLineParser {
37
38 public static class SymbolInfo {
39 public final String functionName;
40 public final String symbol;
41
42 private SymbolInfo(String functionName, String symbol) {
43 this.functionName = functionName;
44 this.symbol = symbol;
45 }
46 }
47
48 private static Logger logger =
49 Logger.getLogger(JsFunctionParser.class.getName());
50
51 /** Pattern for matching functions. */
52 private Pattern pattern;
53
54 /** Matcher used in the parsing. */
55 private Matcher matcher;
56
57 /** Symbols parsed. */
58 private Collection<SymbolInfo> symbols;
59
60 /** Functions to parse */
61 private Collection<String> functionsToParse;
62
63 /**
64 * Constructor
65 *
66 * @param functions Functions to parse.
67 * @param errorManager Handles parse errors.
68 */
69 public JsFunctionParser(
70 Collection<String> functions, ErrorManager errorManager) {
71 super(errorManager);
72 functionsToParse = functions;
73 pattern = getPattern(functions);
74 matcher = pattern.matcher("");
75 }
76
77 /**
78 * Constructs a pattern to extract the arguments of the given functions.
79 *
80 * @param functions Functions to parse.
81 * @return A pattern to extract {@code functions}' arguments.
82 */
83 private Pattern getPattern(Collection<String> functions) {
84 StringBuilder sb = new StringBuilder("(?:^|;)\\s*(");
85
86 for (String function : functions) {
87 sb.append(Pattern.quote(function) + "|");
88 }
89
90 // remove last '|'
91 sb.deleteCharAt(sb.length() - 1);
92 sb.append(")\\s*\\((.*?)\\)");
93
94 return Pattern.compile(sb.toString());
95 }
96
97 /**
98 * Parses the given file and returns the dependency information that it
99 * contained.
100 *
101 * @param filePath Path to the file to parse.
102 * @param fileContents The contents to parse.
103 * @return A collection containing all symbols found in the
104 * file.
105 */
106 public Collection<SymbolInfo> parseFile(
107 String filePath, String fileContents) {
108 return parseReader(filePath, new StringReader(fileContents));
109 }
110
111 private Collection<SymbolInfo> parseReader(
112 String filePath, Reader fileContents) {
113 symbols = Lists.newArrayList();
114
115 logger.fine("Parsing Source: " + filePath);
116 doParse(filePath, fileContents);
117
118 return symbols;
119 }
120
121 /**
122 * Parses a line of JavaScript, extracting dependency information.
123 */
124 @Override
125 protected boolean parseLine(String line) throws ParseException {
126 boolean hasFunctions = false;
127 boolean parseLine = false;
128
129 // Quick sanity check that will catch most cases. This is a performance
130 // win for people with a lot of JS.
131 for (String function : functionsToParse) {
132 if (line.indexOf(function) != -1) {
133 parseLine = true;
134 break;
135 }
136 }
137
138 if (parseLine) {
139 matcher.reset(line);
140 while (matcher.find()) {
141 hasFunctions = true;
142 String functionName = matcher.group(1);
143 String arg = parseJsString(matcher.group(2)); // Parse the param.
144 symbols.add(new SymbolInfo(functionName, arg));
145 }
146 }
147
148 return !shortcutMode || hasFunctions ||
149 CharMatcher.WHITESPACE.matchesAllOf(line);
150 }
151 }