infinisil changed the topic of #nix-lang to: Channel for discussing Nix as a language - https://nixos.org/nix/manual/#chap-writing-nix-expressions - Logs: https://logs.nix.samueldr.com/nix-lang/
hmpffff has joined #nix-lang
hmpffff_ has quit [Ping timeout: 260 seconds]
__monty__ has joined #nix-lang
hmpffff has quit [Read error: Connection reset by peer]
hmpffff_ has joined #nix-lang
<evanjs> somebody on the discord is wondering how they can escape local LAST_RET='${?#0}' and I'm at a loss
<evanjs> local LAST_RET=''${?#0}' seems to get everything but the first apostrophe
<__monty__> ,escape
<{^_^}> __monty__: Did you mean escape"?
<{^_^}> " double quote: \" backslash: \\ bash curly bois: \${} newline: \n tab: \t "
<__monty__> ,escape$
<{^_^}> __monty__: Did you mean escape"?
<{^_^}> " double quote: \" backslash: \\ bash curly bois: \${} newline: \n tab: \t "
<__monty__> ,escape''
<{^_^}> '' two single quotes: ''' bash curly bois: ''${} newline: ''\n tab: ''\t any character x: ''\x ''
<__monty__> evanjs: Is this in a '' quoted string?
<evanjs> Yeah that's what I was wondering but lemme see
<__monty__> > '''\''${?#0}'''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:16
<__monty__> > '''${"\${?#0}"}'''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:19
<__monty__> > ''LAST_RET='${"\${?#0}"}'''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:28
<MichaelRaskin> > '' LAST_RET='${"${?#0}' ''
<{^_^}> error: syntax error, unexpected '?', at (string):310:19
<evanjs> Yeah I was getting hung up on the ?
<MichaelRaskin> " > '' LAST_RET='${"$"}{?#0}' ''
<MichaelRaskin> I just typoed
<MichaelRaskin> > '' LAST_RET='${"$"}{?#0}' ''
<{^_^}> "LAST_RET='${?#0}' "
<ekleog> > '' LAST_RET='' + "'\${?#0}'" + '' [...]'' # should work too
<{^_^}> "LAST_RET='${?#0}'[...]"
<MichaelRaskin> (Unsure why would this string be wanted — ${} in shell sounds like something calling for ", not ')
<evanjs> ekleog: yeah that's what they initially "fixed" it with I think
<evanjs> MichaelRaskin: from the sounds of it, they're trying to import stuff from bashrc and profile but yeah, idk..
<ekleog> MichaelRaskin's solution is better, though
<evanjs> MichaelRaskin++
<{^_^}> MichaelRaskin's karma got increased to 35
<__monty__> > ''${$}''
<{^_^}> error: syntax error, unexpected $undefined, at (string):310:5
<__monty__> That looks like broken parsing to me.
<MichaelRaskin> Why?
<MichaelRaskin> > $
<{^_^}> error: syntax error, unexpected $undefined, at (string):310:1
<MichaelRaskin> Exactly same
<MichaelRaskin> I was using "$"
<MichaelRaskin> Which is just a normal string
<__monty__> Oh, right it actually evaluates stuff.
<pie_> evanjs: escaping is explaine din the manual
<pie_> evanjs: i had to keep looking it up for a ehilr
<pie_> *a while
<__monty__> pie_: Imo more attention could be paid to explaining escaping a dollar after an apostrophe.
<pie_> shits weird yo :D
<evanjs> pie_: Yeah I checked it out it's just weirder when you get a few levels down lol
<infinisil> ,escapeSpecial
<{^_^}> infinisil: Did you mean escape-special?
<{^_^}> A $${foo} will insert the literal string "$${foo}". Use \$${foo} in " strings or ''$${foo} in '' strings to have foo interpolated
<FireFly> funky
<FireFly> I didn't realise it works like that..
__monty__ has quit [Quit: leaving]
<infinisil> Hm, what if {^_^} had a ,howtoescape feature
<infinisil> Where you could insert some string and it would tell you how to make Nix output that string
<infinisil> ,escape"
<{^_^}> " double quote: \" backslash: \\ bash curly bois: \${} newline: \n tab: \t "
<qyliss> That would be very nice
<qyliss> You'd want modes for both " and '' strings
<infinisil> Probably could do them in one by just outputting both
<qyliss> That would work too
<qyliss> And even then you could only output both if they differ, which lots of the time they probably wouldn't.
<infinisil> Ah nice idea
<infinisil> > unicorn
<{^_^}> "🦄"
<infinisil> Now the difficult part of course is how to implement that..
<infinisil> Maybe a sane way is to just escape everything, then make sure the result is what we want, then remove escapings one by one, removing as much as possible until no more can be removed
<infinisil> > "\h\e\l\l\o\$\$\{\}"
<{^_^}> "hello$${}"
<infinisil> Or maybe only escape all characters that *might* be involved in escape shenanigans
<infinisil> Or probably rather only don't escape those that for sure aren't involved in escape shenanigans
<qyliss> well, for double quoting Nix can already do this:
<qyliss> % nix-instantiate --eval -E "{ x }: x" --argstr x 'foo"'
<qyliss> "foo\""
<infinisil> Oh and that always works?
<qyliss> I assume it does
<qyliss> would be weird to have it if that wasn't at least a goal
<infinisil> % nix-instantiate --eval -E "{ x }: x" --argstr x '${}'
<infinisil> "${}"
<qyliss> hmm :/
<infinisil> Guess not quite
<qyliss> that's probably a bug tbh?
<infinisil> Is it a bug if this was never intended to be a feature?
hmpffff_ has quit [Quit: nchrrrr…]
<infinisil> Oh, does it maybe just use json escaping for the output
hmpffff has joined #nix-lang
hmpffff has quit [Client Quit]
hmpffff has joined #nix-lang
hmpffff has quit [Client Quit]
<infinisil> > "${"h"}${"i"}${"$"}${"\""}"
<{^_^}> "hi$\""
<qyliss> I guess I just assumed it would output an escaped Nix string because lots of other interpreted languages do that
<pie_> their escaping is a bit simpler though >.>
<qyliss> not necessarily
<pie_> (citation needed)
<pie_> <-
<infinisil> With IRC at least I won't need to handle newlines :)
<qyliss> Ruby escaping is pretty gnarly
<qyliss> Or, well, it's pretty regular in that you always use a backslash, but there's all sorts of weird syntaxes inside a string
<infinisil> ,escape'
<{^_^}> " double quote: \" backslash: \\ bash curly bois: \${} newline: \n tab: \t "
<{^_^}> infinisil: Did you mean escape"?
<infinisil> ,escape''
<{^_^}> '' two single quotes: ''' bash curly bois: ''${} newline: ''\n tab: ''\t any character x: ''\x ''
<infinisil> > '' '' ''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):311:1
<infinisil> > ''${"'"}${"'"}''
<{^_^}> "''"
<infinisil> How to escape anything in a '' string: Just use ''${<the escaped form for " strings>}'' :)
<infinisil> Oh no, I think trying to find the best escaping is a graph traversal problem
<infinisil> The starting node is the string fully escaped, with ${"x"} for every character
<infinisil> Edges are transformations to that string that don't change the final result
<infinisil> And they need to shorten the string too
<infinisil> The goal is a node that has the shortest string, which you can get to using a series of these edge transformations
<infinisil> > '' \thello ''
<{^_^}> "\\thello "
<infinisil> > '' ''' ''
<{^_^}> "'' "
<infinisil> > '''''''
<{^_^}> "''"
<infinisil> > ''''''''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:9
<infinisil> > '' '''' ''
<{^_^}> "''' "
<infinisil> Huh
<infinisil> > '' ''''''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:10
<infinisil> > '''''' ''
<{^_^}> "''' "
<infinisil> > '' '''' ''
<{^_^}> "''' "
<infinisil> > '' ''''' ''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):311:1
<infinisil> > '' '''''' ''
<{^_^}> "'''' "
<infinisil> > '''''
<{^_^}> error: syntax error, unexpected $end, expecting IND_STR or DOLLAR_CURLY or IND_STRING_CLOSE, at (string):310:6
<infinisil> > ''${"'"}''
<{^_^}> "'"
<infinisil> > ''''\'''
<{^_^}> "'"
<infinisil> > ''''\ ''
<{^_^}> " "
<infinisil> > ''''\n''
<{^_^}> "\n"
<infinisil> What
<infinisil> > '' ''\n ''
<{^_^}> "\n "
<infinisil> > '' ''\t ''
<{^_^}> "\t "
<infinisil> > '' ''\a ''
<{^_^}> "a "
<infinisil> > '' ''\'''\'''\' ''
<{^_^}> "''' "
<infinisil> > '' ''\'''\'''\'''
<{^_^}> "'''"
<infinisil> > ''''\'''\'''\'''
<{^_^}> "'''"
<infinisil> > ''''\'''\'''
<{^_^}> "''"
<MichaelRaskin> infinisil: If you are doing graph traversal, maybe simplify the situation a bit and consider the graph where paths are valid input strings?
<MichaelRaskin> (input strings to the parser)