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] grammar fixes #4388

Merged
merged 12 commits into from
Feb 4, 2025
8 changes: 6 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 Expand Up @@ -248,6 +248,10 @@ bool SwiftSupport::isBinaryOp(antlr4::TokenStream* tokens) {
bool nextIsWS = isRightOperatorWS(nextToken);
//String text = tokens.getText(Interval.of(start, stop));
//System.out.println("isBinaryOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
// accept only '??'' as binary operator
if (currentToken->getType() == Swift5Lexer::QUESTION && start == stop) {
return false;
}
if (prevIsWS) {
return nextIsWS;
} else {
Expand Down
8 changes: 6 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 Expand Up @@ -252,6 +252,10 @@ public static boolean isBinaryOp(TokenStream tokens) {
boolean nextIsWS = isRightOperatorWS(nextToken);
//String text = tokens.getText(Interval.of(start, stop));
//System.out.println("isBinaryOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
// accept only '??'' as binary operator
if (currentToken.getType() == Swift5Lexer.QUESTION && start == stop) {
return false;
}
if (prevIsWS) {
return nextIsWS;
} else {
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
10 changes: 6 additions & 4 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 @@ -1326,7 +1326,7 @@ forced_value_suffix
;

optional_chaining_suffix
: {!this.isBinaryOp(_input)}? QUESTION
: {!this.isBinaryOp(_input) && _input.get(_input.index()-1).getType()!=WS}? QUESTION
;

// Function Call Expression
Expand Down Expand Up @@ -1565,6 +1565,7 @@ identifier
| SELF_BIG
| SET
| CLASS
| STRUCT
| GETTER
| SETTER
| OPERATOR
Expand All @@ -1573,6 +1574,7 @@ identifier
)
| Identifier
| BACKTICK (keyword | Identifier | DOLLAR) BACKTICK
| UNDERSCORE
;

identifier_list
Expand Down Expand Up @@ -1810,10 +1812,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
;
21 changes: 21 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,27 @@ 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)
}

// : Accepts 'struct' as external parameter name
public func create<T>(struct s: T) {
print(s)
}

create(struct: 1)

// : Return expression with two QUESTION tokens
public func GetString<T>(_ s: T, _ val: Int) -> String? {
return val == 0 ? s as? String : nil
}


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