← Back to DAGDA | Support | Source: https://github.com/howl-editor/howl/pull/118
Skip to content

Implement PEP 498 string interpolation - #118

Closed
refi64 wants to merge 4 commits into
masterfrom
pep_498
Closed

refi64 wants to merge 4 commits into
masterfrom
pep_498

Conversation

@refi64

@refi64 refi64 commented Oct 21, 2015

Copy link
Copy Markdown
Contributor

This implements Python PEP 498 string interpolation. Again. The syntax is more complicated than it looks!

@nilnor

nilnor commented Nov 4, 2015

Copy link
Copy Markdown
Contributor

@shalabhc is this something you have or could look at, given that you're more of a Python buff than yours truly?

@shalabhc

shalabhc commented Nov 5, 2015

Copy link
Copy Markdown
Contributor

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.
f'{value:def}' should not style 'def' as keyword.
f'{value:>+#10.2)' is not styled correctly.

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?

@refi64

refi64 commented Nov 9, 2015

Copy link
Copy Markdown
Contributor Author

@shalabhc Hmm...you're probably right...I think I may be able to save the formatting of brackets, too. Hold on a sec...

@refi64

refi64 commented Nov 9, 2015

Copy link
Copy Markdown
Contributor Author

@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)^1

like the LPeg docs for P say, but it didn't match anything.

@shalabhc

Copy link
Copy Markdown
Contributor

@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.

@shalabhc

Copy link
Copy Markdown
Contributor

@kirbyfan64 - So I think instead of matching any char, it might be better to match one or more chars that are not { - what do you think? It might be easier to implement too.

@refi64

refi64 commented Nov 12, 2015

Copy link
Copy Markdown
Contributor Author

@shalabhc All issues addressed! I also made the formatting a bit more lenient to avoid string formatting getting screwed in the middle of typing.

@refi64

refi64 commented Nov 14, 2015

Copy link
Copy Markdown
Contributor Author

Bump?

@shalabhc

Copy link
Copy Markdown
Contributor

Sorry for the delay on this. I tried this out but in f'{value:#format}' I still get '#format' styled as a comment.

Some other implementation comments inline.

Comment thread bundles/python/python_lexer.moon Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shalabhc I just updated it. Is it better now?

@refi64

refi64 commented Nov 16, 2015

Copy link
Copy Markdown
Contributor Author

@shalabhc Are you sure? I just tested it, and it's styled correctly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shalabhc Because the rules recurse into each other with V. Just like in the MoonScript one.

@shalabhc

Copy link
Copy Markdown
Contributor

@kirbyfan64 - ah you're right f'value:#format' is fixed. However I found the following issues:

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?

@refi64

refi64 commented Nov 18, 2015

Copy link
Copy Markdown
Contributor Author

@shalabhc sigh I'm pretty sure I fixed all the issues this time. If all's OK now, I can squash the commits.

@shalabhc

Copy link
Copy Markdown
Contributor

@kirbyfan64 - seems to work pretty well but I found a special case ;)

f'{x!=1}'

I think the != operator has special exception in the pep. If fixing this is too complex, I'm ok with leaving it as is.

@refi64

refi64 commented Nov 19, 2015

Copy link
Copy Markdown
Contributor Author

Crud, my rebase killed the commit history. Hold on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants