String Interpolation for ObjC

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

String Interpolation for ObjC

Sumner, Brian via cfe-dev
Hi,

 I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre

 Since that doesn't actually parse the code, it can only handle object types so far, so I'm thinking I should try to add it to the Clang ObjC compiler proper, instead. But I've never done this, never even built clang. Is there a tutorial somewhere on how to do this, and pointers on where I would start hooking into string escape sequence parsing, and which other parts I'd have to touch?

 My approach is basically a simple code transformation. I'd turn strings containing escape sequences and subexpressions:

        NSLog( "This is \(NSFullUserName()), who is \([myDatabase ageForUser: NSUserName()]) years old." );

into

        NSLog( "This is %1$@, who is %1$d years old.", (NSFullUserName()), ([myDatabase ageForUser: NSUserName()]) );

and that's it. So this *should* be a comparatively simple change, and, moreover should need no changes to any existing libraries, client source code etc. It would just work with existing variadic functions.

Not quite sure how one would localize the format strings (which my preprocessor does), as it's kinda unusual for clang to generate strings files, but maybe one can just generate the code for the main file and output a status message with the generated string key and value, and then one could use that message's text to generate the initial strings file so it will actually work at runtime. But even support for interpolation at the level Swift has right now is an improvement, I suppose.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://orangejuiceliberationfront.com





_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: String Interpolation for ObjC

Sumner, Brian via cfe-dev
On 26. Jul 2017, at 19:43, Uli Kusterer via cfe-dev <[hidden email]> wrote:
> I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre

Doh. Of course this was meant to read "Swift-style string interpolation to Objective-C".

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://orangejuiceliberationfront.com





_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: String Interpolation for ObjC

Sumner, Brian via cfe-dev
In reply to this post by Sumner, Brian via cfe-dev
On 26. Jul 2017, at 19:43, Uli Kusterer via cfe-dev <[hidden email]> wrote:
> I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre

Doh. Of course this was meant to read "Swift-style string interpolation to Objective-C".

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://orangejuiceliberationfront.com

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: String Interpolation for ObjC

Sumner, Brian via cfe-dev
In reply to this post by Sumner, Brian via cfe-dev
On 2017-07-26 19:43, Uli Kusterer via cfe-dev wrote:

> Hi,
>
>   I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre
>
>   Since that doesn't actually parse the code, it can only handle object types so far, so I'm thinking I should try to add it to the Clang ObjC compiler proper, instead. But I've never done this, never even built clang. Is there a tutorial somewhere on how to do this, and pointers on where I would start hooking into string escape sequence parsing, and which other parts I'd have to touch?
>
>   My approach is basically a simple code transformation. I'd turn strings containing escape sequences and subexpressions:
>
> NSLog( "This is \(NSFullUserName()), who is \([myDatabase ageForUser: NSUserName()]) years old." );
>
> into
>
> NSLog( "This is %1$@, who is %1$d years old.", (NSFullUserName()), ([myDatabase ageForUser: NSUserName()]) );
>
> and that's it. So this *should* be a comparatively simple change, and, moreover should need no changes to any existing libraries, client source code etc. It would just work with existing variadic functions.

Highly unlikely, but in theory, someone might have code containing
string literals with the exact syntax that is suggested for string
interpolation. That code would break.

Do you propose to add string interpolation to C string literals or
Objective-C string literals? Because the above are C string literals,
unless some software involved in sending the message removed the at
signs (@).

--
/Jacob Carlborg

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: String Interpolation for ObjC

Sumner, Brian via cfe-dev
In reply to this post by Sumner, Brian via cfe-dev
Hi Uli,

Clang currently accepts `\(` as a valid escape for `(` (apparently it's a GCC extension). It doesn't warn about this unless -Wpedantic is specified. To avoid breaking existing code that relies on this Clang might have to use another character pair (note that '[' and '{' have the same issue), or something like '\@(expression)'.

On 26 July 2017 at 18:43, Uli Kusterer via cfe-dev <[hidden email]> wrote:
Hi,

 I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre

 Since that doesn't actually parse the code, it can only handle object types so far, so I'm thinking I should try to add it to the Clang ObjC compiler proper, instead. But I've never done this, never even built clang. Is there a tutorial somewhere on how to do this, and pointers on where I would start hooking into string escape sequence parsing, and which other parts I'd have to touch?

I would recommend looking at the getting started guide first: http://llvm.org/docs/GettingStarted.html. It contains instructions that describe how to build LLVM and Clang. As well as the getting started guide, I would recommend looking at Clang's internals manual before working on Clang: https://clang.llvm.org/docs/InternalsManual.html. This document contains a section that describes how one can add an new expression to Clang.

I hope this helps,
Alex
 

 My approach is basically a simple code transformation. I'd turn strings containing escape sequences and subexpressions:

        NSLog( "This is \(NSFullUserName()), who is \([myDatabase ageForUser: NSUserName()]) years old." );

into

        NSLog( "This is %1$@, who is %1$d years old.", (NSFullUserName()), ([myDatabase ageForUser: NSUserName()]) );

and that's it. So this *should* be a comparatively simple change, and, moreover should need no changes to any existing libraries, client source code etc. It would just work with existing variadic functions.

Not quite sure how one would localize the format strings (which my preprocessor does), as it's kinda unusual for clang to generate strings files, but maybe one can just generate the code for the main file and output a status message with the generated string key and value, and then one could use that message's text to generate the initial strings file so it will actually work at runtime. But even support for interpolation at the level Swift has right now is an improvement, I suppose.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://orangejuiceliberationfront.com





_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...