<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div>Am 24.11.2017 um 20:13 schrieb Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;:<br><br></div><blockquote type="cite"><div><div dir="ltr">On Thu, Nov 23, 2017 at 5:33 PM, Chris Lattner <span dir="ltr">&lt;<a href="mailto:clattner@nondot.org" target="_blank">clattner@nondot.org</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><span class="gmail-">On Nov 23, 2017, at 10:35 AM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</span><div><span class="gmail-"><blockquote type="cite"><div><div dir="ltr">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></div></span>Thanks for writing this up Xiaodi,</div><span class="gmail-"><div><br><blockquote type="cite"><div><div dir="ltr"><div><div>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><br></div><div>- Teach the lexer about basic /pattern/flag syntax.</div><div>- Add an `<wbr>ExpressibleByRegularExpression<wbr>Literal`, where the initializer would be something like `init(<wbr>regularExpressionLiteralPatter<wbr>n: String, flags: RegularExpressionFlags)` where&nbsp;RegularExpressionFlags&nbsp;<wbr>would be an OptionSet type.</div><div>- Add conformance to `<wbr>ExpressibleByRegularExpression<wbr>Literal` to `NSRegularExpression`.</div><div>- 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></div></span><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><br></div><div>Things I’d like to carefully consider:</div><div><br></div><div>1) We could make the compiler parse and validate regex literals at compile time:</div><div><br></div><div>a) this allows the compiler to emit diagnostics (with fixits!) on malformed literals. &nbsp;</div><div><br></div><div>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><br></div><div><div>c) however, the compiler can’t parse the literal unless it knows the dialect it corresponds to.&nbsp; While we could parameterize this somehow (e.g. as a requirement in ExpressibleByRegularExpression<wbr>Literal), if we weren’t bound by backwards compatibility, we would just keep things simple and say “there is one and only one grammar”.&nbsp; 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></div></blockquote><div><br></div><div>I think we've circled back to a topic that we've discussed here before. I do agree that having more of this validation at compile time would improve the experience. However, I can see a few drawbacks to the _compiler_ doing the validation:</div><div><br></div><div>- In the absence of a `constexpr`-like facility, supporting runtime expressions would mean we'd be writing the same code twice, once in C++ for compile-time validation of literal expressions and another time in Swift for runtime expressions.</div><div><br></div><div>- As seen in these discussions about string literals where users want to copy and paste text and have it "just work," supporting only one dialect in regex literals will inevitably lead users to ask for other types of regex literals for each individual flavor of regex they encounter.</div><div><br></div><div>Just like ExpressibleByDictionaryLiteral doesn't deduplicate keys, leaving that to Dictionary, I think regex literals are better off not validating literal expressions (or, maybe, doing only the barest sanity check), leaving the rest to concrete regex types. As you point out with validation of integer overflows during constant folding, we could get enough compile-time validation even without teaching the compiler itself how to validate the literal.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>2) I’d like to explore the idea of making // syntax be *patterns* instead of simply literals.&nbsp; 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.&nbsp; Here is strawman syntax with a dumb example:</div><div><br></div><div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>if case /([a-zA-Z]+: let firstName) ([a-zA-Z]+: let lastName)/ = getSomeString() {</div><div>&nbsp; &nbsp;print(firstName, lastName)</div><div>}</div></blockquote></div></blockquote><div><br></div><div>This is an interesting idea. But is it significantly more usable than the same type having a collection of named matches using the usual Perl syntax?</div><div><br></div><div>&nbsp; if case /(?&lt;firstName&gt;[a-zA-Z]+) (?&lt;lastName&gt;[a-zA-Z]+)/ = getSomeString() {</div><div>&nbsp; &nbsp; print(Regex.captured["firstName"], Regex.captured["lastName"])</div><div>&nbsp; }</div><div><br></div></div></div></div></div></blockquote><div><br></div>Definitely. Not only is it much more readable, it is much safer as well, as the compiler will tell you that a name is not defined on a typo. Furthermore, as Chris suggested, this can be extended to directly get out other types than strings in a typesafe was (which should be extendible to user defined types conforming to a specific protocol).<div><br></div><div><br><div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div></div><div>3) I see regex string matching as the dual to string interpolation.&nbsp; 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><br></div><div><br></div><div>4) I’d like to consider some of the advances that Perl 6 added to its regex grammar.&nbsp; Everyone knows that modern regex’s aren’t actually regular anyway, so it begs the question of how far to take it.&nbsp; If nothing else, I appreciate the freeform structure supported (including inline comments) which make them more readable.</div></div></blockquote><div><br></div><div>Sounds like we want multiline regex literals :)</div></div></div></div></div></blockquote><div><br></div>Absolutely.</div><div><br></div><div>-Thorsten&nbsp;</div><div><br></div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div></div><div>We should also support a dynamic regex engine as well, because there are sometimes reasons to runtime construct regex’s.&nbsp; 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></blockquote></div></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></div></body></html>