Skip to content

Commit a7a609d

Browse files
committed
Swift: Add tests for parse modes.
1 parent 7748a45 commit a7a609d

File tree

2 files changed

+132
-68
lines changed

2 files changed

+132
-68
lines changed

swift/ql/test/library-tests/regex/parse.expected

Lines changed: 102 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6319,167 +6319,204 @@ redos_variants.swift:
63196319
# 579| [RegExpConstant, RegExpNormalChar] c
63206320

63216321
regex.swift:
6322-
# 103| [RegExpDot] .
6322+
# 110| [RegExpDot] .
63236323

6324-
# 103| [RegExpStar] .*
6324+
# 110| [RegExpStar] .*
63256325
#-----| 0 -> [RegExpDot] .
63266326

6327-
# 125| [RegExpDot] .
6327+
# 132| [RegExpDot] .
63286328

6329-
# 125| [RegExpStar] .*
6329+
# 132| [RegExpStar] .*
63306330
#-----| 0 -> [RegExpDot] .
63316331

6332-
# 142| [RegExpDot] .
6332+
# 149| [RegExpDot] .
63336333

6334-
# 142| [RegExpStar] .*
6334+
# 149| [RegExpStar] .*
63356335
#-----| 0 -> [RegExpDot] .
63366336

6337-
# 142| [RegExpDot] .
6337+
# 149| [RegExpDot] .
63386338

6339-
# 142| [RegExpPlus] .+
6339+
# 149| [RegExpPlus] .+
63406340
#-----| 0 -> [RegExpDot] .
63416341

6342-
# 149| [RegExpGroup] ([\w.]+)
6342+
# 156| [RegExpGroup] ([\w.]+)
63436343
#-----| 0 -> [RegExpPlus] [\w.]+
63446344

6345-
# 149| [RegExpStar] ([\w.]+)*
6345+
# 156| [RegExpStar] ([\w.]+)*
63466346
#-----| 0 -> [RegExpGroup] ([\w.]+)
63476347

6348-
# 149| [RegExpCharacterClass] [\w.]
6348+
# 156| [RegExpCharacterClass] [\w.]
63496349
#-----| 0 -> [RegExpCharacterClassEscape] \w
63506350
#-----| 1 -> [RegExpConstant, RegExpNormalChar] .
63516351

6352-
# 149| [RegExpPlus] [\w.]+
6352+
# 156| [RegExpPlus] [\w.]+
63536353
#-----| 0 -> [RegExpCharacterClass] [\w.]
63546354

6355-
# 149| [RegExpCharacterClassEscape] \w
6355+
# 156| [RegExpCharacterClassEscape] \w
63566356

6357-
# 149| [RegExpConstant, RegExpNormalChar] .
6357+
# 156| [RegExpConstant, RegExpNormalChar] .
63586358

6359-
# 156| [RegExpConstant, RegExpNormalChar]
6360-
# 156|
6359+
# 163| [RegExpConstant, RegExpNormalChar]
6360+
# 163|
63616361

6362-
# 157| [RegExpConstant, RegExpEscape] \n
6362+
# 164| [RegExpConstant, RegExpEscape] \n
63636363

6364-
# 158| [RegExpConstant, RegExpEscape] \n
6364+
# 165| [RegExpConstant, RegExpEscape] \n
63656365

6366-
# 168| [RegExpConstant, RegExpNormalChar] aa
6366+
# 175| [RegExpConstant, RegExpNormalChar] aa
63676367

6368-
# 168| [RegExpAlt] aa|bb
6368+
# 175| [RegExpAlt] aa|bb
63696369
#-----| 0 -> [RegExpConstant, RegExpNormalChar] aa
63706370
#-----| 1 -> [RegExpConstant, RegExpNormalChar] bb
63716371

6372-
# 168| [RegExpConstant, RegExpNormalChar] bb
6372+
# 175| [RegExpConstant, RegExpNormalChar] bb
63736373

6374-
# 172| [RegExpConstant, RegExpNormalChar] aa
6374+
# 179| [RegExpConstant, RegExpNormalChar] aa
63756375

6376-
# 172| [RegExpAlt] aa|
6377-
# 172| bb
6376+
# 179| [RegExpAlt] aa|
6377+
# 179| bb
63786378
#-----| 0 -> [RegExpConstant, RegExpNormalChar] aa
63796379
#-----| 1 -> [RegExpConstant, RegExpNormalChar]
63806380
#-----| bb
63816381

6382-
# 172| [RegExpConstant, RegExpNormalChar]
6383-
# 172| bb
6382+
# 179| [RegExpConstant, RegExpNormalChar]
6383+
# 179| bb
63846384

6385-
# 180| [RegExpCharacterClass] [a-z]
6385+
# 187| [RegExpCharacterClass] [a-z]
63866386
#-----| 0 -> [RegExpCharacterRange] a-z
63876387

6388-
# 180| [RegExpConstant, RegExpNormalChar] a
6388+
# 187| [RegExpConstant, RegExpNormalChar] a
63896389

6390-
# 180| [RegExpCharacterRange] a-z
6390+
# 187| [RegExpCharacterRange] a-z
63916391
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
63926392
#-----| 1 -> [RegExpConstant, RegExpNormalChar] z
63936393

6394-
# 180| [RegExpConstant, RegExpNormalChar] z
6394+
# 187| [RegExpConstant, RegExpNormalChar] z
63956395

6396-
# 181| [RegExpCharacterClass] [a-zA-Z]
6396+
# 188| [RegExpCharacterClass] [a-zA-Z]
63976397
#-----| 0 -> [RegExpCharacterRange] a-z
63986398
#-----| 1 -> [RegExpCharacterRange] A-Z
63996399

6400-
# 181| [RegExpConstant, RegExpNormalChar] a
6400+
# 188| [RegExpConstant, RegExpNormalChar] a
64016401

6402-
# 181| [RegExpCharacterRange] a-z
6402+
# 188| [RegExpCharacterRange] a-z
64036403
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
64046404
#-----| 1 -> [RegExpConstant, RegExpNormalChar] z
64056405

6406-
# 181| [RegExpConstant, RegExpNormalChar] z
6406+
# 188| [RegExpConstant, RegExpNormalChar] z
64076407

6408-
# 181| [RegExpConstant, RegExpNormalChar] A
6408+
# 188| [RegExpConstant, RegExpNormalChar] A
64096409

6410-
# 181| [RegExpCharacterRange] A-Z
6410+
# 188| [RegExpCharacterRange] A-Z
64116411
#-----| 0 -> [RegExpConstant, RegExpNormalChar] A
64126412
#-----| 1 -> [RegExpConstant, RegExpNormalChar] Z
64136413

6414-
# 181| [RegExpConstant, RegExpNormalChar] Z
6414+
# 188| [RegExpConstant, RegExpNormalChar] Z
64156415

6416-
# 184| [RegExpCharacterClass] [a-]
6416+
# 191| [RegExpCharacterClass] [a-]
64176417
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
64186418
#-----| 1 -> [RegExpConstant, RegExpNormalChar] -
64196419

6420-
# 184| [RegExpConstant, RegExpNormalChar] a
6420+
# 191| [RegExpConstant, RegExpNormalChar] a
64216421

6422-
# 184| [RegExpConstant, RegExpNormalChar] -
6422+
# 191| [RegExpConstant, RegExpNormalChar] -
64236423

6424-
# 185| [RegExpCharacterClass] [-a]
6424+
# 192| [RegExpCharacterClass] [-a]
64256425
#-----| 0 -> [RegExpConstant, RegExpNormalChar] -
64266426
#-----| 1 -> [RegExpConstant, RegExpNormalChar] a
64276427

6428-
# 185| [RegExpConstant, RegExpNormalChar] -
6428+
# 192| [RegExpConstant, RegExpNormalChar] -
64296429

6430-
# 185| [RegExpConstant, RegExpNormalChar] a
6430+
# 192| [RegExpConstant, RegExpNormalChar] a
64316431

6432-
# 186| [RegExpCharacterClass] [-]
6432+
# 193| [RegExpCharacterClass] [-]
64336433
#-----| 0 -> [RegExpConstant, RegExpNormalChar] -
64346434

6435-
# 186| [RegExpConstant, RegExpNormalChar] -
6435+
# 193| [RegExpConstant, RegExpNormalChar] -
64366436

6437-
# 187| [RegExpCharacterClass] [*]
6437+
# 194| [RegExpCharacterClass] [*]
64386438
#-----| 0 -> [RegExpConstant, RegExpNormalChar] *
64396439

6440-
# 187| [RegExpConstant, RegExpNormalChar] *
6440+
# 194| [RegExpConstant, RegExpNormalChar] *
64416441

6442-
# 188| [RegExpCharacterClass] [^a]
6442+
# 195| [RegExpCharacterClass] [^a]
64436443
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
64446444

6445-
# 188| [RegExpConstant, RegExpNormalChar] a
6445+
# 195| [RegExpConstant, RegExpNormalChar] a
64466446

6447-
# 189| [RegExpCharacterClass] [a^]
6447+
# 196| [RegExpCharacterClass] [a^]
64486448
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
64496449
#-----| 1 -> [RegExpConstant, RegExpNormalChar] ^
64506450

6451-
# 189| [RegExpConstant, RegExpNormalChar] a
6451+
# 196| [RegExpConstant, RegExpNormalChar] a
64526452

6453-
# 189| [RegExpConstant, RegExpNormalChar] ^
6453+
# 196| [RegExpConstant, RegExpNormalChar] ^
64546454

6455-
# 190| [RegExpCharacterClass] [\\]
6455+
# 197| [RegExpCharacterClass] [\\]
64566456
#-----| 0 -> [RegExpConstant, RegExpEscape] \\
64576457

6458-
# 190| [RegExpConstant, RegExpEscape] \\
6458+
# 197| [RegExpConstant, RegExpEscape] \\
64596459

6460-
# 191| [RegExpCharacterClass] [\\\]]
6460+
# 198| [RegExpCharacterClass] [\\\]]
64616461
#-----| 0 -> [RegExpConstant, RegExpEscape] \\
64626462
#-----| 1 -> [RegExpConstant, RegExpEscape] \]
64636463

6464-
# 191| [RegExpConstant, RegExpEscape] \\
6464+
# 198| [RegExpConstant, RegExpEscape] \\
64656465

6466-
# 191| [RegExpConstant, RegExpEscape] \]
6466+
# 198| [RegExpConstant, RegExpEscape] \]
64676467

6468-
# 192| [RegExpCharacterClass] [:]
6468+
# 199| [RegExpCharacterClass] [:]
64696469
#-----| 0 -> [RegExpConstant, RegExpNormalChar] :
64706470

6471-
# 192| [RegExpConstant, RegExpNormalChar] :
6471+
# 199| [RegExpConstant, RegExpNormalChar] :
64726472

6473-
# 193| [RegExpNamedCharacterProperty] [:digit:]
6473+
# 200| [RegExpNamedCharacterProperty] [:digit:]
64746474

6475-
# 194| [RegExpNamedCharacterProperty] [:alnum:]
6475+
# 201| [RegExpNamedCharacterProperty] [:alnum:]
64766476

6477-
# 197| [RegExpCharacterClass] []a]
6477+
# 204| [RegExpCharacterClass] []a]
64786478
#-----| 0 -> [RegExpConstant, RegExpNormalChar] ]
64796479
#-----| 1 -> [RegExpConstant, RegExpNormalChar] a
64806480

6481-
# 197| [RegExpConstant, RegExpNormalChar] ]
6481+
# 204| [RegExpConstant, RegExpNormalChar] ]
64826482

6483-
# 197| [RegExpConstant, RegExpNormalChar] a
6483+
# 204| [RegExpConstant, RegExpNormalChar] a
6484+
6485+
# 205| [RegExpNamedCharacterProperty] [:aaaaa:]
6486+
6487+
# 209| [RegExpConstant, RegExpNormalChar] i
6488+
6489+
# 209| [RegExpConstant, RegExpNormalChar] abc
6490+
6491+
# 210| [RegExpConstant, RegExpNormalChar] s
6492+
6493+
# 210| [RegExpConstant, RegExpNormalChar] abc
64846494

6485-
# 198| [RegExpNamedCharacterProperty] [:aaaaa:]
6495+
# 211| [RegExpConstant, RegExpNormalChar] is
6496+
6497+
# 211| [RegExpConstant, RegExpNormalChar] abc
6498+
6499+
# 213| [RegExpConstant, RegExpNormalChar] abc
6500+
6501+
# 214| [RegExpConstant, RegExpNormalChar] abc
6502+
6503+
# 215| [RegExpConstant, RegExpNormalChar] abc
6504+
6505+
# 216| [RegExpConstant, RegExpNormalChar] abc
6506+
6507+
# 217| [RegExpConstant, RegExpNormalChar] abc
6508+
6509+
# 219| [RegExpDot] .
6510+
6511+
# 219| [RegExpStar] .*
6512+
#-----| 0 -> [RegExpDot] .
6513+
6514+
# 220| [RegExpDot] .
6515+
6516+
# 220| [RegExpStar] .*
6517+
#-----| 0 -> [RegExpDot] .
6518+
6519+
# 221| [RegExpDot] .
6520+
6521+
# 221| [RegExpStar] .*
6522+
#-----| 0 -> [RegExpDot] .

swift/ql/test/library-tests/regex/regex.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ struct Regex<Output> : RegexComponent {
1717

1818
init(_ pattern: String) throws where Output == AnyRegexOutput { }
1919

20-
func firstMatch(in string: String) throws -> Regex<Output>.Match? { return nil}
21-
func prefixMatch(in string: String) throws -> Regex<Output>.Match? { return nil}
22-
func wholeMatch(in string: String) throws -> Regex<Output>.Match? { return nil}
20+
func ignoresCase(_ ignoresCase: Bool = true) -> Regex<Regex<Output>.RegexOutput> { return self }
21+
func dotMatchesNewlines(_ dotMatchesNewlines: Bool = true) -> Regex<Regex<Output>.RegexOutput> { return self }
22+
23+
func firstMatch(in string: String) throws -> Regex<Output>.Match? { return nil }
24+
func prefixMatch(in string: String) throws -> Regex<Output>.Match? { return nil }
25+
func wholeMatch(in string: String) throws -> Regex<Output>.Match? { return nil }
2326

2427
typealias RegexOutput = Output
2528
}
@@ -49,6 +52,7 @@ class NSString : NSObject {
4952
var rawValue: UInt
5053

5154
static var regularExpression: NSString.CompareOptions { get { return CompareOptions(rawValue: 1) } }
55+
static var caseInsensitive: NSString.CompareOptions { get { return CompareOptions(rawValue: 2) } }
5256
}
5357

5458
convenience init(string aString: String) { self.init() }
@@ -76,6 +80,9 @@ class NSTextCheckingResult : NSObject {
7680
class NSRegularExpression : NSObject {
7781
struct Options : OptionSet {
7882
var rawValue: UInt
83+
84+
static var caseInsensitive: NSRegularExpression.Options { get { return Options(rawValue: 1) } }
85+
static var dotMatchesLineSeparators: NSRegularExpression.Options { get { return Options(rawValue: 2) } }
7986
}
8087

8188
struct MatchingOptions : OptionSet {
@@ -196,4 +203,24 @@ func myRegexpMethodsTests(b: Bool, str_unknown: String) throws {
196203
// invalid (Swift doesn't like these regexs)
197204
_ = try Regex("[]a]").firstMatch(in: input) // this is valid in other regex implementations, and is likely harmless to accept
198205
_ = try Regex("[:aaaaa:]").firstMatch(in: input) // $ hasParseFailure
206+
207+
// --- parse modes ---
208+
209+
_ = try Regex("(?i)abc").firstMatch(in: input) // $ input=input modes=IGNORECASE regex=(?i)abc SPURIOUS: hasParseFailure
210+
_ = try Regex("(?s)abc").firstMatch(in: input) // $ input=input modes=DOTALL regex=(?s)abc SPURIOUS: $hasParseFailure
211+
_ = try Regex("(?is)abc").firstMatch(in: input) // $ input=input regex=(?is)abc MISSING: modes="DOTALL | IGNORECASE" SPURIOUS: modes=IGNORECASE $hasParseFailure
212+
213+
_ = try Regex("abc").dotMatchesNewlines(true).firstMatch(in: input) // $ input=input regex=abc MISSING: modes=DOTALL
214+
_ = try Regex("abc").dotMatchesNewlines(false).firstMatch(in: input) // $ input=input regex=abc
215+
_ = try Regex("abc").dotMatchesNewlines(true).dotMatchesNewlines(false).firstMatch(in: input) // $ input=input regex=abc
216+
_ = try Regex("abc").dotMatchesNewlines(false).dotMatchesNewlines(true).firstMatch(in: input) // $ input=input regex=abc MISSING: modes=DOTALL
217+
_ = try Regex("abc").dotMatchesNewlines().ignoresCase().firstMatch(in: input) // $ input=input regex=abc MISSING: modes="DOTALL | IGNORECASE"
218+
219+
_ = try NSRegularExpression(pattern: ".*", options: .caseInsensitive).firstMatch(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=.* input=input MISSING: modes=IGNORECASE
220+
_ = try NSRegularExpression(pattern: ".*", options: .dotMatchesLineSeparators).firstMatch(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=.* input=input MISSING: modes=DOTALL
221+
_ = try NSRegularExpression(pattern: ".*", options: [.caseInsensitive, .dotMatchesLineSeparators]).firstMatch(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=.* input=input MISSING: modes="DOTALL | IGNORECASE"
222+
223+
_ = input.replacingOccurrences(of: ".*", with: "", options: [.regularExpression, .caseInsensitive]) // $ MISSING: regex=.* input=input modes=IGNORECASE
224+
225+
_ = NSString(string: "abc").replacingOccurrences(of: ".*", with: "", options: [.regularExpression, .caseInsensitive], range: NSMakeRange(0, inputNS.length)) // $ MISSING: regex=.* input=inputNS modes=IGNORECASE
199226
}

0 commit comments

Comments
 (0)