<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">On Nov 23, 2017, at 10:35 AM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">This proposed addition addresses a known pain point, to be sure, but I think it has many implications for the future direction of the language and I'd like to explore them here.</div></div></blockquote><div><br class=""></div>Thanks for writing this up Xiaodi,</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">We should certainly move any discussion about regex literals into its own thread, but to make it clear that I'm not simply suggesting that we implement something in Swift 10 instead of addressing a known pain point now, here's a sketch of how Swift 5 could make meaningful progress:</div><div class=""><br class=""></div><div class="">- Teach the lexer about basic /pattern/flag syntax.</div><div class="">- Add an `ExpressibleByRegularExpressionLiteral`, where the initializer would be something like `init(regularExpressionLiteralPattern: String, flags: RegularExpressionFlags)` where RegularExpressionFlags would be an OptionSet type.</div><div class="">- Add conformance to `ExpressibleByRegularExpressionLiteral` to `NSRegularExpression`.</div><div class="">- Have no default `RegularExpressionLiteralType` for now so that, in the future, we can discuss and design a Swift standard library regular expression type, which is justifiable because we've baked in language support for the literal. This can be postponed.</div></div></div></div></blockquote><br class=""></div><div>This approach could make sense, but it makes a couple of assumptions that I’m not certain are the right way to go (to be clear, I’m not certain that they’re wrong either!).</div><div class=""><br class=""></div><div class="">Things I’d like to carefully consider:</div><div class=""><br class=""></div><div class="">1) We could make the compiler parse and validate regex literals at compile time:</div><div class=""><br class=""></div><div class="">a) this allows the compiler to emit diagnostics (with fixits!) on malformed literals. </div><div class=""><br class=""></div><div class="">b) When the compiler knows the grammar of the regex, it can precompile the regex into a DFA table or static executable code, rather than runtime compiling into a bytecode.</div><div class=""><br class=""></div><div class=""><div class="">c) however, the compiler can’t parse the literal unless it knows the dialect it corresponds to. While we could parameterize this somehow (e.g. as a requirement in ExpressibleByRegularExpressionLiteral), if we weren’t bound by backwards compatibility, we would just keep things simple and say “there is one and only one grammar”. I’d argue that having exactly one grammar supported by the // syntax is also *better* for users, rather than saying “it depends on what library you’re passing the regex into”.</div><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">2) I’d like to explore the idea of making // syntax be *patterns* instead of simply literals. As a pattern, it should be possible to bind submatches directly into variable declarations, eliminating the need to count parens in matches or other gross things. Here is strawman syntax with a dumb example:</div><div class=""><br class=""></div><div class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">if case /([a-zA-Z]+: let firstName) ([a-zA-Z]+: let lastName)/ = getSomeString() {</div><div class=""> print(firstName, lastName)</div><div class="">}</div></blockquote><div class=""><br class=""></div><div class="">3) I see regex string matching as the dual to string interpolation. We already provide the ability for types to specify a default way to print themselves, and it would be great to have default regex’s associated with many types, so you can just say “match an Int here” instead of having to match [0-9]+ and then do a failable conversion to Int outside the regex.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">4) I’d like to consider some of the advances that Perl 6 added to its regex grammar. Everyone knows that modern regex’s aren’t actually regular anyway, so it begs the question of how far to take it. If nothing else, I appreciate the freeform structure supported (including inline comments) which make them more readable.</div><div class=""><br class=""></div><div class="">We should also support a dynamic regex engine as well, because there are sometimes reasons to runtime construct regex’s. This could be handled by having the Regex type support a conversion from String or something, orthogonal to the language support for regex literals/patterns.</div><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div></body></html>