JS: Extract RegExp ASTs from string literals

This commit is contained in:
Asger F
2019-10-14 12:35:34 +01:00
parent 0cf191f70d
commit 0e1246c0e5
4 changed files with 18 additions and 4 deletions

View File

@@ -59,6 +59,11 @@ public class Literal extends Expression implements ITypeExpression {
return tokenType == TokenType.regexp;
}
/** Is this a string literal? */
public boolean isStringLiteral() {
return tokenType == TokenType.string;
}
/** The value of this literal expressed as a string. */
public String getStringValue() {
// regular expressions may have a null value; use the raw value instead

View File

@@ -516,7 +516,11 @@ public class ASTExtractor {
String valueString = nd.getStringValue();
trapwriter.addTuple("literals", valueString, source, key);
if (nd.isRegExp()) regexpExtractor.extract(source.substring(1, source.lastIndexOf('/')), nd);
if (nd.isRegExp()) {
regexpExtractor.extract(source.substring(1, source.lastIndexOf('/')), nd, false);
} else if (nd.isStringLiteral()) {
regexpExtractor.extract(valueString, nd, true);
}
return key;
}

View File

@@ -341,9 +341,14 @@ public class RegExpExtractor {
}
}
public void extract(String src, Node parent) {
this.literalStart = parent.getLoc().getStart();
public void extract(String src, Node parent, boolean isSpeculativeParsing) {
Result res = parser.parse(src);
if (isSpeculativeParsing && res.getErrors().size() > 0) {
return;
}
this.literalStart = parent.getLoc().getStart();
RegExpTerm ast = res.getAST();
new V().visit(ast, trapwriter.localID(parent), 0);

View File

@@ -815,7 +815,7 @@ regexpterm (unique int id: @regexpterm,
int idx: int ref,
varchar(900) tostring: string ref);
@regexpparent = @regexpterm | @regexpliteral;
@regexpparent = @regexpterm | @regexpliteral | @stringliteral;
case @regexpterm.kind of
0 = @regexp_alt