Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift5] Fix closure_parameter rule, add optional UNDERSCORE #4388

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions swift/swift5/Cpp/SwiftSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ SwiftSupport::OperatorCharacter::OperatorCharacter()
SwiftSupport::LeftWS::LeftWS() {
set(Swift5Parser::WS);
set(Swift5Parser::LPAREN);
set(Swift5Parser::Interpolataion_multi_line);
set(Swift5Parser::Interpolataion_single_line);
set(Swift5Parser::Interpolation_multi_line);
set(Swift5Parser::Interpolation_single_line);
set(Swift5Parser::LBRACK);
set(Swift5Parser::LCURLY);
set(Swift5Parser::COMMA);
Expand Down
4 changes: 2 additions & 2 deletions swift/swift5/Java/SwiftSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ To use it in the ternary conditional (? :) operator, it must have

leftWS.set(Swift5Parser.WS);
leftWS.set(Swift5Parser.LPAREN);
leftWS.set(Swift5Parser.Interpolataion_multi_line);
leftWS.set(Swift5Parser.Interpolataion_single_line);
leftWS.set(Swift5Parser.Interpolation_multi_line);
leftWS.set(Swift5Parser.Interpolation_single_line);
leftWS.set(Swift5Parser.LBRACK);
leftWS.set(Swift5Parser.LCURLY);
leftWS.set(Swift5Parser.COMMA);
Expand Down
7 changes: 4 additions & 3 deletions swift/swift5/Swift5Lexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ HASH_IMAGE_LITERAL : '#imageLiteral';
GETTER : 'getter';
SETTER : 'setter';

UNDERSCORE : '_';

Identifier:
Identifier_head Identifier_characters?
| Implicit_parameter_name
Expand Down Expand Up @@ -249,7 +251,6 @@ COLON : ':';
SEMI : ';';
LT : '<';
GT : '>';
UNDERSCORE : '_';
BANG : '!';
QUESTION : '?';
AT : '@';
Expand Down Expand Up @@ -346,15 +347,15 @@ Single_line_string_open: '"' -> pushMode(SingleLine);

mode SingleLine;

Interpolataion_single_line: '\\(' { parenthesis.push(1);} -> pushMode(DEFAULT_MODE);
Interpolation_single_line: '\\(' { parenthesis.push(1);} -> pushMode(DEFAULT_MODE);

Single_line_string_close: '"' -> popMode;

Quoted_single_line_text: Quoted_text;

mode MultiLine;

Interpolataion_multi_line: '\\(' {parenthesis.push(1); } -> pushMode(DEFAULT_MODE);
Interpolation_multi_line: '\\(' {parenthesis.push(1); } -> pushMode(DEFAULT_MODE);

Multi_line_string_close: '"""' -> popMode;

Expand Down
7 changes: 4 additions & 3 deletions swift/swift5/Swift5Parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ closure_parameter_list
;

closure_parameter
: closure_parameter_name = identifier (type_annotation range_operator?)?
: UNDERSCORE? closure_parameter_name = identifier (type_annotation range_operator?)?
;

capture_list
Expand Down Expand Up @@ -1573,6 +1573,7 @@ identifier
)
| Identifier
| BACKTICK (keyword | Identifier | DOLLAR) BACKTICK
| UNDERSCORE
;

identifier_list
Expand Down Expand Up @@ -1810,10 +1811,10 @@ static_string_literal
interpolated_string_literal
: Single_line_string_open (
Quoted_single_line_text
| Interpolataion_single_line (expression | tuple_element COMMA tuple_element_list) RPAREN
| Interpolation_single_line (expression | tuple_element COMMA tuple_element_list) RPAREN
)* Single_line_string_close
| Multi_line_string_open (
Quoted_multi_line_text
| Interpolataion_multi_line (expression | tuple_element COMMA tuple_element_list) RPAREN
| Interpolation_multi_line (expression | tuple_element COMMA tuple_element_list) RPAREN
)* Multi_line_string_close
;
11 changes: 11 additions & 0 deletions swift/swift5/examples/Functions and Closures/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ print(mappedNumbers)
let sortedNumbers = numbers.sorted { $0 > $1 }
print(sortedNumbers)

// : Accepts trailing closure with 2 string parameters and call it
func printTwoStrings(_ closure: (String, String) -> Void) {
closure("Hello", "World")
}

//: Closure parameter name with an underscore before (closures don't have external parameter names, but underscore is allowed)
printTwoStrings { (_ x: String, _ y: String) in
print(x, y)
}




//: [Previous](@previous) | [Next](@next)
Loading