Skip to content

Add string interpolation#518

Merged
xoofx merged 4 commits intoscriban:masterfrom
ranger-turtle:NewBuiltin
Sep 5, 2023
Merged

Add string interpolation#518
xoofx merged 4 commits intoscriban:masterfrom
ranger-turtle:NewBuiltin

Conversation

@ranger-turtle
Copy link
Copy Markdown
Contributor

This is my first attempt to add string interpolation to Scriban.
I could not make the Parser and Lexer to discard $ character fully, since it is needed by ScriptPrinter. Maybe I have overlooked something but I am not sure.
It might close #476 .

Comment thread src/Scriban.Tests/TestFiles/020-interpolation/020-interpolation.txt
Comment thread src/Scriban/Parsing/Lexer.cs Outdated
Comment thread src/Scriban/Parsing/Lexer.cs
Comment thread src/Scriban/Parsing/Lexer.cs Outdated
Comment thread src/Scriban/Parsing/Parser.Terminals.cs Outdated
Comment thread src/Scriban/Parsing/Parser.Terminals.cs
Comment thread src/Scriban/Syntax/Expressions/ScriptInterpolatedExpression.cs
@ranger-turtle
Copy link
Copy Markdown
Contributor Author

ranger-turtle commented Sep 4, 2023

I am testing the interpolation begin escape and I have found that when I use the escape without adding any interpolated expression, there is an error, since the interpolated string is treated as the regular string and it does not accept the escaped {. Without leading $, it would look confusing that user could not escape { in the supposedly non-interpolated string. What is more important, user knows little about tokens used under the hood. I know two solutions:

  • Making the interpolated string to be actually interpolated again when at least one { is found. However, I need to create additional token;
  • Implementing escape for regular strings too. I have applied it to the both types of string and test "400-builtins\\470-html.txt" failed for the first time after such change. I came to the conclusion that the implementation of escapes in regular strings will break the backward compatibility with the previous versions.

@ranger-turtle
Copy link
Copy Markdown
Contributor Author

ranger-turtle commented Sep 4, 2023

Besides, I still considered Ruby-style interpolation in meantime and now I think this might be better idea, since:

  • I would not need to change interpolated string to regular one and to escape { and }, since interpolation expression begins from the combination of two characters instead of one;
  • Syntax is fairly Ruby-like and people experienced with Ruby would be confused with the difference of the interpolation syntax in Ruby and the Scriban. In my opinion, both C# and Ruby interpolation syntaxes are readable and natural. Only difference will be that both of the quote types will be supported, but it is much more subtle.

I respect your opinion about interpolation syntax and your time but we develop Scriban for people who use it, not for only both of us. And the change will remain mostly my business after all.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 4, 2023

I have found that when I use the escape without adding any interpolated expression, there is an error, since the interpolated string is treated as the regular string

Don't treat it as a regular string, but as an interpolated string that doesn't have interpolations. That will avoid entirely the problem. Roundtrip of Scriban text should be preserved anyway, so you would have to keep this information around.

Besides, I still considered Ruby-style interpolation in meantime and now I think this might be better idea, since

No need to change your PR on that aspect. Your PR is close to completion really.

I respect your opinion about interpolation syntax and your time but we develop Scriban for people who use it, not for only both of us.

Just to clarify, all the OSS I develop, that's primarily for my personal usage 🙂. I don't develop things for others, but I'm willing to share with others if that is useful and it is not a huge burden for me to maintain. If you contribute to this repo, it means that it is something I'm ok to "support", you can go away and do something else in your life, I will still support it.

@lofcz
Copy link
Copy Markdown
Contributor

lofcz commented Sep 5, 2023

Please include more tests with nested interpolation {{ $"Scriban { true ? $"yes {4 + 1}" : "no" } can interpolate strings now" }}. Also, it's a nice addition, I'm looking forward to the PR completion.

…en direcly embedded into interpolated expression (thanks to lofcz for pointing conditional expression scenario)

Add some new tests
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 5, 2023

@ranger-turtle Thanks for the changes, much appreciated, it's looking good. Could you push the "Resolve Conversation" buttons above for the items that you have already resolved so that I can reason about what is left.

@ranger-turtle
Copy link
Copy Markdown
Contributor Author

ranger-turtle commented Sep 5, 2023

@ranger-turtle Thanks for the changes, much appreciated, it's looking good. Could you push the "Resolve Conversation" buttons above for the items that you have already resolved so that I can reason about what is left.

@xoofx Done. I think you need to check if the error messages I have added in last two commits are friendly enough.

@xoofx xoofx merged commit f823a82 into scriban:master Sep 5, 2023
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 5, 2023

Thanks @ranger-turtle for the hard work! I'll update the doc as well and will probably release a new version that includes it in the coming days or next week.

@ranger-turtle
Copy link
Copy Markdown
Contributor Author

@xoofx You're welcome. Do not forget about EqualsIgnoreCase builtin (see #509 for reminder).

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 5, 2023

Do not forget about EqualsIgnoreCase builtin (see #509 for reminder).

It was already published for 5.8.0 🙂

@ranger-turtle
Copy link
Copy Markdown
Contributor Author

ranger-turtle commented Sep 5, 2023

Do not forget about EqualsIgnoreCase builtin (see #509 for reminder).

It was already published for 5.8.0 🙂

Merci, but there is no info about that builtin in documentation.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 5, 2023

Merci, but there is no info about that builtin in documentation.

Oh, ok different topic. There is an exe in the solution to generate such doc, it's not done manually. No problem I will verify the doc.

@ranger-turtle

This comment was marked as spam.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Sep 9, 2023

Btw @ranger-turtle, I have updated Scriban in kalk and String interpolation works out of the box 🙂

image

@ranger-turtle
Copy link
Copy Markdown
Contributor Author

ranger-turtle commented Sep 9, 2023

@xoofx Just like in other software dependent on Scriban, including MakiSei, one of the my projects. Merci.

@NeVeSpl
Copy link
Copy Markdown
Contributor

NeVeSpl commented Sep 10, 2023

Great job! AST also works out of the box, and all unit tests that I have in NTypewriter passed without any problem.
image

@coveralls
Copy link
Copy Markdown

coveralls commented May 1, 2024

Pull Request Test Coverage Report for Build 6058994959

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 84 of 293 (28.67%) changed or added relevant lines in 17 files are covered.
  • 145 unchanged lines in 7 files lost coverage.
  • Overall coverage decreased (-1.2%) to 77.397%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Scriban/Parsing/Parser.cs 1 2 50.0%
src/Scriban/Parsing/Parser.Statements.cs 0 2 0.0%
src/Scriban/Parsing/TokenTypeExtensions.cs 1 3 33.33%
src/Scriban/Syntax/ScriptToken.cs 0 2 0.0%
src/Scriban/Syntax/Expressions/ScriptLiteral.cs 7 10 70.0%
src/Scriban/Syntax/Expressions/ScriptBinaryOperator.cs 0 4 0.0%
src/Scriban/Functions/StringFunctions.cs 4 11 36.36%
src/Scriban/Parsing/Parser.Expressions.cs 14 21 66.67%
src/Scriban/ScribanAsync.generated.cs 0 9 0.0%
src/Scriban/Syntax/Expressions/ScriptInterpolatedExpression.cs 0 39 0.0%
Files with Coverage Reduction New Missed Lines %
src/Scriban.Tests/TestLexer.cs 1 92.72%
src/Scriban/Functions/StringFunctions.cs 3 87.36%
src/Scriban/Parsing/Lexer.cs 6 86.85%
src/Scriban/Parsing/Parser.Statements.cs 6 93.94%
src/Scriban/Syntax/Expressions/ScriptLiteral.cs 19 66.35%
src/Scriban/Parsing/Parser.Terminals.cs 53 62.01%
src/Scriban/Parsing/Parser.Expressions.cs 57 88.26%
Totals Coverage Status
Change from base Build 5236590679: -1.2%
Covered Lines: 11605
Relevant Lines: 14214

💛 - Coveralls

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: string interpolation

5 participants