Skip to content

Commit

Permalink
fix catch_pattern_list rule of swift5 parser (#4382)
Browse files Browse the repository at this point in the history
* add hashed labels to expression rule in JavaParser

* fix hashed labels names

* add EOF to compilationUnit rule

* fix trees in tests

* fix catch_pattern_list rule, add test

---------

Co-authored-by: asamatova <[email protected]>
  • Loading branch information
samatanna and asamatova authored Jan 17, 2025
1 parent dee7608 commit 761804c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion swift/swift5/Swift5Parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ catch_clause
;

catch_pattern_list
: catch_pattern (catch_pattern COMMA catch_pattern)*
: catch_pattern (COMMA catch_pattern)*
;

catch_pattern
Expand Down
24 changes: 24 additions & 0 deletions swift/swift5/examples/Error Handling/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ enum PrinterError: Error {
case onFire
}

// One more error
enum JustError: Error {
case someError
}

//: Use `throw` to throw an error and `throws` to mark a function that can throw an error. If you throw an error in a function, the function returns immediately and the code that called the function handles the error.
//:
func send(job: Int, toPrinter printerName: String) throws -> String {
if printerName == "Never Has Toner" {
throw PrinterError.noToner
}
if printerName == "Throw error" {
throw JustError.someError
}
return "Job sent"
}

Expand Down Expand Up @@ -42,6 +50,22 @@ do {
print(error)
}

//: - Experiment:
//: Change the printer name to `"Throw error"`, so that the `send(job:toPrinter:)` function throws an error.
//:
//: You can catch multiple error cases inside a single catch block
//:
do {
let printerResponse = try send(job: 1440, toPrinter: "Gutenberg")
print(printerResponse)
} catch is JustError, PrinterError.noToner {
print("Error.")
} catch let printerError as PrinterError {
print("Printer error: \(printerError).")
} catch {
print(error)
}

//: - Experiment:
//: Add code to throw an error inside the `do` block. What kind of error do you need to throw so that the error is handled by the first `catch` block? What about the second and third blocks?
//:
Expand Down

0 comments on commit 761804c

Please sign in to comment.