I've been investigating adding support for argument labels in variable names over the past couple days. The high-level approach to the parsing work, as I see it, is to identify all* the locations that currently expect an identifier
for a name, and modify them to expect an unqualified-decl-name
instead.
* Certain places are excluded, e.g., argument labels and enum case names.
Part of this effort includes allowing patterns to bind compound names, so that you could write the following:
enum E {
case c(foo: (Int) -> Void)
}
let e: E = .c(foo: { _ in })
switch e {
case .c(let bar(x:)):
bar(x: 0)
}
At the point where the name actually gets parsed, we have the following code:
DeferringContextRAII Deferring(*SyntaxContext);
// ... consume an identifier, create a NamedPattern, etc.
if (SyntaxContext->isEnabled()) {
ParsedPatternSyntax PatternNode =
ParsedSyntaxRecorder::makeIdentifierPattern(SyntaxContext->popToken(),
*SyntaxContext);
ParsedExprSyntax ExprNode =
ParsedSyntaxRecorder::makeUnresolvedPatternExpr(std::move(PatternNode),
*SyntaxContext);
SyntaxContext->addSyntax(std::move(ExprNode));
}
The normal parsing code is straightforward to me, but I'm having a bit of trouble understanding where SyntaxContext
fits into all of this. Big picture question is, what role does it play in the parsing process as opposed to the "normal" AST?
But beyond the big picture question, I'm also unsure what the correct way is to extend this portion of the code to handle compound names. Currently, we create the ParsedPatternSyntax
by simply calling popToken()
on SyntaxContext
, which no longer works in a potentially-compound-name world (since the name may consist of arbitrarily many tokens). It's not clear to me from investigating other use sites how I should go about recovering the proper Parsed*Syntax
bits needed to create the ParsedPatternSyntax
. At the very least, I think I'll need to create a ParsedDeclNamePatternSyntax
that is built up from a ParsedDeclNameSyntax
, but I'm still not sure how to get my hands on that ParsedDeclNameSyntax
once we've gone through the normal parsing route for a DeclName
.
If anyone familiar with this aspect of the parser could help me understand SyntaxParsingContext
(and also how it interacts with DeferringContextRAII
), I would be very appreciative!