This commit is contained in:
nub31
2025-05-31 23:48:29 +02:00
parent a098065136
commit 795b69df1b
14 changed files with 211 additions and 162 deletions

View File

@@ -109,7 +109,7 @@ public class Lexer
Next();
}
Next();
return new DocumentationToken(_sourceText, startIndex, _index, buffer);
return new DocumentationToken(CreateSpan(startIndex), buffer);
}
while (Peek().TryGetValue(out var character) && character != '\n')
@@ -132,20 +132,20 @@ public class Lexer
if (Keywords.TryGetValue(buffer, out var keywordSymbol))
{
return new SymbolToken(_sourceText, startIndex, _index, keywordSymbol);
return new SymbolToken(CreateSpan(startIndex), keywordSymbol);
}
if (Modifiers.TryGetValue(buffer, out var modifer))
{
return new ModifierToken(_sourceText, startIndex, _index, modifer);
return new ModifierToken(CreateSpan(startIndex), modifer);
}
if (buffer is "true" or "false")
{
return new LiteralToken(_sourceText, startIndex, _index, NubPrimitiveType.Bool, buffer);
return new LiteralToken(CreateSpan(startIndex), NubPrimitiveType.Bool, buffer);
}
return new IdentifierToken(_sourceText, startIndex, _index, buffer);
return new IdentifierToken(CreateSpan(startIndex), buffer);
}
if (char.IsDigit(current))
@@ -183,7 +183,7 @@ public class Lexer
}
}
return new LiteralToken(_sourceText, startIndex, _index, isFloat ? NubPrimitiveType.F64 : NubPrimitiveType.I64, buffer);
return new LiteralToken(CreateSpan(startIndex), isFloat ? NubPrimitiveType.F64 : NubPrimitiveType.I64, buffer);
}
// TODO: Revisit this
@@ -203,7 +203,7 @@ public class Lexer
Next();
}
return new SymbolToken(_sourceText, startIndex, _index, chain.Value);
return new SymbolToken(CreateSpan(startIndex), chain.Value);
}
}
}
@@ -211,7 +211,7 @@ public class Lexer
if (Chars.TryGetValue(current, out var charSymbol))
{
Next();
return new SymbolToken(_sourceText, startIndex, _index, charSymbol);
return new SymbolToken(CreateSpan(startIndex), charSymbol);
}
if (current == '"')
@@ -236,17 +236,42 @@ public class Lexer
Next();
}
return new LiteralToken(_sourceText, startIndex, _index, NubPrimitiveType.String, buffer);
return new LiteralToken(CreateSpan(startIndex), NubPrimitiveType.String, buffer);
}
throw new Exception($"Unknown character {current}");
}
private SourceLocation CreateLocation(int index)
{
var line = 1;
var column = 0;
for (var i = 0; i < index; i++)
{
if (_sourceText.Text[i] == '\n')
{
column = 1;
line += 1;
}
else
{
column += 1;
}
}
return new SourceLocation(line, column);
}
private SourceSpan CreateSpan(int startIndex)
{
return new SourceSpan(_sourceText, CreateLocation(startIndex), CreateLocation(_index));
}
private Optional<char> Peek(int offset = 0)
{
if (_index + offset < _sourceText.Content.Length)
if (_index + offset < _sourceText.Text.Length)
{
return _sourceText.Content[_index + offset];
return _sourceText.Text[_index + offset];
}
return Optional<char>.Empty();