@@ -17,6 +17,7 @@ public class ServerCodeExcisionParameters
1717 public bool ShouldOutputUntouchedFiles { get ; set ; } = false ;
1818 public bool IsDryRun { get ; set ; } = false ;
1919 public bool Verify { get ; set ; } = false ;
20+ public bool StrictMode { get ; set ; } = false ;
2021 public bool UseFunctionStats { get ; set ; } = false ;
2122 public bool DontSkip { get ; set ; } = false ;
2223 public float RequiredExcisionRatio { get ; set ; } = - 1.0f ;
@@ -109,8 +110,7 @@ public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExci
109110 {
110111 System . Diagnostics . Debug . Assert ( stats . TotalNrCharacters > 0 , "Something is terribly wrong. We have excised characters, but no total characters..?" ) ;
111112 var excisionRatio = ( float ) stats . CharactersExcised / ( float ) stats . TotalNrCharacters * 100.0f ;
112- Console . WriteLine ( "Excised {0:0.00}% of server only code in file ({1}/{2}): {3}" ,
113- excisionRatio , fileIdx + 1 , allFiles . Length , fileName ) ;
113+ Console . WriteLine ( "Excised {0:0.00}% of server only code in file ({1}/{2}): {3}" , excisionRatio , fileIdx + 1 , allFiles . Length , fileName ) ;
114114 }
115115 else
116116 {
@@ -120,9 +120,10 @@ public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExci
120120 globalStats . CharactersExcised += stats . CharactersExcised ;
121121 globalStats . TotalNrCharacters += stats . TotalNrCharacters ;
122122 }
123- catch ( Exception e )
123+ catch ( Exception )
124124 {
125125 Console . WriteLine ( "Failed to parse ({0}/{1}): {2}" , fileIdx + 1 , allFiles . Length , fileName ) ;
126+ throw ;
126127 }
127128 }
128129 }
@@ -183,10 +184,17 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
183184 // Setup parsing and output.
184185 List < KeyValuePair < int , string > > serverCodeInjections = new List < KeyValuePair < int , string > > ( ) ;
185186 var inputStream = new AntlrInputStream ( script ) ;
186- var lexer = excisionLanguage . CreateLexer ( inputStream ) ;
187+ var lexer = excisionLanguage . CreateLexer < UnrealAngelscriptLexer > ( inputStream ) ;
187188 lexer . AddErrorListener ( new ExcisionLexerErrorListener ( ) ) ;
188189 var commonTokenStream = new CommonTokenStream ( lexer ) ;
189- var parser = excisionLanguage . CreateParser ( commonTokenStream ) ;
190+ var parser = excisionLanguage . CreateParser < UnrealAngelscriptParser > ( commonTokenStream ) ;
191+ parser . AddErrorListener ( new ExcisionParserErrorListener ( ) ) ;
192+
193+ if ( _parameters . StrictMode )
194+ {
195+ parser . ErrorHandler = new BailErrorStrategy ( ) ;
196+ }
197+
190198 var answerText = new StringBuilder ( ) ;
191199 answerText . Append ( script ) ;
192200
@@ -195,7 +203,7 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
195203 {
196204 // We want to excise this entire file.
197205 serverCodeInjections . Add ( new KeyValuePair < int , string > ( 0 , excisionLanguage . ServerScopeStartString + "\r \n " ) ) ;
198- serverCodeInjections . Add ( new KeyValuePair < int , string > ( script . Length , excisionLanguage . ServerScopeEndString ) ) ;
206+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( script . Length , excisionLanguage . ServerScopeEndString + " \r \n " ) ) ;
199207 stats . CharactersExcised += script . Length ;
200208 }
201209 else if ( excisionMode == EExcisionMode . AllFunctions )
@@ -221,7 +229,8 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
221229 // Gather all the injections we want to make
222230 if ( visitor != null )
223231 {
224- visitor . VisitContext ( parser . GetParseTree ( ) ) ;
232+ visitor . VisitContext ( parser . script ( ) ) ;
233+
225234 if ( _parameters . UseFunctionStats )
226235 {
227236 stats . TotalNrCharacters = visitor . TotalNumberOfFunctionCharactersVisited ;
@@ -232,17 +241,17 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
232241 {
233242 if ( currentScope . StartIndex == - 1
234243 || currentScope . StopIndex == - 1
235- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , true , excisionLanguage . ServerScopeStartString )
236- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , false , excisionLanguage . ServerScopeStartString )
237- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StopIndex , false , excisionLanguage . ServerScopeEndString ) )
244+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , true , true , excisionLanguage . ServerScopeStartString )
245+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , false , false , excisionLanguage . ServerScopeStartString )
246+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StopIndex , false , false , excisionLanguage . ServerScopeEndString ) )
238247 {
239248 continue ;
240249 }
241250
242251 // If there are already injected macros where we want to go, we should skip injecting.
243252 System . Diagnostics . Debug . Assert ( currentScope . StopIndex > currentScope . StartIndex , "There must be some invalid pattern here! Stop is before start!" ) ;
244- serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StartIndex , excisionLanguage . ServerScopeStartString ) ) ;
245- serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StopIndex , currentScope . Opt_ElseContent + excisionLanguage . ServerScopeEndString ) ) ;
253+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StartIndex , " \r \n " + excisionLanguage . ServerScopeStartString ) ) ;
254+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StopIndex , currentScope . Opt_ElseContent + excisionLanguage . ServerScopeEndString + " \r \n " ) ) ;
246255 stats . CharactersExcised += currentScope . StopIndex - currentScope . StartIndex ;
247256 }
248257
@@ -257,10 +266,10 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
257266 dummyRefDataBlockString . Append ( "\r \n \t " + dummyVarDef ) ;
258267 }
259268
260- dummyRefDataBlockString . Append ( "\r \n " + excisionLanguage . ServerScopeEndString + "\r \n " ) ;
269+ dummyRefDataBlockString . Append ( "\r \n " + excisionLanguage . ServerScopeEndString + "\r \n \r \n " ) ;
261270
262271 // If there is already a block of dummy reference variables we skip adding new ones, there is no guarantee we are adding the right code.
263- if ( InjectedMacroAlreadyExistsAtLocation ( answerText , dummyRefDataPair . Key , false , dummyVarScope + "\r \n " ) )
272+ if ( InjectedMacroAlreadyExistsAtLocation ( answerText , dummyRefDataPair . Key , false , true , dummyVarScope + "\r \n " ) )
264273 {
265274 continue ;
266275 }
@@ -314,19 +323,47 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
314323 return stats ;
315324 }
316325
317- private bool InjectedMacroAlreadyExistsAtLocation ( StringBuilder script , int index , bool lookAhead , string macro )
326+ private static bool IsWhitespace ( char c )
318327 {
319- int startIndex = lookAhead ? index : ( index - macro . Length ) ;
320- int endIndex = lookAhead ? ( index + macro . Length ) : index ;
328+ return c == ' ' || c == ' \t ' || c == ' \r ' || c == ' \n ' ;
329+ }
321330
322- if ( startIndex < 0 || startIndex >= script . Length
323- || endIndex < 0 || endIndex >= script . Length )
331+ private bool InjectedMacroAlreadyExistsAtLocation ( StringBuilder script , int index , bool lookAhead , bool ignoreWhitespace , string macro )
332+ {
333+ if ( lookAhead )
324334 {
325- return false ;
335+ if ( ignoreWhitespace )
336+ {
337+ while ( index < script . Length && IsWhitespace ( script [ index ] ) )
338+ {
339+ index ++ ;
340+ }
341+ }
342+
343+ if ( script . Length - index < macro . Length )
344+ {
345+ return false ;
346+ }
347+
348+ return script . ToString ( index , macro . Length ) . Equals ( macro ) ;
326349 }
350+ else
351+ {
352+ if ( ignoreWhitespace )
353+ {
354+ while ( index > 0 && IsWhitespace ( script [ index ] ) )
355+ {
356+ index -- ;
357+ }
358+ }
359+
360+ if ( index - macro . Length < 0 )
361+ {
362+ return false ;
363+ }
327364
328- string scriptSection = script . ToString ( startIndex , macro . Length ) ;
329- return scriptSection == macro ;
365+ return script . ToString ( index - macro . Length , macro . Length ) . Equals ( macro ) ;
366+ }
330367 }
331368 }
332369}
0 commit comments