Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions Underanalyzer/Compiler/Lexer/LexContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ public LexContext(CompileContext context, string text, string macroName)
}

/// <summary>
/// Tokenizes the input text until reaching the end.
/// Tokenizes the input text until reaching the end, or until the template
/// string field is closed if the <tt>inTemplateString</tt> parameter is
/// <see langword="true"/>. In the latter case, the closing brace is also
/// consumed.
/// </summary>
public void Tokenize()
public int Tokenize(int startPosition = 0, bool inTemplateString = false)
{
ReadOnlySpan<char> text = Text;
int pos = 0;
int pos = startPosition;
bool newStrings = CompileContext.GameContext.UsingGMS2OrLater;

// Store brace nesting level for template string fields, e.g. $"{{}}"
int templateBraceLevel = 0;

while (pos < text.Length)
{
// Skip whitespace to get to actual tokens
Expand All @@ -97,9 +103,20 @@ public void Tokenize()
{
pos = Identifiers.Parse(this, pos);
}
else if (currChar == '$' || (currChar == '0' && nextChar == 'x'))
else if (currChar == '$')
{
pos = Numbers.ParseHex(this, pos, currChar == '$');
if (nextChar == '"')
{
pos = Strings.ParseTemplate(this, pos);
}
else
{
pos = Numbers.ParseHex(this, pos, true);
}
}
else if (currChar == '0' && nextChar == 'x')
{
pos = Numbers.ParseHex(this, pos, false);
}
else if (char.IsDigit(currChar) || (currChar == '.' && char.IsDigit(nextChar)))
{
Expand Down Expand Up @@ -136,9 +153,36 @@ public void Tokenize()
if (!success)
{
CompileContext.PushError("Unrecognized token", this, pos);
continue;
}

// If within a template string, adjust its brace level (until no braces left)
if (inTemplateString && Tokens[^1] is TokenSeparator sep)
{
switch (sep.Kind)
{
case SeparatorKind.BlockOpen:
templateBraceLevel++;
break;

case SeparatorKind.BlockClose:
if (templateBraceLevel == 0)
{
return pos;
}
templateBraceLevel--;
break;
}
}
}
}

if (inTemplateString)
{
// If not returned yet, we are at the end of the code
CompileContext.PushError("Template string field not closed", this, startPosition);
}
return pos;
}

/// <summary>
Expand Down
Loading
Loading