Swift: Add a simple Regex library.

This commit is contained in:
Geoffrey White
2023-06-05 15:36:19 +01:00
parent c994b4b9dd
commit e04f6bff27

View File

@@ -0,0 +1,76 @@
/**
* Provides classes and predicates for reasoning about use of regular expressions.
*/
import swift
/**
* A call that evaluates a regular expression. For example:
* ```
* Regex("(a|b).*").firstMatch(in: myString)
* ```
*/
abstract class RegexEval extends CallExpr {
Expr regex;
Expr input;
/**
* Gets the regular expression that is evaluated.
*/
Expr getRegex() { result = regex }
/**
* Gets the input string the regular expression is evaluated on.
*/
Expr getInput() { result = input }
}
/**
* A call to a function that always evaluates a regular expression.
*/
private class AlwaysRegexEval extends RegexEval {
AlwaysRegexEval() {
this.getStaticTarget()
.(Method)
.hasQualifiedName("Regex", ["firstMatch(in:)", "prefixMatch(in:)", "wholeMatch(in:)"]) and
regex = this.getQualifier() and
input = this.getArgument(0).getExpr()
or
this.getStaticTarget()
.(Method)
.hasQualifiedName("NSRegularExpression",
[
"numberOfMatches(in:options:range:)", "enumerateMatches(in:options:range:using:)",
"matches(in:options:range:)", "firstMatch(in:options:range:)",
"rangeOfFirstMatch(in:options:range:)",
"replaceMatches(in:options:range:withTemplate:)",
"stringByReplacingMatches(in:options:range:withTemplate:)"
]) and
regex = this.getQualifier() and
input = this.getArgument(0).getExpr()
or
this.getStaticTarget()
.(Method)
.hasQualifiedName("BidirectionalCollection",
[
"contains(_:)", "firstMatch(of:)", "firstRange(of:)", "matches(of:)",
"prefixMatch(of:)", "ranges(of:)",
"split(separator:maxSplits:omittingEmptySubsequences:)", "starts(with:)",
"trimmingPrefix(_:)", "wholeMatch(of:)"
]) and
regex = this.getArgument(0).getExpr() and
input = this.getQualifier()
or
this.getStaticTarget()
.(Method)
.hasQualifiedName("RangeReplaceableCollection",
[
"replace(_:maxReplacements:with:)", "replace(_:with:maxReplacements:)",
"replacing(_:maxReplacements:with:)", "replacing(_:subrange:maxReplacements:with:)",
"replacing(_:with:maxReplacements:)", "replacing(_:with:subrange:maxReplacements:)",
"trimPrefix(_:)"
]) and
regex = this.getArgument(0).getExpr() and
input = this.getQualifier()
}
}