13
13
import SwiftDiagnostics
14
14
import SwiftSyntax
15
15
16
- protocol ASTGenError : DiagnosticMessage { }
16
+ extension ASTGenVisitor {
17
+ /// Emits the given ASTGen diagnostic via the C++ diagnostic engine.
18
+ func diagnose( _ message: ASTGenDiagnostic , highlights: [ Syntax ] ? = nil , notes: [ Note ] = [ ] , fixIts: [ FixIt ] = [ ] ) {
19
+ self . diagnose ( Diagnostic (
20
+ node: message. node,
21
+ message: message,
22
+ highlights: highlights,
23
+ notes: notes,
24
+ fixIts: fixIts
25
+ ) )
26
+ }
17
27
18
- extension ASTGenError {
19
- var diagnosticID : MessageID {
20
- MessageID ( domain: " ASTGen " , id: " \( Self . self) " )
28
+ /// Emits the given diagnostic via the C++ diagnostic engine.
29
+ func diagnose( _ diagnostic: Diagnostic ) {
30
+ emitDiagnostic (
31
+ diagnosticEngine: self . diagnosticEngine,
32
+ sourceFileBuffer: self . base,
33
+ diagnostic: diagnostic,
34
+ diagnosticSeverity: diagnostic. diagMessage. severity
35
+ )
21
36
}
22
37
23
- var severity : DiagnosticSeverity { . error }
38
+ /// Emits the given diagnostics via the C++ diagnostic engine.
39
+ func diagnoseAll( _ diagnostics: [ Diagnostic ] ) {
40
+ diagnostics. forEach ( diagnose)
41
+ }
24
42
}
25
43
26
- /// An error emitted when a token is of an unexpected kind.
27
- struct UnexpectedTokenKindError : ASTGenError {
28
- let token : TokenSyntax
29
- private let parent : Syntax
44
+ struct ASTGenDiagnostic : DiagnosticMessage {
45
+ var node : Syntax
46
+ var message : String
47
+ var severity : DiagnosticSeverity
48
+ var messageID : String
30
49
31
- init ( token: TokenSyntax ) {
32
- guard let parent = token. parent else {
33
- preconditionFailure ( " Expected a child (not a root) token " )
34
- }
50
+ var diagnosticID : MessageID {
51
+ MessageID ( domain: " ASTGen " , id: messageID)
52
+ }
35
53
36
- self . token = token
37
- self . parent = parent
54
+ init ( node: some SyntaxProtocol , message: String , severity: DiagnosticSeverity = . error, messageID: String ) {
55
+ self . node = Syntax ( node)
56
+ self . message = message
57
+ self . severity = severity
58
+ self . messageID = messageID
38
59
}
39
60
40
- var message : String {
41
- return """
42
- unexpected token kind for token:
43
- \( self . token. debugDescription)
44
- in parent:
45
- \( self . parent. debugDescription ( indentString: " " ) )
46
- """
61
+ fileprivate init ( node: some SyntaxProtocol , message: String , severity: DiagnosticSeverity = . error, function: String = #function) {
62
+ // Extract messageID from the function name.
63
+ let messageID = String ( function. prefix ( while: { $0 != " ( " } ) )
64
+ self . init ( node: node, message: message, severity: severity, messageID: messageID)
47
65
}
48
66
}
49
67
50
- /// An error emitted when an optional child token is unexpectedly nil.
51
- struct MissingChildTokenError : ASTGenError {
52
- let parent : Syntax
53
- let kindOfTokenMissing : TokenKind
68
+ extension ASTGenDiagnostic {
69
+ /// An error emitted when a token is of an unexpected kind.
70
+ static func unexpectedTokenKind( token: TokenSyntax ) -> Self {
71
+ guard let parent = token. parent else {
72
+ preconditionFailure ( " Expected a child (not a root) token " )
73
+ }
54
74
55
- init ( parent: some SyntaxProtocol , kindOfTokenMissing: TokenKind ) {
56
- self . parent = Syntax ( parent)
57
- self . kindOfTokenMissing = kindOfTokenMissing
75
+ return Self (
76
+ node: token,
77
+ message: """
78
+ unexpected token kind for token:
79
+ \( token. debugDescription)
80
+ in parent:
81
+ \( parent. debugDescription ( indentString: " " ) )
82
+ """
83
+ )
58
84
}
59
85
60
- var message : String {
61
- """
62
- missing child token of kind ' \( self . kindOfTokenMissing) ' in:
63
- \( parent. debugDescription ( indentString: " " ) )
64
- """
86
+ /// An error emitted when an optional child token is unexpectedly nil.
87
+ static func missingChildToken( parent: some SyntaxProtocol , kindOfTokenMissing: TokenKind ) -> Self {
88
+ Self (
89
+ node: parent,
90
+ message: """
91
+ missing child token of kind ' \( kindOfTokenMissing) ' in:
92
+ \( parent. debugDescription ( indentString: " " ) )
93
+ """
94
+ )
65
95
}
66
- }
67
96
68
- /// An error emitted when a syntax collection entry is encountered that is considered a duplicate of a previous entry
69
- /// per the language grammar.
70
- struct DuplicateSyntaxError : ASTGenError {
71
- let duplicate : Syntax
72
- let original : Syntax
73
-
74
- init ( duplicate: some SyntaxProtocol , original: some SyntaxProtocol ) {
97
+ /// An error emitted when a syntax collection entry is encountered that is
98
+ /// considered a duplicate of a previous entry per the language grammar.
99
+ static func duplicateSyntax( duplicate: some SyntaxProtocol , original: some SyntaxProtocol ) -> Self {
75
100
precondition ( duplicate. kind == original. kind, " Expected duplicate and original to be of same kind " )
76
101
77
102
guard let duplicateParent = duplicate. parent, let originalParent = original. parent,
@@ -80,42 +105,28 @@ struct DuplicateSyntaxError: ASTGenError {
80
105
preconditionFailure ( " Expected a shared syntax collection parent " )
81
106
}
82
107
83
- self . duplicate = Syntax ( duplicate)
84
- self . original = Syntax ( original)
85
- }
86
-
87
- var message : String {
88
- """
89
- unexpected duplicate syntax in list:
90
- \( duplicate. debugDescription ( indentString: " " ) )
91
- previous syntax:
92
- \( original. debugDescription ( indentString: " " ) )
93
- """
94
- }
95
- }
96
-
97
- struct NonTrivialPatternForAccessorError : ASTGenError {
98
- var message : String {
99
- " getter/setter can only be defined for a single variable "
100
- }
101
- }
102
-
103
- struct UnknownAccessorSpecifierError : ASTGenError {
104
- var specifier : TokenSyntax
105
- init ( _ specifier: TokenSyntax ) {
106
- self . specifier = specifier
108
+ return Self (
109
+ node: duplicate,
110
+ message: """
111
+ unexpected duplicate syntax in list:
112
+ \( duplicate. debugDescription ( indentString: " " ) )
113
+ previous syntax:
114
+ \( original. debugDescription ( indentString: " " ) )
115
+ """
116
+ )
107
117
}
108
118
109
- var message : String {
110
- " unknown accessor specifier ' \( specifier. text) ' "
119
+ static func nonTrivialPatternForAccessor( _ pattern: some SyntaxProtocol ) -> Self {
120
+ Self (
121
+ node: pattern,
122
+ message: " getter/setter can only be defined for a single variable "
123
+ )
111
124
}
112
- }
113
125
114
- struct RegexParserError : ASTGenError {
115
- var message : String
116
- init ( _ message: String ) {
117
- self . message = message
126
+ static func unknownAccessorSpecifier( _ specifier: TokenSyntax ) -> Self {
127
+ Self (
128
+ node: specifier,
129
+ message: " unknown accessor specifier ' \( specifier. text) ' "
130
+ )
118
131
}
119
-
120
- var severity : DiagnosticSeverity { . error }
121
132
}
0 commit comments