-
Notifications
You must be signed in to change notification settings - Fork 96
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
Symbolic strings #994
Symbolic strings #994
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,39 @@ use crate::{ | |
types::{TypeF, Types}, | ||
}; | ||
|
||
/// Distinguish between the standard string separators `"`/`"` and the multi-line string separators | ||
/// `m%"`/`"%` in the parser. | ||
/// Distinguish between the standard string opening delimiter `"`, the multi-line string | ||
/// opening delimter `m%"`, and the symbolic string opening delimiter `s%"`. | ||
#[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
pub enum StringKind { | ||
matthew-healy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub enum StringStartDelimiter { | ||
Standard, | ||
Multiline, | ||
Symbolic, | ||
} | ||
|
||
impl StringStartDelimiter { | ||
pub fn is_closed_by(&self, close: &StringEndDelimiter) -> bool { | ||
matches!( | ||
(self, close), | ||
(StringStartDelimiter::Standard, StringEndDelimiter::Standard) | ||
| (StringStartDelimiter::Multiline, StringEndDelimiter::Special) | ||
| (StringStartDelimiter::Symbolic, StringEndDelimiter::Special) | ||
) | ||
} | ||
|
||
pub fn needs_strip_indent(&self) -> bool { | ||
match self { | ||
StringStartDelimiter::Standard => false, | ||
StringStartDelimiter::Multiline | StringStartDelimiter::Symbolic => true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, so what I wrote before is wrong, multiline strings and symbolic strings actually behave the same. I'm not sure how this works with interpolated values though. There is some logic at runtime such that:
evaluates to
I don't think we can easily keep this logic for symbolic strings, unless we delegate this to custom parsing functions (but that start to be cumbersome). |
||
} | ||
} | ||
} | ||
|
||
/// Distinguish between the standard string closing delimter `"` and the "special" string | ||
/// closing delimeter `"%`. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum StringEndDelimiter { | ||
Standard, | ||
Special, | ||
} | ||
|
||
/// Distinguish between a normal case `id => exp` and a default case `_ => exp`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
let {check, ..} = import "lib/assert.ncl" in | ||
|
||
[ | ||
# Static symbolic string | ||
s%"hello, world"% == ["hello, world"], | ||
# Interpolating a string | ||
let s = "test" in | ||
s%"This is a %{s}"% == ["This is a ", "test"], | ||
# Interpolating an interpolated string | ||
let f = "f" in | ||
s%"abc %{"de%{f}"}"% == ["abc ", "def"], | ||
# Interpolating a number | ||
s%"num: %{100}"% == ["num: ", 100], | ||
# Interpolating a bool | ||
s%"bool: %{true}"% == ["bool: ", true], | ||
# Interpolating an array | ||
s%"array: %{[true, 1, "yes"]}"% == ["array: ", [true, 1, "yes"]], | ||
# Interpolating a record | ||
let r = { a = 1, b = false } in | ||
s%"record: %{r}"% == ["record: ", r], | ||
# Interpolating multiple values | ||
let str = "some string" in | ||
let num = 999.999 in | ||
let bool = false in | ||
let array = ["an", "array", 100] in | ||
let record = { a = 1, simple = "yes", record = true } in | ||
let actual = s%" | ||
1. %{str} | ||
2. %{num} | ||
3. %{bool} | ||
4. %{array} | ||
5. %{record}"% | ||
in | ||
let expected = [ | ||
"1. ", str, | ||
"\n2. ", num, | ||
"\n3. ", bool, | ||
"\n4. ", array, | ||
"\n5. ", record | ||
] | ||
in | ||
actual == expected, | ||
] | ||
|> check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this in mind but forget to ask. Should we behave like multiline string with respect to indentation, or not? It seems like you answer "no", which is very reasonable. That being said, I wonder if we then would like to combine the semantics of symbolic strings and multiline strings, e.g. in Nix often we currently use multiline strings to write builder commands, where symbolic strings would be used. This may not be trivial with respect to indenting interpolated values, though. I guess we don't have to answer this question right now.