Skip to content

Commit e04f6bf

Browse files
committed
Swift: Add a simple Regex library.
1 parent c994b4b commit e04f6bf

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Provides classes and predicates for reasoning about use of regular expressions.
3+
*/
4+
5+
import swift
6+
7+
/**
8+
* A call that evaluates a regular expression. For example:
9+
* ```
10+
* Regex("(a|b).*").firstMatch(in: myString)
11+
* ```
12+
*/
13+
abstract class RegexEval extends CallExpr {
14+
Expr regex;
15+
Expr input;
16+
17+
/**
18+
* Gets the regular expression that is evaluated.
19+
*/
20+
Expr getRegex() { result = regex }
21+
22+
/**
23+
* Gets the input string the regular expression is evaluated on.
24+
*/
25+
Expr getInput() { result = input }
26+
}
27+
28+
/**
29+
* A call to a function that always evaluates a regular expression.
30+
*/
31+
private class AlwaysRegexEval extends RegexEval {
32+
AlwaysRegexEval() {
33+
this.getStaticTarget()
34+
.(Method)
35+
.hasQualifiedName("Regex", ["firstMatch(in:)", "prefixMatch(in:)", "wholeMatch(in:)"]) and
36+
regex = this.getQualifier() and
37+
input = this.getArgument(0).getExpr()
38+
or
39+
this.getStaticTarget()
40+
.(Method)
41+
.hasQualifiedName("NSRegularExpression",
42+
[
43+
"numberOfMatches(in:options:range:)", "enumerateMatches(in:options:range:using:)",
44+
"matches(in:options:range:)", "firstMatch(in:options:range:)",
45+
"rangeOfFirstMatch(in:options:range:)",
46+
"replaceMatches(in:options:range:withTemplate:)",
47+
"stringByReplacingMatches(in:options:range:withTemplate:)"
48+
]) and
49+
regex = this.getQualifier() and
50+
input = this.getArgument(0).getExpr()
51+
or
52+
this.getStaticTarget()
53+
.(Method)
54+
.hasQualifiedName("BidirectionalCollection",
55+
[
56+
"contains(_:)", "firstMatch(of:)", "firstRange(of:)", "matches(of:)",
57+
"prefixMatch(of:)", "ranges(of:)",
58+
"split(separator:maxSplits:omittingEmptySubsequences:)", "starts(with:)",
59+
"trimmingPrefix(_:)", "wholeMatch(of:)"
60+
]) and
61+
regex = this.getArgument(0).getExpr() and
62+
input = this.getQualifier()
63+
or
64+
this.getStaticTarget()
65+
.(Method)
66+
.hasQualifiedName("RangeReplaceableCollection",
67+
[
68+
"replace(_:maxReplacements:with:)", "replace(_:with:maxReplacements:)",
69+
"replacing(_:maxReplacements:with:)", "replacing(_:subrange:maxReplacements:with:)",
70+
"replacing(_:with:maxReplacements:)", "replacing(_:with:subrange:maxReplacements:)",
71+
"trimPrefix(_:)"
72+
]) and
73+
regex = this.getArgument(0).getExpr() and
74+
input = this.getQualifier()
75+
}
76+
}

0 commit comments

Comments
 (0)