Skip to content

Commit ba42742

Browse files
committed
process pre-processor tokens differently.
1 parent b481e06 commit ba42742

File tree

9 files changed

+585
-453
lines changed

9 files changed

+585
-453
lines changed
Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
1+
using SharpCompress.Common;
12
using System;
23
using System.Collections.Generic;
34

45
namespace ServerCodeExciser
56
{
7+
public class Marker
8+
{
9+
public bool Start { get; }
10+
public string OptElse { get; }
11+
public string Context { get; }
12+
13+
public Marker(string context, bool start, string optElse = "")
14+
{
15+
Start = start;
16+
OptElse = optElse;
17+
Context = context;
18+
}
19+
20+
public void Write(ScriptBuilder builder)
21+
{
22+
if (Start)
23+
{
24+
builder.AddLine($"#ifdef WITH_SERVER // {Context}");
25+
}
26+
else
27+
{
28+
if (builder.IsInScope("WITH_SERVER"))
29+
{
30+
var values = OptElse.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
31+
foreach (var v in values)
32+
{
33+
builder.AddLine(v);
34+
}
35+
}
36+
builder.AddLine($"#endif // WITH_SERVER {Context}");
37+
}
38+
}
39+
}
40+
641
public class InjectionTable
742
{
8-
private readonly Dictionary<int, List<string>> m_table = new Dictionary<int, List<string>>();
43+
private readonly Dictionary<int, List<Marker>> m_table = new Dictionary<int, List<Marker>>();
944

10-
public void Add(int line, string value)
45+
public void Add(int line, Marker value)
1146
{
1247
if (m_table.TryGetValue(line, out var list))
1348
{
1449
list.Add(value);
1550
}
1651
else
1752
{
18-
m_table.Add(line, new List<string> { value });
53+
m_table.Add(line, new List<Marker> { value });
1954
}
2055
}
2156

22-
public IEnumerable<string> Get(int line)
57+
public IEnumerable<Marker> Get(int line)
2358
{
2459
if (m_table.TryGetValue(line, out var list))
2560
{
2661
return list;
2762
}
28-
return Array.Empty<string>();
63+
return Array.Empty<Marker>();
2964
}
3065
}
3166
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Misc;
3+
using Microsoft.Build.Framework;
4+
using System;
5+
using static System.Formats.Asn1.AsnWriter;
6+
7+
namespace ServerCodeExciser
8+
{
9+
public class Preprocessor : ITokenSource
10+
{
11+
private readonly ITokenSource m_tokenSource;
12+
private readonly ScopeStack m_scope = new ScopeStack();
13+
14+
public Preprocessor(ITokenSource tokenSource)
15+
{
16+
m_tokenSource = tokenSource;
17+
}
18+
19+
public int Line
20+
{
21+
get { return m_tokenSource.Line; }
22+
}
23+
24+
public int Column
25+
{
26+
get { return m_tokenSource.Column; }
27+
}
28+
29+
public ICharStream InputStream
30+
{
31+
get { return m_tokenSource.InputStream; }
32+
}
33+
34+
public string SourceName
35+
{
36+
get { return m_tokenSource.SourceName; }
37+
}
38+
39+
public ITokenFactory TokenFactory
40+
{
41+
get { return m_tokenSource.TokenFactory; }
42+
set { m_tokenSource.TokenFactory = value; }
43+
}
44+
45+
public IToken NextToken()
46+
{
47+
var token = m_tokenSource.NextToken();
48+
while (token.Type == UnrealAngelscriptLexer.Preprocessor || m_scope.IsInScope("WITH_SERVER"))
49+
{
50+
if (token.Type == UnrealAngelscriptLexer.Preprocessor)
51+
{
52+
Process(token.Text);
53+
}
54+
else
55+
{
56+
//Console.WriteLine($"skipping: " + token.Text);
57+
}
58+
token = m_tokenSource.NextToken();
59+
}
60+
return token;
61+
}
62+
63+
private void Process(string line)
64+
{
65+
if (line.StartsWith("#ifdef "))
66+
{
67+
m_scope.Push(line);
68+
}
69+
else if (line.StartsWith("#if "))
70+
{
71+
m_scope.Push(line);
72+
}
73+
else if (line.StartsWith("#ifndef "))
74+
{
75+
m_scope.Push(line);
76+
}
77+
else if (line.StartsWith("#else"))
78+
{
79+
m_scope.Else(out var name);
80+
}
81+
else if (line.StartsWith("#endif"))
82+
{
83+
m_scope.Pop(out var name);
84+
}
85+
}
86+
}
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"ServerCodeExciser": {
4+
"commandName": "Project",
5+
"commandLineArgs": "D:\\p4\\task\\Games\\Shared\\Plugins\\AIScript\\Script -o D:\\out"
6+
}
7+
}
8+
}

ServerCodeExciser/ScopeStack.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class ScopeStack
77
{
88
private Dictionary<string, int> m_scopes = new Dictionary<string, int>();
99
private Stack<string> m_scope = new Stack<string>();
10+
private int m_else = 0;
1011

1112
public bool Push(string name)
1213
{
@@ -37,14 +38,21 @@ public bool Push(string name)
3738
}
3839
}
3940

41+
public void Else(out string name)
42+
{
43+
name = m_scope.Pop();
44+
m_scopes[name] -= 1;
45+
var o = (name[0] == '!') ? name.Substring(1) : ("!" + name);
46+
Push(o);
47+
}
48+
4049
public bool Pop(out string name)
4150
{
4251
if (m_scope.Count <= 0)
4352
{
4453
name = string.Empty;
4554
return false;
4655
}
47-
4856
name = m_scope.Pop();
4957
m_scopes[name] -= 1;
5058
return m_scopes[name] == 0;

ServerCodeExciser/ScriptBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public void AddLine(string line)
3636
{
3737
if (m_scope.Pop(out var name))
3838
{
39-
name = name.Trim('!');
40-
m_scope.Push(name);
4139
m_text.AppendLine($"#else // {name}");
40+
name = (name[0] == '!') ? name.Substring(1) : ("!" + name);
41+
m_scope.Push(name);
4242
}
4343
else
4444
{

ServerCodeExciser/ServerCodeExcisionProcessor.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
191191
var inputStream = new AntlrInputStream(script);
192192
var lexer = excisionLanguage.CreateLexer(inputStream);
193193
lexer.AddErrorListener(new ExcisionLexerErrorListener());
194-
var commonTokenStream = new CommonTokenStream(lexer);
194+
var commonTokenStream = new CommonTokenStream(new Preprocessor(lexer));
195195
var parser = excisionLanguage.CreateParser(commonTokenStream);
196196

197197
IServerCodeVisitor? visitor = null;
198198
if (excisionMode == EExcisionMode.Full)
199199
{
200-
injections.Add(0, excisionLanguage.ServerScopeStartString);
201-
injections.Add(script.Length, excisionLanguage.ServerScopeEndString);
200+
injections.Add(0, new Marker("A", true));
201+
injections.Add(script.Length, new Marker("A", false));
202202
}
203203
else if (excisionMode == EExcisionMode.AllFunctions)
204204
{
@@ -232,16 +232,8 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
232232
// First process all server only scopes.
233233
foreach (ServerOnlyScopeData currentScope in visitor.DetectedServerOnlyScopes)
234234
{
235-
if (string.IsNullOrEmpty(currentScope.Context))
236-
{
237-
injections.Add(currentScope.StartLine, excisionLanguage.ServerScopeStartString);
238-
injections.Add(currentScope.StopLine, excisionLanguage.ServerScopeEndString);
239-
}
240-
else
241-
{
242-
injections.Add(currentScope.StartLine, excisionLanguage.ServerScopeStartString + $" // {currentScope.Context}");
243-
injections.Add(currentScope.StopLine, excisionLanguage.ServerScopeEndString + $" // {currentScope.Context}");
244-
}
235+
injections.Add(currentScope.StartLine, new Marker(currentScope.Context, true));
236+
injections.Add(currentScope.StopLine, new Marker(currentScope.Context, false, currentScope.Opt_ElseContent));
245237
}
246238
}
247239

@@ -258,7 +250,7 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
258250

259251
foreach (var text in injections.Get(lineIndex))
260252
{
261-
builder.AddLine(text);
253+
text.Write(builder);
262254
}
263255

264256
if (line.Contains("UEmbarkServerEventsSubsystem::Get()") && !builder.IsInScope("WITH_SERVER"))
@@ -308,3 +300,5 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
308300
}
309301
}
310302
}
303+
304+

UnrealAngelscriptParser/Grammar/UnrealAngelscriptLexer.g4

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,4 @@ Whitespace: [ \t]+ -> skip;
357357
Newline: ('\r' '\n'? | '\n') -> skip;
358358
BlockComment: '/*' .*? '*/' -> skip;
359359
LineComment: '//' ~ [\r\n]* -> skip;
360-
PreprocessorBranchRemoval: '#else' .*? '#endif' -> skip;
361-
Preprocessor: ('#if' | '#ifdef' | '#else' | '#endif') ~ [\r\n]* -> skip;
360+
Preprocessor: ('#if' | '#ifdef' | '#else' | '#endif') ~ [\r\n]*;

UnrealAngelscriptServerCodeExcision/UnrealAngelscriptSimpleVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,11 @@ protected void DecorateFunctionBody(UnrealAngelscriptParser.FunctionBodyContext
282282
return;
283283
}
284284

285+
285286
// If there is a return statement at the end, we must replace it with a suitable replacement, or code will stop compiling.
286287
var returnData = GetDefaultReturnStatementForScope(context);
287288

288-
ServerOnlyScopeData newData = new ServerOnlyScopeData("", context.Start.Line + 1, context.Stop.Line);
289+
ServerOnlyScopeData newData = new ServerOnlyScopeData("F", context.Start.Line + 1, context.Stop.Line);
289290

290291
if (returnData.ReturnType != EReturnType.NoReturn)
291292
{

0 commit comments

Comments
 (0)