In your question you say:
(The last thing of the replacement-text is to be an unbalanced opening curly brace of category 1.)
When referring to a macro, then the phrase "replacement text" usually denotes the replacement text that belongs to the definition of that macro.
David Carlisle already pointed out in a comment that the replacement text of the definition of a macro cannot contain unbalanced/unmatched braces/explicit character tokens of category 1 or 2.
TeXbook, chapter 20: Definitions (also called Macros), says:
\def⟨control sequence⟩⟨parameter
text⟩{⟨replacement text⟩}
TeXbook, chapter 24: Summary of Vertical Mode, presents the grammar of TeX in Backus/Naur-notation. In that chapter you find:
⟨definition⟩ → ⟨def⟩⟨control sequence⟩⟨definition text⟩
⟨def⟩ → \def | \gdef | \edef | \xdef
⟨definition text⟩ → ⟨parameter text⟩⟨left brace⟩⟨balanced text⟩⟨right brace⟩
[...]
All occurrences of ⟨left brace⟩ and ⟨right brace⟩ tokens within the ⟨balanced text⟩ must be properly nested like parentheses.
So in any case the replacement text of the definition of a macro is formed by ⟨balanced text⟩ where braces are balanced/properly nested.
Therefore I suppose the question is not about a macro whose definition's replacement text contains unmatched/unbalanced braces but is about a macro
- whose definition's replacement text does not contain unmatched/unbalanced braces
- which takes an undelimited argument and which can serve the purpose of initiating an expansion-cascade which after a known amount of expansion-steps yields1 the tokens forming the argument nested in curly braces and trailed by an opening curly brace which is not matched/balanced by a closing curly brace.
Question 1:
How shall braces surrounding \macro's argument be treated if present?
Question 2:
How shall the case of \macro's argument not being surrounded by braces be handled?
If
- the answer to question 1 is "braces surrounding
\macro's argument may be stripped off and another pair of braces may be added instead (probably yielding braces of different character code)" and
- the answer to question 2 is "In the result, the argument shall in any case be surrounded by braces" and
- the question was not about traditional TeX2,
then you can extract the gist of Skillmon's answer to the question "Fully robust way to access the first item in a token list (expandably)", i.e., combine \expanded and \unexpanded.
The following \macro delivers the result after triggering two expansion-steps/after two "hits" by \expandafter.
1The answerer of the question's opinion is that the phrase "yields" does not very precisely describe the requirements.
2The phrase "the question was not about traditional TeX" is bold to emphasize that the answerer is aware that this answer does not really answer the question as in this answer it is deviated from the requirements/conditions given in the question.
\long\def\macro#1{\expanded{\unexpanded{{#1}}\expandafter}\expandafter{\iffalse}\fi}
\long\def\processtwoargs#1#2{%
\message{^^J\unexpanded{argument 1: (#1) argument 2: (#2)}}%
}%
% Tests:
\expandafter\expandafter\expandafter\processtwoargs\macro{first}second}
\expandafter\expandafter\expandafter\processtwoargs\macro{123{4 5{6}}789}second}
\expandafter\expandafter\expandafter\processtwoargs\macro{###}###}
\message{^^JJust make sure there is no confusion about hash-doubling:}
\message{^^J\unexpanded{argument 1: (###) argument 2: (###)}}
\csname bye\endcsname
\csname stop\endcsname
\endinput
Messages on terminal:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdftex)
restricted \write18 enabled.
entering extended mode
(./test.tex
argument 1: (first) argument 2: (second)
argument 1: (123{4 5{6}}789) argument 2: (second)
argument 1: (######) argument 2: (######)
Just make sure there is no confusion about hash-doubling:
argument 1: (######) argument 2: (######) )
No pages of output.
Transcript written on test.log.
\documentclass{...}and ending with\end{document}. – DG' Jul 13 '22 at 08:30{and}? – user202729 Jul 13 '22 at 08:33\iftrue{\else}\fiwhich expands to an unbalanced brace and may or may not meet the use case, depending what the use case is. – David Carlisle Jul 13 '22 at 08:47\macro{argument}expands after some-number-of expansion steps to{argument} {. – user202729 Jul 13 '22 at 08:49{but perhaps the context is\hboxand actually\bgroupis fine. Or perhaps\iftrue{\else}\fiexpanding in a single step to{\else}\fiis fine, there are only certain uses that require fancy processing to remove the\else}\fiin advance. – David Carlisle Jul 13 '22 at 09:00