Skip to content

Commit 98d2074

Browse files
committed
Migrate to PreprocessorParser.
1 parent c969822 commit 98d2074

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

ServerCodeExciser/ServerCodeExcisionProcessor.cs

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,12 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
241241
}
242242

243243
// Determine if there are any existing preprocessor server-code exclusions in the source file.
244-
var detectedPreprocessorServerOnlyScopes = FindPreprocessorGuards(commonTokenStream)
245-
.Where(x => x.Directive.Contains(excisionLanguage.ServerScopeStartString, StringComparison.Ordinal));
244+
var preprocessorScopes = PreprocessorParser.Parse(commonTokenStream);
245+
var detectedPreprocessorServerOnlyScopes = new List<PreprocessorScope>();
246+
FindPreprocessorScopesForSymbolRecursive(
247+
preprocessorScopes,
248+
scope => scope.Directive.Contains(excisionLanguage.ServerScopeStartString, StringComparison.Ordinal),
249+
detectedPreprocessorServerOnlyScopes);
246250

247251
// Process scopes we've evaluated must be server only.
248252
foreach (ServerOnlyScopeData currentScope in visitor.DetectedServerOnlyScopes)
@@ -253,8 +257,8 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
253257
}
254258

255259
// Skip if there's already a server-code exclusion for the scope. (We don't want have duplicate guards.)
256-
var (StartIndex, StopIndex) = TrimWhitespace(script, currentScope);
257-
if (detectedPreprocessorServerOnlyScopes.Any(x => StartIndex >= x.StartIndex && StopIndex <= x.StopIndex))
260+
var (StartIndex, EndIndex) = TrimWhitespace(script, currentScope);
261+
if (detectedPreprocessorServerOnlyScopes.Any(x => StartIndex >= x.Span.StartIndex && EndIndex <= x.Span.EndIndex))
258262
{
259263
continue; // We're inside an existing scope.
260264
}
@@ -358,57 +362,16 @@ private static (int StartIndex, int StopIndex) TrimWhitespace(string script, Ser
358362
return (startIndex, stopIndex);
359363
}
360364

361-
private static List<(string Directive, int StartIndex, int StopIndex)> FindPreprocessorGuards(BufferedTokenStream tokenStream)
365+
private static void FindPreprocessorScopesForSymbolRecursive(List<PreprocessorScope> scopes, Predicate<PreprocessorScope> predicate, List<PreprocessorScope> result)
362366
{
363-
var preprocessorDirectives = tokenStream
364-
.GetTokens()
365-
.Where(t => t.Channel == UnrealAngelscriptLexer.PREPROCESSOR_CHANNEL)
366-
.Where(t => t.Type == UnrealAngelscriptLexer.Directive)
367-
.ToList();
368-
369-
var preprocessorGuards = new List<(string Directive, int StartIndex, int StopIndex)>();
370-
var ifStack = new Stack<IToken>();
371-
372-
foreach (var token in preprocessorDirectives)
367+
foreach (var scope in scopes)
373368
{
374-
switch (token.Text)
369+
if (predicate(scope))
375370
{
376-
case var t when t.StartsWith("#if", StringComparison.Ordinal): // #if, #ifdef, #ifndef
377-
ifStack.Push(token);
378-
break;
379-
380-
case var t when t.StartsWith("#elif", StringComparison.Ordinal): // #elif, #elifdef, #elifndef
381-
{
382-
if (ifStack.TryPop(out var removed))
383-
{
384-
preprocessorGuards.Add((removed.Text, removed.StartIndex, token.StopIndex));
385-
}
386-
ifStack.Push(token);
387-
}
388-
break;
389-
390-
case var t when t.StartsWith("#else", StringComparison.Ordinal):
391-
{
392-
if (ifStack.TryPop(out var removed))
393-
{
394-
preprocessorGuards.Add((removed.Text, removed.StartIndex, token.StopIndex));
395-
}
396-
ifStack.Push(token);
397-
}
398-
break;
399-
400-
case var t when t.StartsWith("#endif", StringComparison.Ordinal):
401-
{
402-
if (ifStack.TryPop(out var removed))
403-
{
404-
preprocessorGuards.Add((removed.Text, removed.StartIndex, token.StopIndex));
405-
}
406-
}
407-
break;
371+
result.Add(scope);
408372
}
373+
FindPreprocessorScopesForSymbolRecursive(scope.Children, predicate, result);
409374
}
410-
411-
return preprocessorGuards;
412375
}
413376

414377
private bool InjectedMacroAlreadyExistsAtLocation(StringBuilder script, int index, bool lookAhead, bool ignoreWhitespace, string macro)

0 commit comments

Comments
 (0)