This commit is contained in:
nub31
2025-07-06 23:25:54 +02:00
parent cc3eab8a1b
commit 69f93cac03
3 changed files with 18 additions and 12 deletions

View File

@@ -3,7 +3,7 @@ using NubLang.Diagnostics;
namespace NubLang.Syntax.Tokenization;
public static class Tokenizer
public sealed class Tokenizer
{
private static readonly Dictionary<string, Symbol> Keywords = new()
{
@@ -62,12 +62,16 @@ public static class Tokenizer
['&'] = Symbol.Ampersand,
};
private static SourceText _sourceText;
private static int _index;
private readonly SourceText _sourceText;
private int _index;
public static IEnumerable<Token> Tokenize(SourceText sourceText, out IEnumerable<Diagnostic> diagnostics)
public Tokenizer(SourceText sourceText)
{
_sourceText = sourceText;
}
public IEnumerable<Token> Tokenize(out IEnumerable<Diagnostic> diagnostics)
{
_index = 0;
List<Token> tokens = [];
@@ -81,7 +85,7 @@ public static class Tokenizer
return tokens;
}
private static Optional<Token> ParseToken()
private Optional<Token> ParseToken()
{
var startIndex = _index;
@@ -226,7 +230,7 @@ public static class Tokenizer
throw new Exception($"Unknown character {current}");
}
private static SourceLocation CreateLocation(int index)
private SourceLocation CreateLocation(int index)
{
var line = 1;
var column = 1;
@@ -246,12 +250,12 @@ public static class Tokenizer
return new SourceLocation(line, column);
}
private static SourceSpan CreateSpan(int startIndex)
private SourceSpan CreateSpan(int startIndex)
{
return new SourceSpan(_sourceText, CreateLocation(startIndex), CreateLocation(_index));
}
private static Optional<char> Peek(int offset = 0)
private Optional<char> Peek(int offset = 0)
{
if (_index + offset < _sourceText.Content.Length)
{
@@ -261,7 +265,7 @@ public static class Tokenizer
return Optional<char>.Empty();
}
private static Optional<char> Next()
private Optional<char> Next()
{
if (_index < _sourceText.Content.Length)
{