|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Antlr4.Runtime; |
| 5 | +using ServerCodeExcisionCommon; |
| 6 | + |
| 7 | +namespace ServerCodeExciser |
| 8 | +{ |
| 9 | + public class PreprocessorParser |
| 10 | + { |
| 11 | + public static List<PreprocessorScope> Parse(BufferedTokenStream tokenStream) |
| 12 | + { |
| 13 | + var directives = tokenStream |
| 14 | + .GetTokens() |
| 15 | + .Where(t => t.Channel == UnrealAngelscriptLexer.PREPROCESSOR_CHANNEL) |
| 16 | + .Where(t => t.Type == UnrealAngelscriptLexer.Directive) |
| 17 | + .OrderBy(t => t.StartIndex) |
| 18 | + .ToList(); |
| 19 | + |
| 20 | + var rootScopes = new List<PreprocessorScope>(); |
| 21 | + var ifStack = new Stack<PreprocessorScope>(); |
| 22 | + |
| 23 | + foreach (var token in directives) |
| 24 | + { |
| 25 | + switch (token.Text) |
| 26 | + { |
| 27 | + case var t when t.StartsWith("#if", StringComparison.Ordinal): // #if, #ifdef, #ifndef |
| 28 | + { |
| 29 | + var scope = CreateScope(token); |
| 30 | + if (ifStack.TryPeek(out var parent)) |
| 31 | + { |
| 32 | + parent.Children.Add(scope); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + rootScopes.Add(scope); |
| 37 | + } |
| 38 | + ifStack.Push(scope); |
| 39 | + } |
| 40 | + break; |
| 41 | + |
| 42 | + case var t when t.StartsWith("#elif", StringComparison.Ordinal) || t.StartsWith("#else", StringComparison.Ordinal): // #elif, #elifdef, #elifndef, #else |
| 43 | + { |
| 44 | + var scope = CreateScope(token); |
| 45 | + if (ifStack.TryPeek(out var parent)) |
| 46 | + { |
| 47 | + parent.Children.Add(scope); |
| 48 | + // adjust #if / #elif scope for closing bounds. |
| 49 | + parent.Span = new SourceSpan |
| 50 | + { |
| 51 | + Start = parent.Span.Start, |
| 52 | + StartIndex = parent.Span.StartIndex, |
| 53 | + End = new SourcePosition(token.Line, token.Column), |
| 54 | + EndIndex = token.StartIndex, |
| 55 | + }; |
| 56 | + } |
| 57 | + } |
| 58 | + break; |
| 59 | + |
| 60 | + case var t when t.StartsWith("#endif", StringComparison.Ordinal): |
| 61 | + { |
| 62 | + if (ifStack.TryPop(out var parent)) |
| 63 | + { |
| 64 | + // close parent (#if) to the range of the entire block. |
| 65 | + parent.Span = new SourceSpan |
| 66 | + { |
| 67 | + Start = parent.Span.Start, |
| 68 | + StartIndex = parent.Span.StartIndex, |
| 69 | + End = new SourcePosition(token.Line, token.Column), |
| 70 | + EndIndex = token.StopIndex, |
| 71 | + }; |
| 72 | + } |
| 73 | + } |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return rootScopes; |
| 79 | + } |
| 80 | + |
| 81 | + private static PreprocessorScope CreateScope(IToken token) |
| 82 | + { |
| 83 | + return new PreprocessorScope( |
| 84 | + token.Text, |
| 85 | + new SourceSpan( |
| 86 | + token.Line, |
| 87 | + token.Column, |
| 88 | + token.Line, |
| 89 | + token.Column, |
| 90 | + token.StartIndex, |
| 91 | + token.StopIndex |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments