Token
using CLanguage.Parser;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace CLanguage.Syntax
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public struct Token : IEquatable<Token>
{
public readonly int Kind;
public readonly Location Location;
public readonly Location EndLocation;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly object Value;
public string StringValue {
get {
string text = Value as string;
if (text == null) {
if (Value == null)
return "";
return Convert.ToString(Value, CultureInfo.InvariantCulture);
}
return text;
}
}
public string Text {
get {
Location location = Location;
if (!location.IsNull) {
location = EndLocation;
if (!location.IsNull && !(Location.Document.Path != EndLocation.Document.Path))
return Location.Document.Content.Substring(Location.Index, EndLocation.Index - Location.Index);
}
return "";
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
public Token(int kind, object value, Location location, Location endLocation)
{
Kind = kind;
Value = (value ?? "");
Location = location;
EndLocation = endLocation;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public Token(int kind, object value)
{
Kind = kind;
Value = value;
Location = Location.Null;
EndLocation = Location.Null;
}
public Token(char kind)
{
Kind = kind;
Value = null;
Location = Location.Null;
EndLocation = Location.Null;
}
public override string ToString()
{
string str = (!Location.IsNull) ? Text : (Value?.ToString() ?? ((Kind < 127) ? ((char)(ushort)Kind).ToString() : ""));
if (Kind >= 127)
return "\"" + str + "\": " + CParser.yyname(Kind);
return "\"" + str + "\"";
}
public Token AsKind(int kind)
{
return new Token(kind, Value, Location, EndLocation);
}
public override bool Equals(object obj)
{
if (obj is Token)
return Equals((Token)obj);
return false;
}
public bool Equals(Token other)
{
if (Kind == other.Kind && Location.Equals(other.Location)) {
if (Value != null || other.Value != null) {
if (Value != null)
return Value.Equals(other);
return false;
}
return true;
}
return false;
}
public override int GetHashCode()
{
return ((666775603 * -1521134295 + Kind.GetHashCode()) * -1521134295 + EqualityComparer<Location>.Default.GetHashCode(Location)) * -1521134295 + ((Value != null) ? EqualityComparer<object>.Default.GetHashCode(Value) : 0);
}
public static bool operator ==(Token token1, Token token2)
{
return token1.Equals(token2);
}
public static bool operator !=(Token token1, Token token2)
{
return !(token1 == token2);
}
}
}