Skip to content

Commit

Permalink
fix '?' token ambiguity, add STRUCT to identifier rule
Browse files Browse the repository at this point in the history
  • Loading branch information
asamatova committed Jan 31, 2025
1 parent 464f891 commit aca468c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions swift/swift5/Cpp/SwiftSupport.cpp
Original file line number Diff line number Diff line change
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
4 changes: 4 additions & 0 deletions swift/swift5/Java/SwiftSupport.java
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion swift/swift5/Swift5Parser.g4
Original file line number Diff line number Diff line change
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 Down
10 changes: 10 additions & 0 deletions swift/swift5/examples/Functions and Closures/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ 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)

0 comments on commit aca468c

Please sign in to comment.