Expanding macro arguments and interpolations, inside and outside quoted strings #668
Replies: 9 comments 2 replies
-
I propose something closer to the 0.4.1 behavior, where macro args and interpolation are both always expanded and then lexed further; but with a way to explicitly request that the contents be taken directly / string-escaped. This is easy to do for interpolation (should be added post- #646 though): using a format specifier can escape the contents, otherwise not. So For macro args, it would need something further. C has its
|
Beta Was this translation helpful? Give feedback.
-
My own preference would be for plain (So for a guaranteed valid string, you could do This would avoid breaking asm code, since both quoted and unquoted macro args and interpolations would work the same as in 0.4.1 and 0.4.2. (Except unquoted interpolations, which acted like quoted ones in 0.4.1, and were banned in 0.4.2, and would now expand just like macro args.) It would also allow the lexer to be simplified to not make |
Beta Was this translation helpful? Give feedback.
-
Another rationale for not introducing an explicit format spec type for escaped strings:
Meanwhile, the plain untyped |
Beta Was this translation helpful? Give feedback.
-
Regarding |
Beta Was this translation helpful? Give feedback.
-
On the other hand, |
Beta Was this translation helpful? Give feedback.
-
While I'm throwing out alternatives for discussion: consider if
Thus if (So if you're confident that a macro arg or an |
Beta Was this translation helpful? Give feedback.
-
I agree that However, I've pondered a lot on how to transition into that change, and I haven't found a satisfactory answer yet. This is not behavior that I want to directly change (so I'm embarrassed that @Rangi42 opened #685 so soon), but I'm not sure how to deprecate it either. Release 0.4.3(-pre?) with a known bug and deprecation warning, and fix it properly in 0.4.4? |
Beta Was this translation helpful? Give feedback.
-
Is #685 unsatisfactory besides being a breaking change? It could be made more transition-friendly by having string literals allow |
Beta Was this translation helpful? Give feedback.
-
A summary of my intended implemented behavior: #685 (comment) |
Beta Was this translation helpful? Give feedback.
-
In the examples below, let's say that
X EQUS "a\"b"
and\1
isc"d
(as inmymacro c"d
).Behavior in 0.4.1 and previous:
{X}
and"{X}"
would act like"a\"b"
: the contents of the interpolated symbol get concatenated directly, essentially string-escaping their contents.\1
would expand toc"d
and"\1"
would expand to"c"d"
: both syntax errors due to unbalanced quotes, since macro args would always be expanded and then lexed further, as if they had been literally typed where the\
expansion was.In 0.4.2 unquoted
{X}
became a syntax error.As of 2020-12-28, the post-0.4.2
master
behavior is an attempt to have useful behavior as needed: when inside a quoted string (i.e. whenreadString
is doing the lexing), interpolation and macro arg contents are concatenated as-is / i.e. string-escaped; outside a quoted string, they're not. So:{X}
expands to the syntax errora"b
, but"{X}"
expands to"a\"b"
\1
expands to the syntax errorc"d
, but"\1"
expands to"c\"d"
However, this still has the potential for awkward behavior when you try to open or close a string with an interpolation or macro arg. For example, if
Y EQUS "\"hello"
, thenprintt {Y} world"
will printhello world
; but ifW EQUS "world\""
, thenprintt "hello {W}
will give anUnterminated string
error before printinghello world"
, because the final quote inW
is concatenated to the string contents, not used to close the string. Doingprintt {Y} {W}
gives the same result; and using macro args instead of interpolations has the same issue. Various constructions involving triple-quoted string literals also give errors like this, which are predictable given the current implementation but are probably not useful.There's another inconsistency: macro arguments are lexed in raw mode, which doesn't handle the difference between quoted and unquoted expansions in the same way. For example:
This prints:
Beta Was this translation helpful? Give feedback.
All reactions