[swift-evolution] Strings in Swift 4
jhull at gbis.com
Thu Jan 26 05:49:43 CST 2017
> On Jan 25, 2017, at 7:32 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>> 4. Pattern matching greatness: One of the most obnoxious/error prone
>> aspects of regex’s in many languages is that when you match a pattern,
>> the various matches are dumped into numbered result values (often by
>> the order of the parens in the pattern). This is totally barbaric: it
>> begs for off by one errors, often breaks as the program is being
>> evolved/maintained, etc. It is just as bad as printf/scanf!
>> You should instead be able to directly bind subexpressions into local
>> variables. For example if you were trying to match something like
>> “42: Chris”, you should be able to use straw man syntax like this:
>> case /(let id: \d+): (let name: \w+)/: print(id); print(name)
> This is a good start, but inadequate for handling the kind of recursive
> grammars to which you want to generalize regexes, because you have to
> bind the same variable multiple times—often re-entrantly—during the same
> match. Actually the Kleene star (*) already has this basic problem,
> without the re-entrancy, but if you want to build real parsers, you need
> to do more than simply capture the last substring matched by each group.
I had a realization a few weeks ago that regexes with capture groups actually correspond to a type, where successive capture groups form a tuple and recursive ones form arrays of the capture groups they recurse (and ‘?’ conveniently forms an optional). For example the type for the regex above would be (Int,String). Those types could be pretty hairy for complex regexes though.
let (id,name) = /(\d+): (\w+)/
I’m not sure how pleasant it would be for complex regexes, but at least the type checker would keep you honest. Just wanted to throw it out there incase it jogs an idea in someone else…
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution