Skip to content

Commit 6aee036

Browse files
authored
Merge pull request swiftlang#22214 from bannzai/syntax/add/function/make_ellipsis_token
[SwiftSyntax] Add SyntaxFactory.makeEllipsisToken function.
2 parents c8effd2 + 814cf82 commit 6aee036

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,10 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
360360
}
361361

362362
// '...'?
363-
if (Tok.isEllipsis())
363+
if (Tok.isEllipsis()) {
364+
Tok.setKind(tok::ellipsis);
364365
param.EllipsisLoc = consumeToken();
366+
}
365367

366368
// ('=' expr)?
367369
if (Tok.is(tok::equal)) {

test/Frontend/emit-syntax.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,47 @@ struct Foo {
1313
// CHECK: "kind":"r_brace"
1414
}
1515

16+
// CHECK: "kind":"kw_func"
17+
// CHECK: "kind":"identifier"
18+
// CHECK: "text":"Bar"
19+
// CHECK: "kind":"l_paren"
20+
// CHECK: "kind":"FunctionParameterList"
21+
// CHECK: "kind":"FunctionParameter"
22+
// CHECK: "kind":"identifier"
23+
// CHECK: "text":"arg1"
24+
// CHECK: "kind":"colon"
25+
// CHECK: "kind":"identifier"
26+
// CHECK: "text":"String"
27+
// CHECK: null,null
28+
// CHECK: "kind":"comma"
29+
// CHECK: "kind":"identifier"
30+
// CHECK: "text":"arg2"
31+
// CHECK: "kind":"colon"
32+
// CHECK: "kind":"identifier"
33+
// CHECK: "text":"Int"
34+
// CHECK: "kind":"r_paren"
35+
// CHECK: "kind":"l_brace"
36+
func Bar(arg1: String, arg2: Int) {
37+
// CHECK: "kind":"r_brace"
38+
}
39+
40+
// CHECK: "kind":"kw_func"
41+
// CHECK: "text":"CheckParameterList"
42+
// CHECK: "kind":"l_paren"
43+
// CHECK: "text":"arg1"
44+
// CHECK: "text":"String"
45+
// CHECK: "kind":"ellipsis"
46+
// CHECK: "presence":"Present"}
47+
// CHECK: null
48+
// CHECK: "kind":"comma"
49+
// CHECK: "text":"arg2"
50+
// CHECK: "text":"Int"
51+
// CHECK: "kind":"r_paren"
52+
// CHECK: "kind":"l_brace"
53+
func CheckParameterList(arg1: String..., arg2: Int) {
54+
// CHECK: "kind":"r_brace"
55+
}
56+
1657
// CHECK: "leadingTrivia":[
1758
// CHECK: "kind":"LineComment",
1859
// CHECK: "value":"\/\/ Comment at the end of the file"

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,34 @@ TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
324324
}
325325
}
326326

327+
TEST(DeclSyntaxTests, FunctionParameterWithEllipsis) {
328+
auto ExternalName = SyntaxFactory::makeIdentifier("for", {},
329+
Trivia::spaces(1));
330+
auto LocalName = SyntaxFactory::makeIdentifier("integer", {}, {});
331+
auto Colon = SyntaxFactory::makeColonToken(Trivia::spaces(1),
332+
Trivia::spaces(1));
333+
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {},
334+
Trivia::spaces(0));
335+
auto Ellipsis = SyntaxFactory::makeEllipsisToken({},
336+
Trivia::spaces(1));
337+
auto Comma = SyntaxFactory::makeCommaToken({}, {});
338+
339+
{
340+
SmallString<48> Scratch;
341+
llvm::raw_svector_ostream OS(Scratch);
342+
getCannedFunctionParameter()
343+
.withFirstName(ExternalName)
344+
.withSecondName(LocalName)
345+
.withColon(Colon)
346+
.withType(Int)
347+
.withEllipsis(Ellipsis)
348+
.withDefaultArgument(llvm::None)
349+
.withTrailingComma(Comma)
350+
.print(OS);
351+
ASSERT_EQ(OS.str().str(), "for integer : Int... ,");
352+
}
353+
}
354+
327355
#pragma mark - parameter-list
328356

329357
TEST(DeclSyntaxTests, FunctionParameterListMakeAPIs) {

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@
335335
is_optional=True),
336336
Child('Type', kind='Type',
337337
is_optional=True),
338-
Child('Ellipsis', kind='Token',
338+
Child('Ellipsis', kind='EllipsisToken',
339339
is_optional=True),
340340
Child('DefaultArgument', kind='InitializerClause',
341341
is_optional=True),

utils/gyb_syntax_support/Token.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def macro_name(self):
222222
Punctuator('PrefixPeriod', 'period_prefix', text='.',
223223
serialization_code=87),
224224
Punctuator('Comma', 'comma', text=',', serialization_code=84),
225+
Punctuator('Ellipsis', 'ellipsis', text='...', serialization_code=118),
225226
Punctuator('Colon', 'colon', text=':', serialization_code=82),
226227
Punctuator('Semicolon', 'semi', text=';', serialization_code=83),
227228
Punctuator('Equal', 'equal', text='=', serialization_code=86),

0 commit comments

Comments
 (0)