| 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.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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 52 |
private Pattern pattern; |
| 53 |
|
| 54 |
|
| 55 |
private Matcher matcher; |
| 56 |
|
| 57 |
|
| 58 |
private Collection<SymbolInfo> symbols; |
| 59 |
|
| 60 |
|
| 61 |
private Collection<String> functionsToParse; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 91 |
sb.deleteCharAt(sb.length() - 1); |
| 92 |
sb.append(")\\s*\\((.*?)\\)"); |
| 93 |
|
| 94 |
return Pattern.compile(sb.toString()); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
protected boolean parseLine(String line) throws ParseException { |
| 126 |
boolean hasFunctions = false; |
| 127 |
boolean parseLine = false; |
| 128 |
|
| 129 |
|
| 130 |
|
| 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)); |
| 144 |
symbols.add(new SymbolInfo(functionName, arg)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return !shortcutMode || hasFunctions || |
| 149 |
CharMatcher.WHITESPACE.matchesAllOf(line); |
| 150 |
} |
| 151 |
} |