Repository navigation
Conversation
|
@shalabhc is this something you have or could look at, given that you're more of a Python buff than yours truly? |
|
It would be great to have these format strings styled. I've looked at it briefly and will review more thoroughly. For now here are some issues: f'{value:#format}' should not style '#format' as a comment. This seems to handle the built in format specification well. Handing everything properly is quite a challenge. Also, given that the language allows 'Each formattable type may define how the format specification is to be interpreted' (https://docs.python.org/2/library/string.html#formatspec) - I wonder if it's useful to handle the numeric format specifications at all? Note that the format spec can be absolutely any string (including my examples above). I'm thinking that anything after the ':' - i.e. the entire format spec - could be styled as a single style, maybe 'special'. While this would lose the special style for numeric format strings, it will considerably simplify the implementation and be more correct in the general sense. However, this will also lose the style for nested {expression} in the format spec. Maybe that is OK - what do you think @kirbyfan64? |
|
@shalabhc Hmm...you're probably right...I think I may be able to save the formatting of brackets, too. Hold on a sec... |
|
@shalabhc I pushed a new commit...but is it possible with LPeg to match any character? I tried replacing this line with: format_spec = c('operator', ':') * (format_number + c 'special', P 1)^1like the LPeg docs for |
|
@kirbyfan64 - not sure. Sorry my knowledge of LPeg isn't great but I'll poke around a bit and see if I can figure this out. |
|
@kirbyfan64 - So I think instead of matching any char, it might be better to match one or more chars that are not |
|
@shalabhc All issues addressed! I also made the formatting a bit more lenient to avoid string formatting getting screwed in the middle of typing. |
|
Bump? |
|
Sorry for the delay on this. I tried this out but in Some other implementation comments inline. |
There was a problem hiding this comment.
It would be nice to not have all variants of the f strings here. Instead just call these, say, f_string as a single rule that is defined separately above using any. Also, style wise, it seems strange to use interpolation with 'ts' etc. to refer to other rules - it makes it somewhat hard to follow. Would be possible to just generate all the variants of the f strings by iterating over the names of the normal strings? Maybe you could add the generated rules to a list and then make an any rule out of it?
There was a problem hiding this comment.
@shalabhc I just updated it. Is it better now?
|
@shalabhc Are you sure? I just tested it, and it's styled correctly. |
There was a problem hiding this comment.
I'm not sure why it is necessary to inject all interpolations into the main rule set? Since f_string is already in all, wouldn't that just work?
There was a problem hiding this comment.
@shalabhc Because the rules recurse into each other with V. Just like in the MoonScript one.
|
@kirbyfan64 - ah you're right f'{value:abc{f()}}' # not good, string not closed?
f'{val!#comment}' # not good, styled as comment, should be error or unstyled ?
f'{a + b}' # not good, string not closed? |
|
@shalabhc sigh I'm pretty sure I fixed all the issues this time. If all's OK now, I can squash the commits. |
|
@kirbyfan64 - seems to work pretty well but I found a special case ;) I think the |
|
Crud, my rebase killed the commit history. Hold on... |
This implements Python PEP 498 string interpolation. Again. The syntax is more complicated than it looks!