-
Notifications
You must be signed in to change notification settings - Fork 92
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
String interpolation #82
Comments
Here’s the dhall standard for multiline strings, which improves on the nix multiline strings: https://github.com/dhall-lang/dhall-lang/blob/master/standard/multiline.md We have to make sure that our strings are “8-bit clean”, meaning you can put arbitrary bytes in them via escape codes, otherwise people are severely restricted in what they can generate configuration for. I’ve noticed that for generating code (in this case generating nickel code) it’s super useful to have raw strings in the target language, which don’t require any escaping. |
Care to explain how? (Not that I doubt it, but the standard doesn't explain this very clearly) |
Iirc it pertains to when \n is added. I don’t remember the exact
differences, but creating a table that compares examples would be a good
first step.
…On Wed, Jun 17, 2020 at 2:28 PM Théophane Hufschmitt < ***@***.***> wrote:
Here’s the dhall standard for multiline strings, which improves on the nix
multiline strings
Care to explain how? (Not that I doubt it, but the standard doesn't
explain this very clearly)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#82 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAYB5ZQGPMSDITPYAMPJASDRXCZHZANCNFSM4NQVAJXA>
.
|
We should probably reconsider the use of
|
After reading Dhall's standard it seems to me the only difference is that in Dhall the last line, even if it only contains whitespaces, is significant for determining global indentation, that is
desugars to Then Dhall also preserve leading whitespaces when interpolating strings with new lines inside multiline strings, as I mentioned in the issue, but this is not described in the linked document. |
Something I didn't see mentioned is whether Nickel should support Nix-style string contexts, i.e. whether the value |
string contexts seem to be something that could be tremendously useful for other use-cases. At least in Bazel, starlark doesn't have any equivalent of these and it bite me a lot of time because I was referring to some target in a script but had forgotten to add it to the dependencies. It might be useful to have something even more dynamic/lazy though, as we could also want something like (in a totally hypotetical cloud deployment tool based on nickel and imagined just for the sake of the argument, although the same might be true in something like terraform too): rec {
my_first_instance = spawn_ec2_instance {...};
my_other_instance = spawn_ec2_instance {
system_config = { ... }: {
networking.hosts.${my_first_instance.ip_address} = "my-fist-instance";
...
};
};
} In which case |
I'm reopening the discussion, as interpolated strings landed in #131, and multiline strings are next.
@edolstra, do you mean that usual double quote delimited strings would act like multiline strings in Nix
should be parsed as "indented\nstring" ? In this case, what about " indented\n string", do we differentiate escaped new line
This is probably the better solution. The only thing is maybe that this is confusing with respect to other languages, as these kind of delimiters are used for raw strings everywhere, but we probably want to still allow interpolation in our multiline strings. |
The fate of a Nickel program is eventually to produce text in the form of configuration files. A fair amount of time is spent gathering and gluing together various pieces of strings in order to produce this configuration. Hence string interpolation, making text substitution easier, is a critical feature.
We propose to unashamedly reuse Nix string interpolation, which is well appreciated in the ecosystem and proved itself efficient for writing self-contained expressions, which mix strings containing shell commands, shell scripts, text descriptions, URLs, and so on. The syntax using
$
is common (Shell, JavaScript, Perl, Scala, Kotlin, Julia, etc.) while the required additional{
avoid clashing with the simple shell interpolation$var
when embedding scripts or commands inside strings.In the following, we provide a concrete proposal, as a starting point for discussion.
Examples
Definition
We propose two ways of defining strings:
"foo"
''foo''
The alternative delimiter
''
is intended for enclosing strings that contain numerous"
, such as shell commands, which would be then tiresome to escape. For indented strings enclosed by double single quotes, as the second example of the previous section, a number of spaces corresponding to the minimal indentation of the string as a whole is stripped from the beginning of each line. Also, the first line is removed if it is empty or contains only whitespaces. This makes it possible to write multi-line strings with the same level of indentation as the surrounding code without polluting the final string with unnecessary heading spaces.Interpolation allows to embed a Nickel expression, which must evaluate to a string, inside another string using the
${expr}
syntax. Such expressions can be arbitrary Nickel expressions and may contain themselves nested interpolated strings.Syntax
str ::=
"
strprts"
|''
strprts''
strprts ::= ε | strprts STR | strprts ${e}
where e is any Nickel expression, STR is a sequence of string characters (without the special sequence
${
, unless escaped), ε denotes an empty string and spaces between tokens denote concatenation.Escaping
Inside
"
,\
is used as usual to represent special sequences ("\n"
,"\t"
, and so on), to escape itself or to escape the interpolation sequence${
. On the other hand, it has no special meaning alone inside''
, and must be prefixed with''
to recover it. For example, a tab is written''''\t''
."
''
${
''${
\${
'
'''
'
\
\
\\
Semantics
"
strprts"
''
strprts''
where stripIndent implements the behavior described above about smart indentation.
Remarks
We may want to improve the behavior of Nix when substituting multi-line strings inside multi-line, indented strings (See #543 for Nix, or #200 for a similar discussion about Dhall).
The text was updated successfully, but these errors were encountered: