84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
// Port of Go conf/lex.go token types.
|
|
|
|
using System.Text.Json;
|
|
|
|
namespace NATS.Server.Configuration;
|
|
|
|
public enum TokenType
|
|
{
|
|
Error,
|
|
Eof,
|
|
Key,
|
|
Text,
|
|
String,
|
|
Bool,
|
|
Integer,
|
|
Float,
|
|
DateTime,
|
|
ArrayStart,
|
|
ArrayEnd,
|
|
MapStart,
|
|
MapEnd,
|
|
Variable,
|
|
Include,
|
|
Comment,
|
|
}
|
|
|
|
public readonly record struct Token(TokenType Type, string Value, int Line, int Position);
|
|
|
|
/// <summary>
|
|
/// Pedantic token wrapper matching Go conf/parse.go token accessors.
|
|
/// </summary>
|
|
public sealed class PedanticToken
|
|
{
|
|
private readonly Token _item;
|
|
private readonly object? _value;
|
|
private readonly bool _usedVariable;
|
|
private readonly string _sourceFile;
|
|
|
|
/// <summary>
|
|
/// Creates a parser token wrapper that preserves resolved value and source metadata.
|
|
/// </summary>
|
|
/// <param name="item">Raw lexer token captured from the configuration source.</param>
|
|
/// <param name="value">Optional parsed value override when token text has been normalized.</param>
|
|
/// <param name="usedVariable">Indicates whether this token originated from variable substitution.</param>
|
|
/// <param name="sourceFile">Source file path associated with this token, when available.</param>
|
|
public PedanticToken(Token item, object? value = null, bool usedVariable = false, string sourceFile = "")
|
|
{
|
|
_item = item;
|
|
_value = value;
|
|
_usedVariable = usedVariable;
|
|
_sourceFile = sourceFile ?? string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes the token value into JSON, matching Go parser diagnostics formatting.
|
|
/// </summary>
|
|
public string MarshalJson() => JsonSerializer.Serialize(Value());
|
|
|
|
/// <summary>
|
|
/// Returns the resolved token value, or raw token text when no typed value is stored.
|
|
/// </summary>
|
|
public object? Value() => _value ?? _item.Value;
|
|
|
|
/// <summary>
|
|
/// Returns the 1-based source line where the token was parsed.
|
|
/// </summary>
|
|
public int Line() => _item.Line;
|
|
|
|
/// <summary>
|
|
/// Returns whether variable interpolation contributed to this token.
|
|
/// </summary>
|
|
public bool IsUsedVariable() => _usedVariable;
|
|
|
|
/// <summary>
|
|
/// Returns the source file path associated with this token.
|
|
/// </summary>
|
|
public string SourceFile() => _sourceFile;
|
|
|
|
/// <summary>
|
|
/// Returns the 1-based character position of the token on its source line.
|
|
/// </summary>
|
|
public int Position() => _item.Position;
|
|
}
|