mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-12-26 07:47:56 +01:00
* Move Spectre.Console.Cli to its own repository * Update build script to use Cake.Sdk and .NET Make * Remove StyleCop (unmaintained) * Add linting using dotnet format * Fix generator which was broken * Update dependencies
55 lines
1.1 KiB
C#
55 lines
1.1 KiB
C#
namespace Spectre.Console.Json;
|
|
|
|
internal sealed class JsonTokenReader
|
|
{
|
|
private readonly List<JsonToken> _reader;
|
|
private readonly int _length;
|
|
|
|
public int Position { get; private set; }
|
|
public bool Eof => Position >= _length;
|
|
|
|
public JsonTokenReader(List<JsonToken> tokens)
|
|
{
|
|
_reader = tokens;
|
|
_length = tokens.Count;
|
|
|
|
Position = 0;
|
|
}
|
|
|
|
public JsonToken Consume(JsonTokenType type)
|
|
{
|
|
var read = Read();
|
|
if (read == null)
|
|
{
|
|
throw new InvalidOperationException("Could not read token");
|
|
}
|
|
|
|
if (read.Type != type)
|
|
{
|
|
throw new InvalidOperationException($"Expected '{type}' token, but found '{read.Type}'");
|
|
}
|
|
|
|
return read;
|
|
}
|
|
|
|
public JsonToken? Peek()
|
|
{
|
|
if (Eof)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return _reader[Position];
|
|
}
|
|
|
|
public JsonToken? Read()
|
|
{
|
|
if (Eof)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Position++;
|
|
return _reader[Position - 1];
|
|
}
|
|
} |