Skip to content

Commit 2ecd293

Browse files
committed
Initial transition to SourceSpan.
1 parent 4224fbb commit 2ecd293

File tree

5 files changed

+206
-27
lines changed

5 files changed

+206
-27
lines changed

ServerCodeExciser/ServerCodeExcisionProcessor.cs

Lines changed: 121 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,22 +243,135 @@ private EExciserReturnValues ProcessCodeFile(string fileName, string inputPath,
243243
// Process scopes we've evaluated must be server only.
244244
foreach (ServerOnlyScopeData currentScope in visitor.DetectedServerOnlyScopes)
245245
{
246-
if (currentScope.StartIndex == -1 || currentScope.StopIndex == -1)
246+
if (currentScope.Span.StartIndex == -1 || currentScope.Span.EndIndex == -1)
247247
{
248248
continue;
249249
}
250250

251251
// Skip if there's already a server-code exclusion for the scope. (We don't want have duplicate guards.)
252-
var (StartIndex, StopIndex) = TrimWhitespace(script, currentScope);
252+
var (StartIndex, StopIndex) = TrimWhitespace(script, currentScope.Span);
253253
if (detectedPreprocessorServerOnlyScopes.Any(x => StartIndex >= x.StartIndex && StopIndex <= x.StopIndex))
254254
{
255255
continue; // We're inside an existing scope.
256256
}
257257

258-
System.Diagnostics.Debug.Assert(currentScope.StopIndex > currentScope.StartIndex, "There must be some invalid pattern here! Stop is before start!");
259-
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StartIndex, "\r\n" + excisionLanguage.ServerScopeStartString));
260-
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StopIndex, currentScope.Opt_ElseContent + excisionLanguage.ServerScopeEndString + "\r\n"));
261-
stats.CharactersExcised += currentScope.StopIndex - currentScope.StartIndex;
258+
int ScanToStartOfLine(int index)
259+
{
260+
while (index > 0)
261+
{
262+
if (script[index - 1] == '\n')
263+
{
264+
return index;
265+
}
266+
index--;
267+
}
268+
return index;
269+
}
270+
271+
int TrimWhitespace2(int index)
272+
{
273+
while (index > 0)
274+
{
275+
switch (script[index - 1])
276+
{
277+
case ' ':
278+
case '\t':
279+
break;
280+
default:
281+
return index;
282+
}
283+
284+
index--;
285+
}
286+
return index;
287+
}
288+
289+
int ScanToNextLineBreak(int index)
290+
{
291+
while (index < script.Length)
292+
{
293+
if (script[index] == '\n')
294+
{
295+
return index + 1;
296+
}
297+
index++;
298+
}
299+
return index;
300+
}
301+
302+
const bool markers = false;
303+
304+
if (markers)
305+
{
306+
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.Span.StartIndex + 1, "<<!!>>"));
307+
}
308+
309+
/*var startIndex = script[currentScope.Span.StartIndex] switch
310+
{
311+
'{' => ScanToNextLineBreak(currentScope.Span.StartIndex + 1),
312+
';' => ScanToNextLineBreak(currentScope.Span.StartIndex + 1),
313+
')' => ScanToNextLineBreak(currentScope.Span.StartIndex + 1),
314+
_ => currentScope.Span.StartIndex,
315+
};*/
316+
317+
int startIndex = currentScope.Span.StartIndex + 1;
318+
319+
//var startIndex = ScanToNextLineBreak(currentScope.Span.StartIndex + 1);
320+
var startText = $"{excisionLanguage.ServerScopeStartString}\r\n";
321+
if (startText.StartsWith("#"))
322+
{
323+
startIndex = ScanToNextLineBreak(currentScope.Span.StartIndex + 1);
324+
}
325+
326+
var builder = new StringBuilder();
327+
builder.Append(markers ? "<START>" : "");
328+
builder.Append(startText);
329+
builder.Append(markers ? "</START>" : "");
330+
serverCodeInjections.Add(new KeyValuePair<int, string>(startIndex, builder.ToString()));
331+
332+
/*if (!string.IsNullOrEmpty(currentScope.Opt_ElseContent))
333+
{
334+
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.Opt_ElseIndex, currentScope.Opt_ElseContent));
335+
}*/
336+
337+
var endIndex = script[currentScope.Span.EndIndex] switch
338+
{
339+
'}' => ScanToStartOfLine(currentScope.Span.EndIndex),
340+
';' => ScanToNextLineBreak(currentScope.Span.EndIndex),
341+
_ => currentScope.Span.EndIndex,
342+
};
343+
344+
//string elseText = "";
345+
346+
//var endIndex = ScanToStartOfLine(currentScope.Span.EndIndex);
347+
//var endIndex = TrimWhitespace2(currentScope.Span.EndIndex + 1);
348+
//string endText = $"<ELSE>{currentScope.Opt_ElseContent}</ELSE>{excisionLanguage.ServerScopeEndString}<END>\r\n";// excisionLanguage.ServerScopeEndString;
349+
//endText += "\r\n" + new string('\t', currentScope.Span.End.Column);
350+
351+
//serverCodeInjections.Add(new KeyValuePair<int, string>(endIndex, endText));
352+
353+
var endText = $"{excisionLanguage.ServerScopeEndString}\r\n";
354+
if (endText.StartsWith("#"))
355+
{
356+
endIndex = ScanToStartOfLine(endIndex);
357+
}
358+
359+
builder = new StringBuilder();
360+
361+
if (!string.IsNullOrEmpty(currentScope.Opt_ElseContent))
362+
{
363+
builder.Append(markers ? "<ELSE>" : "");
364+
builder.Append(currentScope.Opt_ElseContent);
365+
builder.Append(markers ? "</ELSE>" : "");
366+
}
367+
368+
builder.Append(markers ? "<END>" : "");
369+
builder.Append(endText);
370+
builder.Append(markers ? "</END>" : "");
371+
372+
serverCodeInjections.Add(new KeyValuePair<int, string>(endIndex, builder.ToString()));
373+
374+
stats.CharactersExcised += currentScope.Span.EndIndex - currentScope.Span.StartIndex;
262375
}
263376

264377
// Next we must add dummy reference variables if they exist.
@@ -340,9 +453,9 @@ private static bool IsWhitespace(char c)
340453
/// <summary>
341454
/// Resize a scope range by excluding whitespace characters.
342455
/// </summary>
343-
private static (int StartIndex, int StopIndex) TrimWhitespace(string script, ServerOnlyScopeData scope)
456+
private static (int StartIndex, int StopIndex) TrimWhitespace(string script, SourceSpan span)
344457
{
345-
var (startIndex, stopIndex) = (scope.StartIndex, scope.StopIndex);
458+
var (startIndex, stopIndex) = (span.StartIndex, span.EndIndex);
346459

347460
while (IsWhitespace(script[startIndex]))
348461
{

ServerCodeExcisionCommon/IServerCodeVisitor.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
23

34
namespace ServerCodeExcisionCommon
45
{
56
public struct ServerOnlyScopeData
67
{
7-
public int StartIndex;
8+
public string CalledFrom { get; }
89

9-
public int StopIndex;
10+
public SourceSpan Span { get; }
11+
12+
public int Opt_ElseIndex { get; }
1013

1114
public string Opt_ElseContent { get; set; }
1215

13-
public ServerOnlyScopeData(int startIndex, int stopIndex)
16+
public ServerOnlyScopeData(SourceSpan span, int elseIndex, [CallerMemberName] string caller = "")
1417
{
15-
StartIndex = startIndex;
16-
StopIndex = stopIndex;
18+
CalledFrom = caller;
19+
Span = span;
20+
Opt_ElseIndex = elseIndex;
1721
Opt_ElseContent = "";
1822
}
1923
}

ServerCodeExcisionCommon/SourceSpan.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace ServerCodeExcisionCommon
24
{
35
/// <summary>
@@ -21,6 +23,11 @@ public readonly struct SourceSpan
2123

2224
public SourceSpan(SourcePosition start, SourcePosition end, int startIndex, int endIndex)
2325
{
26+
if (startIndex > endIndex)
27+
{
28+
throw new ArgumentException($"{nameof(startIndex)} is greater than {nameof(endIndex)}");
29+
}
30+
2431
Start = start;
2532
End = end;
2633
StartIndex = startIndex;

UnrealAngelscriptServerCodeExcision/UnrealAngelscriptSimpleVisitor.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.Configuration;
35
using System.Text;
6+
using System.Xml.Xsl;
47
using ServerCodeExcisionCommon;
8+
using static Antlr4.Runtime.Atn.SemanticContext;
59

610
namespace UnrealAngelscriptServerCodeExcision
711
{
@@ -269,15 +273,9 @@ protected Antlr4.Runtime.Tree.IParseTree GetFirstChildOfType<T>(Antlr4.Runtime.T
269273
return null;
270274
}
271275

272-
protected string BuildIndentationForColumnCount(int nrCols)
276+
protected static string BuildIndentationForColumnCount(int nrCols)
273277
{
274-
var indentation = new StringBuilder("");
275-
for (int indentIdx = 0; indentIdx < nrCols; indentIdx++)
276-
{
277-
indentation.Append("\t");
278-
}
279-
280-
return indentation.ToString();
278+
return new string('\t', nrCols);
281279
}
282280

283281
protected void DecorateFunctionBody(UnrealAngelscriptParser.FunctionBodyContext context)
@@ -297,7 +295,12 @@ protected void DecorateFunctionBody(UnrealAngelscriptParser.FunctionBodyContext
297295
var returnData = GetDefaultReturnStatementForScope(context);
298296

299297
ServerOnlyScopeData newData = new ServerOnlyScopeData(
300-
context.Start.StopIndex + 1,
298+
new SourceSpan(
299+
new SourcePosition(context.Start.Line, context.Start.Column),
300+
new SourcePosition(context.Stop.Line, context.Stop.Column),
301+
context.Start.StartIndex,
302+
context.Stop.StopIndex
303+
),
301304
ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(context.Stop.Line, 0)));
302305

303306
if (returnData.ReturnType != EReturnType.NoReturn)

UnrealAngelscriptServerCodeExcision/UnrealAngelscriptSymbolVisitor.cs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Text.RegularExpressions;
34
using Antlr4.Runtime;
5+
using Antlr4.Runtime.Tree;
46
using ServerCodeExcisionCommon;
57

68
namespace UnrealAngelscriptServerCodeExcision
@@ -120,7 +122,12 @@ private void AddDetectedScopeFromSelectionChild(UnrealAngelscriptParser.Selectio
120122
var returnData = GetDefaultReturnStatementForScope(selectionScope);
121123

122124
ServerOnlyScopeData newData = new ServerOnlyScopeData(
123-
selectionScope.Start.StopIndex + 1,
125+
new SourceSpan(
126+
new SourcePosition(selectionScope.Start.Line, selectionScope.Start.Column),
127+
new SourcePosition(selectionScope.Stop.Line, selectionScope.Stop.Column),
128+
selectionScope.Start.StartIndex,
129+
selectionScope.Stop.StopIndex
130+
),
124131
ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(selectionScope.Stop.Line, 0)));
125132

126133
if (returnData.ReturnType != EReturnType.NoReturn)
@@ -141,9 +148,44 @@ private void AddDetectedScopeFromSelectionChild(UnrealAngelscriptParser.Selectio
141148
var oneLineScope = context.GetChild(childIdx) as UnrealAngelscriptParser.StatementContext;
142149
if (oneLineScope != null)
143150
{
151+
//ServerOnlyScopeData newData = new ServerOnlyScopeData(
152+
// MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(oneLineScope.Start.Line, 0)), false),
153+
// MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(oneLineScope.Stop.Line, oneLineScope.Stop.Column)) + 1, true));
154+
155+
int ScanToEndOfPreviousLine(int index)
156+
{
157+
while (index > 0)
158+
{
159+
if (Script[index - 1] == '\n')
160+
{
161+
index--;
162+
return (Script[index - 1] == '\r') ? index - 2 : index - 1;
163+
}
164+
index--;
165+
}
166+
return index;
167+
}
168+
169+
//var start = new SourcePosition(oneLineScope.Start.Line, 0);
170+
//var startIndex = MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, start), false);
171+
//var stop = new SourcePosition(oneLineScope.Stop.Line, oneLineScope.Stop.Column);
172+
//var stopIndex = MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, stop) + 1, true);
173+
174+
// We want the index of the last character for the parent 'if' condition, or ")".
175+
// This denotes the start of the single-line if's scope.
176+
if (context.GetChild(childIdx - 1) is not ITerminalNode prevSyntax)
177+
{
178+
throw new InvalidOperationException();
179+
}
180+
144181
ServerOnlyScopeData newData = new ServerOnlyScopeData(
145-
MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(oneLineScope.Start.Line, 0)), false),
146-
MoveOneLine(ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(oneLineScope.Stop.Line, oneLineScope.Stop.Column)) + 1, true));
182+
new SourceSpan(
183+
new SourcePosition(prevSyntax.Symbol.Line, prevSyntax.Symbol.Column), //new SourcePosition(start.Line, start.Column),
184+
new SourcePosition(oneLineScope.Stop.Line, oneLineScope.Stop.Column), //new SourcePosition(stop.Line, stop.Column),
185+
prevSyntax.Symbol.StopIndex,
186+
oneLineScope.Stop.StopIndex // include ;
187+
),
188+
ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(oneLineScope.Stop.Line, 0)));
147189

148190
// If there is a return statement at the end, we must replace it with a suitable replacement, or code will stop compiling.
149191
// For one-liners, we actually remove the entire scope, which means we must replace it completely.
@@ -192,7 +234,12 @@ private void AddParentScopePostIfScope(UnrealAngelscriptParser.SelectionStatemen
192234
if (parentScope != null && ifScopeStop != null)
193235
{
194236
ServerOnlyScopeData newData = new ServerOnlyScopeData(
195-
ifScopeStop.StopIndex + 1,
237+
new SourceSpan(
238+
new SourcePosition(ifScopeStop.Line, ifScopeStop.Column),
239+
new SourcePosition(parentScope.Stop.Line, parentScope.Stop.Column),
240+
ifScopeStop.StartIndex,
241+
parentScope.Stop.StopIndex
242+
),
196243
ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(parentScope.Stop.Line, 0)));
197244

198245
// If there is a return statement at the end, we must replace it with a suitable replacement, or code will stop compiling.
@@ -252,7 +299,12 @@ public override UnrealAngelscriptNode VisitPostfixExpression(UnrealAngelscriptPa
252299
var returnData = GetDefaultReturnStatementForScope(parentScope);
253300

254301
ServerOnlyScopeData newData = new ServerOnlyScopeData(
255-
simpleDeclaration.Stop.StopIndex + 1,
302+
new SourceSpan(
303+
new SourcePosition(simpleDeclaration.Stop.Line, simpleDeclaration.Stop.Column),
304+
new SourcePosition(parentScope.Stop.Line, parentScope.Stop.Column),
305+
simpleDeclaration.Stop.StartIndex,
306+
parentScope.Stop.StopIndex
307+
),
256308
ExcisionUtils.FindScriptIndexForCodePoint(Script, new SourcePosition(parentScope.Stop.Line, 0)));
257309

258310
if (returnData.ReturnType != EReturnType.NoReturn)

0 commit comments

Comments
 (0)