Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic f-strings (intepolated strings) #258

Merged
merged 7 commits into from
Mar 2, 2024
Merged

Basic f-strings (intepolated strings) #258

merged 7 commits into from
Mar 2, 2024

Conversation

GreyCat
Copy link
Member

@GreyCat GreyCat commented Oct 14, 2023

Implements step 1 out of formatting strings proposal.

  • Added basic f-string parsing in expression language (step 1 only, just interpolation, without any extra formatting specification)
  • Added basic interpolated string (f-string) translation into target languages, mostly working off concatenation and existing other-types-to-string conversions. Supports only integers and strings now.
    • GoTranslator: given non-string nature, added custom implementation using fmt.Sprintf
    • CommonLiterals: split generation of string "body" (without quotes) and adding quotes to it
    • CommonMethods: param name cleanup

Both are supported by unit tests.

@generalmimon
Copy link
Member

@GreyCat I also have a question - is there a way to get a literal { in the f-string? From what I see in the expression parsing code, it doesn't look like there is, but I may have missed something.

In Python, for example, you can escape a { by doubling it - I wonder if we can get this too?

>>> f"1 + 2 = {1 + 2} {{basic math}}"
'1 + 2 = 3 {basic math}'

GreyCat added 2 commits March 1, 2024 10:34
commit 1dbafe3
Author: Mikhail Yakshin <[email protected]>
Date:   Thu Feb 29 15:08:24 2024 +0000

    ExpressionsSpec: f-strings: added test with newline in the middle, fixed name of test with double quote in the middle

commit 8605f3f
Author: Mikhail Yakshin <[email protected]>
Date:   Thu Feb 29 15:04:40 2024 +0000

    Lexical: moved fstringItem + fstringChar from Expressions; Expressions: fixed rep -> repX to avoid whitespace problem

commit 17f9e40
Author: Mikhail Yakshin <[email protected]>
Date:   Sat Oct 14 21:07:58 2023 +0100

    Added similar escaped quote+space test for double-quoted string

commit c0083fb
Author: Mikhail Yakshin <[email protected]>
Date:   Sat Oct 14 20:48:19 2023 +0100

    Added tests suggested in PR review: quote in f-string, regular string in f-string, f-string in f-string

commit 1b22258
Author: Mikhail Yakshin <[email protected]>
Date:   Sat Oct 14 20:38:19 2023 +0100

    Apply suggestions from code review

    Co-authored-by: Petr Pučil <[email protected]>

commit 97ffceb
Author: Mikhail Yakshin <[email protected]>
Date:   Sat Oct 14 08:50:41 2023 +0100

    Added basic interpolated string (f-string) translation into target languages, mostly working off concatenation and existing other-types-to-string conversions. Supports only integers and strings now. Added basic unit tests.

    * GoTranslator: given non-string nature, added custom implementation using `fmt.Sprintf`
    * CommonLiterals: split generation of string "body" (without quotes) and adding quotes to it
    * CommonMethods: param name cleanup

commit 1c7c759
Author: Mikhail Yakshin <[email protected]>
Date:   Sat Oct 14 08:23:12 2023 +0100

    Implemented basic f-string parsing in expression language
@GreyCat
Copy link
Member Author

GreyCat commented Mar 1, 2024

@GreyCat I also have a question - is there a way to get a literal { in the f-string? From what I see in the expression parsing code, it doesn't look like there is, but I may have missed something.

In Python, for example, you can escape a { by doubling it - I wonder if we can get this too?

To be honest, I don't want to overcomplicate it, nor I see a lot of value in having this escaping behavior.

In Python itself, it looks pretty weird per se — e.g.:

  • {} is a valid expression to specify empty dict.
  • {foo} is a valid expression for one-element set containing whatever foo variable has
  • {foo:x} is a valid expression for a dict containing one key-value pair; key is whatever foo variable has, and value is whatever x variable has

Therefore, if I want to include a set in f-string, I'm down to unpleasant surprise:

>>> foo = "bar"
>>> x = 42
>>> f"{{foo}}"
'{foo}'
>>> f"{{foo:x}}"
'{foo:x}'

It's possible to work around with extra spaces:

>>> f"{ {foo}}"
"{'bar'}" # as expected
>>> f"{ {foo} }"
"{'bar'}" # as expected
>>> f"{ {foo:x}}"
"{'bar': 42}"
>>> f"{ {foo:x} }"
"{'bar': 42}"

... and then you try some other combinations and it gets progressively weirder, e.g. this throws a very weird error:

>>> f"{{}:x}"
  File "<stdin>", line 1
    f"{{}:x}"
             ^
SyntaxError: f-string: single '}' is not allowed

while adding initial space makes it work as expected:

>>> f"{ {}:x}"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to dict.__format__

So, my proposal is basically avoiding going into that rabbit hole, at least yet. If anybody wants to have literal { or } in our f-strings, just keep it simple and do f"1 + 2 = {1 + 2} " + "{basic math}"

GreyCat added 2 commits March 1, 2024 21:20
…wance, added lots of tests to ensure that it works as intended
…ings; modified common test to accommodate this pattern
@GreyCat GreyCat requested a review from generalmimon March 1, 2024 21:39
@GreyCat
Copy link
Member Author

GreyCat commented Mar 1, 2024

@generalmimon If you'll have some time, can I ask you to take another look? It looks like I've accommodated all your feedback, implemented all the fixes, and rebased it off the latest master — so it looks reasonably good as a first step to merge in.

Meanwhile I'll start adding .kst tests for this and once this is good to go, I'll start stage 2, which will hopefully give us better formatting options. Then, finally, we can probably deprecate -web-ide-representation.

@GreyCat GreyCat merged commit b47078c into master Mar 2, 2024
3 of 6 checks passed
@GreyCat GreyCat deleted the fstrings branch March 2, 2024 12:42
@GreyCat
Copy link
Member Author

GreyCat commented Mar 2, 2024

Interpreting thumbs up as all good — therefore merging. Thanks!

Mingun added a commit to Mingun/ksc-rs that referenced this pull request Sep 6, 2024
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.

2 participants