@@ -69,23 +69,30 @@ public LocalizedMessage (int code, string value)
69
69
public static LocalizedMessage WarningBaseInterfaceNotFound => new LocalizedMessage ( 0x8C00 , Localization . Resources . Generator_BG8C00 ) ;
70
70
public static LocalizedMessage WarningBaseInterfaceInvalid => new LocalizedMessage ( 0x8C01 , Localization . Resources . Generator_BG8C01 ) ;
71
71
72
- public static void LogCodedError ( LocalizedMessage message , params string [ ] args )
73
- => LogCodedError ( message , null , null , - 1 , - 1 , args ) ;
72
+ public static void LogCodedErrorAndExit ( LocalizedMessage message , params string [ ] args )
73
+ => LogCodedErrorAndExit ( message , null , null , args ) ;
74
74
75
- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , params string [ ] args )
76
- => LogCodedError ( message , innerException , null , - 1 , - 1 , args ) ;
75
+ public static void LogCodedErrorAndExit ( LocalizedMessage message , Exception ? innerException , params string [ ] args )
76
+ => LogCodedErrorAndExit ( message , innerException , null , args ) ;
77
77
78
- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , XNode node , params string ? [ ] args )
78
+ public static void LogCodedErrorAndExit ( LocalizedMessage message , Exception ? innerException , XNode ? node , params string ? [ ] args )
79
79
{
80
- var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
81
- var line_info = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
80
+ LogCodedError ( message , node , args ) ;
82
81
83
- LogCodedError ( message , innerException , file , line_info ? . LineNumber ?? - 1 , line_info ? . LinePosition ?? - 1 , args ) ;
82
+ // Throwing a BindingGeneratorException will cause generator to terminate
83
+ throw new BindingGeneratorException ( message . Code , string . Format ( message . Value , args ) , innerException ) ;
84
84
}
85
85
86
- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , string ? sourceFile , int line , int column , params string ? [ ] args )
86
+ public static void LogCodedError ( LocalizedMessage message , XNode ? node , params string ? [ ] args )
87
87
{
88
- throw new BindingGeneratorException ( message . Code , sourceFile , line , column , string . Format ( message . Value , args ) , innerException ) ;
88
+ var ( file , line , col ) = GetLineInfo ( node ) ;
89
+
90
+ LogCodedError ( message , file , line , col , args ) ;
91
+ }
92
+
93
+ public static void LogCodedError ( LocalizedMessage message , string ? sourceFile , int line , int column , params string ? [ ] args )
94
+ {
95
+ Console . Error . WriteLine ( Format ( true , message . Code , sourceFile , line , column , message . Value , args ) ) ;
89
96
}
90
97
91
98
public static void LogCodedWarning ( int verbosity , LocalizedMessage message , params string ? [ ] args )
@@ -99,10 +106,9 @@ public static void LogCodedWarning (int verbosity, LocalizedMessage message, Exc
99
106
100
107
public static void LogCodedWarning ( int verbosity , LocalizedMessage message , Exception ? innerException , XNode node , params string ? [ ] args )
101
108
{
102
- var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
103
- var line_info = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
109
+ var ( file , line , col ) = GetLineInfo ( node ) ;
104
110
105
- LogCodedWarning ( verbosity , message , innerException , file , line_info ? . LineNumber ?? - 1 , line_info ? . LinePosition ?? - 1 , args ) ;
111
+ LogCodedWarning ( verbosity , message , innerException , file , line , col , args ) ;
106
112
}
107
113
108
114
public static void LogCodedWarning ( int verbosity , LocalizedMessage message , Exception ? innerException , string ? sourceFile , int line , int column , params string ? [ ] args )
@@ -145,12 +151,26 @@ public static string Format (bool error, int errorCode, string? sourceFile, int
145
151
return ret + ": " ;
146
152
147
153
if ( column > 0 )
148
- return ret + $ "({ line } , { column } ): ";
154
+ return ret + $ "({ line } ,{ column } ): ";
149
155
150
156
return ret + $ "({ line } ): ";
151
157
}
158
+
159
+ static ( string ? file , int line , int col ) GetLineInfo ( XNode ? node )
160
+ {
161
+ if ( node is null )
162
+ return ( null , - 1 , - 1 ) ;
163
+
164
+ var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
165
+ var pos = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
166
+
167
+ return ( file , pos ? . LineNumber ?? - 1 , pos ? . LinePosition ?? - 1 ) ;
168
+ }
152
169
}
153
-
170
+
171
+ /// <summary>
172
+ /// Throwing this exception will cause generator to exit gracefully.
173
+ /// </summary>
154
174
public class BindingGeneratorException : Exception
155
175
{
156
176
public BindingGeneratorException ( int errorCode , string message )
0 commit comments