Skip to content

Commit a577548

Browse files
committed
[Parser] Adopt ArgumentList
Split up the expr list parsing members such that there are separate entry points for tuple and argument list parsing, and start using the argument list parsing member for call and subscripts.
1 parent 01a082a commit a577548

File tree

6 files changed

+205
-284
lines changed

6 files changed

+205
-284
lines changed

include/swift/Parse/Parser.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,24 +1695,32 @@ class Parser {
16951695
SourceLoc &inLoc);
16961696

16971697
Expr *parseExprAnonClosureArg();
1698-
ParserResult<Expr> parseExprList(tok LeftTok, tok RightTok,
1699-
syntax::SyntaxKind Kind);
17001698

1701-
/// Parse an expression list, keeping all of the pieces separated.
1702-
ParserStatus parseExprList(tok leftTok, tok rightTok,
1703-
bool isPostfix,
1704-
bool isExprBasic,
1705-
SourceLoc &leftLoc,
1706-
SmallVectorImpl<Expr *> &exprs,
1707-
SmallVectorImpl<Identifier> &exprLabels,
1708-
SmallVectorImpl<SourceLoc> &exprLabelLocs,
1709-
SourceLoc &rightLoc,
1710-
SmallVectorImpl<TrailingClosure> &trailingClosures,
1711-
syntax::SyntaxKind Kind);
1699+
/// An element of an expression list, which may become e.g a tuple element or
1700+
/// argument list argument.
1701+
struct ExprListElt {
1702+
SourceLoc LabelLoc;
1703+
Identifier Label;
1704+
Expr *E;
1705+
};
17121706

1713-
ParserStatus
1714-
parseTrailingClosures(bool isExprBasic, SourceRange calleeRange,
1715-
SmallVectorImpl<TrailingClosure> &closures);
1707+
/// Parse a tuple or paren expr.
1708+
ParserResult<Expr> parseTupleOrParenExpr(tok leftTok, tok rightTok);
1709+
1710+
/// Parse an argument list.
1711+
ParserResult<ArgumentList>
1712+
parseArgumentList(tok leftTok, tok rightTok, bool isExprBasic,
1713+
bool allowTrailingClosure = true);
1714+
1715+
/// Parse one or more trailing closures after an argument list.
1716+
ParserStatus parseTrailingClosures(bool isExprBasic, SourceRange calleeRange,
1717+
SmallVectorImpl<Argument> &closures);
1718+
1719+
/// Parse an expression list.
1720+
ParserStatus parseExprList(tok leftTok, tok rightTok, bool isArgumentList,
1721+
SourceLoc &leftLoc,
1722+
SmallVectorImpl<ExprListElt> &elts,
1723+
SourceLoc &rightLoc, SyntaxKind Kind);
17161724

17171725
/// Parse an object literal.
17181726
///

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,18 +2762,11 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
27622762
return ParserResult<CustomAttr>(ParserStatus(type));
27632763
}
27642764

2765-
// Parse the optional arguments.
2766-
SourceLoc lParenLoc, rParenLoc;
2767-
SmallVector<Expr *, 2> args;
2768-
SmallVector<Identifier, 2> argLabels;
2769-
SmallVector<SourceLoc, 2> argLabelLocs;
2770-
SmallVector<TrailingClosure, 2> trailingClosures;
2771-
bool hasInitializer = false;
2772-
27732765
// If we're not in a local context, we'll need a context to parse
27742766
// initializers into (should we have one). This happens for properties
27752767
// and global variables in libraries.
27762768
ParserStatus status;
2769+
ArgumentList *argList = nullptr;
27772770
if (Tok.isFollowingLParen() && isCustomAttributeArgument()) {
27782771
if (peekToken().is(tok::code_complete)) {
27792772
consumeToken(tok::l_paren);
@@ -2800,22 +2793,20 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
28002793

28012794
initParser.emplace(*this, initContext);
28022795
}
2803-
status |= parseExprList(tok::l_paren, tok::r_paren,
2804-
/*isPostfix=*/false, /*isExprBasic=*/true,
2805-
lParenLoc, args, argLabels, argLabelLocs,
2806-
rParenLoc,
2807-
trailingClosures,
2808-
SyntaxKind::TupleExprElementList);
2809-
assert(trailingClosures.empty() && "Cannot parse a trailing closure here");
2810-
hasInitializer = true;
2796+
auto result = parseArgumentList(tok::l_paren, tok::r_paren,
2797+
/*isExprBasic*/ true,
2798+
/*allowTrailingClosure*/ false);
2799+
status |= result;
2800+
argList = result.get();
2801+
assert(!argList->hasAnyTrailingClosures() &&
2802+
"Cannot parse a trailing closure here");
28112803
}
28122804
}
28132805

28142806
// Form the attribute.
28152807
auto *TE = new (Context) TypeExpr(type.get());
2816-
auto customAttr = CustomAttr::create(Context, atLoc, TE, hasInitializer,
2817-
initContext, lParenLoc, args, argLabels,
2818-
argLabelLocs, rParenLoc);
2808+
auto *customAttr = CustomAttr::create(Context, atLoc, TE, initContext,
2809+
argList);
28192810
return makeParserResult(status, customAttr);
28202811
}
28212812

0 commit comments

Comments
 (0)