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

Feature: String templates #2630

Merged
merged 36 commits into from
Dec 1, 2023

Conversation

timotheeguerin
Copy link
Member

@timotheeguerin timotheeguerin commented Nov 3, 2023

fix #1591

String templates

Playground examples

import "@typespec/json-schema";

using TypeSpec.JsonSchema;

@jsonSchema
namespace Schemas;

alias myconst = "foobar";

model Person {
  simple: "Simple ${123} end";
  multiline: """
  Multi 
     ${123} 
    ${true}
  line
  """;

  ref: "Ref this alias ${myconst} end";
  template: Template<"custom">;
}

alias Template<T extends valueof string> = "Foo ${T} bar";

Other fixes

Also fixes https://github.com/Azure/typespec-azure/issues/3399(Show invalid escape sequence char instead of the whole string)
image

Copy link
Contributor

github-actions bot commented Nov 3, 2023

Changes in this PR will be published to the following url to try(check status of TypeSpec Pull Request Try It pipeline for publish status):
Playground: https://cadlplayground.z22.web.core.windows.net/prs/2630/

Website: https://tspwebsitepr.z22.web.core.windows.net/prs/2630/

@@ -102,6 +102,14 @@ import {
StdTypes,
StringLiteral,
StringLiteralNode,
StringTemplate,
Copy link
Member Author

@timotheeguerin timotheeguerin Nov 3, 2023

Choose a reason for hiding this comment

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

Do the names look good? Alternatives could have been

  • TemplateString
  • TemplateStringLiteral
  • TemplateLiteral
  • StringTemplateLiteral

Copy link
Contributor

@daviwil daviwil Nov 16, 2023

Choose a reason for hiding this comment

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

StringTemplateLiteral is probably more accurate

EDIT: Ahh, I didn't see the rest of the names. Probably don't want all of them to be StringTemplateLiteralX so StringTemplate is fine IMO

@timotheeguerin timotheeguerin marked this pull request as ready for review November 14, 2023 15:43
@@ -102,6 +107,14 @@ import {
StdTypes,
StringLiteral,
StringLiteralNode,
StringTemplate,
StringTemplateExpressionNode,
StringTemplateHeadNode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you split this into head/middle/end so that you only capture the part that needs to be computed as the middle?

Copy link
Member Author

Choose a reason for hiding this comment

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

Those are technically slightly different nodes and that's how typescript also structure their template so thought be good to keep inline as they must have had a reason to do that. But right now the data inside is basically the same

@@ -154,14 +154,28 @@ <h1>Lexical Grammar</h1>
StringLiteral :
`"` StringCharacters? `"`
`"""` TripleQuotedStringCharacters? `"""`
StringTemplateHead

StringTemplateHead ::
Copy link
Member

Choose a reason for hiding this comment

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

I think this grammar is incomplete because we cannot reach StringTemplateMiddle/StringTemplateTail from the top-level goal symbol InputElement. We will need a separate goal symbol (say, InputElementTemplateTail). I also think it should be a parse error to have an empty substitution, which would require separating out right brace from the list of tokens and only allowing it with the InputElementTemplateTail.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated, let me know if that looks better

@timotheeguerin timotheeguerin merged commit 360add2 into microsoft:main Dec 1, 2023
10 checks passed
@timotheeguerin timotheeguerin deleted the feature/string-template branch December 1, 2023 22:33
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.

Implementation: Support string interpolation
4 participants