Skip to content

Commit 573f0d0

Browse files
authored
Apply keyword replacements in reverse order (#4)
When multiple replacements are required, they must be applied in reverse order to avoid errors caused by position offsets in subsequent tokens.
1 parent a870f1b commit 573f0d0

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/Bonsai.Scripting.Expressions.Tests/ExpressionScriptingTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public Task TestExpressionTransform<TSource, TResult>(string expression, TSource
6363
[DataRow("guid.empty.tobytearray()[0]", 42, 0)]
6464
[DataRow("timespan.tickspermillisecond", 0, TimeSpan.TicksPerMillisecond)]
6565
[DataRow("it > 0 ? convert.toint16(it) : int16.minvalue", 42, (short)42)]
66+
[DataRow("new(single(it) as X, single(it) as Y).X", 42, 42f)]
6667
public Task TestCasingCompatibility<TSource, TResult>(string expression, TSource value, TResult expected)
6768
{
6869
return AssertExpressionTransform(expression, value, expected);

src/Bonsai.Scripting.Expressions/CompatibilityAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static bool ReplaceLegacyKeywords(ParsingConfig? parsingConfig, string te
5252
if (replacements?.Count > 0)
5353
{
5454
var sb = new StringBuilder(text);
55-
for (int i = 0; i < replacements.Count; i++)
55+
for (int i = replacements.Count - 1; i >= 0; i--)
5656
{
5757
var (token, keyword) = replacements[i];
5858
sb.Remove(token.Pos, token.Text.Length);

0 commit comments

Comments
 (0)