-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExprProtocol.swift
More file actions
31 lines (28 loc) · 1.06 KB
/
ExprProtocol.swift
File metadata and controls
31 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import SwiftSyntax
/// A protocol for expression syntax wrappers to conform to. Allows simple conversion between expression wrappers.
public protocol ExprProtocol {
/// The type of the underlying syntax node being wrapped.
associatedtype WrappedSyntax: ExprSyntaxProtocol
/// The underlying syntax node being wrapped.
var _syntax: WrappedSyntax { get }
/// Wraps a syntax node.
init(_ syntax: WrappedSyntax)
}
extension ExprProtocol {
/// Attempts to initialize the wrapper from an arbitrary expression (succeeds
/// if the expression is the right type of syntax).
public init?(_ syntax: ExprSyntaxProtocol) {
guard let syntax = syntax.as(WrappedSyntax.self) else {
return nil
}
self.init(syntax)
}
/// Attempts to initialize the wrapper from an arbitrary expression (succeeds
/// if the expression is the right type of syntax).
public init?(_ expr: Expr) {
guard let syntax = expr._syntax.as(WrappedSyntax.self) else {
return nil
}
self.init(syntax)
}
}